![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Coursework
main.cpp
- Committer:
- sesa514652
- Date:
- 2022-01-24
- Revision:
- 26:8826ed43fad2
- Parent:
- 25:9798017b837d
- Child:
- 27:d48bf4e8f1a0
File content as of revision 26:8826ed43fad2:
#include "mbed.h" #include "Joystick.h" #include "N5110.h" #include "hcsr04.h" #include "Piezo.h" #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; // y x button Joystick joystick(PTB10,PTB11,PTC16); HCSR04 sensor(D14, D15); Piezo Buzzer(PTC10); //Bringing in buttons InterruptIn buttonA(PTB9); 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 }, { 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 }, }; 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 int buttonCountA(); int buttonCountB(); //volatile int g_buttonA_counter = 0; // Global counter //Test function after research C++ lanuage 05/01/22 int cube(int num){ return num*num; }; // Menu Items 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 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(){; Scan1[0] = sensor.distance(); Scan1[1] = sensor.distance();; Scan1[2] = sensor.distance();; Scan1[3] = sensor.distance();; Scan1[4] = sensor.distance();; return Scan1[0,1,2,3,4];} //void returnme(){ //int num; // return num*num; // }; int main() { //initialise Joystick 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(); // 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 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); //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) { // section to try to get POC of number comparitor without using the US for repeatablity// to get if statements correct possibly change to switch long Scan1[5]; long Scan2[5]; long Scan3[5]; long Scan4[5]; long Scan5[5]; int num2; int num3; int num4; // cout << "Test value collection"; //Scan1Input(); /* cout << Scan1[0];cout <<"/n"; cout << Scan1[1];"/n"; cout << Scan1[2];"/n"; cout << Scan1[3];"/n"; cout << Scan1[4];"/n"; cout << Scan1[5];"/n"; cout << Scan1[6];"/n"; cout << Scan1[7];"/n"; cout << Scan1[8];"/n"; cout << Scan1[9];"/n"; */ // IF Statement to decide on shape to draw if (Scan1[0] > Scan1[1]){ printf("01Higher\n%d %d",Scan1[0],Scan1[1]); }else if (Scan1[0] = Scan1[1]){ printf("01Equal\n%d %d",Scan1[0],Scan1[1]);} else if (Scan1[0] < Scan1[1]){ // cout <<'Lower';} printf("01Lower \n%d %d",Scan1[0],Scan1[1]);} if (Scan1[1] > Scan1[2]){ printf("12Higher\n"); }else if (Scan1[1] = Scan1[2]){ printf("12Equal\n");} else if (Scan1[1] < Scan1[2]){ // cout <<'Lower';} printf("12Lower \n");} if (Scan1[2] > Scan1[3]){ printf("23Higher\n"); }else if (Scan1[2] = Scan1[3]){ printf("23Equal\n");} else if (Scan1[2] < Scan1[3]){ // cout <<'Lower';} printf("23Lower \n");} if (Scan1[3] > Scan1[4]){ printf("34Higher\n"); }else if (Scan1[3] = Scan1[4]){ printf("34Equal\n");} else if (Scan1[3] < Scan1[4]){ // cout <<'Lower';} printf("34Lower \n");} if (Scan1[4] > Scan1[5]){ printf("45Higher\n"); }else if (Scan1[4] = Scan1[5]){ printf("Equal\n");} else if (Scan1[4] < Scan1[5]){ // cout <<'Lower';} printf("Lower \n");} printf("Scan1 1 : %d cm\n", Scan1[0]); printf("Scan1 2 : %d cm\n", Scan1[1]); printf("Scan1 3 : %d cm\n", Scan1[2]); printf("Scan1 4 : %d cm\n", Scan1[3]); printf("Scan1 5 : %d cm\n", Scan1[4]); printf("Scan2 1 : %d cm\n", Scan2[0]); printf("Scan2 2 : %d cm\n", Scan2[1]); printf("Scan2 3 : %d cm\n", Scan2[2]); printf("Scan2 4 : %d cm\n", Scan2[3]); printf("Scan2 5 : %d cm\n", Scan2[4]); printf("Scan3 1 : %d cm\n", Scan3[0]); printf("Scan3 2 : %d cm\n", Scan3[1]); printf("Scan3 3 : %d cm\n", Scan3[2]); printf("Scan3 4 : %d cm\n", Scan3[3]); printf("Scan3 5 : %d cm\n", Scan3[4]); printf("Scan4 1 : %d cm\n", Scan4[0]); printf("Scan4 2 : %d cm\n", Scan4[1]); printf("Scan4 3 : %d cm\n", Scan4[2]); printf("Scan4 4 : %d cm\n", Scan4[3]); printf("Scan4 5 : %d cm\n", Scan4[4]); printf("Scan5 1 : %d cm\n", Scan5[0]); 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("I always Get printed \n"); // cout << Scan1[2]; // cout << Scan1[6]; printf("I Should Be printed always \n"); /* if (Scan1[1] > Scan1[2]){ cout <<'+'; }else if (Scan1[1]=Scan1[2]){ cout <<'=';} else { cout <<'-';} if (Scan1[2] > Scan1[3]){ cout <<'+'; }else if (Scan1[2]=Scan1[3]){ cout <<'=';} else { cout <<'-';} if (Scan1[3] > Scan1[4]){ cout <<'+'; }else if (Scan1[3]=Scan1[4]){ cout <<'=';} else { cout <<'-';} if (Scan1[4] > Scan1[5]){ cout <<'+'; }else if (Scan1[4]=Scan1[5]){ cout <<'=';} else { cout <<'-';} if (Scan1[5] > Scan1[6]){ cout <<'+'; }else if (Scan1[5]=Scan1[6]){ cout <<'=';} else { cout <<'-';} if (Scan1[6] > Scan1[7]){ cout <<'+'; }else if (Scan1[6]=Scan1[7]){ cout <<'=';} else { cout <<'-';} if (Scan1[7] > Scan1[8]){ cout <<'+'; }else if (Scan1[7]=Scan1[8]){ cout <<'=';} else { cout <<'-';} if (Scan1[8] > Scan1[9]){ cout <<'+'; }else if (Scan1[8]=Scan1[9]){ cout <<'=';} else { cout <<'-';} if (Scan1[9] > Scan1[10]){ cout <<'+'; }else if (Scan1[9]=Scan1[10]){ cout <<'=';} else { cout <<'-';} */ ///////////////////////////////////////////////////////////////////////////////////////////////////// switch(PressA){ default: lcd.clear(); lcd.printString("Main Menu",0,0); lcd.drawLine(0,10,80,10,2); lcd.printString("A to begin new Scan",0,2); lcd.printString("B to Display last object",0,3); lcd.refresh(); wait_ms(50); break; ////////////////////////////////////////////////////////////////////////////////////////////////// case 1: PressA =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(); wait(2); Buzzer.play(200,120); lcd.printString("X",0,2);lcd.refresh(); Scan1[1] = sensor.distance(); wait(2); Buzzer.play(200,120); lcd.printString("X",0,3);lcd.refresh(); Scan1[2] = sensor.distance(); wait(2); Buzzer.play(200,120); lcd.printString("X",0,4);lcd.refresh(); Scan1[3] = sensor.distance();; 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(); wait(2); // required to stop Black screen issue} lcd.clear();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(); lcd.printString("Second Scan",0,0); 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); Buzzer.play(200,120); lcd.printString("X",15,3);lcd.refresh(); Scan2[2] = sensor.distance(); wait(2); Buzzer.play(200,120); lcd.printString("X",15,4);lcd.refresh(); Scan2[3] = sensor.distance(); 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(); lcd.clear();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(); 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(); 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();; 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(); 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(); 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();; 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(); 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(); 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();; wait(2); Buzzer.play(200,120); lcd.printString("X",60,5);lcd.refresh(); Scan3[4] = sensor.distance();; break; //////////////////////////////////////////////////////////////////////////// case 2: PressA= 2; long secondScan[10]; secondScan[5] = sensor.distance(); printf("Distance : %d cm", secondScan[5]); lcd.clear(); lcd.printString("2nd Scan up down",0,0); lcd.drawLine(0,10,80,10,2); break; //////////////////////////////////////////////////////////////////// case 3: PressA= 3; lcd.clear(); lcd.printString("Menu 3",0,0); long us = sensor.distance(); printf("Distance : %d cm",us); lcd.refresh(); wait_ms(50); //required to stop Black screen issue break; } } /* 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); 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); } } } */ } int buttonCountA(){ PressA++; if (PressA>3){ PressA= 0; } return PressA; } int buttonCountB(){ PressB++; if (PressB>3){ PressB= 0; } return PressB; } //Button A event-triggered interrupt void buttonA_isr() { long distanced = sensor.distance(); buttonCountA(); 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() { buttonCountB(); g_buttonB_flag = 1; // set flag in ISR lcd.clear(); lcd.printString("Button B Pressed",0,0); lcd.refresh(); // set flag in ISR printf("press count = %i\n",PressB); } //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 }