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