diff --git a/src/pms.h b/src/pms.h index 71ff760..d45deab 100644 --- a/src/pms.h +++ b/src/pms.h @@ -32,6 +32,12 @@ char(*__countof_helper(_CountofType(&_Array)[_SizeOfArray]))[_SizeOfArray]; using __uint24 = uint32_t; #endif +#if defined ESP8266 +#define MIN_FUNC _min +#else +#define MIN_FUNC min +#endif + //////////////////////////////////////// namespace pmsx { @@ -67,7 +73,7 @@ namespace pmsx { "Serial port not initialized", "Status:unknown" }; - return errorMsg[min(value, _countof(errorMsg))]; + return errorMsg[MIN_FUNC(value, _countof(errorMsg))]; } }; diff --git a/src/pmsConfig.h b/src/pmsConfig.h index 4d18618..516ad4c 100644 --- a/src/pmsConfig.h +++ b/src/pmsConfig.h @@ -5,13 +5,19 @@ // Use one of: // it depends on Serial Library (and serial pin connection) +#if defined ESP8266 +#define PMS_SOFTWARESERIAL +#else #define PMS_ALTSOFTSERIAL +#endif #if defined PMS_ALTSOFTSERIAL // Install https://github.com/DrDiettrich/AltSoftSerial.git) #include +#elif defined PMS_SOFTWARESERIAL +#include #else -#error "At least one of: [ PMS_ALTSOFTSERIAL ] have to be defined in pmsConfig.h" +#error "At least one of: [ PMS_ALTSOFTSERIAL PMS_SOFTWARESERIAL ] have to be defined in pmsConfig.h" #endif //////////////////////////////////////////// diff --git a/src/pmsSerialSoftwareSerial.h b/src/pmsSerialSoftwareSerial.h new file mode 100644 index 0000000..c588e8b --- /dev/null +++ b/src/pmsSerialSoftwareSerial.h @@ -0,0 +1,67 @@ +#pragma once + +#include +#include + +class PmsSoftwareSerial : public IPmsSerial { + SoftwareSerial* serial; + unsigned long int timeout = 0; + +public: + PmsSoftwareSerial(SoftwareSerial* iSerial) : serial(iSerial) {} + + void setTimeout(const unsigned long int iTimeout) override { + timeout = iTimeout; + } + + size_t available() override { + return serial->available(); + } + + bool begin(const uint32_t baudRate) override { + serial->begin(baudRate); + return true; + } + + void end() override { + serial->end(); + } + + void flushInput() override { + serial->flush(); + } + + uint8_t peek() override { + return serial->peek(); + } + + uint8_t read() override { + return serial->read(); + } + + size_t read(uint8_t *buffer, const size_t length) override { + unsigned long int readStart = millis(); + size_t byteRead = 0; + + while (byteRead < length) { + int datum = serial->read(); + + if (datum == -1){ + if (millis() - readStart > timeout) { + break; + } + delay(1); + continue; + } + + buffer[byteRead] = datum; + byteRead++; + } + + return byteRead; + } + + size_t write(const uint8_t *buffer, const size_t size) override { + return serial->write(buffer, size); + } +};