Coursework

Revision:
19:32f9fc794ef8
Parent:
18:bb36d96142dd
--- a/main.cpp	Sat Jan 15 23:02:39 2022 +0000
+++ b/main.cpp	Sun Jan 16 20:27:42 2022 +0000
@@ -78,30 +78,17 @@
 string Listitem2 = "Measure"; // measure distance of object
 string Listitem3 = "Detect"; // alarm when object is dectected
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
- /* Test of the interup an operating the buzzer this kills the board 
- void buzzme(){
-        Buzzer.play(200,120);
-            wait_ms(5);
-       Buzzer.play(200,120);;
- }
- */
- void printme(){
-     
-   volatile     double x ;
-  x = 1;
-     printf("%f\n",x);
-    // return 0;
-     }
-     
 
-//void returnme(){
-//int num;
-  //    return num*num;
-  //    };
 int main() {
 //initialise Joystick
-       joystick.init();
+    joystick.init();
+//first need to initialise display
+       lcd.init();
+//change set contrast in range 0.0 to 1.0
+//0.5 appears to be a good starting point
+       lcd.setContrast(0.5);
        
+        lcd.clear();       
          // create a thread that'll run the event queue's dispatch function
   Thread eventThread;
   eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
@@ -114,9 +101,7 @@
   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();    
-
+    
     // Button A is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
     buttonA.mode(PullDown);
     // Button B is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
@@ -133,143 +118,40 @@
     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);
-    
-
-
-
-       
-//change set contrast in range 0.0 to 1.0
-//0.5 appears to be a good starting point
-       lcd.setContrast(0.5);
-       
-//Buzzer.period(10.0f);
-//Buzzer.pulsewidth(1);   
-//Test Struct after research 05/01/22
-struct ObjectDefine{
-int qwer;
-    float  Distance;
-    char te;
-    double dubs;
- string namestruct;
-   // string ;
-};
-// Test Class 06/01/22
-   class myClass{
-   public:
-   string nameclass ;
-   double dubss;
-   int inty;
-   };
-   myClass Class1;
-   Class1.nameclass ="firstclass";
-   ObjectDefine Object1;
-   Object1.qwer = 12;
-   Object1.Distance = 34;
-   Object1.te = 'A';
-   Object1.dubs = 23;
-   Object1.namestruct = "name";
  
     while(1) {
 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  %f\n", button_counter);
-            }
-if (g_buttonB_flag){
-                   g_buttonB_flag = 0;  // if it has, clear the flag
-
-            // send message over serial port - can observe in CoolTerm etc.
-            printf("Button B pressed\n");
-            }
-if (g_buttonX_flag){
-                   g_buttonX_flag = 0;  // if it has, clear the flag
-                               // swap direction when button has been pressed
-            // (could just use ! but want this to be explicit to aid understanding)
-            if (direction == UP) {
-                direction = DOWN;    
-            }
-            else {
-                direction = UP;
-            }
-
-            // send message over serial port - can observe in CoolTerm etc.
-            printf("Button X pressed\n");
-                    lcd.clear();
-        lcd.printString("Button Pressed!",0,0);
-            lcd.refresh();
-            wait(1);
-            }
-if (g_buttonY_flag){
-                   g_buttonY_flag = 0;  // if it has, clear the flag
-
-            // send message over serial port - can observe in CoolTerm etc.
-            printf("Button Y pressed\n");
-            }
-       Buzzer.play(200,120);
+     switch(PressA){
+     case 1: 
+     PressA= 1;             
+             lcd.clear();     
+     lcd.printString("Menu 1",0,1);
+     lcd.drawRect(0,0,40,10,FILL_TRANSPARENT);         
+    lcd.refresh();
+    wait_ms(20);
+    
+        break;
+        
+     case 2: 
+    PressA= 2;
+    lcd.clear();
+       lcd.printString("Menu 2",0,0);        
+         lcd.refresh();
+                Buzzer.play(200,120);
             wait_ms(5);
        Buzzer.play(200,120);
-        long distanced = sensor.distance(); 
-        if (distanced >= 400 || distanced <= 2) 
-        {
-        printf("Out of range");
-//Calling dummy function 05/01/22
-        int answer = cube(2);
-        printf("%f\n",answer);                  
-        wait(1.0);
-        }
-    else    
-    {
-       printf("Distance :  %d cm",distanced);
-       wait(1.0); // 1 sec  
-    }
-        lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK); 
-//lcd.refresh must be used to update the lcd Display
-        lcd.refresh();
-        wait(5.0);
-//these are default settings so not strictly needed
-        lcd.normalMode();      // normal colour mode
-        lcd.setBrightness(0.5); // put LED backlight on 50%        
-        lcd.clear();
-        lcd.printString("Hello, World!",0,0);
-        Vector2D coord = joystick.get_coord();
-        lcd.refresh();
-        printf("Coord = %f,%f\n",coord.x,coord.y);
-        char buffer[14];
-        Vector2D mapped_coord = joystick.get_mapped_coord(); 
-        printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y); 
-        
-         int length = sprintf(buffer,"T = %2f",coord.x);
-//it is important the format specifier ensures the length will fit in the buffer
-        if (length <= 14)  // if string will fit on display (assuming printing at x=0)
-           lcd.printString(buffer,0,1);  // display on screen
-        lcd.refresh();
-        lcd.clear();
-//times by 50 to try get the point moving across screen
-         float x50 = mapped_coord.x*5;
-         float y50 = mapped_coord.y*8;
-//to see what the values are 
-         printf(" *50 = %f,%f\n",x50,y50);       
-//example of how to draw circles
-        lcd.printChar('o',WIDTH/2,HEIGHT/2);
-        lcd.refresh();      
-        float mag = joystick.get_mag();
-        float angle = joystick.get_angle();
-        printf("Mag = %f Angle = %f\n",mag,angle);
-        Direction d = joystick.get_direction();
-        printf("Direction = %i\n",d);
-        if (joystick.button_pressed() ) {
-            printf("Button Pressed\n");  
-            wait(5);
-        }
-    
-    }  
+     break;
+     case 3:
+              PressA= 3;
+         lcd.clear();
+       lcd.printString("Menu 3",0,0);        
+         lcd.refresh();
+         wait_ms(30);
+     break;
  }
- 
- 
+ }
+ }
  
    int buttonCountA(){
         PressA++;
@@ -292,46 +174,12 @@
     
     long distanced = sensor.distance(); 
     buttonCountA();
-     switch(PressA){
-     case 1: 
-     PressA= 1;
-     for(int i = 0; i < 5; i++){
-             
-             lcd.clear();     
-     lcd.printString("Menu 1",0,0);        
-    lcd.refresh();
-    lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK);
-    lcd.refresh();}
-    
-        break;
-        
-     case 2: 
-    PressA= 2;
-    lcd.clear();
-       lcd.printString("Menu 2",0,0);        
-         lcd.refresh();
-     break;
-     case 3:
-              PressA= 3;
-         lcd.clear();
-       lcd.printString("Menu 3",0,0);        
-         lcd.refresh();
-     break;
-     }
      
-        
     
     g_buttonA_flag = 1;   // set flag in ISR
   
         printf("press count = %i\n",PressA);
-            
-      // lcd.clear(); 
-        //lcd.printString("Button A Pressed",0,0);
-        
-     //    lcd.refresh(); // set flag in ISR
-}
- 
-
+            }
   //Button B event-triggered interrupt
 void buttonB_isr()
 {