データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリです。 https://mlkcca.com/

Dependencies:   MQTT

Dependents:   MilkcocoaSample MilkcocoaSampleESP8266_LED MilkcocoaSampleESP8266 MilkcocoaSample_3G ... more

Committer:
jksoft
Date:
Tue Dec 15 09:56:32 2015 +0000
Revision:
0:23e533c4b1ec
??; Subscribe?1?????; ?Milkcocoa::on???????????????????????

Who changed what in which revision?

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