led example with 2 timers

Dependencies:   mbed mbed-rtos

Committer:
passelin
Date:
Wed Feb 12 07:07:48 2014 +0000
Revision:
5:726d7857fd33
Parent:
4:af325c921e79
02:07

Who changed what in which revision?

UserRevisionLine numberNew contents of line
passelin 1:6e31c704f4d6 1 #include "main.h"
passelin 1:6e31c704f4d6 2
passelin 1:6e31c704f4d6 3 extern Serial pc;
passelin 2:124a066878cc 4 extern Queue<int, 16> ReaderQueue;
passelin 2:124a066878cc 5 extern Mail<message_t, 16> mailBox;
passelin 1:6e31c704f4d6 6
passelin 2:124a066878cc 7 void Modulator_init()
passelin 1:6e31c704f4d6 8 {
passelin 2:124a066878cc 9
passelin 1:6e31c704f4d6 10 }
passelin 1:6e31c704f4d6 11
passelin 2:124a066878cc 12 void buildTrame(string msg)
passelin 1:6e31c704f4d6 13 {
passelin 2:124a066878cc 14 string temp = "";
passelin 2:124a066878cc 15 string msgTemp = "";
passelin 2:124a066878cc 16
passelin 2:124a066878cc 17 //Append preamble
passelin 2:124a066878cc 18 temp += bitset<8>(PREAMBLE).to_string<char,string::traits_type,string::allocator_type>();
passelin 2:124a066878cc 19
passelin 2:124a066878cc 20 //Append Start bit
passelin 2:124a066878cc 21 temp += bitset<8>(START).to_string<char,string::traits_type,string::allocator_type>();
passelin 2:124a066878cc 22
passelin 2:124a066878cc 23 //Append Type
passelin 2:124a066878cc 24 temp += bitset<8>(TYPE).to_string<char,string::traits_type,string::allocator_type>();
passelin 2:124a066878cc 25
passelin 2:124a066878cc 26 //Append Length
passelin 2:124a066878cc 27 temp += bitset<8>(msg.size()*8).to_string<char,string::traits_type,string::allocator_type>();
passelin 2:124a066878cc 28
passelin 2:124a066878cc 29 for (std::size_t i = 0; i < msg.size(); i++)
passelin 1:6e31c704f4d6 30 {
passelin 2:124a066878cc 31 msgTemp += bitset<8>(msg.c_str()[i]).to_string<char,string::traits_type,string::allocator_type>();
passelin 1:6e31c704f4d6 32 }
passelin 2:124a066878cc 33
passelin 2:124a066878cc 34 temp += msgTemp;
passelin 2:124a066878cc 35
passelin 2:124a066878cc 36 //Append CRC
passelin 2:124a066878cc 37 char *a = (char*)msgTemp.c_str();
passelin 2:124a066878cc 38 uint16_t crc = calculate_crc16(a, sizeof(a));
passelin 2:124a066878cc 39
passelin 2:124a066878cc 40 temp += bitset<16>(crc).to_string<char,string::traits_type,string::allocator_type>();
passelin 2:124a066878cc 41
passelin 2:124a066878cc 42 //Append Stop bit
passelin 2:124a066878cc 43 temp += bitset<8>(STOP).to_string<char,string::traits_type,string::allocator_type>();
passelin 2:124a066878cc 44
passelin 2:124a066878cc 45 //Put the completed trame in the mail box
passelin 3:204e23521e39 46 message_t *mbTrame = mailBox.alloc();
passelin 2:124a066878cc 47 mbTrame->trame = temp;
passelin 4:af325c921e79 48 mbTrame->size = temp.size();
passelin 3:204e23521e39 49 mailBox.put(mbTrame);
passelin 1:6e31c704f4d6 50 }
passelin 1:6e31c704f4d6 51
passelin 1:6e31c704f4d6 52 void Modulator_thread(void const *args)
passelin 1:6e31c704f4d6 53 {
passelin 2:124a066878cc 54 string *queueItem;
passelin 2:124a066878cc 55 osEvent evt;
passelin 2:124a066878cc 56
passelin 1:6e31c704f4d6 57 Modulator_init();
passelin 1:6e31c704f4d6 58
passelin 1:6e31c704f4d6 59 while(1)
passelin 1:6e31c704f4d6 60 {
passelin 2:124a066878cc 61 evt = ReaderQueue.get();
passelin 2:124a066878cc 62 if (evt.status == osEventMessage)
passelin 2:124a066878cc 63 {
passelin 2:124a066878cc 64 queueItem = (string*)evt.value.p;
passelin 2:124a066878cc 65 buildTrame(*queueItem);
passelin 2:124a066878cc 66 }
passelin 2:124a066878cc 67
passelin 1:6e31c704f4d6 68 }
passelin 2:124a066878cc 69 }
passelin 2:124a066878cc 70 // pc.printf("Test CRC16: %04X\r\n",calculate_crc16(TestString, length));