arduino / arduino/ArduinoCore-avr
SoftwareSerial: Being able to set the receive and transmit pin at runtime
- Dominant language
- C
- Stars
- 1.5k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
I'm trying to create a chain of attiny's that use SoftwareSerial to communicate in a one-to-many fashion with one sender and many listeners. The attiny's should all be running the same firmware only the role is different (sender of receiver). Because of the limited number of pins on an attiny only one wire should be used.
If I use SoftwareSerial for this I run into problems:
This won't work:
``` cpp
SoftwareSerial mySerial( 4, 4 );
```
OK, fair enough, I can just use two different objects, right?
``` cpp
SoftwareSerial myTx( -1, 4 );
SoftwareSerial myRx( 4, -1 );
if ( role == SENDER ) {
myTx.println( "msg" );
} else {
char c = myRx.read();
}
```
This works great for receiving but not for sending!
If I swap the declarations like this:
``` cpp
SoftwareSerial myRx( 4, -1 );
SoftwareSerial myTx( -1, 4 );
```
Then it works for sending but not for receiving!
Could the interface be changed so that the receive and transmit pins can be set _after_ declaration?
I fixed it for now by moving the setRX() and setTX() methods from the private to the public section.
``` cpp
if ( role == SENDER ) {
mySerial.setTX( 4 );
mySerial.setRX( -1 );
} else {
mySerial.setTX( -1 );
mySerial.setRX( 4 );
}
```
but the most elegant way to solve this would perhaps be to add optional receivePin and transmitPin arguments to the begin() method so we could just do this:
``` cpp
SoftwareSerial mySerial( -1, -1 );
void setup()
{
if ( role == SENDER ) {
mySerial.begin( 9600, -1, 4 );
} else {
mySerial.begin( 9600, 4, -1 );
}
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reading the SoftwareSerial constructor, setRX(), setTX(), and begin() entry points described in the issue, then compare the proposed runtime pin-selection approaches. Done means a single SoftwareSerial object can select transmit-only or receive-only pins after declaration for the two roles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- arduino, cpp
- Domain
- embedded-iot
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100