Software UART program using Infra-Red LED and IR-Detector

Dependents:   TestVirtualisation Bf_SoftSerial_IR

SoftSerial_IR.cpp

Committer:
kenjiArai
Date:
2018-12-17
Revision:
12:79d63607bbb1
Parent:
SoftSerial.cpp@ 11:a0029614de72
Child:
13:2b5649a1278a

File content as of revision 12:79d63607bbb1:

#include "SoftSerial_IR.h"

SoftSerial_IR::SoftSerial_IR(PinName TX, PinName RX, const char* name) {
    tx_en = rx_en = false;
    read_buffer = 0;
    if (TX != NC) {
        tx = new PwmOut(TX);
        tx_en = true;
        tx->write(0.0f);
        tx_bit = -1;
        send_tx_bit = 0;
        tx->period(1.0f / 38000.0f);    // PWM = 38kHz
        txticker.attach(this, &SoftSerial_IR::tx_handler);
    }
    if (RX != NC) {
        rx = new InterruptIn(RX);
        rx_en = true;
        out_valid = false;
        rxticker.attach(this, &SoftSerial_IR::rx_handler);
        rx->fall(this, &SoftSerial_IR::rx_gpio_irq_handler);
    }
    
    baud(1200);
    format();
}

SoftSerial_IR::~SoftSerial_IR() {
    if (tx_en){
        delete(tx);
    }
    if (rx_en){
        delete(rx);
    }
}

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

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