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

Dependents:   TestVirtualisation Bf_SoftSerial_IR

Committer:
Sissors
Date:
Sat Apr 26 14:46:03 2014 +0000
Revision:
0:8edaa7abe724
Child:
1:f8b4b764ace7
TX basic finished

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:8edaa7abe724 1 #include "SoftSerial.h"
Sissors 0:8edaa7abe724 2
Sissors 0:8edaa7abe724 3 int SoftSerial::_putc(int c) {
Sissors 0:8edaa7abe724 4 while(!writeable());
Sissors 0:8edaa7abe724 5 tx_bit = 0;
Sissors 0:8edaa7abe724 6 _char = c;
Sissors 0:8edaa7abe724 7 tx_handler();
Sissors 0:8edaa7abe724 8 return 0;
Sissors 0:8edaa7abe724 9 }
Sissors 0:8edaa7abe724 10
Sissors 0:8edaa7abe724 11 int SoftSerial::writeable(void) {
Sissors 0:8edaa7abe724 12 if (!tx_en)
Sissors 0:8edaa7abe724 13 return false;
Sissors 0:8edaa7abe724 14 if (tx_bit == -1)
Sissors 0:8edaa7abe724 15 return true;
Sissors 0:8edaa7abe724 16 return false;
Sissors 0:8edaa7abe724 17 }
Sissors 0:8edaa7abe724 18
Sissors 0:8edaa7abe724 19 void SoftSerial::tx_handler(void) {
Sissors 0:8edaa7abe724 20 if (get_bit(tx_bit) == -1) {
Sissors 0:8edaa7abe724 21 tx_bit = -1;
Sissors 0:8edaa7abe724 22 return;
Sissors 0:8edaa7abe724 23 }
Sissors 0:8edaa7abe724 24
Sissors 0:8edaa7abe724 25 //Flip output
Sissors 0:8edaa7abe724 26 int cur_out = tx->read();
Sissors 0:8edaa7abe724 27 tx->write(!cur_out);
Sissors 0:8edaa7abe724 28
Sissors 0:8edaa7abe724 29 //Calculate when to do it again
Sissors 0:8edaa7abe724 30 int count = bit_period;
Sissors 0:8edaa7abe724 31 tx_bit++;
Sissors 0:8edaa7abe724 32 while(get_bit(tx_bit) == !cur_out) {
Sissors 0:8edaa7abe724 33 count+=bit_period;
Sissors 0:8edaa7abe724 34 tx_bit++;
Sissors 0:8edaa7abe724 35 }
Sissors 0:8edaa7abe724 36
Sissors 0:8edaa7abe724 37 timeout.attach_us(this, &SoftSerial::tx_handler, count);
Sissors 0:8edaa7abe724 38 }
Sissors 0:8edaa7abe724 39
Sissors 0:8edaa7abe724 40 int SoftSerial::get_bit(int bit) {
Sissors 0:8edaa7abe724 41 //Start bit is never asked
Sissors 0:8edaa7abe724 42
Sissors 0:8edaa7abe724 43 //Data bits
Sissors 0:8edaa7abe724 44 if (bit <= _bits )
Sissors 0:8edaa7abe724 45 return ((_char >> (bit - 1)) & 0x01);
Sissors 0:8edaa7abe724 46
Sissors 0:8edaa7abe724 47 //Parity
Sissors 0:8edaa7abe724 48 bool retval;
Sissors 0:8edaa7abe724 49 if (bit == _bits + 1) {
Sissors 0:8edaa7abe724 50 switch (_parity) {
Sissors 0:8edaa7abe724 51 case Forced1:
Sissors 0:8edaa7abe724 52 return 1;
Sissors 0:8edaa7abe724 53 case Forced0:
Sissors 0:8edaa7abe724 54 return 0;
Sissors 0:8edaa7abe724 55 case Even:
Sissors 0:8edaa7abe724 56 retval = false;
Sissors 0:8edaa7abe724 57 for (int i = 0; i<_bits; i++) {
Sissors 0:8edaa7abe724 58 if (((_char >> i) & 0x01) == 1)
Sissors 0:8edaa7abe724 59 retval = !retval;
Sissors 0:8edaa7abe724 60 }
Sissors 0:8edaa7abe724 61 return retval;
Sissors 0:8edaa7abe724 62 case Odd:
Sissors 0:8edaa7abe724 63 retval = true;
Sissors 0:8edaa7abe724 64 for (int i = 0; i<_bits; i++) {
Sissors 0:8edaa7abe724 65 if (((_char >> i) & 0x01) == 1)
Sissors 0:8edaa7abe724 66 retval = !retval;
Sissors 0:8edaa7abe724 67 }
Sissors 0:8edaa7abe724 68 return retval;
Sissors 0:8edaa7abe724 69 }
Sissors 0:8edaa7abe724 70 }
Sissors 0:8edaa7abe724 71
Sissors 0:8edaa7abe724 72 //Stop bits:
Sissors 0:8edaa7abe724 73 if (bit < 1 + _bits + (bool)_parity + _stop_bits)
Sissors 0:8edaa7abe724 74 return 1;
Sissors 0:8edaa7abe724 75
Sissors 0:8edaa7abe724 76 return -1;
Sissors 0:8edaa7abe724 77 }