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

Dependents:   TestVirtualisation Bf_SoftSerial_IR

Committer:
kenjiArai
Date:
Thu Dec 20 00:43:12 2018 +0000
Revision:
13:2b5649a1278a
Parent:
12:79d63607bbb1
Child:
14:dc766032cdd6
1st test revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 13:2b5649a1278a 1 // Apply for Infrared LED and IR-Detector
kenjiArai 13:2b5649a1278a 2 // Modified by JH1PJL Dec. 19th, 2018
kenjiArai 13:2b5649a1278a 3
kenjiArai 12:79d63607bbb1 4 #include "SoftSerial_IR.h"
Sissors 0:8edaa7abe724 5
kenjiArai 12:79d63607bbb1 6 int SoftSerial_IR::_putc(int c)
Sissors 2:9e01a38606b4 7 {
Sissors 0:8edaa7abe724 8 while(!writeable());
Sissors 2:9e01a38606b4 9 prepare_tx(c);
Sissors 0:8edaa7abe724 10 tx_bit = 0;
kenjiArai 13:2b5649a1278a 11 txticker.prime(timestamp_offset);
Sissors 0:8edaa7abe724 12 tx_handler();
Sissors 0:8edaa7abe724 13 return 0;
Sissors 2:9e01a38606b4 14 }
Sissors 0:8edaa7abe724 15
kenjiArai 12:79d63607bbb1 16 void SoftSerial_IR::send_break(void) {
Sissors 7:7de3e1019e23 17 while(!writeable());
kenjiArai 13:2b5649a1278a 18 //Just to make sure it appears as non-writable to other threads/IRQs
kenjiArai 13:2b5649a1278a 19 tx_bit = 0;
kenjiArai 13:2b5649a1278a 20 tx->write(0.5f); // output 0
Sissors 8:332b66de89d3 21 wait_us((bit_period * _total_bits * 3) / 2);
kenjiArai 13:2b5649a1278a 22 tx->write(0.0f); // output 1
Sissors 7:7de3e1019e23 23 tx_bit = -1;
Sissors 7:7de3e1019e23 24 }
Sissors 7:7de3e1019e23 25
kenjiArai 12:79d63607bbb1 26 int SoftSerial_IR::writeable(void)
Sissors 2:9e01a38606b4 27 {
Sissors 0:8edaa7abe724 28 if (!tx_en)
Sissors 0:8edaa7abe724 29 return false;
Sissors 0:8edaa7abe724 30 if (tx_bit == -1)
Sissors 0:8edaa7abe724 31 return true;
Sissors 0:8edaa7abe724 32 return false;
Sissors 0:8edaa7abe724 33 }
Sissors 0:8edaa7abe724 34
kenjiArai 12:79d63607bbb1 35 void SoftSerial_IR::tx_handler(void)
Sissors 2:9e01a38606b4 36 {
Sissors 2:9e01a38606b4 37 if (tx_bit == _total_bits) {
Sissors 0:8edaa7abe724 38 tx_bit = -1;
Sissors 4:c010265ed202 39 fpointer[TxIrq].call();
Sissors 0:8edaa7abe724 40 return;
Sissors 0:8edaa7abe724 41 }
Sissors 2:9e01a38606b4 42
Sissors 0:8edaa7abe724 43 //Flip output
kenjiArai 13:2b5649a1278a 44 float tx_out = tx->read();
kenjiArai 13:2b5649a1278a 45 int cur_out = 0;
kenjiArai 13:2b5649a1278a 46 if (tx_out == 0.5f){ // current duty 50%
kenjiArai 13:2b5649a1278a 47 cur_out = 0; // current = 0
kenjiArai 13:2b5649a1278a 48 tx->write(0.0f); // output port 0 to 1
kenjiArai 12:79d63607bbb1 49 } else {
kenjiArai 13:2b5649a1278a 50 cur_out = 1; // current = 1
kenjiArai 13:2b5649a1278a 51 tx->write(0.5f); // output port 1 to 0
kenjiArai 12:79d63607bbb1 52 }
Sissors 2:9e01a38606b4 53
Sissors 0:8edaa7abe724 54 //Calculate when to do it again
Sissors 0:8edaa7abe724 55 int count = bit_period;
Sissors 0:8edaa7abe724 56 tx_bit++;
kenjiArai 13:2b5649a1278a 57 while(((_char >> tx_bit) & 0x01) == !cur_out) {
Sissors 0:8edaa7abe724 58 count+=bit_period;
Sissors 0:8edaa7abe724 59 tx_bit++;
Sissors 0:8edaa7abe724 60 }
Sissors 2:9e01a38606b4 61
Sissors 6:517082212c00 62 txticker.setNext(count);
Sissors 0:8edaa7abe724 63 }
Sissors 0:8edaa7abe724 64
kenjiArai 12:79d63607bbb1 65 void SoftSerial_IR::prepare_tx(int c)
Sissors 2:9e01a38606b4 66 {
Sissors 2:9e01a38606b4 67 _char = c << 1;
Sissors 2:9e01a38606b4 68
Sissors 2:9e01a38606b4 69 bool parity;
Sissors 2:9e01a38606b4 70 switch (_parity) {
Sissors 2:9e01a38606b4 71 case Forced1:
Sissors 2:9e01a38606b4 72 _char |= 1 << (_bits + 1);
Sissors 2:9e01a38606b4 73 case Even:
Sissors 2:9e01a38606b4 74 parity = false;
Sissors 2:9e01a38606b4 75 for (int i = 0; i<_bits; i++) {
Sissors 2:9e01a38606b4 76 if (((_char >> i) & 0x01) == 1)
Sissors 2:9e01a38606b4 77 parity = !parity;
Sissors 2:9e01a38606b4 78 }
Sissors 2:9e01a38606b4 79 _char |= parity << (_bits + 1);
Sissors 2:9e01a38606b4 80 case Odd:
Sissors 2:9e01a38606b4 81 parity = true;
Sissors 2:9e01a38606b4 82 for (int i = 0; i<_bits; i++) {
Sissors 2:9e01a38606b4 83 if (((_char >> i) & 0x01) == 1)
Sissors 2:9e01a38606b4 84 parity = !parity;
Sissors 2:9e01a38606b4 85 }
Sissors 2:9e01a38606b4 86 _char |= parity << (_bits + 1);
Sissors 0:8edaa7abe724 87 }
Sissors 0:8edaa7abe724 88
Sissors 2:9e01a38606b4 89 _char |= 0xFFFF << (1 + _bits + (bool)_parity);
Sissors 2:9e01a38606b4 90 _char &= ~(1<<_total_bits);
Sissors 2:9e01a38606b4 91 }