![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Coursework
main.cpp@20:4e1f2917a857, 2022-01-17 (annotated)
- Committer:
- sesa514652
- Date:
- Mon Jan 17 21:25:21 2022 +0000
- Revision:
- 20:4e1f2917a857
- Parent:
- 18:bb36d96142dd
- Child:
- 21:2e37f80e8b5b
Added Arrays to store data
Who changed what in which revision?
User | Revision | Line number | New 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 | 20:4e1f2917a857 | 7 | #include "array" |
sesa514652 | 12:ceea378d5691 | 8 | // FSM Menu |
sesa514652 | 12:ceea378d5691 | 9 | // defines directions as 0/1. Note UPPERCASE |
sesa514652 | 12:ceea378d5691 | 10 | #define UP 0 |
sesa514652 | 12:ceea378d5691 | 11 | #define DOWN 1 |
sesa514652 | 12:ceea378d5691 | 12 | // set inital state |
sesa514652 | 12:ceea378d5691 | 13 | volatile int state = 0; |
sesa514652 | 12:ceea378d5691 | 14 | // set initial direction |
sesa514652 | 12:ceea378d5691 | 15 | int direction = UP; |
sesa514652 | 12:ceea378d5691 | 16 | // array of states in the FSM, each element is the output of the counter |
sesa514652 | 12:ceea378d5691 | 17 | // set the output in binary to make it easier, 0 is LED on, 1 is LED off |
sesa514652 | 12:ceea378d5691 | 18 | string fsm[4] = {"buttonA_isr","buttonB_isr","buttonAsss_isr","buttonA_isr"}; |
sesa514652 | 18:bb36d96142dd | 19 | volatile int PressA; |
sesa514652 | 18:bb36d96142dd | 20 | volatile int PressB; |
sesa514652 | 10:42e70b596099 | 21 | |
sesa514652 | 5:9b2c976ca318 | 22 | // y x button |
sesa514652 | 0:1f799c7cce2b | 23 | Joystick joystick(PTB10,PTB11,PTC16); |
sesa514652 | 0:1f799c7cce2b | 24 | HCSR04 sensor(D14, D15); |
sesa514652 | 3:6ecb75a2675c | 25 | Piezo Buzzer(PTC10); |
sesa514652 | 8:770d168713cc | 26 | //Bringing in buttons |
sesa514652 | 8:770d168713cc | 27 | InterruptIn buttonA(PTB9); |
sesa514652 | 8:770d168713cc | 28 | InterruptIn buttonB(PTD0); |
sesa514652 | 8:770d168713cc | 29 | InterruptIn buttonX(PTC17); |
sesa514652 | 8:770d168713cc | 30 | InterruptIn buttonY(PTC12); |
sesa514652 | 11:1dec05b7d1c1 | 31 | InterruptIn buttonStart(PTC5); |
sesa514652 | 11:1dec05b7d1c1 | 32 | InterruptIn buttonBack(PTB19); |
sesa514652 | 11:1dec05b7d1c1 | 33 | InterruptIn buttonLeft(PTB18); |
sesa514652 | 11:1dec05b7d1c1 | 34 | InterruptIn buttonRight(PTB3); |
sesa514652 | 0:1f799c7cce2b | 35 | |
sesa514652 | 11:1dec05b7d1c1 | 36 | // create an event queue |
sesa514652 | 11:1dec05b7d1c1 | 37 | EventQueue queue; // events are needed to get arround the printf issue |
sesa514652 | 5:9b2c976ca318 | 38 | //rows,cols |
sesa514652 | 0:1f799c7cce2b | 39 | int sprite[8][5] = { |
sesa514652 | 0:1f799c7cce2b | 40 | { 0,0,1,0,0 }, |
sesa514652 | 0:1f799c7cce2b | 41 | { 0,1,1,1,0 }, |
sesa514652 | 0:1f799c7cce2b | 42 | { 0,0,1,0,0 }, |
sesa514652 | 0:1f799c7cce2b | 43 | { 0,1,1,1,0 }, |
sesa514652 | 0:1f799c7cce2b | 44 | { 1,1,1,1,1 }, |
sesa514652 | 0:1f799c7cce2b | 45 | { 1,1,1,1,1 }, |
sesa514652 | 0:1f799c7cce2b | 46 | { 1,1,0,1,1 }, |
sesa514652 | 0:1f799c7cce2b | 47 | { 1,1,0,1,1 }, |
sesa514652 | 5:9b2c976ca318 | 48 | }; |
sesa514652 | 11:1dec05b7d1c1 | 49 | void init_K64F(); // Start K64fBoard |
sesa514652 | 11:1dec05b7d1c1 | 50 | |
sesa514652 | 11:1dec05b7d1c1 | 51 | void buttonA_isr(); // Button A interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 52 | void buttonB_isr(); // Button B interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 53 | void buttonX_isr(); // Button X interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 54 | void buttonY_isr(); // Button Y interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 55 | void buttonStart_isr(); // Button Start interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 56 | void buttonBack_isr(); // Button Back interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 57 | void buttonLeft_isr(); // Button Left interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 58 | void buttonRight_isr(); // Button Right interrupt service routine |
sesa514652 | 11:1dec05b7d1c1 | 59 | volatile int g_buttonA_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 60 | volatile int g_buttonB_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 61 | volatile int g_buttonX_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 62 | volatile int g_buttonY_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 63 | volatile int g_buttonStart_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 64 | volatile int g_buttonBack_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 65 | volatile int g_buttonLeft_flag = 0; // Global Flag |
sesa514652 | 11:1dec05b7d1c1 | 66 | volatile int g_buttonRight_flag = 0; // Global Flag |
sesa514652 | 18:bb36d96142dd | 67 | int buttonCountA(); |
sesa514652 | 18:bb36d96142dd | 68 | int buttonCountB(); |
sesa514652 | 11:1dec05b7d1c1 | 69 | //volatile int g_buttonA_counter = 0; // Global counter |
sesa514652 | 8:770d168713cc | 70 | //Test function after research C++ lanuage 05/01/22 |
sesa514652 | 4:167ce930c9d5 | 71 | int cube(int num){ |
sesa514652 | 11:1dec05b7d1c1 | 72 | return num*num; |
sesa514652 | 5:9b2c976ca318 | 73 | }; |
sesa514652 | 10:42e70b596099 | 74 | // Menu Items |
sesa514652 | 10:42e70b596099 | 75 | int page1; |
sesa514652 | 10:42e70b596099 | 76 | int page2; |
sesa514652 | 10:42e70b596099 | 77 | int page3; |
sesa514652 | 10:42e70b596099 | 78 | string Listitem1 = "Game"; // guess distance |
sesa514652 | 10:42e70b596099 | 79 | string Listitem2 = "Measure"; // measure distance of object |
sesa514652 | 10:42e70b596099 | 80 | string Listitem3 = "Detect"; // alarm when object is dectected |
sesa514652 | 5:9b2c976ca318 | 81 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
sesa514652 | 9:cdd7d9a123f9 | 82 | /* Test of the interup an operating the buzzer this kills the board |
sesa514652 | 9:cdd7d9a123f9 | 83 | void buzzme(){ |
sesa514652 | 9:cdd7d9a123f9 | 84 | Buzzer.play(200,120); |
sesa514652 | 9:cdd7d9a123f9 | 85 | wait_ms(5); |
sesa514652 | 9:cdd7d9a123f9 | 86 | Buzzer.play(200,120);; |
sesa514652 | 9:cdd7d9a123f9 | 87 | } |
sesa514652 | 9:cdd7d9a123f9 | 88 | */ |
sesa514652 | 11:1dec05b7d1c1 | 89 | void printme(){ |
sesa514652 | 9:cdd7d9a123f9 | 90 | |
sesa514652 | 11:1dec05b7d1c1 | 91 | volatile double x ; |
sesa514652 | 11:1dec05b7d1c1 | 92 | x = 1; |
sesa514652 | 10:42e70b596099 | 93 | printf("%f\n",x); |
sesa514652 | 11:1dec05b7d1c1 | 94 | // return 0; |
sesa514652 | 9:cdd7d9a123f9 | 95 | } |
sesa514652 | 10:42e70b596099 | 96 | |
sesa514652 | 10:42e70b596099 | 97 | |
sesa514652 | 10:42e70b596099 | 98 | //void returnme(){ |
sesa514652 | 10:42e70b596099 | 99 | //int num; |
sesa514652 | 10:42e70b596099 | 100 | // return num*num; |
sesa514652 | 10:42e70b596099 | 101 | // }; |
sesa514652 | 0:1f799c7cce2b | 102 | int main() { |
sesa514652 | 18:bb36d96142dd | 103 | //initialise Joystick |
sesa514652 | 5:9b2c976ca318 | 104 | joystick.init(); |
sesa514652 | 5:9b2c976ca318 | 105 | |
sesa514652 | 11:1dec05b7d1c1 | 106 | // create a thread that'll run the event queue's dispatch function |
sesa514652 | 11:1dec05b7d1c1 | 107 | Thread eventThread; |
sesa514652 | 11:1dec05b7d1c1 | 108 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
sesa514652 | 11:1dec05b7d1c1 | 109 | |
sesa514652 | 11:1dec05b7d1c1 | 110 | buttonA.rise(queue.event(&buttonA_isr)); // This is used to call the function putting it in queue allowing the screen toupdat no muxt error |
sesa514652 | 11:1dec05b7d1c1 | 111 | buttonB.rise(queue.event(&buttonB_isr)); |
sesa514652 | 11:1dec05b7d1c1 | 112 | buttonX.rise(queue.event(&buttonX_isr)); |
sesa514652 | 11:1dec05b7d1c1 | 113 | buttonY.rise(queue.event(&buttonY_isr)); |
sesa514652 | 11:1dec05b7d1c1 | 114 | buttonStart.rise(queue.event(&buttonStart_isr)); // This is used to call the function putting it in queue allowing the screen toupdat no muxt error |
sesa514652 | 11:1dec05b7d1c1 | 115 | buttonBack.rise(queue.event(&buttonBack_isr)); |
sesa514652 | 11:1dec05b7d1c1 | 116 | buttonLeft.rise(queue.event(&buttonLeft_isr)); |
sesa514652 | 11:1dec05b7d1c1 | 117 | buttonRight.rise(queue.event(&buttonRight_isr)); |
sesa514652 | 5:9b2c976ca318 | 118 | //first need to initialise display |
sesa514652 | 5:9b2c976ca318 | 119 | lcd.init(); |
sesa514652 | 8:770d168713cc | 120 | |
sesa514652 | 12:ceea378d5691 | 121 | // 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 | 122 | buttonA.mode(PullDown); |
sesa514652 | 10:42e70b596099 | 123 | // 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 | 124 | buttonB.mode(PullDown); |
sesa514652 | 10:42e70b596099 | 125 | // 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 | 126 | buttonX.mode(PullDown); |
sesa514652 | 10:42e70b596099 | 127 | // 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 | 128 | buttonY.mode(PullDown); |
sesa514652 | 11:1dec05b7d1c1 | 129 | // Button Start is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 11:1dec05b7d1c1 | 130 | buttonStart.mode(PullDown); |
sesa514652 | 11:1dec05b7d1c1 | 131 | // Button Back is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 11:1dec05b7d1c1 | 132 | buttonBack.mode(PullDown); |
sesa514652 | 11:1dec05b7d1c1 | 133 | // Button Left is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 11:1dec05b7d1c1 | 134 | buttonLeft.mode(PullDown); |
sesa514652 | 11:1dec05b7d1c1 | 135 | // Button Right is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister |
sesa514652 | 11:1dec05b7d1c1 | 136 | buttonRight.mode(PullDown); |
sesa514652 | 8:770d168713cc | 137 | |
sesa514652 | 18:bb36d96142dd | 138 | |
sesa514652 | 8:770d168713cc | 139 | |
sesa514652 | 8:770d168713cc | 140 | |
sesa514652 | 5:9b2c976ca318 | 141 | |
sesa514652 | 5:9b2c976ca318 | 142 | //change set contrast in range 0.0 to 1.0 |
sesa514652 | 5:9b2c976ca318 | 143 | //0.5 appears to be a good starting point |
sesa514652 | 5:9b2c976ca318 | 144 | lcd.setContrast(0.5); |
sesa514652 | 5:9b2c976ca318 | 145 | |
sesa514652 | 5:9b2c976ca318 | 146 | //Buzzer.period(10.0f); |
sesa514652 | 5:9b2c976ca318 | 147 | //Buzzer.pulsewidth(1); |
sesa514652 | 6:f7f30e0e3bed | 148 | //Test Struct after research 05/01/22 |
sesa514652 | 6:f7f30e0e3bed | 149 | struct ObjectDefine{ |
sesa514652 | 7:ce70a873aa70 | 150 | int qwer; |
sesa514652 | 6:f7f30e0e3bed | 151 | float Distance; |
sesa514652 | 7:ce70a873aa70 | 152 | char te; |
sesa514652 | 7:ce70a873aa70 | 153 | double dubs; |
sesa514652 | 7:ce70a873aa70 | 154 | string namestruct; |
sesa514652 | 8:770d168713cc | 155 | // string ; |
sesa514652 | 6:f7f30e0e3bed | 156 | }; |
sesa514652 | 7:ce70a873aa70 | 157 | // Test Class 06/01/22 |
sesa514652 | 10:42e70b596099 | 158 | class myClass{ |
sesa514652 | 7:ce70a873aa70 | 159 | public: |
sesa514652 | 7:ce70a873aa70 | 160 | string nameclass ; |
sesa514652 | 7:ce70a873aa70 | 161 | double dubss; |
sesa514652 | 7:ce70a873aa70 | 162 | int inty; |
sesa514652 | 7:ce70a873aa70 | 163 | }; |
sesa514652 | 7:ce70a873aa70 | 164 | myClass Class1; |
sesa514652 | 7:ce70a873aa70 | 165 | Class1.nameclass ="firstclass"; |
sesa514652 | 10:42e70b596099 | 166 | ObjectDefine Object1; |
sesa514652 | 10:42e70b596099 | 167 | Object1.qwer = 12; |
sesa514652 | 6:f7f30e0e3bed | 168 | Object1.Distance = 34; |
sesa514652 | 7:ce70a873aa70 | 169 | Object1.te = 'A'; |
sesa514652 | 7:ce70a873aa70 | 170 | Object1.dubs = 23; |
sesa514652 | 10:42e70b596099 | 171 | Object1.namestruct = "name"; |
sesa514652 | 18:bb36d96142dd | 172 | |
sesa514652 | 0:1f799c7cce2b | 173 | while(1) { |
sesa514652 | 20:4e1f2917a857 | 174 | long firstScan[2]; |
sesa514652 | 20:4e1f2917a857 | 175 | switch(PressA){ |
sesa514652 | 20:4e1f2917a857 | 176 | case 1: |
sesa514652 | 20:4e1f2917a857 | 177 | PressA= 1; |
sesa514652 | 20:4e1f2917a857 | 178 | long firstScan[2]; |
sesa514652 | 20:4e1f2917a857 | 179 | firstScan[0] = sensor.distance(); |
sesa514652 | 20:4e1f2917a857 | 180 | printf("Distance : %d cm",firstScan[0]); |
sesa514652 | 20:4e1f2917a857 | 181 | lcd.clear(); |
sesa514652 | 20:4e1f2917a857 | 182 | lcd.printString("Menu 1",0,0); |
sesa514652 | 20:4e1f2917a857 | 183 | lcd.refresh(); |
sesa514652 | 20:4e1f2917a857 | 184 | // lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK); |
sesa514652 | 20:4e1f2917a857 | 185 | lcd.refresh(); |
sesa514652 | 20:4e1f2917a857 | 186 | wait_ms(50); // required to stop Black screen issue |
sesa514652 | 20:4e1f2917a857 | 187 | break; |
sesa514652 | 20:4e1f2917a857 | 188 | |
sesa514652 | 20:4e1f2917a857 | 189 | case 2: |
sesa514652 | 20:4e1f2917a857 | 190 | PressA= 2; |
sesa514652 | 20:4e1f2917a857 | 191 | firstScan[1] = sensor.distance(); |
sesa514652 | 20:4e1f2917a857 | 192 | printf("Distance : %d cm",firstScan[1]); |
sesa514652 | 20:4e1f2917a857 | 193 | lcd.clear(); |
sesa514652 | 20:4e1f2917a857 | 194 | lcd.printString("Menu 2",0,0); |
sesa514652 | 20:4e1f2917a857 | 195 | lcd.refresh(); |
sesa514652 | 20:4e1f2917a857 | 196 | wait_ms(50); // required to stop Black screen issue |
sesa514652 | 20:4e1f2917a857 | 197 | break; |
sesa514652 | 20:4e1f2917a857 | 198 | case 3: |
sesa514652 | 20:4e1f2917a857 | 199 | PressA= 3; |
sesa514652 | 20:4e1f2917a857 | 200 | lcd.clear(); |
sesa514652 | 20:4e1f2917a857 | 201 | lcd.printString("Menu 3",0,0); |
sesa514652 | 20:4e1f2917a857 | 202 | lcd.refresh(); |
sesa514652 | 20:4e1f2917a857 | 203 | wait_ms(50); //required to stop Black screen issue |
sesa514652 | 20:4e1f2917a857 | 204 | break; |
sesa514652 | 20:4e1f2917a857 | 205 | } |
sesa514652 | 20:4e1f2917a857 | 206 | |
sesa514652 | 20:4e1f2917a857 | 207 | } |
sesa514652 | 20:4e1f2917a857 | 208 | } |
sesa514652 | 20:4e1f2917a857 | 209 | /* |
sesa514652 | 11:1dec05b7d1c1 | 210 | int button_counter; |
sesa514652 | 10:42e70b596099 | 211 | if (g_buttonA_flag){ |
sesa514652 | 11:1dec05b7d1c1 | 212 | button_counter = button_counter +1; |
sesa514652 | 11:1dec05b7d1c1 | 213 | //case (test) |
sesa514652 | 10:42e70b596099 | 214 | g_buttonA_flag = 0; // if it has, clear the flag |
sesa514652 | 10:42e70b596099 | 215 | |
sesa514652 | 10:42e70b596099 | 216 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 11:1dec05b7d1c1 | 217 | printf("Button A pressed %f\n", button_counter); |
sesa514652 | 10:42e70b596099 | 218 | } |
sesa514652 | 10:42e70b596099 | 219 | if (g_buttonB_flag){ |
sesa514652 | 10:42e70b596099 | 220 | g_buttonB_flag = 0; // if it has, clear the flag |
sesa514652 | 8:770d168713cc | 221 | |
sesa514652 | 10:42e70b596099 | 222 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 10:42e70b596099 | 223 | printf("Button B pressed\n"); |
sesa514652 | 10:42e70b596099 | 224 | } |
sesa514652 | 10:42e70b596099 | 225 | if (g_buttonX_flag){ |
sesa514652 | 10:42e70b596099 | 226 | g_buttonX_flag = 0; // if it has, clear the flag |
sesa514652 | 12:ceea378d5691 | 227 | // swap direction when button has been pressed |
sesa514652 | 12:ceea378d5691 | 228 | // (could just use ! but want this to be explicit to aid understanding) |
sesa514652 | 12:ceea378d5691 | 229 | if (direction == UP) { |
sesa514652 | 12:ceea378d5691 | 230 | direction = DOWN; |
sesa514652 | 12:ceea378d5691 | 231 | } |
sesa514652 | 12:ceea378d5691 | 232 | else { |
sesa514652 | 12:ceea378d5691 | 233 | direction = UP; |
sesa514652 | 12:ceea378d5691 | 234 | } |
sesa514652 | 10:42e70b596099 | 235 | |
sesa514652 | 10:42e70b596099 | 236 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 10:42e70b596099 | 237 | printf("Button X pressed\n"); |
sesa514652 | 10:42e70b596099 | 238 | lcd.clear(); |
sesa514652 | 10:42e70b596099 | 239 | lcd.printString("Button Pressed!",0,0); |
sesa514652 | 10:42e70b596099 | 240 | lcd.refresh(); |
sesa514652 | 11:1dec05b7d1c1 | 241 | wait(1); |
sesa514652 | 10:42e70b596099 | 242 | } |
sesa514652 | 10:42e70b596099 | 243 | if (g_buttonY_flag){ |
sesa514652 | 10:42e70b596099 | 244 | g_buttonY_flag = 0; // if it has, clear the flag |
sesa514652 | 10:42e70b596099 | 245 | |
sesa514652 | 10:42e70b596099 | 246 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 10:42e70b596099 | 247 | printf("Button Y pressed\n"); |
sesa514652 | 10:42e70b596099 | 248 | } |
sesa514652 | 5:9b2c976ca318 | 249 | Buzzer.play(200,120); |
sesa514652 | 5:9b2c976ca318 | 250 | wait_ms(5); |
sesa514652 | 5:9b2c976ca318 | 251 | Buzzer.play(200,120); |
sesa514652 | 1:757728321abd | 252 | long distanced = sensor.distance(); |
sesa514652 | 1:757728321abd | 253 | if (distanced >= 400 || distanced <= 2) |
sesa514652 | 5:9b2c976ca318 | 254 | { |
sesa514652 | 1:757728321abd | 255 | printf("Out of range"); |
sesa514652 | 5:9b2c976ca318 | 256 | //Calling dummy function 05/01/22 |
sesa514652 | 4:167ce930c9d5 | 257 | int answer = cube(2); |
sesa514652 | 5:9b2c976ca318 | 258 | printf("%f\n",answer); |
sesa514652 | 1:757728321abd | 259 | wait(1.0); |
sesa514652 | 5:9b2c976ca318 | 260 | } |
sesa514652 | 5:9b2c976ca318 | 261 | else |
sesa514652 | 5:9b2c976ca318 | 262 | { |
sesa514652 | 5:9b2c976ca318 | 263 | printf("Distance : %d cm",distanced); |
sesa514652 | 5:9b2c976ca318 | 264 | wait(1.0); // 1 sec |
sesa514652 | 1:757728321abd | 265 | } |
sesa514652 | 5:9b2c976ca318 | 266 | lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK); |
sesa514652 | 5:9b2c976ca318 | 267 | //lcd.refresh must be used to update the lcd Display |
sesa514652 | 5:9b2c976ca318 | 268 | lcd.refresh(); |
sesa514652 | 5:9b2c976ca318 | 269 | wait(5.0); |
sesa514652 | 5:9b2c976ca318 | 270 | //these are default settings so not strictly needed |
sesa514652 | 0:1f799c7cce2b | 271 | lcd.normalMode(); // normal colour mode |
sesa514652 | 5:9b2c976ca318 | 272 | lcd.setBrightness(0.5); // put LED backlight on 50% |
sesa514652 | 0:1f799c7cce2b | 273 | lcd.clear(); |
sesa514652 | 0:1f799c7cce2b | 274 | lcd.printString("Hello, World!",0,0); |
sesa514652 | 0:1f799c7cce2b | 275 | Vector2D coord = joystick.get_coord(); |
sesa514652 | 0:1f799c7cce2b | 276 | lcd.refresh(); |
sesa514652 | 0:1f799c7cce2b | 277 | printf("Coord = %f,%f\n",coord.x,coord.y); |
sesa514652 | 0:1f799c7cce2b | 278 | char buffer[14]; |
sesa514652 | 0:1f799c7cce2b | 279 | Vector2D mapped_coord = joystick.get_mapped_coord(); |
sesa514652 | 0:1f799c7cce2b | 280 | printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y); |
sesa514652 | 0:1f799c7cce2b | 281 | |
sesa514652 | 5:9b2c976ca318 | 282 | int length = sprintf(buffer,"T = %2f",coord.x); |
sesa514652 | 5:9b2c976ca318 | 283 | //it is important the format specifier ensures the length will fit in the buffer |
sesa514652 | 0:1f799c7cce2b | 284 | if (length <= 14) // if string will fit on display (assuming printing at x=0) |
sesa514652 | 5:9b2c976ca318 | 285 | lcd.printString(buffer,0,1); // display on screen |
sesa514652 | 0:1f799c7cce2b | 286 | lcd.refresh(); |
sesa514652 | 5:9b2c976ca318 | 287 | lcd.clear(); |
sesa514652 | 5:9b2c976ca318 | 288 | //times by 50 to try get the point moving across screen |
sesa514652 | 0:1f799c7cce2b | 289 | float x50 = mapped_coord.x*5; |
sesa514652 | 0:1f799c7cce2b | 290 | float y50 = mapped_coord.y*8; |
sesa514652 | 5:9b2c976ca318 | 291 | //to see what the values are |
sesa514652 | 5:9b2c976ca318 | 292 | printf(" *50 = %f,%f\n",x50,y50); |
sesa514652 | 5:9b2c976ca318 | 293 | //example of how to draw circles |
sesa514652 | 1:757728321abd | 294 | lcd.printChar('o',WIDTH/2,HEIGHT/2); |
sesa514652 | 5:9b2c976ca318 | 295 | lcd.refresh(); |
sesa514652 | 0:1f799c7cce2b | 296 | float mag = joystick.get_mag(); |
sesa514652 | 0:1f799c7cce2b | 297 | float angle = joystick.get_angle(); |
sesa514652 | 0:1f799c7cce2b | 298 | printf("Mag = %f Angle = %f\n",mag,angle); |
sesa514652 | 0:1f799c7cce2b | 299 | Direction d = joystick.get_direction(); |
sesa514652 | 0:1f799c7cce2b | 300 | printf("Direction = %i\n",d); |
sesa514652 | 0:1f799c7cce2b | 301 | if (joystick.button_pressed() ) { |
sesa514652 | 0:1f799c7cce2b | 302 | printf("Button Pressed\n"); |
sesa514652 | 9:cdd7d9a123f9 | 303 | wait(5); |
sesa514652 | 0:1f799c7cce2b | 304 | } |
sesa514652 | 8:770d168713cc | 305 | |
sesa514652 | 8:770d168713cc | 306 | } |
sesa514652 | 16:2646c1c4e62e | 307 | } |
sesa514652 | 20:4e1f2917a857 | 308 | */ |
sesa514652 | 18:bb36d96142dd | 309 | |
sesa514652 | 18:bb36d96142dd | 310 | |
sesa514652 | 18:bb36d96142dd | 311 | int buttonCountA(){ |
sesa514652 | 18:bb36d96142dd | 312 | PressA++; |
sesa514652 | 18:bb36d96142dd | 313 | if (PressA>3){ |
sesa514652 | 18:bb36d96142dd | 314 | PressA= 0; |
sesa514652 | 18:bb36d96142dd | 315 | } |
sesa514652 | 18:bb36d96142dd | 316 | return PressA; |
sesa514652 | 18:bb36d96142dd | 317 | } |
sesa514652 | 18:bb36d96142dd | 318 | int buttonCountB(){ |
sesa514652 | 18:bb36d96142dd | 319 | PressB++; |
sesa514652 | 18:bb36d96142dd | 320 | if (PressB>3){ |
sesa514652 | 18:bb36d96142dd | 321 | PressB= 0; |
sesa514652 | 18:bb36d96142dd | 322 | } |
sesa514652 | 18:bb36d96142dd | 323 | return PressB; |
sesa514652 | 18:bb36d96142dd | 324 | } |
sesa514652 | 15:234321e80678 | 325 | |
sesa514652 | 12:ceea378d5691 | 326 | //Button A event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 327 | void buttonA_isr() |
sesa514652 | 10:42e70b596099 | 328 | { |
sesa514652 | 12:ceea378d5691 | 329 | |
sesa514652 | 18:bb36d96142dd | 330 | long distanced = sensor.distance(); |
sesa514652 | 18:bb36d96142dd | 331 | buttonCountA(); |
sesa514652 | 18:bb36d96142dd | 332 | |
sesa514652 | 16:2646c1c4e62e | 333 | |
sesa514652 | 12:ceea378d5691 | 334 | |
sesa514652 | 12:ceea378d5691 | 335 | g_buttonA_flag = 1; // set flag in ISR |
sesa514652 | 12:ceea378d5691 | 336 | |
sesa514652 | 18:bb36d96142dd | 337 | printf("press count = %i\n",PressA); |
sesa514652 | 12:ceea378d5691 | 338 | |
sesa514652 | 12:ceea378d5691 | 339 | // lcd.clear(); |
sesa514652 | 15:234321e80678 | 340 | //lcd.printString("Button A Pressed",0,0); |
sesa514652 | 12:ceea378d5691 | 341 | |
sesa514652 | 15:234321e80678 | 342 | // lcd.refresh(); // set flag in ISR |
sesa514652 | 12:ceea378d5691 | 343 | } |
sesa514652 | 12:ceea378d5691 | 344 | |
sesa514652 | 11:1dec05b7d1c1 | 345 | |
sesa514652 | 10:42e70b596099 | 346 | //Button B event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 347 | void buttonB_isr() |
sesa514652 | 10:42e70b596099 | 348 | { |
sesa514652 | 18:bb36d96142dd | 349 | buttonCountB(); |
sesa514652 | 10:42e70b596099 | 350 | g_buttonB_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 351 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 352 | lcd.printString("Button B Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 353 | lcd.refresh(); // set flag in ISR |
sesa514652 | 18:bb36d96142dd | 354 | printf("press count = %i\n",PressB); |
sesa514652 | 10:42e70b596099 | 355 | } |
sesa514652 | 10:42e70b596099 | 356 | //Button X event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 357 | void buttonX_isr() |
sesa514652 | 10:42e70b596099 | 358 | { |
sesa514652 | 10:42e70b596099 | 359 | g_buttonX_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 360 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 361 | lcd.printString("Button X Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 362 | lcd.refresh(); // set flag in ISR |
sesa514652 | 10:42e70b596099 | 363 | } |
sesa514652 | 10:42e70b596099 | 364 | //Button Y event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 365 | void buttonY_isr() |
sesa514652 | 10:42e70b596099 | 366 | { |
sesa514652 | 10:42e70b596099 | 367 | g_buttonY_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 368 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 369 | lcd.printString("Button Y Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 370 | lcd.refresh(); // set flag in ISR |
sesa514652 | 10:42e70b596099 | 371 | } |
sesa514652 | 11:1dec05b7d1c1 | 372 | //Button Start event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 373 | void buttonStart_isr() |
sesa514652 | 11:1dec05b7d1c1 | 374 | { |
sesa514652 | 11:1dec05b7d1c1 | 375 | g_buttonStart_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 376 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 377 | lcd.printString("Button Start Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 378 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 379 | } |
sesa514652 | 11:1dec05b7d1c1 | 380 | //Button Back event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 381 | void buttonBack_isr() |
sesa514652 | 11:1dec05b7d1c1 | 382 | { |
sesa514652 | 11:1dec05b7d1c1 | 383 | g_buttonBack_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 384 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 385 | lcd.printString("Button Back Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 386 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 387 | } |
sesa514652 | 11:1dec05b7d1c1 | 388 | //Button Left event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 389 | void buttonLeft_isr() |
sesa514652 | 11:1dec05b7d1c1 | 390 | { |
sesa514652 | 11:1dec05b7d1c1 | 391 | g_buttonLeft_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 392 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 393 | lcd.printString("Button Left Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 394 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 395 | } |
sesa514652 | 11:1dec05b7d1c1 | 396 | //Button Right event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 397 | void buttonRight_isr() |
sesa514652 | 11:1dec05b7d1c1 | 398 | { |
sesa514652 | 11:1dec05b7d1c1 | 399 | g_buttonRight_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 400 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 401 | lcd.printString("Button Right Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 402 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 403 | } |