Alexandre Lemay
/
APP4_FunTimes
first
sender.cpp@7:332766fb3114, 2017-10-24 (annotated)
- Committer:
- ThierryLeonard
- Date:
- Tue Oct 24 07:04:28 2017 +0000
- Revision:
- 7:332766fb3114
- Parent:
- 6:ac7c0ccf9b5d
- Child:
- 9:8f479f7c1b54
G.G
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
evrast | 3:f7a9f0784ca6 | 1 | |
evrast | 3:f7a9f0784ca6 | 2 | #include "sender.h" |
ThierryLeonard | 6:ac7c0ccf9b5d | 3 | #include "defines.h" |
ThierryLeonard | 7:332766fb3114 | 4 | #include "CRC.h" |
ThierryLeonard | 7:332766fb3114 | 5 | vector<unsigned char> createFrame(const vector<unsigned char> &message) |
ThierryLeonard | 7:332766fb3114 | 6 | { |
ThierryLeonard | 7:332766fb3114 | 7 | vector<unsigned char> trame; |
ThierryLeonard | 7:332766fb3114 | 8 | trame.push_back(preamble_byte); |
ThierryLeonard | 7:332766fb3114 | 9 | trame.push_back(STARTBYTE); |
ThierryLeonard | 7:332766fb3114 | 10 | trame.push_back(0x0); |
ThierryLeonard | 7:332766fb3114 | 11 | trame.push_back((unsigned char)message.size()); |
ThierryLeonard | 7:332766fb3114 | 12 | for(int i =0;i<message.size();i++){ |
ThierryLeonard | 7:332766fb3114 | 13 | trame.push_back(message[i]); |
ThierryLeonard | 7:332766fb3114 | 14 | } |
ThierryLeonard | 7:332766fb3114 | 15 | int crc; |
ThierryLeonard | 7:332766fb3114 | 16 | crc = crc16Remainder(trame); |
ThierryLeonard | 7:332766fb3114 | 17 | trame.push_back(crc>>8); |
ThierryLeonard | 7:332766fb3114 | 18 | trame.push_back(crc & 0xFF); |
ThierryLeonard | 7:332766fb3114 | 19 | |
ThierryLeonard | 7:332766fb3114 | 20 | trame.push_back(ENDBYTE); |
ThierryLeonard | 7:332766fb3114 | 21 | return trame; |
ThierryLeonard | 7:332766fb3114 | 22 | } |
evrast | 3:f7a9f0784ca6 | 23 | |
evrast | 3:f7a9f0784ca6 | 24 | |
evrast | 3:f7a9f0784ca6 | 25 | void Sender::send() |
evrast | 3:f7a9f0784ca6 | 26 | { |
evrast | 3:f7a9f0784ca6 | 27 | switch(send_state) |
evrast | 3:f7a9f0784ca6 | 28 | { |
evrast | 3:f7a9f0784ca6 | 29 | case prepare: |
ThierryLeonard | 6:ac7c0ccf9b5d | 30 | prepareBit(); |
ThierryLeonard | 6:ac7c0ccf9b5d | 31 | MO = _next_bit; |
evrast | 3:f7a9f0784ca6 | 32 | myled = MO; |
ThierryLeonard | 6:ac7c0ccf9b5d | 33 | send_state = sendData; |
evrast | 3:f7a9f0784ca6 | 34 | break; |
evrast | 3:f7a9f0784ca6 | 35 | case sendData: |
ThierryLeonard | 6:ac7c0ccf9b5d | 36 | MO = !_next_bit; |
evrast | 3:f7a9f0784ca6 | 37 | myled = MO; |
ThierryLeonard | 6:ac7c0ccf9b5d | 38 | send_state = prepare; |
ThierryLeonard | 6:ac7c0ccf9b5d | 39 | break; |
evrast | 3:f7a9f0784ca6 | 40 | } |
evrast | 3:f7a9f0784ca6 | 41 | |
evrast | 3:f7a9f0784ca6 | 42 | } |
evrast | 3:f7a9f0784ca6 | 43 | void Sender::prepareBit() |
evrast | 3:f7a9f0784ca6 | 44 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 45 | if(_byteSentCount == data.size()) |
evrast | 3:f7a9f0784ca6 | 46 | { |
evrast | 3:f7a9f0784ca6 | 47 | _ticker.detach(); |
ThierryLeonard | 7:332766fb3114 | 48 | _sending = false; |
evrast | 3:f7a9f0784ca6 | 49 | } |
evrast | 3:f7a9f0784ca6 | 50 | _next_bit = data[_byteSentCount] & (1 << 7 - _bitSentCount); |
evrast | 3:f7a9f0784ca6 | 51 | // pc.printf(_next_bit ? "true\n" : "false\n"); |
evrast | 3:f7a9f0784ca6 | 52 | _bitSentCount++; |
evrast | 3:f7a9f0784ca6 | 53 | if(_bitSentCount==8) |
evrast | 3:f7a9f0784ca6 | 54 | { |
evrast | 3:f7a9f0784ca6 | 55 | _bitSentCount = 0; |
evrast | 3:f7a9f0784ca6 | 56 | _byteSentCount++; |
evrast | 3:f7a9f0784ca6 | 57 | } |
evrast | 3:f7a9f0784ca6 | 58 | } |
ThierryLeonard | 7:332766fb3114 | 59 | void Sender::sendMessage(const vector<unsigned char> & data) |
evrast | 3:f7a9f0784ca6 | 60 | { |
ThierryLeonard | 7:332766fb3114 | 61 | while(_sending = false) |
ThierryLeonard | 7:332766fb3114 | 62 | { |
ThierryLeonard | 7:332766fb3114 | 63 | Thread::yield(); |
ThierryLeonard | 7:332766fb3114 | 64 | } |
ThierryLeonard | 7:332766fb3114 | 65 | _sending = true; |
evrast | 3:f7a9f0784ca6 | 66 | this->data = data; |
evrast | 3:f7a9f0784ca6 | 67 | send_state = prepare; |
evrast | 3:f7a9f0784ca6 | 68 | _byteSentCount = 0; |
evrast | 3:f7a9f0784ca6 | 69 | _bitSentCount = 0; |
evrast | 3:f7a9f0784ca6 | 70 | _next_bit =false; |
ThierryLeonard | 7:332766fb3114 | 71 | _ticker.detach(); |
ThierryLeonard | 6:ac7c0ccf9b5d | 72 | _ticker.attach_us(callback(this, &Sender::send), us_period/2); |
ThierryLeonard | 7:332766fb3114 | 73 | } |
ThierryLeonard | 7:332766fb3114 | 74 | |
ThierryLeonard | 7:332766fb3114 | 75 | Sender::Sender(): MO(p22),pc(USBTX, USBRX),myled(LED2),_sending(false) |
ThierryLeonard | 7:332766fb3114 | 76 | { |
ThierryLeonard | 7:332766fb3114 | 77 | myled = 1; |
ThierryLeonard | 6:ac7c0ccf9b5d | 78 | } |