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

Dependents:   TestVirtualisation Bf_SoftSerial_IR

SoftSerial_tx_IR.cpp

Committer:
kenjiArai
Date:
2018-12-20
Revision:
13:2b5649a1278a
Parent:
12:79d63607bbb1
Child:
14:dc766032cdd6

File content as of revision 13:2b5649a1278a:

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

#include "SoftSerial_IR.h"

int SoftSerial_IR::_putc(int c)
{
    while(!writeable());
    prepare_tx(c);
    tx_bit = 0;
    txticker.prime(timestamp_offset);
    tx_handler();
    return 0;
}

void SoftSerial_IR::send_break(void) {
    while(!writeable());
    //Just to make sure it appears as non-writable to other threads/IRQs
    tx_bit = 0;
    tx->write(0.5f);    // output 0
    wait_us((bit_period * _total_bits * 3) / 2);
    tx->write(0.0f);    // output 1
    tx_bit = -1;
}

int SoftSerial_IR::writeable(void)
{
    if (!tx_en)
        return false;
    if (tx_bit == -1)
        return true;
    return false;
}

void SoftSerial_IR::tx_handler(void)
{
    if (tx_bit == _total_bits) {
        tx_bit = -1;
        fpointer[TxIrq].call();
        return;
    }

    //Flip output
    float tx_out = tx->read();
    int cur_out = 0;
    if (tx_out == 0.5f){    // current duty 50%
        cur_out = 0;        // current = 0
        tx->write(0.0f);    // output port 0 to 1
    } else {
        cur_out = 1;        // current = 1
        tx->write(0.5f);    // output port 1 to 0
    }

    //Calculate when to do it again
    int count = bit_period;
    tx_bit++;
    while(((_char >> tx_bit) & 0x01) == !cur_out) {
        count+=bit_period;
        tx_bit++;
    }

    txticker.setNext(count);
}

void SoftSerial_IR::prepare_tx(int c)
{
    _char = c << 1;

    bool parity;
    switch (_parity) {
        case Forced1:
            _char |= 1 << (_bits + 1);
        case Even:
            parity = false;
            for (int i = 0; i<_bits; i++) {
                if (((_char >> i) & 0x01) == 1)
                    parity = !parity;
            }
            _char |= parity << (_bits + 1);
        case Odd:
            parity = true;
            for (int i = 0; i<_bits; i++) {
                if (((_char >> i) & 0x01) == 1)
                    parity = !parity;
            }
            _char |= parity << (_bits + 1);
    }
    
    _char |= 0xFFFF << (1 + _bits + (bool)_parity);
    _char &= ~(1<<_total_bits);
}