Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Added support for ESP8266 #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/pms.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))];
}
};

Expand Down
8 changes: 7 additions & 1 deletion src/pmsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pmsSerialAltSoftSerial.h>
#elif defined PMS_SOFTWARESERIAL
#include <pmsSerialSoftwareSerial.h>
#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

////////////////////////////////////////////
Expand Down
67 changes: 67 additions & 0 deletions src/pmsSerialSoftwareSerial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#include <pmsSerial.h>
#include <SoftwareSerial.h>

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);
}
};