ワークショップ用のサンプルプログラムです。

Dependencies:   DHT mbed

Committer:
jksoft
Date:
Fri Apr 29 21:07:24 2016 +0000
Revision:
0:a967c8649563
??

Who changed what in which revision?

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