led example with 2 timers

Dependencies:   mbed mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers modulator.cpp Source File

modulator.cpp

00001 #include "main.h"
00002 
00003 extern Serial pc;
00004 extern Queue<int, 16> ReaderQueue;
00005 extern Mail<message_t, 16> mailBox;
00006 
00007 void Modulator_init()
00008 {
00009 
00010 }
00011 
00012 void buildTrame(string msg)
00013 {
00014     string temp = "";
00015     string msgTemp = "";
00016     
00017     //Append preamble
00018     temp += bitset<8>(PREAMBLE).to_string<char,string::traits_type,string::allocator_type>(); 
00019     
00020     //Append Start bit
00021     temp += bitset<8>(START).to_string<char,string::traits_type,string::allocator_type>();
00022     
00023     //Append Type
00024     temp += bitset<8>(TYPE).to_string<char,string::traits_type,string::allocator_type>();
00025     
00026     //Append Length
00027     temp += bitset<8>(msg.size()*8).to_string<char,string::traits_type,string::allocator_type>();
00028     
00029     for (std::size_t i = 0; i < msg.size(); i++)
00030     {
00031          msgTemp += bitset<8>(msg.c_str()[i]).to_string<char,string::traits_type,string::allocator_type>(); 
00032     }
00033     
00034     temp += msgTemp;
00035 
00036     //Append CRC
00037     char *a = (char*)msgTemp.c_str();
00038     uint16_t crc = calculate_crc16(a, sizeof(a));
00039 
00040     temp += bitset<16>(crc).to_string<char,string::traits_type,string::allocator_type>();
00041     
00042     //Append Stop bit
00043     temp += bitset<8>(STOP).to_string<char,string::traits_type,string::allocator_type>();
00044 
00045     //Put the completed trame in the mail box
00046     message_t *mbTrame = mailBox.alloc();
00047     mbTrame->trame = temp;
00048     mbTrame->size = temp.size();
00049     mailBox.put(mbTrame);
00050 }
00051 
00052 void Modulator_thread(void const *args)
00053 {
00054     string *queueItem;
00055     osEvent evt;
00056  
00057     Modulator_init();
00058     
00059     while(1)
00060     {
00061         evt = ReaderQueue.get();
00062         if (evt.status == osEventMessage) 
00063         {
00064             queueItem = (string*)evt.value.p;
00065             buildTrame(*queueItem);
00066         }
00067         
00068     }
00069 }
00070 // pc.printf("Test CRC16:     %04X\r\n",calculate_crc16(TestString, length));