Software serial, for when you are out of serial pins

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

SoftSerial.cpp

Committer:
Sissors
Date:
2014-04-26
Revision:
2:9e01a38606b4
Parent:
1:f8b4b764ace7
Child:
5:acfd0329f648

File content as of revision 2:9e01a38606b4:

#include "SoftSerial.h"

SoftSerial::SoftSerial(PinName TX, PinName RX) {
    tx_en = rx_en = false;
    if (TX != NC) {
        tx = new DigitalOut(TX);
        tx_en = true;
        tx->write(1);
        tx_bit = -1;
    }
    if (RX != NC) {
        rx = new InterruptIn(RX);
        rx_en = true;
        out_valid = false;
        rx->fall(this, &SoftSerial::rx_gpio_irq_handler);
    }
    
    baud(9600);
    format();
}

void SoftSerial::baud(int baudrate) {
    bit_period = 1000000 / baudrate;
}

void SoftSerial::format(int bits, Parity parity, int stop_bits) {
    _bits = bits;
    _parity = parity;
    _stop_bits = stop_bits;
    _total_bits = 1 + _bits + _stop_bits + (bool)_parity;
}