Coursework

Revision:
11:1dec05b7d1c1
Parent:
10:42e70b596099
Child:
12:ceea378d5691
--- a/main.cpp	Fri Jan 07 12:09:52 2022 +0000
+++ b/main.cpp	Fri Jan 07 19:51:48 2022 +0000
@@ -14,7 +14,13 @@
 InterruptIn buttonB(PTD0);
 InterruptIn buttonX(PTC17);
 InterruptIn buttonY(PTC12);
+InterruptIn buttonStart(PTC5);
+InterruptIn buttonBack(PTB19);
+InterruptIn buttonLeft(PTB18);
+InterruptIn buttonRight(PTB3);
 
+// create an event queue
+EventQueue queue; // events are needed to get arround the printf issue 
 //rows,cols
 int sprite[8][5] =   {
     { 0,0,1,0,0 },
@@ -26,24 +32,29 @@
     { 1,1,0,1,1 },
     { 1,1,0,1,1 },
       };
-      void init_K64F();
-// Button A interrupt service routine
-void buttonA_isr();
-// Button B interrupt service routine
-void buttonB_isr();
-// Button X interrupt service routine
-void buttonX_isr();
-// Button Y interrupt service routine
-void buttonY_isr();
-volatile int g_buttonA_flag = 0;
-volatile int g_buttonB_flag = 0;
-volatile int g_buttonX_flag = 0;
-volatile int g_buttonY_flag = 0;
+void init_K64F();   //  Start K64fBoard
+
+void buttonA_isr(); // Button A interrupt service routine
+void buttonB_isr(); // Button B interrupt service routine
+void buttonX_isr(); // Button X interrupt service routine
+void buttonY_isr(); // Button Y interrupt service routine
+void buttonStart_isr(); // Button Start interrupt service routine
+void buttonBack_isr(); // Button Back interrupt service routine
+void buttonLeft_isr(); // Button Left interrupt service routine
+void buttonRight_isr(); // Button Right interrupt service routine
+volatile int g_buttonA_flag = 0; // Global Flag
+volatile int g_buttonB_flag = 0; // Global Flag
+volatile int g_buttonX_flag = 0; // Global Flag
+volatile int g_buttonY_flag = 0; // Global Flag
+volatile int g_buttonStart_flag = 0; // Global Flag
+volatile int g_buttonBack_flag = 0; // Global Flag
+volatile int g_buttonLeft_flag = 0; // Global Flag
+volatile int g_buttonRight_flag = 0; // Global Flag
+
+//volatile int g_buttonA_counter = 0; // Global counter
 //Test function after research C++ lanuage 05/01/22
 int cube(int num){
-     int result;
-     result = num*num;
-     return result;
+     return num*num;
       };
 // Menu Items
 int page1;
@@ -60,12 +71,12 @@
        Buzzer.play(200,120);;
  }
  */
- double printme(){
+ void printme(){
      
-     double x ;
-     x = 1;
+   volatile     double x ;
+  x = 1;
      printf("%f\n",x);
-     return 0;
+    // return 0;
      }
      
 
@@ -77,6 +88,18 @@
 //initialise Joystic
        joystick.init();
        
+         // create a thread that'll run the event queue's dispatch function
+  Thread eventThread;
+  eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
+ 
+  buttonA.rise(queue.event(&buttonA_isr)); // This is used to call the function putting it in queue allowing the screen toupdat no muxt error
+  buttonB.rise(queue.event(&buttonB_isr));
+  buttonX.rise(queue.event(&buttonX_isr));
+  buttonY.rise(queue.event(&buttonY_isr));
+  buttonStart.rise(queue.event(&buttonStart_isr)); // This is used to call the function putting it in queue allowing the screen toupdat no muxt error
+  buttonBack.rise(queue.event(&buttonBack_isr));
+  buttonLeft.rise(queue.event(&buttonLeft_isr));
+  buttonRight.rise(queue.event(&buttonRight_isr));
 //first need to initialise display
        lcd.init();    
 
@@ -88,15 +111,23 @@
     buttonX.mode(PullDown);
     // Button Y is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
     buttonY.mode(PullDown);
+    // Button Start is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
+    buttonStart.mode(PullDown);
+    // Button Back is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
+    buttonBack.mode(PullDown);
+    // Button Left is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
+    buttonLeft.mode(PullDown);
+    // Button Right is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
+    buttonRight.mode(PullDown);
     
 // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
-   buttonA.rise(&buttonA_isr);
+ //  buttonA.rise(&buttonA_isr);
    // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
-   buttonB.rise(&buttonB_isr);
+ //  buttonB.rise(&buttonB_isr);
    // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
-   buttonX.rise(&buttonX_isr);
+   //buttonX.rise(&buttonX_isr);
    // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
-   buttonY.rise(&buttonY_isr);
+  // buttonY.rise(&buttonY_isr);
 
 
        
@@ -140,11 +171,17 @@
 //bool button = buttonA.read();
 //printf("%f\n", g_buttonA_flag );
 //Recording button presses
+int result;
+ result = cube(5);
+    printf("Button A pressed  %f\n", cube(5));
+int button_counter;
 if (g_buttonA_flag){
+                    button_counter = button_counter +1;
+                    //case (test)
                    g_buttonA_flag = 0;  // if it has, clear the flag
 
             // send message over serial port - can observe in CoolTerm etc.
-            printf("Button A pressed\n");
+            printf("Button A pressed  %f\n", button_counter);
             }
 if (g_buttonB_flag){
                    g_buttonB_flag = 0;  // if it has, clear the flag
@@ -160,7 +197,7 @@
                     lcd.clear();
         lcd.printString("Button Pressed!",0,0);
             lcd.refresh();
-            wait(10);
+            wait(1);
             }
 if (g_buttonY_flag){
                    g_buttonY_flag = 0;  // if it has, clear the flag
@@ -232,20 +269,69 @@
   //Button A event-triggered interrupt
 void buttonA_isr()
 {
-    g_buttonA_flag = 1;   // set flag in ISR
-}
+    g_buttonA_flag = 1;  
+      //  printf("flag is now %f\n", g_buttonA_flag); 
+        lcd.clear(); 
+        lcd.printString("Button A Pressed",0,0);
+         lcd.refresh(); // set flag in ISR
+       
+        };
+    
+
+//}
   //Button B event-triggered interrupt
 void buttonB_isr()
 {
     g_buttonB_flag = 1;   // set flag in ISR
+       lcd.clear(); 
+        lcd.printString("Button B Pressed",0,0);
+         lcd.refresh(); // set flag in ISR
 }
   //Button X event-triggered interrupt
 void buttonX_isr()
 {
     g_buttonX_flag = 1;   // set flag in ISR
+       lcd.clear(); 
+        lcd.printString("Button X Pressed",0,0);
+         lcd.refresh(); // set flag in ISR
 }
   //Button Y event-triggered interrupt
 void buttonY_isr()
 {
     g_buttonY_flag = 1;   // set flag in ISR
+       lcd.clear(); 
+        lcd.printString("Button Y Pressed",0,0);
+         lcd.refresh(); // set flag in ISR
 }
+  //Button Start event-triggered interrupt
+void buttonStart_isr()
+{
+    g_buttonStart_flag = 1;   // set flag in ISR
+       lcd.clear(); 
+        lcd.printString("Button Start Pressed",0,0);
+         lcd.refresh(); // set flag in ISR
+         }
+           //Button Back event-triggered interrupt
+void buttonBack_isr()
+{
+    g_buttonBack_flag = 1;   // set flag in ISR
+       lcd.clear(); 
+        lcd.printString("Button Back Pressed",0,0);
+         lcd.refresh(); // set flag in ISR
+         }
+           //Button Left event-triggered interrupt
+void buttonLeft_isr()
+{
+    g_buttonLeft_flag = 1;   // set flag in ISR
+       lcd.clear(); 
+        lcd.printString("Button Left Pressed",0,0);
+         lcd.refresh(); // set flag in ISR
+         }
+  //Button Right event-triggered interrupt
+void buttonRight_isr()
+{
+    g_buttonRight_flag = 1;   // set flag in ISR
+       lcd.clear(); 
+        lcd.printString("Button Right Pressed",0,0);
+         lcd.refresh(); // set flag in ISR
+         }
\ No newline at end of file