Coursework

Revision:
28:aab0aa846cf6
Parent:
27:d48bf4e8f1a0
Child:
29:1aef0ecf9840
diff -r d48bf4e8f1a0 -r aab0aa846cf6 main.cpp
--- a/main.cpp	Tue Jan 25 20:45:48 2022 +0000
+++ b/main.cpp	Wed Jan 26 21:12:31 2022 +0000
@@ -1,3 +1,12 @@
+/**
+*.....Kory Hamill
+*.....IOT Smart Electronics Coursework
+*.....Acknowlegements Libary Makers 
+*.....18689004
+*/
+/**
+* Section of code to bring in the #includes
+*/
 #include "mbed.h"
 #include "Joystick.h"
 #include "N5110.h"
@@ -6,25 +15,24 @@
 #include "string"
 #include "array"
 #include <iostream>
-// FSM Menu 
-// defines directions as 0/1. Note UPPERCASE
-#define UP 0
-#define DOWN 1
-// set inital state
-  volatile  int state = 0;
-    // set initial direction
-    int direction = UP;
-// array of states in the FSM, each element is the output of the counter
-// set the output in binary to make it easier, 0 is LED on, 1 is LED off
-string fsm[4] = {"buttonA_isr","buttonB_isr","buttonAsss_isr","buttonA_isr"};
-volatile int PressA;
-volatile int PressB;
+/**
+*...This Section of the code is used to bring in volatile Varible's, These can be wrote to throught the code.
+*...The Varibles are used to track button press's on the board.
+*/
+volatile int g_ButtonAPress;
+volatile int g_ButtonBPress;
+volatile int g_buttonA_flag = 0; 
+volatile int g_buttonB_flag = 0; 
+volatile int g_buttonX_flag = 0; 
+volatile int g_buttonY_flag = 0; 
+volatile int g_buttonStart_flag = 0; 
+volatile int g_buttonBack_flag = 0; 
+volatile int g_buttonLeft_flag = 0; 
+volatile int g_buttonRight_flag = 0;
 
-// y  x  button
-Joystick joystick(PTB10,PTB11,PTC16);
-HCSR04 sensor(D14, D15); 
-Piezo Buzzer(PTC10);
-//Bringing in buttons
+/**
+*... Section of code to assign names to IO pins on the K64f
+*/
 InterruptIn buttonA(PTB9);
 InterruptIn buttonB(PTD0);
 InterruptIn buttonX(PTC17);
@@ -33,22 +41,29 @@
 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
+Joystick joystick(PTB10,PTB11,PTC16);
+HCSR04 sensor(D14, D15); 
+Piezo Buzzer(PTC10);
+N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
+ 
+/**
+*...For used with the LCD Screen. rows,cols
+*/
 int sprite[8][5] =   {
-    { 0,0,1,0,0 },
-    { 0,1,1,1,0 },
-    { 0,0,1,0,0 },
-    { 0,1,1,1,0 },
-    { 1,1,1,1,1 },
-    { 1,1,1,1,1 },
-    { 1,1,0,1,1 },
-    { 1,1,0,1,1 },
-      };
+           { 0,0,1,0,0 },
+           { 0,1,1,1,0 },
+           { 0,0,1,0,0 },
+           { 0,1,1,1,0 },
+           { 1,1,1,1,1 },
+           { 1,1,1,1,1 },
+           { 1,1,0,1,1 },
+           { 1,1,0,1,1 },
+                       };
+/**
+*...Section for starting the board and seeting up interrupt routines
+*...Along with other functions to be called later
+*/
 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
@@ -57,16 +72,15 @@
 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
 int buttonCountA();
 int buttonCountB();
+
+EventQueue queue; /**< Required to get around issues with printing onto LCD and the Printf */
+
+
+/** Code which was never used
+*
+*.. was for initial learning of C++
 //volatile int g_buttonA_counter = 0; // Global counter
 //Test function after research C++ lanuage 05/01/22
 int cube(int num){
@@ -76,25 +90,15 @@
 int page1;
 int page2;
 int page3;
-string Listitem1 = "Game"; // guess distance
-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 
+string Listitem1 = "Game"; // 
+string Listitem2 = "Measure"; // 
+string Listitem3 = "Detect"; // 
+/ 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;
-     }
-     
        int Scan1[5];
        int Scan2[5];
         int Scan1Input(){;        
@@ -103,60 +107,52 @@
          Scan1[2] = sensor.distance();;
          Scan1[3] = sensor.distance();;
          Scan1[4] = sensor.distance();;
-
         return Scan1[0,1,2,3,4];}
-
-//void returnme(){
+        //void returnme(){
 //int num;
   //    return num*num;
   //    };
+ */
+
 int main() {
-//initialise Joystick
+/**
+*.. Intialise joystick and Lcd
+*/
        joystick.init();
-       
-         // create a thread that'll run the event queue's dispatch function
+            lcd.init();
+  lcd.setContrast(0.5);
+/**
+*.. Issues were found with the LCd and printf while using interrupts,  to get around
+*.. this issue these queue's to the event thread were added.
+*/
   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
+  eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); 
+  buttonA.rise(queue.event(&buttonA_isr)); 
   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
+  buttonStart.rise(queue.event(&buttonStart_isr)); 
   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
+  /**
+  *..Buttons were connected to IO pins which required a pull down resistor to be active.
+  */
     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
     buttonB.mode(PullDown);
-    // Button X is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
     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);
-    
-
-
-
        
-//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);
+/**
+*.. Section of code not used in main program.
+*
+Buzzer.period(10.0f);
 //Buzzer.pulsewidth(1);   
-//Test Struct after research 05/01/22
+Test Struct after research 05/01/22
 struct ObjectDefine{
 int qwer;
     float  Distance;
@@ -180,20 +176,22 @@
    Object1.te = 'A';
    Object1.dubs = 23;
    Object1.namestruct = "name";
- 
+*/ 
     while(1) {
-        // section to try to get POC of number comparitor without using the US for repeatablity// to get if statements correct possibly change to switch 
+/**
+*..Bring in 5 arrays of 5 for each of the 5 required scans.
+*..array Ref starts at 0,
+*/ 
          int Scan1[5];
          int Scan2[5];
          int Scan3[5];
          int Scan4[5];
          int Scan5[5];
-         int num2;
-         int num3;
-         int num4;
-char Decide[5];
+         char Decide[5];
         
-       //  IF Statement to decide on shape to draw
+/**  IF Statement to decide on shape to draw
+*.. This section of code compares the each array element with the one proceding it and makes a logical decision if higher lower or equal.
+*/
           if (Scan1[0] > Scan1[1]){
             printf("+");
             Decide[0]= 'x';
@@ -236,10 +234,11 @@
               //  cout <<'Lower';}
                printf("Lower \n");}
     
-
-       // cout << "Test value collection";
-     //Scan1Input();    
-     /*
+/**
+*.. It was found Cout was not working as expected.
+ cout << "Test value collection";
+Scan1Input();    
+    
 cout << Scan1[0];cout <<"/n";
 cout << Scan1[1];"/n";
 cout << Scan1[2];"/n";
@@ -252,8 +251,7 @@
 cout << Scan1[9];"/n";
 */
 
-printf("press count = %i\n",PressA);
-
+printf("press count = %i\n",g_ButtonAPress);
  printf("Scan1 1 :  %d cm\n", Scan1[0]);
   printf("Scan1 2 :  %d cm\n", Scan1[1]);
    printf("Scan1 3 :  %d cm\n", Scan1[2]);
@@ -278,16 +276,14 @@
   printf("Scan5 2 :  %d cm\n", Scan5[1]);
    printf("Scan5 3 :  %d cm\n", Scan5[2]);
      printf("Scan5 4 :  %d cm\n", Scan5[3]);
-      printf("Scan5 5 :  %d cm\n", Scan5[4]);
-
-           
+      printf("Scan5 5 :  %d cm\n", Scan5[4]);           
  printf("I always Get printed \n");
-        
      //   cout << Scan1[2];
        // cout << Scan1[6];
-
-              printf("I Should Be printed always \n"); 
-               /*
+printf("I Should Be printed always \n"); 
+/**
+*..Code section on Comparing data Cout was causing issue
+*
                 if (Scan1[1] > Scan1[2]){
             cout <<'+';
             }else if (Scan1[1]=Scan1[2]){
@@ -344,15 +340,18 @@
             cout <<'=';}
             else {
                 cout <<'-';}
-                */
+*/
         
 /////////////////////////////////////////////////////////////////////////////////////////////////////
-        switch(PressA){
+/**
+*..Initial state when starting the Board.
+*/
+        switch(g_ButtonAPress){
      default:
-     lcd.clear();     
+     lcd.clear();
+
      lcd.printString("Main Menu",0,0);
-     lcd.drawLine(0,10,80,10,2);
-      
+     lcd.drawLine(0,10,80,10,2);      
      lcd.printString("A to begin new Scan",0,2);
      lcd.printString("Scan Starts move pad",0,3);
      lcd.printString("B to display Shape",0,4);
@@ -362,13 +361,18 @@
      wait_ms(50);
      
      break;
+
 //////////////////////////////////////////////////////////////////////////////////////////////////     
+/**
+*.. When Button A is pressed this section of code is ran to collect data points of the object.
+*..Scanning takes marks the LCD with locationof points which data has been collected for.
+*/     
      case 1:
-     PressA =1;
+     g_ButtonAPress =1;
       lcd.clear();        
       lcd.printString("First Scan",0,0); 
       lcd.refresh();
-         Buzzer.play(300,120);   lcd.printString("X",0,1); lcd.refresh();     Scan1[0] = sensor.distance();
+         Buzzer.play(300,120);  lcd.printString("X",0,1); lcd.refresh();       Scan1[0] = sensor.distance();
          wait(2);
          Buzzer.play(200,120);  lcd.printString("X",0,2);lcd.refresh();        Scan1[1] = sensor.distance();
          wait(2);
@@ -378,12 +382,13 @@
          wait(2);
          Buzzer.play(200,120);  lcd.printString("X",0,5);lcd.refresh();         Scan1[4] = sensor.distance();;
          wait(2);
-         lcd.printString("Scan 1 Complete",0,0); lcd.refresh();
+         lcd.printString("Scan 1 Done",0,0); lcd.refresh();
+
          wait(2); // required to stop Black screen issue}
          lcd.clear();lcd.refresh();
+         lcd.printString("Second Scan",0,0);
          lcd.printString("X",0,1),lcd.printString("X",0,2),lcd.printString("X",0,3),lcd.printString("X",0,4),lcd.printString("X",0,5); lcd.refresh();
-         lcd.printString("Second Scan",0,0); 
-         Buzzer.play(300,120);   lcd.printString("X",15,1); lcd.refresh();     Scan2[0] = sensor.distance();
+         Buzzer.play(300,120);  lcd.printString("X",15,1); lcd.refresh();       Scan2[0] = sensor.distance();
          wait(2);
          Buzzer.play(200,120);  lcd.printString("X",15,2);lcd.refresh();        Scan2[1] = sensor.distance();
          wait(2);
@@ -393,58 +398,61 @@
          wait(2);
          Buzzer.play(200,120);  lcd.printString("X",15,5);lcd.refresh();         Scan2[4] = sensor.distance();
          lcd.printString("Scan 2 Done",0,0); lcd.refresh();
+
+         wait(2); // required to stop Black screen issue}
          lcd.clear();lcd.refresh();
+         lcd.printString("Third Scan",0,0); 
          lcd.printString("X",0,1),lcd.printString("X",0,2),lcd.printString("X",0,3),lcd.printString("X",0,4),lcd.printString("X",0,5); lcd.refresh();
          lcd.printString("X",15,1),lcd.printString("X",15,2),lcd.printString("X",15,3),lcd.printString("X",15,4),lcd.printString("X",15,5); lcd.refresh();
-         lcd.printString("Third Scan",0,0); 
-         Buzzer.play(300,120);   lcd.printString("X",30,1); lcd.refresh();     Scan3[0] = sensor.distance();
+         Buzzer.play(300,120);  lcd.printString("X",30,1); lcd.refresh();       Scan3[0] = sensor.distance();
          wait(2);
          Buzzer.play(200,120);  lcd.printString("X",30,2);lcd.refresh();        Scan3[1] = sensor.distance();
          wait(2);
          Buzzer.play(200,120);  lcd.printString("X",30,3);lcd.refresh();        Scan3[2] = sensor.distance();
          wait(2);
-         Buzzer.play(200,120);  lcd.printString("X",30,4);lcd.refresh();         Scan3[3] = sensor.distance();;
+         Buzzer.play(200,120);  lcd.printString("X",30,4);lcd.refresh();         Scan3[3] = sensor.distance();
          wait(2);
-         Buzzer.play(200,120);  lcd.printString("X",30,5);lcd.refresh();         Scan3[4] = sensor.distance();;
-         lcd.printString("Scan 3 Done",0,0); lcd.refresh();  
+         Buzzer.play(200,120);  lcd.printString("X",30,5);lcd.refresh();         Scan3[4] = sensor.distance();
+         lcd.printString("Scan 3 Done",0,0); lcd.refresh(); 
+
+         wait(2); // required to stop Black screen issue}
+         lcd.printString("Fourth Scan",0,0); 
          lcd.printString("X",0,1),lcd.printString("X",0,2),lcd.printString("X",0,3),lcd.printString("X",0,4),lcd.printString("X",0,5); lcd.refresh();
          lcd.printString("X",15,1),lcd.printString("X",15,2),lcd.printString("X",15,3),lcd.printString("X",15,4),lcd.printString("X",15,5); lcd.refresh();
-         lcd.printString("X",30,1),lcd.printString("X",30,2),lcd.printString("X",30,3),lcd.printString("X",30,4),lcd.printString("X",30,5); lcd.refresh();
-         wait(2);
-         lcd.printString("Fourth Scan",0,0); 
-         Buzzer.play(300,120);   lcd.printString("X",45,1); lcd.refresh();     Scan4[0] = sensor.distance();
+         lcd.printString("X",30,1),lcd.printString("X",30,2),lcd.printString("X",30,3),lcd.printString("X",30,4),lcd.printString("X",30,5); lcd.refresh();        
+         Buzzer.play(300,120);  lcd.printString("X",45,1); lcd.refresh();       Scan4[0] = sensor.distance();
          wait(2);
          Buzzer.play(200,120);  lcd.printString("X",45,2);lcd.refresh();        Scan4[1] = sensor.distance();
          wait(2);
          Buzzer.play(200,120);  lcd.printString("X",45,3);lcd.refresh();        Scan4[2] = sensor.distance();
          wait(2);
-         Buzzer.play(200,120);  lcd.printString("X",45,4);lcd.refresh();         Scan4[3] = sensor.distance();;
+         Buzzer.play(200,120);  lcd.printString("X",45,4);lcd.refresh();         Scan4[3] = sensor.distance();
          wait(2);
-         Buzzer.play(200,120);  lcd.printString("X",45,5);lcd.refresh();         Scan4[4] = sensor.distance();;
-        lcd.printString("Scan 4 Done",0,0); lcd.refresh();
-                 lcd.printString("X",0,1),lcd.printString("X",0,2),lcd.printString("X",0,3),lcd.printString("X",0,4),lcd.printString("X",0,5); lcd.refresh();
+         Buzzer.play(200,120);  lcd.printString("X",45,5);lcd.refresh();         Scan4[4] = sensor.distance();
+         lcd.printString("Scan 4 Done",0,0); lcd.refresh();
+
+         wait(2);
+         lcd.printString("Fifth Scan",0,0);
+         lcd.printString("X",0,1),lcd.printString("X",0,2),lcd.printString("X",0,3),lcd.printString("X",0,4),lcd.printString("X",0,5); lcd.refresh();
          lcd.printString("X",15,1),lcd.printString("X",15,2),lcd.printString("X",15,3),lcd.printString("X",15,4),lcd.printString("X",15,5); lcd.refresh();
-        lcd.printString("X",30,1),lcd.printString("X",30,2),lcd.printString("X",30,3),lcd.printString("X",30,4),lcd.printString("X",30,5); lcd.refresh();
-            lcd.printString("X",45,1),lcd.printString("X",45,2),lcd.printString("X",45,3),lcd.printString("X",45,4),lcd.printString("X",45,5); lcd.refresh();
-                    wait(2);
-        lcd.printString("Fifth Scan",0,0); 
-          Buzzer.play(300,120);   lcd.printString("X",60,1); lcd.refresh();     Scan3[0] = sensor.distance();
+         lcd.printString("X",30,1),lcd.printString("X",30,2),lcd.printString("X",30,3),lcd.printString("X",30,4),lcd.printString("X",30,5); lcd.refresh();
+         lcd.printString("X",45,1),lcd.printString("X",45,2),lcd.printString("X",45,3),lcd.printString("X",45,4),lcd.printString("X",45,5); lcd.refresh(); 
+         Buzzer.play(300,120);  lcd.printString("X",60,1); lcd.refresh();       Scan3[0] = sensor.distance();
          wait(2);
          Buzzer.play(200,120);  lcd.printString("X",60,2);lcd.refresh();        Scan3[1] = sensor.distance();
          wait(2);
          Buzzer.play(200,120);  lcd.printString("X",60,3);lcd.refresh();        Scan3[2] = sensor.distance();
          wait(2);
-         Buzzer.play(200,120);  lcd.printString("X",60,4);lcd.refresh();         Scan3[3] = sensor.distance();;
+         Buzzer.play(200,120);  lcd.printString("X",60,4);lcd.refresh();         Scan3[3] = sensor.distance();
          wait(2);
-         Buzzer.play(200,120);  lcd.printString("X",60,5);lcd.refresh();         Scan3[4] = sensor.distance();;
+         Buzzer.play(200,120);  lcd.printString("X",60,5);lcd.refresh();         Scan3[4] = sensor.distance();
+         lcd.printString("Scan 5 Done",0,0); lcd.refresh();
+         g_ButtonAPress=0;
      
-        PressA=0;
-     
-        
         break;
 ////////////////////////////////////////////////////////////////////////////        
      case 2: 
-    PressA= 2;
+    g_ButtonAPress= 2;
     long secondScan[10];
     
                  secondScan[5] = sensor.distance();
@@ -455,7 +463,7 @@
      break;
 ////////////////////////////////////////////////////////////////////     
      case 3:
-              PressA= 3;
+              g_ButtonAPress= 3;
          lcd.clear();
        lcd.printString("Menu 3",0,0);        
        long us = sensor.distance(); 
@@ -570,18 +578,18 @@
  }
  
    int buttonCountA(){
-        PressA++;
-        if (PressA>3){
-            PressA= 0;
+        g_ButtonAPress++;
+        if (g_ButtonAPress>3){
+            g_ButtonAPress= 0;
             }
-        return PressA;
+        return g_ButtonAPress;
         }
            int buttonCountB(){
-        PressB++;
-        if (PressB>3){
-            PressB= 0;
+        g_ButtonBPress++;
+        if (g_ButtonBPress>3){
+            g_ButtonBPress= 0;
             }
-        return PressB;
+        return g_ButtonBPress;
         }
         
    //Button A event-triggered interrupt
@@ -595,7 +603,7 @@
     
     g_buttonA_flag = 1;   // set flag in ISR
   
-        printf("press count = %i\n",PressA);
+        printf("press count = %i\n",g_ButtonAPress);
             
       // lcd.clear(); 
         //lcd.printString("Button A Pressed",0,0);
@@ -613,7 +621,7 @@
         lcd.printString("Button B Pressed",0,0);
         
          lcd.refresh(); // set flag in ISR
-           printf("press count = %i\n",PressB);
+           printf("press count = %i\n",g_ButtonBPress);
 }
   //Button X event-triggered interrupt
 void buttonX_isr()