Alexandre Lemay / Mbed 2 deprecated APP4_FunTimes

Dependencies:   mbed mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sender.cpp Source File

sender.cpp

00001 
00002 #include "sender.h"
00003 #include "defines.h"
00004 #include "CRC.h"
00005 vector<unsigned char> createFrame(const vector<unsigned char> &message)
00006 {
00007     vector<unsigned char> trame;
00008     trame.push_back(preamble_byte);
00009     trame.push_back(STARTBYTE);
00010     trame.push_back(0x0);
00011     trame.push_back((unsigned char)message.size());
00012     for(int i =0;i<message.size();i++){
00013         trame.push_back(message[i]);   
00014     }
00015     int crc;
00016     crc = crc16Remainder(trame);
00017     trame.push_back(crc>>8);
00018     trame.push_back(crc & 0xFF);
00019     
00020     trame.push_back(ENDBYTE);
00021     return trame;
00022 }
00023 
00024 
00025 void Sender::send()
00026 {
00027     switch(send_state)
00028     {
00029         case prepare:
00030             prepareBit();           
00031             MO = _next_bit;
00032             myled = MO;
00033             send_state = sendData;
00034             break;
00035         case sendData:
00036             MO = !_next_bit;
00037             myled = MO;
00038             send_state = prepare;
00039             break;    
00040     }
00041     
00042 }
00043 void Sender::prepareBit()
00044 {
00045     if(_byteSentCount == data.size())
00046     {
00047         _ticker.detach();    
00048         _sending = false;
00049     }
00050     _next_bit = data[_byteSentCount] & (1 << 7 - _bitSentCount);
00051 //    pc.printf(_next_bit ? "true\n" : "false\n");
00052     _bitSentCount++;
00053     if(_bitSentCount==8)
00054     {
00055         _bitSentCount = 0;
00056         _byteSentCount++;
00057     }
00058 }
00059 void Sender::sendMessage(const vector<unsigned char> & data)
00060 {
00061     while(_sending == true)
00062     {
00063         Thread::yield();
00064     }
00065     _sending = true;
00066     this->data = data;
00067     send_state = prepare;
00068     _byteSentCount = 0;
00069     _bitSentCount = 0;
00070     _next_bit =false;
00071     _ticker.detach();
00072     _ticker.attach_us(callback(this, &Sender::send), us_period/2);   
00073 }
00074 
00075 Sender::Sender(): MO(p22),pc(USBTX, USBRX),myled(LED2),_sending(false)
00076 {
00077     myled = 1;
00078 }