smple
Dependencies: ESP8266Interface IFTTT mbed
Fork of IFTTT_ESP8266_Example by
SoftSerialSendOnly/SoftSerialSendOnry_tx.cpp@4:3994cd534bd5, 2015-07-27 (annotated)
- Committer:
- jksoft
- Date:
- Mon Jul 27 11:43:18 2015 +0000
- Revision:
- 4:3994cd534bd5
rev0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 4:3994cd534bd5 | 1 | #include "SoftSerialSendOnry.h" |
jksoft | 4:3994cd534bd5 | 2 | |
jksoft | 4:3994cd534bd5 | 3 | int SoftSerialSendOnry::_putc(int c) |
jksoft | 4:3994cd534bd5 | 4 | { |
jksoft | 4:3994cd534bd5 | 5 | while(!writeable()); |
jksoft | 4:3994cd534bd5 | 6 | prepare_tx(c); |
jksoft | 4:3994cd534bd5 | 7 | tx_bit = 0; |
jksoft | 4:3994cd534bd5 | 8 | txticker.prime(); |
jksoft | 4:3994cd534bd5 | 9 | tx_handler(); |
jksoft | 4:3994cd534bd5 | 10 | return 0; |
jksoft | 4:3994cd534bd5 | 11 | } |
jksoft | 4:3994cd534bd5 | 12 | |
jksoft | 4:3994cd534bd5 | 13 | void SoftSerialSendOnry::send_break(void) { |
jksoft | 4:3994cd534bd5 | 14 | while(!writeable()); |
jksoft | 4:3994cd534bd5 | 15 | tx_bit = 0; //Just to make sure it appears as non-writable to other threads/IRQs |
jksoft | 4:3994cd534bd5 | 16 | tx->write(0); |
jksoft | 4:3994cd534bd5 | 17 | wait_us((bit_period * _total_bits * 3) / 2); |
jksoft | 4:3994cd534bd5 | 18 | tx->write(1); |
jksoft | 4:3994cd534bd5 | 19 | tx_bit = -1; |
jksoft | 4:3994cd534bd5 | 20 | } |
jksoft | 4:3994cd534bd5 | 21 | |
jksoft | 4:3994cd534bd5 | 22 | int SoftSerialSendOnry::writeable(void) |
jksoft | 4:3994cd534bd5 | 23 | { |
jksoft | 4:3994cd534bd5 | 24 | if (!tx_en) |
jksoft | 4:3994cd534bd5 | 25 | return false; |
jksoft | 4:3994cd534bd5 | 26 | if (tx_bit == -1) |
jksoft | 4:3994cd534bd5 | 27 | return true; |
jksoft | 4:3994cd534bd5 | 28 | return false; |
jksoft | 4:3994cd534bd5 | 29 | } |
jksoft | 4:3994cd534bd5 | 30 | |
jksoft | 4:3994cd534bd5 | 31 | void SoftSerialSendOnry::tx_handler(void) |
jksoft | 4:3994cd534bd5 | 32 | { |
jksoft | 4:3994cd534bd5 | 33 | if (tx_bit == _total_bits) { |
jksoft | 4:3994cd534bd5 | 34 | tx_bit = -1; |
jksoft | 4:3994cd534bd5 | 35 | fpointer[TxIrq].call(); |
jksoft | 4:3994cd534bd5 | 36 | return; |
jksoft | 4:3994cd534bd5 | 37 | } |
jksoft | 4:3994cd534bd5 | 38 | |
jksoft | 4:3994cd534bd5 | 39 | //Flip output |
jksoft | 4:3994cd534bd5 | 40 | int cur_out = tx->read(); |
jksoft | 4:3994cd534bd5 | 41 | tx->write(!cur_out); |
jksoft | 4:3994cd534bd5 | 42 | |
jksoft | 4:3994cd534bd5 | 43 | //Calculate when to do it again |
jksoft | 4:3994cd534bd5 | 44 | int count = bit_period; |
jksoft | 4:3994cd534bd5 | 45 | tx_bit++; |
jksoft | 4:3994cd534bd5 | 46 | while(((_char >> tx_bit) & 0x01) == !cur_out) { |
jksoft | 4:3994cd534bd5 | 47 | count+=bit_period; |
jksoft | 4:3994cd534bd5 | 48 | tx_bit++; |
jksoft | 4:3994cd534bd5 | 49 | } |
jksoft | 4:3994cd534bd5 | 50 | |
jksoft | 4:3994cd534bd5 | 51 | txticker.setNext(count); |
jksoft | 4:3994cd534bd5 | 52 | } |
jksoft | 4:3994cd534bd5 | 53 | |
jksoft | 4:3994cd534bd5 | 54 | void SoftSerialSendOnry::prepare_tx(int c) |
jksoft | 4:3994cd534bd5 | 55 | { |
jksoft | 4:3994cd534bd5 | 56 | _char = c << 1; |
jksoft | 4:3994cd534bd5 | 57 | |
jksoft | 4:3994cd534bd5 | 58 | bool parity; |
jksoft | 4:3994cd534bd5 | 59 | switch (_parity) { |
jksoft | 4:3994cd534bd5 | 60 | case Forced1: |
jksoft | 4:3994cd534bd5 | 61 | _char |= 1 << (_bits + 1); |
jksoft | 4:3994cd534bd5 | 62 | case Even: |
jksoft | 4:3994cd534bd5 | 63 | parity = false; |
jksoft | 4:3994cd534bd5 | 64 | for (int i = 0; i<_bits; i++) { |
jksoft | 4:3994cd534bd5 | 65 | if (((_char >> i) & 0x01) == 1) |
jksoft | 4:3994cd534bd5 | 66 | parity = !parity; |
jksoft | 4:3994cd534bd5 | 67 | } |
jksoft | 4:3994cd534bd5 | 68 | _char |= parity << (_bits + 1); |
jksoft | 4:3994cd534bd5 | 69 | case Odd: |
jksoft | 4:3994cd534bd5 | 70 | parity = true; |
jksoft | 4:3994cd534bd5 | 71 | for (int i = 0; i<_bits; i++) { |
jksoft | 4:3994cd534bd5 | 72 | if (((_char >> i) & 0x01) == 1) |
jksoft | 4:3994cd534bd5 | 73 | parity = !parity; |
jksoft | 4:3994cd534bd5 | 74 | } |
jksoft | 4:3994cd534bd5 | 75 | _char |= parity << (_bits + 1); |
jksoft | 4:3994cd534bd5 | 76 | } |
jksoft | 4:3994cd534bd5 | 77 | |
jksoft | 4:3994cd534bd5 | 78 | _char |= 0xFFFF << (1 + _bits + (bool)_parity); |
jksoft | 4:3994cd534bd5 | 79 | _char &= ~(1<<_total_bits); |
jksoft | 4:3994cd534bd5 | 80 | } |