![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Coursework
main.cpp
- Committer:
- sesa514652
- Date:
- 2022-01-16
- Revision:
- 19:32f9fc794ef8
- Parent:
- 18:bb36d96142dd
File content as of revision 19:32f9fc794ef8:
#include "mbed.h" #include "Joystick.h" #include "N5110.h" #include "hcsr04.h" #include "Piezo.h" #include "string" // 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); int main() { //initialise Joystick 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)); 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)); // 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); while(1) { int button_counter; 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); break; case 3: PressA= 3; lcd.clear(); lcd.printString("Menu 3",0,0); lcd.refresh(); wait_ms(30); break; } } } 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); } //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 }