@@ -93,6 +93,8 @@ bool SoftwareSerial::listen()
93
93
{
94
94
if (active_object)
95
95
active_object->stopListening ();
96
+ if (_singleWirePin)
97
+ setupRXPin (_singleWirePin);
96
98
97
99
_buffer_overflow = false ;
98
100
_receive_buffer_head = _receive_buffer_tail = 0 ;
@@ -111,6 +113,8 @@ bool SoftwareSerial::stopListening()
111
113
if (active_object == this )
112
114
{
113
115
setRxIntMsk (false );
116
+ if (_singleWirePin)
117
+ setupTXPin (_singleWirePin);
114
118
active_object = NULL ;
115
119
return true ;
116
120
}
@@ -246,7 +250,8 @@ ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
246
250
//
247
251
// Constructor
248
252
//
249
- SoftwareSerial::SoftwareSerial (uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */ ) :
253
+ SoftwareSerial::SoftwareSerial (uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */ ) :
254
+ _singleWirePin(receivePin == transmitPin ? receivePin : 0 ),
250
255
_rx_delay_centering(0 ),
251
256
_rx_delay_intrabit(0 ),
252
257
_rx_delay_stopbit(0 ),
@@ -256,6 +261,9 @@ SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inv
256
261
{
257
262
setTX (transmitPin);
258
263
setRX (receivePin);
264
+
265
+ if (_singleWirePin)
266
+ setupTXPin (_singleWirePin);
259
267
}
260
268
261
269
//
@@ -266,24 +274,38 @@ SoftwareSerial::~SoftwareSerial()
266
274
end ();
267
275
}
268
276
269
- void SoftwareSerial::setTX (uint8_t tx)
277
+ void SoftwareSerial::setupTXPin (uint8_t tx)
270
278
{
271
279
// First write, then set output. If we do this the other way around,
272
280
// the pin would be output low for a short while before switching to
273
281
// output high. Now, it is input with pullup for a short while, which
274
282
// is fine. With inverse logic, either order is fine.
275
283
digitalWrite (tx, _inverse_logic ? LOW : HIGH);
276
284
pinMode (tx, OUTPUT);
285
+ }
286
+
287
+ void SoftwareSerial::setTX (uint8_t tx)
288
+ {
289
+ if (!_singleWirePin)
290
+ setupTXPin (tx);
291
+
277
292
_transmitBitMask = digitalPinToBitMask (tx);
278
293
uint8_t port = digitalPinToPort (tx);
279
294
_transmitPortRegister = portOutputRegister (port);
280
295
}
281
296
282
- void SoftwareSerial::setRX (uint8_t rx)
297
+ void SoftwareSerial::setupRXPin (uint8_t rx)
283
298
{
284
299
pinMode (rx, INPUT);
285
300
if (!_inverse_logic)
286
301
digitalWrite (rx, HIGH); // pullup for normal logic!
302
+ }
303
+
304
+ void SoftwareSerial::setRX (uint8_t rx)
305
+ {
306
+ if (!_singleWirePin)
307
+ setupRXPin (rx);
308
+
287
309
_receivePin = rx;
288
310
_receiveBitMask = digitalPinToBitMask (rx);
289
311
uint8_t port = digitalPinToPort (rx);
@@ -371,7 +393,9 @@ void SoftwareSerial::begin(long speed)
371
393
pinMode (_DEBUG_PIN2, OUTPUT);
372
394
#endif
373
395
374
- listen ();
396
+ // Single-wire will be in TX mode by default
397
+ if (!_singleWirePin)
398
+ listen ();
375
399
}
376
400
377
401
void SoftwareSerial::setRxIntMsk (bool enable)
@@ -414,6 +438,9 @@ int SoftwareSerial::available()
414
438
415
439
size_t SoftwareSerial::write (uint8_t b)
416
440
{
441
+ if (_singleWirePin && isListening ())
442
+ return 0 ;
443
+
417
444
if (_tx_delay == 0 ) {
418
445
setWriteError ();
419
446
return 0 ;
0 commit comments