Coursework

Committer:
sesa514652
Date:
Fri Jan 07 12:09:52 2022 +0000
Revision:
10:42e70b596099
Parent:
9:cdd7d9a123f9
Child:
11:1dec05b7d1c1
Button Press recording on screen,  to make this action occur instantly

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sesa514652 0:1f799c7cce2b 1 #include "mbed.h"
sesa514652 0:1f799c7cce2b 2 #include "Joystick.h"
sesa514652 0:1f799c7cce2b 3 #include "N5110.h"
sesa514652 0:1f799c7cce2b 4 #include "hcsr04.h"
sesa514652 3:6ecb75a2675c 5 #include "Piezo.h"
sesa514652 8:770d168713cc 6 #include "string"
sesa514652 10:42e70b596099 7
sesa514652 5:9b2c976ca318 8 // y x button
sesa514652 0:1f799c7cce2b 9 Joystick joystick(PTB10,PTB11,PTC16);
sesa514652 0:1f799c7cce2b 10 HCSR04 sensor(D14, D15);
sesa514652 3:6ecb75a2675c 11 Piezo Buzzer(PTC10);
sesa514652 8:770d168713cc 12 //Bringing in buttons
sesa514652 8:770d168713cc 13 InterruptIn buttonA(PTB9);
sesa514652 8:770d168713cc 14 InterruptIn buttonB(PTD0);
sesa514652 8:770d168713cc 15 InterruptIn buttonX(PTC17);
sesa514652 8:770d168713cc 16 InterruptIn buttonY(PTC12);
sesa514652 0:1f799c7cce2b 17
sesa514652 5:9b2c976ca318 18 //rows,cols
sesa514652 0:1f799c7cce2b 19 int sprite[8][5] = {
sesa514652 0:1f799c7cce2b 20 { 0,0,1,0,0 },
sesa514652 0:1f799c7cce2b 21 { 0,1,1,1,0 },
sesa514652 0:1f799c7cce2b 22 { 0,0,1,0,0 },
sesa514652 0:1f799c7cce2b 23 { 0,1,1,1,0 },
sesa514652 0:1f799c7cce2b 24 { 1,1,1,1,1 },
sesa514652 0:1f799c7cce2b 25 { 1,1,1,1,1 },
sesa514652 0:1f799c7cce2b 26 { 1,1,0,1,1 },
sesa514652 0:1f799c7cce2b 27 { 1,1,0,1,1 },
sesa514652 5:9b2c976ca318 28 };
sesa514652 8:770d168713cc 29 void init_K64F();
sesa514652 8:770d168713cc 30 // Button A interrupt service routine
sesa514652 8:770d168713cc 31 void buttonA_isr();
sesa514652 10:42e70b596099 32 // Button B interrupt service routine
sesa514652 10:42e70b596099 33 void buttonB_isr();
sesa514652 10:42e70b596099 34 // Button X interrupt service routine
sesa514652 10:42e70b596099 35 void buttonX_isr();
sesa514652 10:42e70b596099 36 // Button Y interrupt service routine
sesa514652 10:42e70b596099 37 void buttonY_isr();
sesa514652 8:770d168713cc 38 volatile int g_buttonA_flag = 0;
sesa514652 10:42e70b596099 39 volatile int g_buttonB_flag = 0;
sesa514652 10:42e70b596099 40 volatile int g_buttonX_flag = 0;
sesa514652 10:42e70b596099 41 volatile int g_buttonY_flag = 0;
sesa514652 8:770d168713cc 42 //Test function after research C++ lanuage 05/01/22
sesa514652 4:167ce930c9d5 43 int cube(int num){
sesa514652 4:167ce930c9d5 44 int result;
sesa514652 4:167ce930c9d5 45 result = num*num;
sesa514652 4:167ce930c9d5 46 return result;
sesa514652 5:9b2c976ca318 47 };
sesa514652 10:42e70b596099 48 // Menu Items
sesa514652 10:42e70b596099 49 int page1;
sesa514652 10:42e70b596099 50 int page2;
sesa514652 10:42e70b596099 51 int page3;
sesa514652 10:42e70b596099 52 string Listitem1 = "Game"; // guess distance
sesa514652 10:42e70b596099 53 string Listitem2 = "Measure"; // measure distance of object
sesa514652 10:42e70b596099 54 string Listitem3 = "Detect"; // alarm when object is dectected
sesa514652 5:9b2c976ca318 55 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
sesa514652 9:cdd7d9a123f9 56 /* Test of the interup an operating the buzzer this kills the board
sesa514652 9:cdd7d9a123f9 57 void buzzme(){
sesa514652 9:cdd7d9a123f9 58 Buzzer.play(200,120);
sesa514652 9:cdd7d9a123f9 59 wait_ms(5);
sesa514652 9:cdd7d9a123f9 60 Buzzer.play(200,120);;
sesa514652 9:cdd7d9a123f9 61 }
sesa514652 9:cdd7d9a123f9 62 */
sesa514652 10:42e70b596099 63 double printme(){
sesa514652 9:cdd7d9a123f9 64
sesa514652 10:42e70b596099 65 double x ;
sesa514652 10:42e70b596099 66 x = 1;
sesa514652 10:42e70b596099 67 printf("%f\n",x);
sesa514652 10:42e70b596099 68 return 0;
sesa514652 9:cdd7d9a123f9 69 }
sesa514652 10:42e70b596099 70
sesa514652 10:42e70b596099 71
sesa514652 10:42e70b596099 72 //void returnme(){
sesa514652 10:42e70b596099 73 //int num;
sesa514652 10:42e70b596099 74 // return num*num;
sesa514652 10:42e70b596099 75 // };
sesa514652 0:1f799c7cce2b 76 int main() {
sesa514652 5:9b2c976ca318 77 //initialise Joystic
sesa514652 5:9b2c976ca318 78 joystick.init();
sesa514652 5:9b2c976ca318 79
sesa514652 5:9b2c976ca318 80 //first need to initialise display
sesa514652 5:9b2c976ca318 81 lcd.init();
sesa514652 8:770d168713cc 82
sesa514652 8:770d168713cc 83 // Button A is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
sesa514652 8:770d168713cc 84 buttonA.mode(PullDown);
sesa514652 10:42e70b596099 85 // Button B is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
sesa514652 10:42e70b596099 86 buttonB.mode(PullDown);
sesa514652 10:42e70b596099 87 // Button X is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
sesa514652 10:42e70b596099 88 buttonX.mode(PullDown);
sesa514652 10:42e70b596099 89 // Button Y is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
sesa514652 10:42e70b596099 90 buttonY.mode(PullDown);
sesa514652 8:770d168713cc 91
sesa514652 8:770d168713cc 92 // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
sesa514652 10:42e70b596099 93 buttonA.rise(&buttonA_isr);
sesa514652 10:42e70b596099 94 // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
sesa514652 10:42e70b596099 95 buttonB.rise(&buttonB_isr);
sesa514652 10:42e70b596099 96 // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
sesa514652 10:42e70b596099 97 buttonX.rise(&buttonX_isr);
sesa514652 10:42e70b596099 98 // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
sesa514652 10:42e70b596099 99 buttonY.rise(&buttonY_isr);
sesa514652 8:770d168713cc 100
sesa514652 8:770d168713cc 101
sesa514652 5:9b2c976ca318 102
sesa514652 5:9b2c976ca318 103 //change set contrast in range 0.0 to 1.0
sesa514652 5:9b2c976ca318 104 //0.5 appears to be a good starting point
sesa514652 5:9b2c976ca318 105 lcd.setContrast(0.5);
sesa514652 5:9b2c976ca318 106
sesa514652 5:9b2c976ca318 107 //Buzzer.period(10.0f);
sesa514652 5:9b2c976ca318 108 //Buzzer.pulsewidth(1);
sesa514652 6:f7f30e0e3bed 109 //Test Struct after research 05/01/22
sesa514652 6:f7f30e0e3bed 110 struct ObjectDefine{
sesa514652 7:ce70a873aa70 111 int qwer;
sesa514652 6:f7f30e0e3bed 112 float Distance;
sesa514652 7:ce70a873aa70 113 char te;
sesa514652 7:ce70a873aa70 114 double dubs;
sesa514652 7:ce70a873aa70 115 string namestruct;
sesa514652 8:770d168713cc 116 // string ;
sesa514652 6:f7f30e0e3bed 117 };
sesa514652 7:ce70a873aa70 118 // Test Class 06/01/22
sesa514652 10:42e70b596099 119 class myClass{
sesa514652 7:ce70a873aa70 120 public:
sesa514652 7:ce70a873aa70 121 string nameclass ;
sesa514652 7:ce70a873aa70 122 double dubss;
sesa514652 7:ce70a873aa70 123 int inty;
sesa514652 7:ce70a873aa70 124 };
sesa514652 7:ce70a873aa70 125 myClass Class1;
sesa514652 7:ce70a873aa70 126 Class1.nameclass ="firstclass";
sesa514652 10:42e70b596099 127 ObjectDefine Object1;
sesa514652 10:42e70b596099 128 Object1.qwer = 12;
sesa514652 6:f7f30e0e3bed 129 Object1.Distance = 34;
sesa514652 7:ce70a873aa70 130 Object1.te = 'A';
sesa514652 7:ce70a873aa70 131 Object1.dubs = 23;
sesa514652 10:42e70b596099 132 Object1.namestruct = "name";
sesa514652 9:cdd7d9a123f9 133 //buttonA.rise(&buzzme); Omitted due to error
sesa514652 10:42e70b596099 134 //buttonA.rise(&printme,10);
sesa514652 10:42e70b596099 135 //buttonA.rise(&returnme);
sesa514652 0:1f799c7cce2b 136 while(1) {
sesa514652 5:9b2c976ca318 137 //Tune(Buzzer,So6,8);
sesa514652 5:9b2c976ca318 138 //Stop_tunes(Buzzer);
sesa514652 8:770d168713cc 139 //Section of code to try to get the button press to be recorded
sesa514652 9:cdd7d9a123f9 140 //bool button = buttonA.read();
sesa514652 10:42e70b596099 141 //printf("%f\n", g_buttonA_flag );
sesa514652 10:42e70b596099 142 //Recording button presses
sesa514652 10:42e70b596099 143 if (g_buttonA_flag){
sesa514652 10:42e70b596099 144 g_buttonA_flag = 0; // if it has, clear the flag
sesa514652 10:42e70b596099 145
sesa514652 10:42e70b596099 146 // send message over serial port - can observe in CoolTerm etc.
sesa514652 10:42e70b596099 147 printf("Button A pressed\n");
sesa514652 10:42e70b596099 148 }
sesa514652 10:42e70b596099 149 if (g_buttonB_flag){
sesa514652 10:42e70b596099 150 g_buttonB_flag = 0; // if it has, clear the flag
sesa514652 8:770d168713cc 151
sesa514652 10:42e70b596099 152 // send message over serial port - can observe in CoolTerm etc.
sesa514652 10:42e70b596099 153 printf("Button B pressed\n");
sesa514652 10:42e70b596099 154 }
sesa514652 10:42e70b596099 155 if (g_buttonX_flag){
sesa514652 10:42e70b596099 156 g_buttonX_flag = 0; // if it has, clear the flag
sesa514652 10:42e70b596099 157
sesa514652 10:42e70b596099 158 // send message over serial port - can observe in CoolTerm etc.
sesa514652 10:42e70b596099 159 printf("Button X pressed\n");
sesa514652 10:42e70b596099 160 lcd.clear();
sesa514652 10:42e70b596099 161 lcd.printString("Button Pressed!",0,0);
sesa514652 10:42e70b596099 162 lcd.refresh();
sesa514652 10:42e70b596099 163 wait(10);
sesa514652 10:42e70b596099 164 }
sesa514652 10:42e70b596099 165 if (g_buttonY_flag){
sesa514652 10:42e70b596099 166 g_buttonY_flag = 0; // if it has, clear the flag
sesa514652 10:42e70b596099 167
sesa514652 10:42e70b596099 168 // send message over serial port - can observe in CoolTerm etc.
sesa514652 10:42e70b596099 169 printf("Button Y pressed\n");
sesa514652 10:42e70b596099 170 }
sesa514652 5:9b2c976ca318 171 Buzzer.play(200,120);
sesa514652 5:9b2c976ca318 172 wait_ms(5);
sesa514652 5:9b2c976ca318 173 Buzzer.play(200,120);
sesa514652 1:757728321abd 174 long distanced = sensor.distance();
sesa514652 1:757728321abd 175 if (distanced >= 400 || distanced <= 2)
sesa514652 5:9b2c976ca318 176 {
sesa514652 1:757728321abd 177 printf("Out of range");
sesa514652 5:9b2c976ca318 178 //Calling dummy function 05/01/22
sesa514652 4:167ce930c9d5 179 int answer = cube(2);
sesa514652 5:9b2c976ca318 180 printf("%f\n",answer);
sesa514652 1:757728321abd 181 wait(1.0);
sesa514652 5:9b2c976ca318 182 }
sesa514652 5:9b2c976ca318 183 else
sesa514652 5:9b2c976ca318 184 {
sesa514652 5:9b2c976ca318 185 printf("Distance : %d cm",distanced);
sesa514652 5:9b2c976ca318 186 wait(1.0); // 1 sec
sesa514652 1:757728321abd 187 }
sesa514652 5:9b2c976ca318 188 lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK);
sesa514652 5:9b2c976ca318 189 //lcd.refresh must be used to update the lcd Display
sesa514652 5:9b2c976ca318 190 lcd.refresh();
sesa514652 5:9b2c976ca318 191 wait(5.0);
sesa514652 5:9b2c976ca318 192 //these are default settings so not strictly needed
sesa514652 0:1f799c7cce2b 193 lcd.normalMode(); // normal colour mode
sesa514652 5:9b2c976ca318 194 lcd.setBrightness(0.5); // put LED backlight on 50%
sesa514652 0:1f799c7cce2b 195 lcd.clear();
sesa514652 0:1f799c7cce2b 196 lcd.printString("Hello, World!",0,0);
sesa514652 0:1f799c7cce2b 197 Vector2D coord = joystick.get_coord();
sesa514652 0:1f799c7cce2b 198 lcd.refresh();
sesa514652 0:1f799c7cce2b 199 printf("Coord = %f,%f\n",coord.x,coord.y);
sesa514652 0:1f799c7cce2b 200 char buffer[14];
sesa514652 0:1f799c7cce2b 201 Vector2D mapped_coord = joystick.get_mapped_coord();
sesa514652 0:1f799c7cce2b 202 printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y);
sesa514652 0:1f799c7cce2b 203
sesa514652 5:9b2c976ca318 204 int length = sprintf(buffer,"T = %2f",coord.x);
sesa514652 5:9b2c976ca318 205 //it is important the format specifier ensures the length will fit in the buffer
sesa514652 0:1f799c7cce2b 206 if (length <= 14) // if string will fit on display (assuming printing at x=0)
sesa514652 5:9b2c976ca318 207 lcd.printString(buffer,0,1); // display on screen
sesa514652 0:1f799c7cce2b 208 lcd.refresh();
sesa514652 5:9b2c976ca318 209 lcd.clear();
sesa514652 5:9b2c976ca318 210 //times by 50 to try get the point moving across screen
sesa514652 0:1f799c7cce2b 211 float x50 = mapped_coord.x*5;
sesa514652 0:1f799c7cce2b 212 float y50 = mapped_coord.y*8;
sesa514652 5:9b2c976ca318 213 //to see what the values are
sesa514652 5:9b2c976ca318 214 printf(" *50 = %f,%f\n",x50,y50);
sesa514652 5:9b2c976ca318 215 //example of how to draw circles
sesa514652 1:757728321abd 216 lcd.printChar('o',WIDTH/2,HEIGHT/2);
sesa514652 5:9b2c976ca318 217 lcd.refresh();
sesa514652 0:1f799c7cce2b 218 float mag = joystick.get_mag();
sesa514652 0:1f799c7cce2b 219 float angle = joystick.get_angle();
sesa514652 0:1f799c7cce2b 220 printf("Mag = %f Angle = %f\n",mag,angle);
sesa514652 0:1f799c7cce2b 221 Direction d = joystick.get_direction();
sesa514652 0:1f799c7cce2b 222 printf("Direction = %i\n",d);
sesa514652 0:1f799c7cce2b 223 if (joystick.button_pressed() ) {
sesa514652 0:1f799c7cce2b 224 printf("Button Pressed\n");
sesa514652 9:cdd7d9a123f9 225 wait(5);
sesa514652 0:1f799c7cce2b 226 }
sesa514652 8:770d168713cc 227
sesa514652 8:770d168713cc 228 }
sesa514652 8:770d168713cc 229 }
sesa514652 10:42e70b596099 230
sesa514652 10:42e70b596099 231
sesa514652 10:42e70b596099 232 //Button A event-triggered interrupt
sesa514652 10:42e70b596099 233 void buttonA_isr()
sesa514652 10:42e70b596099 234 {
sesa514652 10:42e70b596099 235 g_buttonA_flag = 1; // set flag in ISR
sesa514652 10:42e70b596099 236 }
sesa514652 10:42e70b596099 237 //Button B event-triggered interrupt
sesa514652 10:42e70b596099 238 void buttonB_isr()
sesa514652 10:42e70b596099 239 {
sesa514652 10:42e70b596099 240 g_buttonB_flag = 1; // set flag in ISR
sesa514652 10:42e70b596099 241 }
sesa514652 10:42e70b596099 242 //Button X event-triggered interrupt
sesa514652 10:42e70b596099 243 void buttonX_isr()
sesa514652 10:42e70b596099 244 {
sesa514652 10:42e70b596099 245 g_buttonX_flag = 1; // set flag in ISR
sesa514652 10:42e70b596099 246 }
sesa514652 10:42e70b596099 247 //Button Y event-triggered interrupt
sesa514652 10:42e70b596099 248 void buttonY_isr()
sesa514652 10:42e70b596099 249 {
sesa514652 10:42e70b596099 250 g_buttonY_flag = 1; // set flag in ISR
sesa514652 10:42e70b596099 251 }