Skip to content

Commit

Permalink
Throw major release number to stop confusion over ctor/begin() split …
Browse files Browse the repository at this point in the history
…of rx/tx pin setting. (#121)

For complete maximum default parameter uses, this is now backward compatible with releases 5.0.4 and prior.
  • Loading branch information
dok-net authored Nov 10, 2019
1 parent 1a14723 commit 9bb7388
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 29 deletions.
8 changes: 4 additions & 4 deletions examples/loopback/loopback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void setup() {
Serial.begin(IUTBITRATE);
Serial.swap();
Serial.setRxBufferSize(2 * BLOCKSIZE);
logger.begin(9600, -1, TX);
logger.begin(9600, swSerialConfig, -1, TX);
#else
Serial.begin(9600);
#endif
Expand All @@ -96,7 +96,7 @@ void setup() {
Serial2.setRxBufferSize(2 * BLOCKSIZE);
logger.begin(9600);
#elif defined(HWSOURCESINK)
serialIUT.begin(IUTBITRATE, SERIAL_8N1, D5, D6);
serialIUT.begin(IUTBITRATE, swSerialConfig, D5, D6);
serialIUT.setRxBufferSize(2 * BLOCKSIZE);
logger.begin(9600);
#else
Expand All @@ -108,12 +108,12 @@ void setup() {

#if !defined(HWSOURCESINK)
#if defined(ESP8266)
serialIUT.begin(IUTBITRATE, D5, D6, swSerialConfig, false, 4 * BLOCKSIZE);
serialIUT.begin(IUTBITRATE, swSerialConfig, D5, D6, false, 4 * BLOCKSIZE);
#ifdef HALFDUPLEX
serialIUT.enableIntTx(false);
#endif
#elif defined(ESP32)
serialIUT.begin(IUTBITRATE, D5, D6, swSerialConfig, false, 2 * BLOCKSIZE);
serialIUT.begin(IUTBITRATE, swSerialConfig, D5, D6, false, 2 * BLOCKSIZE);
#ifdef HALFDUPLEX
serialIUT.enableIntTx(false);
#endif
Expand Down
4 changes: 2 additions & 2 deletions examples/onewiretest/onewiretest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ void setup() {
delay(2000);
Serial.begin(115200);
Serial.println("\nOne Wire Half Duplex Serial Tester");
swSer1.begin(115200, 12, 12, SWSERIAL_8N1, false, 256);
swSer1.begin(115200, SWSERIAL_8N1, 12, 12, false, 256);
swSer1.enableIntTx(true);
swSer2.begin(115200, 14, 14, SWSERIAL_8N1, false, 256);
swSer2.begin(115200, SWSERIAL_8N1, 14, 14, false, 256);
swSer2.enableIntTx(true);
}

Expand Down
6 changes: 3 additions & 3 deletions examples/repeater/repeater.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ void setup() {
repeater.begin(IUTBITRATE);
repeater.setRxBufferSize(2 * BLOCKSIZE);
repeater.swap();
logger.begin(9600, RX, TX);
logger.begin(9600, swSerialConfig, RX, TX);
#elif defined(ESP32)
repeater.begin(IUTBITRATE, SERIAL_8N1, D7, D8);
repeater.setRxBufferSize(2 * BLOCKSIZE);
logger.begin(9600);
#endif
#else
#if defined(ESP8266)
repeater.begin(IUTBITRATE, D7, D8, swSerialConfig, false, 2 * BLOCKSIZE);
repeater.begin(IUTBITRATE, swSerialConfig, D7, D8, false, 2 * BLOCKSIZE);
#elif defined(ESP32)
repeater.begin(IUTBITRATE, D7, D8, swSerialConfig, false, 2 * BLOCKSIZE);
repeater.begin(IUTBITRATE, swSerialConfig, D7, D8, false, 2 * BLOCKSIZE);
#endif
#ifdef HALFDUPLEX
repeater.enableIntTx(false);
Expand Down
2 changes: 1 addition & 1 deletion examples/servoTester/servoTester.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void setup() {
delay(2000);
Serial.begin(115200);
Serial.println("\nAlpha 1S Servo Tester");
swSer.begin(115200, 12, 12, SWSERIAL_8N1, false, 256);
swSer.begin(115200, SWSERIAL_8N1, 12, 12, false, 256);
}

void loop() {
Expand Down
2 changes: 1 addition & 1 deletion examples/swsertest/swsertest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SoftwareSerial swSer;

void setup() {
Serial.begin(115200);
swSer.begin(BAUD_RATE, D5, D6, SWSERIAL_8N1, false, 95, 11);
swSer.begin(BAUD_RATE, SWSERIAL_8N1, D5, D6, false, 95, 11);

Serial.println("\nSoftware serial test started");

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "EspSoftwareSerial",
"version": "5.4.0",
"version": "6.0.0",
"keywords": [
"serial", "io", "softwareserial"
],
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=EspSoftwareSerial
version=5.4.0
version=6.0.0
author=Peter Lerup, Dirk Kaar
maintainer=Peter Lerup <[email protected]>
sentence=Implementation of the Arduino software serial for ESP8266/ESP32.
Expand Down
25 changes: 17 additions & 8 deletions src/SoftwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ SoftwareSerial::SoftwareSerial() {
m_isrOverflow = false;
}

SoftwareSerial::SoftwareSerial(int8_t rxPin, int8_t txPin)
{
m_isrOverflow = false;
m_rxPin = rxPin;
m_txPin = txPin;
}


SoftwareSerial::~SoftwareSerial() {
end();
}
Expand All @@ -49,12 +57,14 @@ bool SoftwareSerial::isValidGPIOpin(int8_t pin) {
#endif
}

void SoftwareSerial::begin(uint32_t baud, int8_t rxPin, int8_t txPin,
SoftwareSerialConfig config, bool invert, int bufCapacity, int isrBufCapacity) {
m_oneWire = (rxPin == txPin);
void SoftwareSerial::begin(uint32_t baud, SoftwareSerialConfig config,
int8_t rxPin, int8_t txPin,
bool invert, int bufCapacity, int isrBufCapacity) {
if (-1 != rxPin) m_rxPin = rxPin;
if (-1 != txPin) m_txPin = txPin;
m_oneWire = (m_rxPin == m_txPin);
m_invert = invert;
if (isValidGPIOpin(rxPin)) {
m_rxPin = rxPin;
if (isValidGPIOpin(m_rxPin)) {
std::unique_ptr<circular_queue<uint8_t> > buffer(new circular_queue<uint8_t>((bufCapacity > 0) ? bufCapacity : 64));
m_buffer = move(buffer);
std::unique_ptr<circular_queue<uint32_t> > isrBuffer(new circular_queue<uint32_t>((isrBufCapacity > 0) ? isrBufCapacity : (sizeof(uint8_t) * 8 + 2) * bufCapacity));
Expand All @@ -64,14 +74,13 @@ void SoftwareSerial::begin(uint32_t baud, int8_t rxPin, int8_t txPin,
pinMode(m_rxPin, INPUT_PULLUP);
}
}
if (isValidGPIOpin(txPin)
if (isValidGPIOpin(m_txPin)
#ifdef ESP8266
|| ((txPin == 16) && !m_oneWire)) {
|| ((m_txPin == 16) && !m_oneWire)) {
#else
) {
#endif
m_txValid = true;
m_txPin = txPin;
if (!m_oneWire) {
pinMode(m_txPin, OUTPUT);
digitalWrite(m_txPin, !m_invert);
Expand Down
27 changes: 19 additions & 8 deletions src/SoftwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include <Stream.h>
#include <functional>

/// If only one tx or rx wanted then use this as parameter for the unused pin.
constexpr int SW_SERIAL_UNUSED_PIN = -1;

enum SoftwareSerialConfig {
SWSERIAL_5N1 = 0,
SWSERIAL_6N1,
Expand All @@ -47,11 +44,25 @@ enum SoftwareSerialConfig {
class SoftwareSerial : public Stream {
public:
SoftwareSerial();
/// Ctor to set defaults for pins.
/// @param rxPin the GPIO pin used for RX
/// @param txPin -1 for onewire protocol, GPIO pin used for twowire TX
SoftwareSerial(int8_t rxPin, int8_t txPin = -1);
SoftwareSerial(const SoftwareSerial&) = delete;
SoftwareSerial& operator= (const SoftwareSerial&) = delete;
virtual ~SoftwareSerial();
void begin(uint32_t baud, int8_t rxPin, int8_t txPin = -1,
SoftwareSerialConfig config = SWSERIAL_8N1,
/// Configure the SoftwareSerial object for use.
/// @param baud the TX/RX bitrate
/// @param config sets databits, parity, and stop bit count
/// @param rxPin -1 or default: either no RX pin, or keeps the rxPin set in the ctor
/// @param txPin -1 or default: either no TX pin (onewire), or keeps the txPin set in the ctor
/// @param invert true: uses invert line level logic
/// @param bufCapacity the capacity for the received bytes buffer
/// @param isrBufCapacity 0: derived from bufCapacity. The capacity of the internal asynchronous
/// bit receive buffer, a suggested size is bufCapacity times the sum of
/// start, data, parity and stop bit count.
void begin(uint32_t baud, SoftwareSerialConfig config = SWSERIAL_8N1,
int8_t rxPin = -1, int8_t txPin = -1,
bool invert = false, int bufCapacity = 64, int isrBufCapacity = 0);
uint32_t baudRate();
/// Transmit control pin.
Expand Down Expand Up @@ -126,9 +137,9 @@ class SoftwareSerial : public Stream {

// Member variables
bool m_oneWire;
int8_t m_rxPin = SW_SERIAL_UNUSED_PIN;
int8_t m_txPin = SW_SERIAL_UNUSED_PIN;
int8_t m_txEnablePin = SW_SERIAL_UNUSED_PIN;
int8_t m_rxPin = -1;
int8_t m_txPin = -1;
int8_t m_txEnablePin = -1;
bool m_rxValid = false;
bool m_rxEnabled = false;
bool m_txValid = false;
Expand Down

0 comments on commit 9bb7388

Please sign in to comment.