Software serial, for when you are out of serial pins

Dependents:   BufferedSoftSerial neurGAI_Seeed_BLUETOOTH LPC-SD-35 ESP-WROOM-02_test ... more

Revision:
2:9e01a38606b4
Parent:
1:f8b4b764ace7
Child:
4:c010265ed202
--- a/SoftSerial_tx.cpp	Sat Apr 26 15:13:01 2014 +0000
+++ b/SoftSerial_tx.cpp	Sat Apr 26 16:21:42 2014 +0000
@@ -1,14 +1,16 @@
 #include "SoftSerial.h"
 
-int SoftSerial::_putc(int c) {
+int SoftSerial::_putc(int c)
+{
     while(!writeable());
+    prepare_tx(c);
     tx_bit = 0;
-    _char = c;
     tx_handler();
     return 0;
-    }
+}
 
-int SoftSerial::writeable(void) {
+int SoftSerial::writeable(void)
+{
     if (!tx_en)
         return false;
     if (tx_bit == -1)
@@ -16,62 +18,52 @@
     return false;
 }
 
-void SoftSerial::tx_handler(void) {
-    if (get_bit(tx_bit) == -1) {
+void SoftSerial::tx_handler(void)
+{
+    if (tx_bit == _total_bits) {
         tx_bit = -1;
         return;
     }
-    
+
     //Flip output
     int cur_out = tx->read();
     tx->write(!cur_out);
-         
+
     //Calculate when to do it again
     int count = bit_period;
     tx_bit++;
-    while(get_bit(tx_bit) == !cur_out) {
+    while(((_char >> tx_bit) & 0x01) == !cur_out) {
         count+=bit_period;
         tx_bit++;
     }
-    
+
     txout.attach_us(this, &SoftSerial::tx_handler, count);
 }
 
-int SoftSerial::get_bit(int bit) {
-    //Start bit is never asked
-    
-    //Data bits
-    if (bit <= _bits )
-        return ((_char >> (bit - 1)) & 0x01);
-    
-    //Parity
-    bool retval;
-    if (bit == _bits + 1) {
-        switch (_parity) {
-            case Forced1:
-                return 1;
-            case Forced0:
-                return 0;
-            case Even:
-                retval = false;
-                for (int i = 0; i<_bits; i++) {
-                    if (((_char >> i) & 0x01) == 1)
-                        retval = !retval;
-                }
-                return retval;
-            case Odd:
-                retval = true;
-                for (int i = 0; i<_bits; i++) {
-                    if (((_char >> i) & 0x01) == 1)
-                        retval = !retval;
-                }
-                return retval;
-        }
+void SoftSerial::prepare_tx(int c)
+{
+    _char = c << 1;
+
+    bool parity;
+    switch (_parity) {
+        case Forced1:
+            _char |= 1 << (_bits + 1);
+        case Even:
+            parity = false;
+            for (int i = 0; i<_bits; i++) {
+                if (((_char >> i) & 0x01) == 1)
+                    parity = !parity;
+            }
+            _char |= parity << (_bits + 1);
+        case Odd:
+            parity = true;
+            for (int i = 0; i<_bits; i++) {
+                if (((_char >> i) & 0x01) == 1)
+                    parity = !parity;
+            }
+            _char |= parity << (_bits + 1);
     }
     
-    //Stop bits:
-    if (bit < 1 + _bits + (bool)_parity + _stop_bits) 
-        return 1;
-    
-    return -1;   
-}
\ No newline at end of file
+    _char |= 0xFFFF << (1 + _bits + (bool)_parity);
+    _char &= ~(1<<_total_bits);
+}