Example software of using the mbed-rtos to control a simple vehicle's on board computer

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Revision:
1:cdf851858518
Parent:
0:f7d6ed1dfe3e
Child:
2:13a9394ba2e0
--- a/main.cpp	Mon Mar 21 13:57:22 2016 +0000
+++ b/main.cpp	Tue Mar 29 19:25:06 2016 +0000
@@ -1,67 +1,138 @@
 #include "mbed.h"
 #include "rtos.h"
 
-DigitalOut myled(LED1);
+#define TRUE 1
+#define FALSE 0
+
+AnalogIn Brake(p19);
 AnalogIn Accelerometer(p20);
 
-Mutex readvalues;
-Semaphore read_s(1);
+DigitalIn EngineState(p18);
+DigitalIn LeftIndicator(p17);
+DigitalIn RightIndicator(p16);
+DigitalIn SideLightIndicator(p15);
+
+DigitalOut EngineStateInd(LED1);
+DigitalOut SideLightInd(LED2);
+
+PwmOut LeftLightInd(LED3);
+PwmOut RightLightInd(LED4);
+PwmOut OverSpeedInd(p21);
 
 
-float readValue = 1.0;
-
-typedef struct
+void Task8_ReadSideLight(void const *arg)
 {
-    float value;
-} value_t;
+    if(SideLightIndicator)
+    {
+        SideLightInd = TRUE;
+    }
+    else
+    {
+        SideLightInd = FALSE;  
+    }  
+}
 
-void thread1(void const *args)
+void Task9_ReadIndicatorLights(void const *arg)
 {
-    while(1)
+    // Left Indicator Only
+    if(LeftIndicator && !RightIndicator)
+    {
+        LeftIndicator.period(1.0);
+        LeftIndicator.pulsewidth(0.5);
+    }
+    
+    // Right Indicator Only
+    else if(!LeftIndicator && RightIndicator)
     {
-//        read_s.wait();
-        value_t *mail = mail_box.alloc();
+        RightIndicator.period(1.0);
+        RightIndicator.pulsewidth(0.5);
+    }
+    
+    // Left and Right Indicators
+    else if(LeftIndicator && RightIndicator)
+    {
+        LeftIndicator.period(0.5);
+        RightIndicator.period(0.5);
         
-        readvalues.lock();
-        value_t -> value = Accelerometer.read();
-        readvalues.unlock();
-//        read_s.release();
-//        printf("hello-1 = %1.3f\r\n",readValue);  
-        Thread::wait(1000); 
+        LeftIndicator.pulsewidth(0.25);
+        RightIndicator.pulsewidth(0.25);
     }
+}
+        
+
+int main()
+{
+ 
+ RtosTimer Task8(Task8_ReadSideLight,osTimerPeriodic); 
+ RtosTimer Task9(Task9_ReadIndicatorLights,osTimerPeriodic);
+ 
+ Task8.start(1000);
+ Task9.start(2000);
+ 
+ Thread::wait(osWaitForever);
+    
     
 }
 
-void thread2(void const *args)
-{
-    while(1)
-    {
-        readvalues.lock();
-//        read_s.wait();
-        printf("readValue = %s\r\n",(const char*)args);  
-//        read_s.release();
-        readvalues.unlock();
-        Thread::wait(500); 
-    }
-    
-}
-
-int main() {
-    char array[10];
-    
-    Thread thread_1(thread1);    
-    Thread thread_2(thread2,(void *)array);
-    
-    while(1) {
-        
-        readvalues.lock();
-        int var = sprintf(array,"%1.3f",readValue);
-        printf("%s\r\n",array);
-        readvalues.unlock();
-        
-        myled = 1;
-        wait(0.2);
-        myled = 0;
-        wait(0.2);
-    }
-}
+//
+//
+//Mutex readvalues;
+//Semaphore read_s(1);
+//
+//Mail<value_t, 100> mail_box;
+//
+//
+//float readValue = 1.0;
+//
+//typedef struct
+//{
+//    float AccelerationRaw;
+//    float BrakeRaw;
+//} value_t;
+//
+//
+//void readAnalogPins(void const *args)
+//{
+//    while(1)
+//    {
+//        value_t *ReadValue = mail_box.alloc();
+//        
+//        ReadValue -> AccelerationrRaw = Accelerometer.read();
+//        ReadValue -> BrakeRaw = Brake.read();
+//        
+//        mail_box.put(ReadValue);
+//        Thread::wait(100); 
+//    }
+//    
+//}
+//
+//void sendToPC(void const *args)
+//{
+//    while(1)
+//    {
+//        osEvent evt = mail_box.get();
+//        
+//        if(evt.status == osEventMail)
+//        {
+//            value_t *ReadValue = (value_t*)evt.value.p;
+//            printf("Value: %1.3f\r\n", ReadValue-> vale);
+//            mail_box.free(ReadValue);
+//        }
+//        Thread::wait(5000); 
+//    }
+//    
+//}
+//
+//int main() {
+//    
+//    Thread thread_1(readData);    
+//    Thread thread_2(sendToPC);
+//    
+//    while(1) {
+//        
+//        myled = 1;
+//        wait(0.2);
+//        myled = 0;
+//        wait(0.2);
+//    }
+//}