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