Software serial, for when you are out of serial pins

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

Committer:
SonyPony
Date:
Tue Apr 12 16:58:22 2016 +0000
Revision:
11:a0029614de72
Parent:
9:4e4617c4a441
Fix: set read_buffer to 0 in cunstructor, it caused that first received byte was not correct

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:8edaa7abe724 1 #include "SoftSerial.h"
Sissors 0:8edaa7abe724 2
Sissors 5:acfd0329f648 3 SoftSerial::SoftSerial(PinName TX, PinName RX, const char* name) {
Sissors 0:8edaa7abe724 4 tx_en = rx_en = false;
SonyPony 11:a0029614de72 5 read_buffer = 0;
Sissors 0:8edaa7abe724 6 if (TX != NC) {
Sissors 0:8edaa7abe724 7 tx = new DigitalOut(TX);
Sissors 0:8edaa7abe724 8 tx_en = true;
Sissors 0:8edaa7abe724 9 tx->write(1);
Sissors 0:8edaa7abe724 10 tx_bit = -1;
Sissors 6:517082212c00 11 txticker.attach(this, &SoftSerial::tx_handler);
Sissors 0:8edaa7abe724 12 }
Sissors 0:8edaa7abe724 13 if (RX != NC) {
Sissors 0:8edaa7abe724 14 rx = new InterruptIn(RX);
Sissors 0:8edaa7abe724 15 rx_en = true;
Sissors 1:f8b4b764ace7 16 out_valid = false;
Sissors 6:517082212c00 17 rxticker.attach(this, &SoftSerial::rx_handler);
Sissors 0:8edaa7abe724 18 rx->fall(this, &SoftSerial::rx_gpio_irq_handler);
Sissors 0:8edaa7abe724 19 }
Sissors 0:8edaa7abe724 20
Sissors 0:8edaa7abe724 21 baud(9600);
Sissors 0:8edaa7abe724 22 format();
Sissors 0:8edaa7abe724 23 }
Sissors 0:8edaa7abe724 24
Sissors 9:4e4617c4a441 25 SoftSerial::~SoftSerial() {
Sissors 9:4e4617c4a441 26 if (tx_en)
Sissors 9:4e4617c4a441 27 delete(tx);
Sissors 9:4e4617c4a441 28 if (rx_en)
Sissors 9:4e4617c4a441 29 delete(rx);
Sissors 9:4e4617c4a441 30 }
Sissors 9:4e4617c4a441 31
Sissors 0:8edaa7abe724 32 void SoftSerial::baud(int baudrate) {
Sissors 0:8edaa7abe724 33 bit_period = 1000000 / baudrate;
Sissors 0:8edaa7abe724 34 }
Sissors 0:8edaa7abe724 35
Sissors 0:8edaa7abe724 36 void SoftSerial::format(int bits, Parity parity, int stop_bits) {
Sissors 0:8edaa7abe724 37 _bits = bits;
Sissors 0:8edaa7abe724 38 _parity = parity;
Sissors 0:8edaa7abe724 39 _stop_bits = stop_bits;
Sissors 2:9e01a38606b4 40 _total_bits = 1 + _bits + _stop_bits + (bool)_parity;
Sissors 0:8edaa7abe724 41 }