finished

Dependencies:   C12832 DebouncedInterrupt MMA7660 mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
cathal66
Date:
Fri May 08 10:21:04 2015 +0000
Parent:
2:ff8262f6a385
Commit message:
finished

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r ff8262f6a385 -r c31a01f2c064 main.cpp
--- a/main.cpp	Tue Mar 03 13:42:32 2015 +0000
+++ b/main.cpp	Fri May 08 10:21:04 2015 +0000
@@ -46,166 +46,162 @@
 DigitalOut led_B(p25); //LED RGB Blue
 
 //leds for debug
-DigitalOut led4(LED4); //LED 
+DigitalOut led4(LED4); //LED
 DigitalOut led3(LED3); //LED
 DigitalOut led2(LED2); //LED
-        
+
 //Global varible
 int button_press=0;
 
-    
-void timeout_event(void const *n) 
+
+void timeout_event(void const *n)
 {
-       
-        //event via a message queue
-        message_t *message = mpool.alloc();
-        message->event = TIME_OUT; 
-        queue.put(message);
-        
-        led4 =  !led4;
-        
+
+    //event via a message queue
+    message_t *message = mpool.alloc();
+    message->event = TIME_OUT;
+    queue.put(message);
+
+    led4 =  !led4;
+
 }
 
 
-void button_event_thread(void const *argument) {
-    
+void button_event_thread(void const *argument)
+{
+
     while (true) {
-        
-        if (button_press == 1)
-            {
-           
+
+        if (button_press == 1) {
+
             //event via a message queue
             message_t *message = mpool.alloc();
-            message->event = BUTTON_PRESS; 
+            message->event = BUTTON_PRESS;
             queue.put(message);
-            
+
             led3 = !led3;
             button_press = 0;
             Thread::wait(500);
-            }
-            
-            
-          
+        }
+
+
+
     }
 }
 
 
-void tilt_event_thread(void const *argument) {
+void tilt_event_thread(void const *argument)
+{
     float tilt_value_Y = MMA.y();
     float tilt_value_X = MMA.x();
-    while (true) 
-        {
-     
+    while (true) {
+
         //debouce delay for switch
-        
+
 
-        if (tilt_value_Y <= MMA.y()-0.2 ^ tilt_value_Y >= MMA.y()+0.2 )
-            {
+        if (tilt_value_Y <= MMA.y()-0.2 ^ tilt_value_Y >= MMA.y()+0.2 ) {
 
             //event via a message queue
             message_t *message = mpool.alloc();
-            message->event = TILT; 
+            message->event = TILT;
             queue.put(message);
-            
+
             led3 = !led3;
             Thread::wait(1500);
             tilt_value_Y = MMA.y();
-            }
-            
-        
         }
-        
-    
+
+
+    }
+
+
 }
- 
-void push_event_thread(void const *argument) {
+
+void push_event_thread(void const *argument)
+{
     float push_value = push.read();
-    while (true) 
-        {
-     
+    while (true) {
+
         //debouce delay for switch
-        
-        if (push_value >= push.read()- 0.15 ^ push_value <= push.read()+ 0.15 )
-            {
+
+        if (push_value >= push.read()- 0.15 ^ push_value <= push.read()+ 0.15 ) {
             //event via a message queue
             message_t *message = mpool.alloc();
-            message->event = PUSH; 
+            message->event = PUSH;
             queue.put(message);
             led3 = !led3;
             Thread::wait(1000);
             push_value = push.read();
-            }
         }
-        
-    
+    }
+
+
 }
-void flash_led_thread(void const *argument) {
-    while (true) 
-        {
+void flash_led_thread(void const *argument)
+{
+    while (true) {
         Thread::signal_wait(0x1);
-    
-        for (int i=0;i<=20;i++)
-            {
+
+        for (int i=0; i<=20; i++) {
             LED_RGB.lock();
-            led_R = !led_R; 
-            LED_RGB.unlock(); 
+            led_R = !led_R;
+            LED_RGB.unlock();
             Thread::wait(100);
-            }
+        }
         LED_RGB.lock();
-        led_R = 1; 
-        LED_RGB.unlock();             
-        }
-     
-}      
+        led_R = 1;
+        LED_RGB.unlock();
+    }
+
+}
 
 
 
-void Button_Inter() {
-//Flash_LED_Thread.signal_set(0x1);  
+void Button_Inter()
+{
+//Flash_LED_Thread.signal_set(0x1);
+
+    button_press=1;
+
+}
 
-button_press=1;
-    
-} 
-    
-int main (void) {
-    
-//Thread fsm(fsm_thread); 
-Thread button_event(button_event_thread);
-Thread tilt_event(tilt_event_thread);
-Thread push_event(push_event_thread);
-Thread Flash_LED_Thread(flash_led_thread); 
-RtosTimer timer(timeout_event, osTimerPeriodic, (void *)0);
+int main (void)
+{
+
+//Thread fsm(fsm_thread);
+    Thread button_event(button_event_thread);
+    Thread tilt_event(tilt_event_thread);
+    Thread push_event(push_event_thread);
+    Thread Flash_LED_Thread(flash_led_thread);
+    RtosTimer timer(timeout_event, osTimerPeriodic, (void *)0);
 
 //Interrupts
-button.rise(&Button_Inter);
+    button.rise(&Button_Inter);
 
-int state = STATE_0;
+    int state = STATE_0;
 
-LED_RGB.lock();
-led_R=1;
-led_G=1;
-led_B=1;
-LED_RGB.unlock();
+    LED_RGB.lock();
+    led_R=1;
+    led_G=1;
+    led_B=1;
+    LED_RGB.unlock();
 
-if (MMA.testConnection())   //setup accler
+    if (MMA.testConnection())   //setup accler
 
-    //start timer with a 2 sec timeout 
-    timer.start(2000); 
-    
+        //start timer with a 2 sec timeout
+        timer.start(2000);
+
     while (true) {
-     
-        
-        switch(state)
-            {
-            case STATE_0:            
+
+
+        switch(state) {
+            case STATE_0:
                 osEvent evt = queue.get();
-                if (evt.status == osEventMessage) 
-                    {
+                if (evt.status == osEventMessage) {
                     message_t *message = (message_t*)evt.value.p;
-                
-                       
-                    if(message->event == BUTTON_PRESS) 
-                        {
+
+
+                    if(message->event == BUTTON_PRESS) {
                         LED_RGB.lock();
                         led_G=0;
                         led_R=1;
@@ -214,9 +210,8 @@
                         lcd.locate(0,2);
                         lcd.printf("Enter");
                         state = STATE_1;
-                        }
-                    if(message->event == PUSH) 
-                        {
+                    }
+                    if(message->event == PUSH) {
                         LED_RGB.lock();
                         led_G=1;    //off
                         led_R=0;    //on
@@ -225,9 +220,8 @@
                         lcd.locate(0,2);
                         lcd.printf("Insert Coin push");
                         state = STATE_0;
-                        }
-                    if(message->event == TILT) 
-                        {
+                    }
+                    if(message->event == TILT) {
                         LED_RGB.lock();
                         led_G=1;
                         led_R=1;
@@ -236,12 +230,11 @@
                         lcd.locate(0,0);
                         lcd.printf("STOP");
                         lcd.locate(0,10);
-                        lcd.printf("Please Insert Coin");  
-                        Flash_LED_Thread.signal_set(0x1);                 
+                        lcd.printf("Please Insert Coin");
+                        Flash_LED_Thread.signal_set(0x1);
                         state = STATE_2;
-                        }                        
-                    if(message->event == TIME_OUT) 
-                        {
+                    }
+                    if(message->event == TIME_OUT) {
                         LED_RGB.lock();
                         led_G=1;
                         led_R=0;
@@ -250,31 +243,28 @@
                         lcd.locate(0,2);
                         lcd.printf("Hello");
                         state = STATE_0;
-                        }
+                    }
                     mpool.free(message);
-                    }
-                
+                }
+
                 timer.start(2000);
-            
-            break;
-            
+
+                break;
+
             case STATE_1:
-               
+
                 evt = queue.get();
-                if (evt.status == osEventMessage) 
-                    {
+                if (evt.status == osEventMessage) {
                     message_t *message = (message_t*)evt.value.p;
-                
-                       
-                    if(message->event == BUTTON_PRESS) 
-                        {
+
+
+                    if(message->event == BUTTON_PRESS) {
                         lcd.cls();
                         lcd.locate(0,2);
                         lcd.printf("Thanks");
                         state = STATE_1;
-                        }
-                    if(message->event == PUSH)
-                        {
+                    }
+                    if(message->event == PUSH) {
                         LED_RGB.lock();
                         led_R=0;
                         led_G=1;
@@ -283,25 +273,23 @@
                         lcd.locate(0,2);
                         lcd.printf("Hello");
                         state = STATE_0;
-                        }
-                                        
+                    }
+
                     mpool.free(message);
-                    }
-                 
-            timer.start(2000);
-                
-            
-            break;
-            
+                }
+
+                timer.start(2000);
+
+
+                break;
+
             case STATE_2:
 
-               evt = queue.get();
-               if (evt.status == osEventMessage) 
-                    {
+                evt = queue.get();
+                if (evt.status == osEventMessage) {
                     message_t *message = (message_t*)evt.value.p;
-                
-                    if(message->event == BUTTON_PRESS) 
-                        {
+
+                    if(message->event == BUTTON_PRESS) {
                         LED_RGB.lock();
                         led_G=0;
                         led_R=1;
@@ -310,20 +298,18 @@
                         lcd.locate(0,2);
                         lcd.printf("Enter");
                         state = STATE_1;
-                        
-                        }   
-                    if(message->event == TILT) 
-                        {
+
+                    }
+                    if(message->event == TILT) {
                         lcd.cls();
                         lcd.locate(0,0);
                         lcd.printf("STOP");
                         lcd.locate(0,10);
-                        lcd.printf("Please Insert Coin");  
+                        lcd.printf("Please Insert Coin");
                         state = STATE_2;
-                        Flash_LED_Thread.signal_set(0x1);                        
-                        }
-                    if(message->event == TIME_OUT)
-                        {
+                        Flash_LED_Thread.signal_set(0x1);
+                    }
+                    if(message->event == TIME_OUT) {
                         LED_RGB.lock();
                         led_R=0;
                         led_G=1;
@@ -332,22 +318,22 @@
                         lcd.locate(0,2);
                         lcd.printf("Insert Coin");
                         state = STATE_0;
-                        }
+                    }
                     mpool.free(message);
-                    }
+                }
 
                 timer.start(2000);
-            
-            break;          
-            
-          
-       
+
+                break;
+
+
+
         }//End of switch
-        
-    //toggle led for local testing    
-    //led2= !led2;
-                                       
+
+        //toggle led for local testing
+        //led2= !led2;
+
     }//end of while(1)
-    
-    
+
+
 }
\ No newline at end of file