Simple IoT Board用のライブラリです。 ESP8266ライブラリの軽量化 送信のみのソフトシリアルライブラリを含んでいます。

Dependents:   SITB_HttpGetSample SITB_IFTTTSample SITB_INA226PRC AmbientExampleSITB ... more

Committer:
jksoft
Date:
Sun Nov 15 13:36:44 2015 +0000
Revision:
0:890c12951e96
??

Who changed what in which revision?

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