led example with 2 timers

Dependencies:   mbed mbed-rtos

Revision:
4:af325c921e79
Parent:
2:124a066878cc
Child:
5:726d7857fd33
--- a/sender.cpp	Tue Feb 11 06:37:34 2014 +0000
+++ b/sender.cpp	Tue Feb 11 20:38:43 2014 +0000
@@ -1,52 +1,87 @@
 #include "main.h"
 
 extern Serial pc;
+extern Mail<message_t, 16> mailBox;
 DigitalOut myled(LED1);
+DigitalOut M0(p26);
 volatile unsigned short timer_count;
+unsigned int trameSize = 0; 
+unsigned int trameCounter = 0;
+char trameArray[696];
+bool trameReady;
+
 
 void Sender_init(void)
 {
     myled = 0;
+    trameCounter = 0;
+    trameReady = false;
     
     LPC_SC->PCONP |=1<1;            //timer0 power on
-    LPC_TIM0->MR0 = 2398000;        //100 msec
+    LPC_TIM0->MR0 = 2398000;        //100 msec ??
     LPC_TIM0->MCR = 3;              //interrupt and reset control
                                     //3 = Interrupt & reset timer0 on match
                                     //1 = Interrupt only, no reset of timer0
     NVIC_EnableIRQ(TIMER0_IRQn);    //enable timer0 interrupt
     LPC_TIM0->TCR = 1;              //enable Timer0
-    //pc.printf("Done timer0_init\n\r");
 }
 
 extern "C" void TIMER0_IRQHandler (void)
-{
-if((LPC_TIM0->IR & 0x01) == 0x01)   // if MR0 interrupt, proceed
+{    
+    if((LPC_TIM0->IR & 0x01) == 0x01)   // if MR0 interrupt, proceed
     {
-    LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
-    timer_count++;                  //increment timer_count
-   
-    // pc.printf("timer_count %d \n\r", timer_count);
-    if (timer_count >= 80)      //Set timer_count for about 8 seconds
+        LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
+    
+        
+        if(trameReady == true)
+        {
+            if(trameCounter <= trameSize)
             {
-            if (myled == 1)             //If LED off, turn it on &
+                if(trameArray[trameCounter] == 0x30)
+                {
+                    M0 = 0;    
+                }    
+                else
                 {
-                myled = 0;
-                timer_count = 0;        //reset count
+                    M0 = 1;    
                 }
+                myled =!myled;
+                trameCounter++;
+                
+            }
             else
-                {
-                myled = 1;              //If LED on, turn it off &
-                timer_count = 0;        //reset count
-                }
+            {
+                trameReady = false;
+                trameCounter = 0;  
+                M0 =0;
+                myled= 0;   
             }
+        }   
     }
 }
+
 void Sender_thread(void const *args)
 {
     Sender_init();
     
     while(1)
     {
-        // wait forever...        
+        if(trameReady == false)
+        {
+            osEvent evt = mailBox.get();
+            if (evt.status == osEventMail) 
+            {
+                message_t *mbTrame = (message_t*)evt.value.p;
+                trameSize = mbTrame->size;
+                
+                strcpy(trameArray, mbTrame->trame.c_str());
+                mailBox.free(mbTrame);  
+                
+                pc.printf("\n\r%s", trameArray);
+                pc.printf("\n\r%d", trameSize);
+                
+                trameReady = true;      
+            } 
+        }
     }
-}
\ No newline at end of file
+}