Skip to content

Commit ddcdc90

Browse files
Simplify SoftwareSerial::write
Before, there was nearly identical code for the inverted and regular cases. However, simply inverting the byte in the inverted case allows using the regular code twice, reducing the generated code size by 100 bytes (on an Arduino Uno and gcc 4.3, on gcc 4.8 the reduction is 50 bytes).
1 parent cf0cc48 commit ddcdc90

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

libraries/SoftwareSerial/SoftwareSerial.cpp

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -486,34 +486,21 @@ size_t SoftwareSerial::write(uint8_t b)
486486

487487
// Write each of the 8 bits
488488
if (_inverse_logic)
489-
{
490-
for (byte mask = 0x01; mask; mask <<= 1)
491-
{
492-
if (b & mask) // choose bit
493-
tx_pin_write(LOW); // send 1
494-
else
495-
tx_pin_write(HIGH); // send 0
496-
497-
tunedDelay(_tx_delay);
498-
}
489+
b = ~b;
499490

500-
tx_pin_write(LOW); // restore pin to natural state
501-
}
502-
else
491+
for (byte mask = 0x01; mask; mask <<= 1)
503492
{
504-
for (byte mask = 0x01; mask; mask <<= 1)
505-
{
506-
if (b & mask) // choose bit
507-
tx_pin_write(HIGH); // send 1
508-
else
509-
tx_pin_write(LOW); // send 0
510-
511-
tunedDelay(_tx_delay);
512-
}
493+
if (b & mask) // choose bit
494+
tx_pin_write(HIGH); // send 1
495+
else
496+
tx_pin_write(LOW); // send 0
513497

514-
tx_pin_write(HIGH); // restore pin to natural state
498+
tunedDelay(_tx_delay);
515499
}
516500

501+
// restore pin to natural state
502+
tx_pin_write(_inverse_logic ? LOW : HIGH);
503+
517504
SREG = oldSREG; // turn interrupts back on
518505
tunedDelay(_tx_delay);
519506

0 commit comments

Comments
 (0)