finished

Dependencies:   C12832 DebouncedInterrupt MMA7660 mbed-rtos mbed

Revision:
1:57407e283b03
Parent:
0:151f0dbf3ca8
Child:
2:ff8262f6a385
diff -r 151f0dbf3ca8 -r 57407e283b03 main.cpp
--- a/main.cpp	Thu Feb 26 15:00:42 2015 +0000
+++ b/main.cpp	Mon Mar 02 12:56:15 2015 +0000
@@ -1,19 +1,25 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "C12832.h"
+#include "MMA7660.h"
+
+
+
 
 //FINITE STATE MACHINE EVENTS
 #define NO_EVENT 0
 #define TIME_OUT 1
 #define BUTTON_PRESS 2
+#define TILT 3
+#define PUSH 4
 
 //STATES
 #define STATE_0 0
 #define STATE_1 1
 #define STATE_2 2
-#define STATE_3 3
-#define STATE_4 4
-#define STATE_5 5
+
+//Mutex
+Mutex LED_RGB;
 
 //pass event via message queue
 typedef struct {
@@ -23,26 +29,29 @@
 MemoryPool<message_t, 16> mpool;
 Queue<message_t, 16> queue;
 
-//button plus local display
+//Digital input
 DigitalIn coin(p14);
+InterruptIn button(p15);
 
+//Analog input
+AnalogIn push(p19);
+
+//Setup hardware
+MMA7660 MMA(p28, p27);
 C12832 lcd(p5, p7, p6, p8, p11);
 
-//leds for debug
+//RGB LEDs
 DigitalOut led_R(p23); //LED RGB red
 DigitalOut led_G(p24); //LED RGB green
+DigitalOut led_B(p25); //LED RGB Blue
 
+//leds for debug
 DigitalOut led4(LED4); //LED RGB green
 DigitalOut led3(LED3); //LED RGB green
 
     
 void timeout_event(void const *n) 
 {
-        //generate local display info
-        lcd.cls();
-        lcd.locate(40,5);
-        lcd.printf("timeout event");
-        
        
         //event via a message queue
         message_t *message = mpool.alloc();
@@ -59,17 +68,11 @@
     while (true) {
      
         //debouce delay for switch
-        
         if (coin)
             {
                 button_press = 1;
             if (button_press == 1 & coin == 0)
                 {
-             
-                lcd.cls();    
-                lcd.locate(0,1);
-                lcd.printf("button event");    
-                    
                
                 //event via a message queue
                 message_t *message = mpool.alloc();
@@ -80,44 +83,170 @@
                 button_press = 0;
                 }
             }
-        else 
-            {
-                //button_press = coin;
-                //Thread::wait(100);
-            }
+            
+          
     }
 }
 
+
+void tilt_event_thread(void const *argument) {
+    float tilt_value_Y = MMA.y();
+    float tilt_value_X = MMA.x();
+    while (true) 
+        {
+     
+        //debouce delay for switch
+        Thread::wait(2000);
+
+        if (tilt_value_Y <= MMA.y()-0.1 ^ tilt_value_Y >= MMA.y()+0.1 )
+            {
+
+            //event via a message queue
+            message_t *message = mpool.alloc();
+            message->event = TILT; 
+            queue.put(message);
+            
+            led3 = !led3;
+            tilt_value_Y = MMA.y();
+            }
+            
+       
+        }
         
+    
+}
+ 
+void push_event_thread(void const *argument) {
+    float push_value = push.read();
+    while (true) 
+        {
+     
+        //debouce delay for switch
+        Thread::wait(1000);
+        if (push_value >= push.read()- 0.05 ^ push_value <= push.read()+ 0.05 )
+            {
+            push_value = push.read();
+            //event via a message queue
+            message_t *message = mpool.alloc();
+            message->event = PUSH; 
+            queue.put(message);
+            led3 = !led3;
+            
+            }
+        }
+        
+    
+}
+void flash_led_thread(void const *argument) {
+    while (true) 
+        {
+        Thread::signal_wait(0x1);
+    
+        for (int i=0;i<=20;i++)
+            {
+            LED_RGB.lock();
+            led_R = !led_R; 
+            LED_RGB.unlock(); 
+            Thread::wait(100);
+            }
+        LED_RGB.lock();
+        led_R = 1; 
+        LED_RGB.unlock();             
+        }
+     
+}      
+
+void Button_Inter() {
+ 
+//Flash_LED_Thread.signal_set(0x1);
+     
+}  
+
+    
 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);
+
 int state = STATE_0;
 
- 
+LED_RGB.lock();
+led_R=1;
+led_G=1;
+led_B=1;
+LED_RGB.unlock();
+
+if (MMA.testConnection())   //setup accler
+
     //start timer with a 2 sec timeout 
     timer.start(2000); 
     
     while (true) {
+     
+        
         switch(state)
             {
-            case STATE_0:
-                lcd.cls();
-                lcd.locate(0,2);
-                lcd.printf("FSM START");
-                
+            case STATE_0:            
                 osEvent evt = queue.get();
                 if (evt.status == osEventMessage) 
                     {
                     message_t *message = (message_t*)evt.value.p;
                 
                        
-                    if(message->event == BUTTON_PRESS) state = STATE_1;
-                    if(message->event == TIME_OUT) state = STATE_0;
-                
+                    if(message->event == BUTTON_PRESS) 
+                        {
+                        LED_RGB.lock();
+                        led_G=0;
+                        led_R=1;
+                        LED_RGB.unlock();
+                        lcd.cls();
+                        lcd.locate(0,2);
+                        lcd.printf("Enter");
+                        state = STATE_1;
+                        }
+                    if(message->event == PUSH) 
+                        {
+                        LED_RGB.lock();
+                        led_G=1;    //off
+                        led_R=0;    //on
+                        LED_RGB.unlock();
+                        lcd.cls();
+                        lcd.locate(0,2);
+                        lcd.printf("Insert Coin push");
+                        state = STATE_0;
+                        }
+                    if(message->event == TILT) 
+                        {
+                        LED_RGB.lock();
+                        led_G=1;
+                        led_R=1;
+                        LED_RGB.unlock();
+                        lcd.cls();
+                        lcd.locate(0,0);
+                        lcd.printf("STOP");
+                        lcd.locate(0,10);
+                        lcd.printf("Please Insert Coin");  
+                        Flash_LED_Thread.signal_set(0x1);                 
+                        state = STATE_2;
+                        }                        
+                    if(message->event == TIME_OUT) 
+                        {
+                        LED_RGB.lock();
+                        led_G=1;
+                        led_R=0;
+                        LED_RGB.unlock();
+                        lcd.cls();
+                        lcd.locate(0,2);
+                        lcd.printf("Hello");
+                        state = STATE_0;
+                        }
                     mpool.free(message);
                     }
                 
@@ -126,41 +255,80 @@
             break;
             
             case STATE_1:
-                lcd.cls();
-                lcd.locate(0,2);
-                lcd.printf("FSM COUNTER  1"); 
-                
+               
                 evt = queue.get();
                 if (evt.status == osEventMessage) 
                     {
                     message_t *message = (message_t*)evt.value.p;
                 
                        
-                    if(message->event == BUTTON_PRESS) state = STATE_2;
-                    if(message->event == TIME_OUT) state = STATE_0;
-                
+                    if(message->event == BUTTON_PRESS) 
+                        {
+                        lcd.cls();
+                        lcd.locate(0,2);
+                        lcd.printf("Thanks");
+                        state = STATE_1;
+                        }
+                    if(message->event == PUSH)
+                        {
+                        LED_RGB.lock();
+                        led_R=0;
+                        led_G=1;
+                        LED_RGB.unlock();
+                        lcd.cls();
+                        lcd.locate(0,2);
+                        lcd.printf("Hello");
+                        state = STATE_0;
+                        }
+                                        
                     mpool.free(message);
                     }
                  
-             
-                timer.start(2000);
+            timer.start(2000);
+                
             
             break;
             
             case STATE_2:
-                lcd.cls();
-                lcd.locate(0,2);
-                lcd.printf("FSM COUNTER  2"); 
 
                evt = queue.get();
                if (evt.status == osEventMessage) 
                     {
                     message_t *message = (message_t*)evt.value.p;
                 
-                       
-                    if(message->event == BUTTON_PRESS) state = STATE_3;
-                    if(message->event == TIME_OUT) state = STATE_1;
-                
+                    if(message->event == BUTTON_PRESS) 
+                        {
+                        LED_RGB.lock();
+                        led_G=0;
+                        led_R=1;
+                        LED_RGB.unlock();
+                        lcd.cls();
+                        lcd.locate(0,2);
+                        lcd.printf("Enter");
+                        state = STATE_1;
+                        
+                        }   
+                    if(message->event == TILT) 
+                        {
+                        lcd.cls();
+                        lcd.locate(0,0);
+                        lcd.printf("STOP");
+                        lcd.locate(0,10);
+                        lcd.printf("Please Insert Coin");  
+                        state = STATE_2;
+                        Flash_LED_Thread.signal_set(0x1);                        
+                        }
+                    if(message->event == TIME_OUT)
+                        {
+                        LED_RGB.lock();
+                        led_R=0;
+                        led_G=1;
+                        LED_RGB.unlock();
+                        lcd.cls();
+                        lcd.locate(0,2);
+                        lcd.printf("Insert Coin");
+                        state = STATE_0;
+                        }
                     mpool.free(message);
                     }
 
@@ -168,71 +336,8 @@
             
             break;          
             
-            case STATE_3:
-                lcd.cls();
-                lcd.locate(0,2);
-                lcd.printf("FSM COUNTER  3"); 
-                
-                evt = queue.get();
-               if (evt.status == osEventMessage) 
-                    {
-                    message_t *message = (message_t*)evt.value.p;
-                
-                       
-                    if(message->event == BUTTON_PRESS) state = STATE_4;
-                    if(message->event == TIME_OUT) state = STATE_2;
-                
-                    mpool.free(message);
-                    }
-                 
-               
-                timer.start(2000);
-            
-            break;    
-            
-            case STATE_4:
-                lcd.cls();
-                lcd.locate(0,2);
-                lcd.printf("FSM COUNTER  4"); 
-                
-                evt = queue.get();
-                if (evt.status == osEventMessage) 
-                    {
-                    message_t *message = (message_t*)evt.value.p;
-                
-                       
-                    if(message->event == BUTTON_PRESS) state = STATE_5;
-                    if(message->event == TIME_OUT) state = STATE_3;
-                
-                    mpool.free(message);
-                    }
-    
-                timer.start(2000);
-            
-            break;    
-            
-            case STATE_5:
-                lcd.cls();
-                lcd.locate(0,2);
-                lcd.printf("FSM COUNTER  5"); 
-                
-                evt = queue.get();
-                if (evt.status == osEventMessage) 
-                    {
-                    message_t *message = (message_t*)evt.value.p;
-                
-                       
-                    if(message->event == BUTTON_PRESS) state = STATE_5;
-                    if(message->event == TIME_OUT) state = STATE_4;
-                
-                    mpool.free(message);
-                    }
-                 
-
-                
-                timer.start(2000);
-            
-            break;  
+          
+       
         }//End of switch
         
     //toggle led for local testing