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

Dependents:   TestVirtualisation Bf_SoftSerial_IR

SoftSerial_IR.cpp

Committer:
kenjiArai
Date:
2018-12-28
Revision:
14:dc766032cdd6
Parent:
13:2b5649a1278a
Child:
15:8d343be3382d

File content as of revision 14:dc766032cdd6:

// Apply for Infrared LED and IR-Detector
//      Modified by JH1PJL Dec. 28th, 2018

#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;
        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(2400);
    format();
    set_parameter();
}

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;
}

void SoftSerial_IR::set_parameter(
        uint32_t tick_offset,
        int32_t rx_detect_center_offset
    )
{
    timestamp_offset = tick_offset;
    overhead_us_IR = rx_detect_center_offset;
}