Software serial, for when you are out of serial pins

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SoftSerial.cpp Source File

SoftSerial.cpp

00001 #include "SoftSerial.h"
00002 
00003 SoftSerial::SoftSerial(PinName TX, PinName RX, const char* name) {
00004     tx_en = rx_en = false;
00005     read_buffer = 0;
00006     if (TX != NC) {
00007         tx = new DigitalOut(TX);
00008         tx_en = true;
00009         tx->write(1);
00010         tx_bit = -1;
00011         txticker.attach(this, &SoftSerial::tx_handler);
00012     }
00013     if (RX != NC) {
00014         rx = new InterruptIn(RX);
00015         rx_en = true;
00016         out_valid = false;
00017         rxticker.attach(this, &SoftSerial::rx_handler);
00018         rx->fall(this, &SoftSerial::rx_gpio_irq_handler);
00019     }
00020     
00021     baud(9600);
00022     format();
00023 }
00024 
00025 SoftSerial::~SoftSerial() {
00026     if (tx_en)
00027         delete(tx);
00028     if (rx_en)
00029         delete(rx);
00030 }
00031 
00032 void SoftSerial::baud(int baudrate) {
00033     bit_period = 1000000 / baudrate;
00034 }
00035 
00036 void SoftSerial::format(int bits, Parity parity, int stop_bits) {
00037     _bits = bits;
00038     _parity = parity;
00039     _stop_bits = stop_bits;
00040     _total_bits = 1 + _bits + _stop_bits + (bool)_parity;
00041 }