Skip to content

Add option for custom rxPin and txPin with HardwareSerial (e.g. for ESP32) #34

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

Open
wants to merge 5 commits 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ sds.begin(); // you can pass custom baud rate as parameter (9600 by default)
```arduino
SdsDustSensor sds(Serial1); // passing HardwareSerial as parameter, you can tune retry mechanism with additional parameters: retryDelayMs and maxRetriesNotAvailable
sds.begin(); // you can pass custom baud rate as parameter (9600 by default)
// or you can pass custom rxPin and txPin to HardwareSerial, if your board (e.g. ESP32) supports remapping of GPIOs to Serial
// sds.begin(9600, SERIAL_8N1, rxPin, txPin);
```

## Supported operations
Expand Down
6 changes: 3 additions & 3 deletions src/SdsDustSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class SdsDustSensor {
}
}

void begin(int baudRate = 9600) {
abstractSerial->begin(baudRate);
}
void begin(int baudRate = 9600, uint32_t config = SERIAL_8N1, int pinRx = -1, int pinTx = -1) {
abstractSerial->begin(baudRate, config, pinRx, pinTx);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep 2 spaces indentation to be consistent with the rest of the project

}

byte *getLastResponse() {
return response;
Expand Down
12 changes: 6 additions & 6 deletions src/Serials.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ namespace Serials {
// just to satisfy linker in gcc I needed to add empty parentheses to other virtual methods...
class AbstractSerial {
public:
virtual void begin(int baudRate) = 0;
virtual void begin(int baudRate, uint32_t config, int pinRx, int pinTx) = 0;
virtual Stream *getStream() = 0;
virtual ~AbstractSerial() {};
};

struct Hardware: public AbstractSerial {
Hardware(HardwareSerial &serial): serial(serial) {}

void begin(int baudRate) {
serial.begin(baudRate);
void begin(int baudRate = 9600, uint32_t config = SERIAL_8N1, int pinRx = -1, int pinTx = -1) {
serial.begin(baudRate, config, pinRx, pinTx);
Copy link
Owner

@lewapek lewapek Feb 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esp32 HardwareSerial is different - and if you try to compile for arduino this would not work
the solution is to put you modified API (with additional params) behind preprocessor directive so it's compiled only in case of esp32

}

Stream *getStream() {
Expand All @@ -40,7 +40,7 @@ namespace Serials {
struct Software: public AbstractSerial {
Software(SoftwareSerial &serial): serial(serial) {}

void begin(int baudRate) {
void begin(int baudRate = 9600, uint32_t config = SERIAL_8N1, int pinRx = -1, int pinTx = -1) {
serial.begin(baudRate);
}

Expand All @@ -61,8 +61,8 @@ namespace Serials {
}
}

void begin(int baudRate) {
serial->begin(baudRate);
void begin(int baudRate = 9600, uint32_t config = SERIAL_8N1, int pinRx = -1, int pinTx = -1) {
serial.begin(baudRate);
}

Stream *getStream() {
Expand Down