![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Coursework
main.cpp@21:2e37f80e8b5b, 2022-01-18 (annotated)
- Committer:
- sesa514652
- Date:
- Tue Jan 18 21:01:54 2022 +0000
- Revision:
- 21:2e37f80e8b5b
- Parent:
- 20:4e1f2917a857
- Child:
- 22:82b01fdfc61f
Working further with arrays and trying get information stored within array
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 | 21:2e37f80e8b5b | 174 | |
sesa514652 | 21:2e37f80e8b5b | 175 | |
sesa514652 | 21:2e37f80e8b5b | 176 | |
sesa514652 | 20:4e1f2917a857 | 177 | switch(PressA){ |
sesa514652 | 21:2e37f80e8b5b | 178 | default: |
sesa514652 | 21:2e37f80e8b5b | 179 | lcd.clear(); |
sesa514652 | 21:2e37f80e8b5b | 180 | lcd.printString("Main Menu",0,0); |
sesa514652 | 21:2e37f80e8b5b | 181 | lcd.drawLine(0,10,80,10,2); |
sesa514652 | 21:2e37f80e8b5b | 182 | |
sesa514652 | 21:2e37f80e8b5b | 183 | lcd.printString("A to begin new Scan",0,2); |
sesa514652 | 21:2e37f80e8b5b | 184 | lcd.printString("B to Display last object",0,3); |
sesa514652 | 21:2e37f80e8b5b | 185 | lcd.refresh(); |
sesa514652 | 21:2e37f80e8b5b | 186 | wait_ms(50); |
sesa514652 | 21:2e37f80e8b5b | 187 | |
sesa514652 | 21:2e37f80e8b5b | 188 | break; |
sesa514652 | 21:2e37f80e8b5b | 189 | case 1: |
sesa514652 | 21:2e37f80e8b5b | 190 | PressA =1; |
sesa514652 | 21:2e37f80e8b5b | 191 | lcd.clear(); |
sesa514652 | 21:2e37f80e8b5b | 192 | long firstScan[10]; |
sesa514652 | 21:2e37f80e8b5b | 193 | lcd.printString("First Scan Left Right",0,0); |
sesa514652 | 21:2e37f80e8b5b | 194 | lcd.refresh(); |
sesa514652 | 21:2e37f80e8b5b | 195 | for (int i = 0; i < 5; i++) { |
sesa514652 | 21:2e37f80e8b5b | 196 | printf(" i is: %d cm",i); |
sesa514652 | 21:2e37f80e8b5b | 197 | } |
sesa514652 | 20:4e1f2917a857 | 198 | wait_ms(50); // required to stop Black screen issue |
sesa514652 | 20:4e1f2917a857 | 199 | break; |
sesa514652 | 21:2e37f80e8b5b | 200 | |
sesa514652 | 20:4e1f2917a857 | 201 | case 2: |
sesa514652 | 20:4e1f2917a857 | 202 | PressA= 2; |
sesa514652 | 21:2e37f80e8b5b | 203 | // firstScan[1] = sensor.distance(); |
sesa514652 | 21:2e37f80e8b5b | 204 | // printf("Distance : %d cm",firstScan[x]); |
sesa514652 | 20:4e1f2917a857 | 205 | lcd.clear(); |
sesa514652 | 20:4e1f2917a857 | 206 | lcd.printString("Menu 2",0,0); |
sesa514652 | 20:4e1f2917a857 | 207 | lcd.refresh(); |
sesa514652 | 20:4e1f2917a857 | 208 | wait_ms(50); // required to stop Black screen issue |
sesa514652 | 20:4e1f2917a857 | 209 | break; |
sesa514652 | 20:4e1f2917a857 | 210 | case 3: |
sesa514652 | 20:4e1f2917a857 | 211 | PressA= 3; |
sesa514652 | 20:4e1f2917a857 | 212 | lcd.clear(); |
sesa514652 | 20:4e1f2917a857 | 213 | lcd.printString("Menu 3",0,0); |
sesa514652 | 21:2e37f80e8b5b | 214 | long us = sensor.distance(); |
sesa514652 | 21:2e37f80e8b5b | 215 | printf("Distance : %d cm",us); |
sesa514652 | 20:4e1f2917a857 | 216 | lcd.refresh(); |
sesa514652 | 20:4e1f2917a857 | 217 | wait_ms(50); //required to stop Black screen issue |
sesa514652 | 20:4e1f2917a857 | 218 | break; |
sesa514652 | 20:4e1f2917a857 | 219 | } |
sesa514652 | 20:4e1f2917a857 | 220 | |
sesa514652 | 20:4e1f2917a857 | 221 | } |
sesa514652 | 20:4e1f2917a857 | 222 | } |
sesa514652 | 20:4e1f2917a857 | 223 | /* |
sesa514652 | 11:1dec05b7d1c1 | 224 | int button_counter; |
sesa514652 | 10:42e70b596099 | 225 | if (g_buttonA_flag){ |
sesa514652 | 11:1dec05b7d1c1 | 226 | button_counter = button_counter +1; |
sesa514652 | 11:1dec05b7d1c1 | 227 | //case (test) |
sesa514652 | 10:42e70b596099 | 228 | g_buttonA_flag = 0; // if it has, clear the flag |
sesa514652 | 10:42e70b596099 | 229 | |
sesa514652 | 10:42e70b596099 | 230 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 11:1dec05b7d1c1 | 231 | printf("Button A pressed %f\n", button_counter); |
sesa514652 | 10:42e70b596099 | 232 | } |
sesa514652 | 10:42e70b596099 | 233 | if (g_buttonB_flag){ |
sesa514652 | 10:42e70b596099 | 234 | g_buttonB_flag = 0; // if it has, clear the flag |
sesa514652 | 8:770d168713cc | 235 | |
sesa514652 | 10:42e70b596099 | 236 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 10:42e70b596099 | 237 | printf("Button B pressed\n"); |
sesa514652 | 10:42e70b596099 | 238 | } |
sesa514652 | 10:42e70b596099 | 239 | if (g_buttonX_flag){ |
sesa514652 | 10:42e70b596099 | 240 | g_buttonX_flag = 0; // if it has, clear the flag |
sesa514652 | 12:ceea378d5691 | 241 | // swap direction when button has been pressed |
sesa514652 | 12:ceea378d5691 | 242 | // (could just use ! but want this to be explicit to aid understanding) |
sesa514652 | 12:ceea378d5691 | 243 | if (direction == UP) { |
sesa514652 | 12:ceea378d5691 | 244 | direction = DOWN; |
sesa514652 | 12:ceea378d5691 | 245 | } |
sesa514652 | 12:ceea378d5691 | 246 | else { |
sesa514652 | 12:ceea378d5691 | 247 | direction = UP; |
sesa514652 | 12:ceea378d5691 | 248 | } |
sesa514652 | 10:42e70b596099 | 249 | |
sesa514652 | 10:42e70b596099 | 250 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 10:42e70b596099 | 251 | printf("Button X pressed\n"); |
sesa514652 | 10:42e70b596099 | 252 | lcd.clear(); |
sesa514652 | 10:42e70b596099 | 253 | lcd.printString("Button Pressed!",0,0); |
sesa514652 | 10:42e70b596099 | 254 | lcd.refresh(); |
sesa514652 | 11:1dec05b7d1c1 | 255 | wait(1); |
sesa514652 | 10:42e70b596099 | 256 | } |
sesa514652 | 10:42e70b596099 | 257 | if (g_buttonY_flag){ |
sesa514652 | 10:42e70b596099 | 258 | g_buttonY_flag = 0; // if it has, clear the flag |
sesa514652 | 10:42e70b596099 | 259 | |
sesa514652 | 10:42e70b596099 | 260 | // send message over serial port - can observe in CoolTerm etc. |
sesa514652 | 10:42e70b596099 | 261 | printf("Button Y pressed\n"); |
sesa514652 | 10:42e70b596099 | 262 | } |
sesa514652 | 5:9b2c976ca318 | 263 | Buzzer.play(200,120); |
sesa514652 | 5:9b2c976ca318 | 264 | wait_ms(5); |
sesa514652 | 5:9b2c976ca318 | 265 | Buzzer.play(200,120); |
sesa514652 | 1:757728321abd | 266 | long distanced = sensor.distance(); |
sesa514652 | 1:757728321abd | 267 | if (distanced >= 400 || distanced <= 2) |
sesa514652 | 5:9b2c976ca318 | 268 | { |
sesa514652 | 1:757728321abd | 269 | printf("Out of range"); |
sesa514652 | 5:9b2c976ca318 | 270 | //Calling dummy function 05/01/22 |
sesa514652 | 4:167ce930c9d5 | 271 | int answer = cube(2); |
sesa514652 | 5:9b2c976ca318 | 272 | printf("%f\n",answer); |
sesa514652 | 1:757728321abd | 273 | wait(1.0); |
sesa514652 | 5:9b2c976ca318 | 274 | } |
sesa514652 | 5:9b2c976ca318 | 275 | else |
sesa514652 | 5:9b2c976ca318 | 276 | { |
sesa514652 | 5:9b2c976ca318 | 277 | printf("Distance : %d cm",distanced); |
sesa514652 | 5:9b2c976ca318 | 278 | wait(1.0); // 1 sec |
sesa514652 | 1:757728321abd | 279 | } |
sesa514652 | 5:9b2c976ca318 | 280 | lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK); |
sesa514652 | 5:9b2c976ca318 | 281 | //lcd.refresh must be used to update the lcd Display |
sesa514652 | 5:9b2c976ca318 | 282 | lcd.refresh(); |
sesa514652 | 5:9b2c976ca318 | 283 | wait(5.0); |
sesa514652 | 5:9b2c976ca318 | 284 | //these are default settings so not strictly needed |
sesa514652 | 0:1f799c7cce2b | 285 | lcd.normalMode(); // normal colour mode |
sesa514652 | 5:9b2c976ca318 | 286 | lcd.setBrightness(0.5); // put LED backlight on 50% |
sesa514652 | 0:1f799c7cce2b | 287 | lcd.clear(); |
sesa514652 | 0:1f799c7cce2b | 288 | lcd.printString("Hello, World!",0,0); |
sesa514652 | 0:1f799c7cce2b | 289 | Vector2D coord = joystick.get_coord(); |
sesa514652 | 0:1f799c7cce2b | 290 | lcd.refresh(); |
sesa514652 | 0:1f799c7cce2b | 291 | printf("Coord = %f,%f\n",coord.x,coord.y); |
sesa514652 | 0:1f799c7cce2b | 292 | char buffer[14]; |
sesa514652 | 0:1f799c7cce2b | 293 | Vector2D mapped_coord = joystick.get_mapped_coord(); |
sesa514652 | 0:1f799c7cce2b | 294 | printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y); |
sesa514652 | 0:1f799c7cce2b | 295 | |
sesa514652 | 5:9b2c976ca318 | 296 | int length = sprintf(buffer,"T = %2f",coord.x); |
sesa514652 | 5:9b2c976ca318 | 297 | //it is important the format specifier ensures the length will fit in the buffer |
sesa514652 | 0:1f799c7cce2b | 298 | if (length <= 14) // if string will fit on display (assuming printing at x=0) |
sesa514652 | 5:9b2c976ca318 | 299 | lcd.printString(buffer,0,1); // display on screen |
sesa514652 | 0:1f799c7cce2b | 300 | lcd.refresh(); |
sesa514652 | 5:9b2c976ca318 | 301 | lcd.clear(); |
sesa514652 | 5:9b2c976ca318 | 302 | //times by 50 to try get the point moving across screen |
sesa514652 | 0:1f799c7cce2b | 303 | float x50 = mapped_coord.x*5; |
sesa514652 | 0:1f799c7cce2b | 304 | float y50 = mapped_coord.y*8; |
sesa514652 | 5:9b2c976ca318 | 305 | //to see what the values are |
sesa514652 | 5:9b2c976ca318 | 306 | printf(" *50 = %f,%f\n",x50,y50); |
sesa514652 | 5:9b2c976ca318 | 307 | //example of how to draw circles |
sesa514652 | 1:757728321abd | 308 | lcd.printChar('o',WIDTH/2,HEIGHT/2); |
sesa514652 | 5:9b2c976ca318 | 309 | lcd.refresh(); |
sesa514652 | 0:1f799c7cce2b | 310 | float mag = joystick.get_mag(); |
sesa514652 | 0:1f799c7cce2b | 311 | float angle = joystick.get_angle(); |
sesa514652 | 0:1f799c7cce2b | 312 | printf("Mag = %f Angle = %f\n",mag,angle); |
sesa514652 | 0:1f799c7cce2b | 313 | Direction d = joystick.get_direction(); |
sesa514652 | 0:1f799c7cce2b | 314 | printf("Direction = %i\n",d); |
sesa514652 | 0:1f799c7cce2b | 315 | if (joystick.button_pressed() ) { |
sesa514652 | 0:1f799c7cce2b | 316 | printf("Button Pressed\n"); |
sesa514652 | 9:cdd7d9a123f9 | 317 | wait(5); |
sesa514652 | 0:1f799c7cce2b | 318 | } |
sesa514652 | 8:770d168713cc | 319 | |
sesa514652 | 8:770d168713cc | 320 | } |
sesa514652 | 16:2646c1c4e62e | 321 | } |
sesa514652 | 20:4e1f2917a857 | 322 | */ |
sesa514652 | 18:bb36d96142dd | 323 | |
sesa514652 | 18:bb36d96142dd | 324 | |
sesa514652 | 18:bb36d96142dd | 325 | int buttonCountA(){ |
sesa514652 | 18:bb36d96142dd | 326 | PressA++; |
sesa514652 | 18:bb36d96142dd | 327 | if (PressA>3){ |
sesa514652 | 18:bb36d96142dd | 328 | PressA= 0; |
sesa514652 | 18:bb36d96142dd | 329 | } |
sesa514652 | 18:bb36d96142dd | 330 | return PressA; |
sesa514652 | 18:bb36d96142dd | 331 | } |
sesa514652 | 18:bb36d96142dd | 332 | int buttonCountB(){ |
sesa514652 | 18:bb36d96142dd | 333 | PressB++; |
sesa514652 | 18:bb36d96142dd | 334 | if (PressB>3){ |
sesa514652 | 18:bb36d96142dd | 335 | PressB= 0; |
sesa514652 | 18:bb36d96142dd | 336 | } |
sesa514652 | 18:bb36d96142dd | 337 | return PressB; |
sesa514652 | 18:bb36d96142dd | 338 | } |
sesa514652 | 15:234321e80678 | 339 | |
sesa514652 | 12:ceea378d5691 | 340 | //Button A event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 341 | void buttonA_isr() |
sesa514652 | 10:42e70b596099 | 342 | { |
sesa514652 | 12:ceea378d5691 | 343 | |
sesa514652 | 18:bb36d96142dd | 344 | long distanced = sensor.distance(); |
sesa514652 | 18:bb36d96142dd | 345 | buttonCountA(); |
sesa514652 | 18:bb36d96142dd | 346 | |
sesa514652 | 16:2646c1c4e62e | 347 | |
sesa514652 | 12:ceea378d5691 | 348 | |
sesa514652 | 12:ceea378d5691 | 349 | g_buttonA_flag = 1; // set flag in ISR |
sesa514652 | 12:ceea378d5691 | 350 | |
sesa514652 | 18:bb36d96142dd | 351 | printf("press count = %i\n",PressA); |
sesa514652 | 12:ceea378d5691 | 352 | |
sesa514652 | 12:ceea378d5691 | 353 | // lcd.clear(); |
sesa514652 | 15:234321e80678 | 354 | //lcd.printString("Button A Pressed",0,0); |
sesa514652 | 12:ceea378d5691 | 355 | |
sesa514652 | 15:234321e80678 | 356 | // lcd.refresh(); // set flag in ISR |
sesa514652 | 12:ceea378d5691 | 357 | } |
sesa514652 | 12:ceea378d5691 | 358 | |
sesa514652 | 11:1dec05b7d1c1 | 359 | |
sesa514652 | 10:42e70b596099 | 360 | //Button B event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 361 | void buttonB_isr() |
sesa514652 | 10:42e70b596099 | 362 | { |
sesa514652 | 18:bb36d96142dd | 363 | buttonCountB(); |
sesa514652 | 10:42e70b596099 | 364 | g_buttonB_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 365 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 366 | lcd.printString("Button B Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 367 | lcd.refresh(); // set flag in ISR |
sesa514652 | 18:bb36d96142dd | 368 | printf("press count = %i\n",PressB); |
sesa514652 | 10:42e70b596099 | 369 | } |
sesa514652 | 10:42e70b596099 | 370 | //Button X event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 371 | void buttonX_isr() |
sesa514652 | 10:42e70b596099 | 372 | { |
sesa514652 | 10:42e70b596099 | 373 | g_buttonX_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 374 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 375 | lcd.printString("Button X Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 376 | lcd.refresh(); // set flag in ISR |
sesa514652 | 10:42e70b596099 | 377 | } |
sesa514652 | 10:42e70b596099 | 378 | //Button Y event-triggered interrupt |
sesa514652 | 10:42e70b596099 | 379 | void buttonY_isr() |
sesa514652 | 10:42e70b596099 | 380 | { |
sesa514652 | 10:42e70b596099 | 381 | g_buttonY_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 382 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 383 | lcd.printString("Button Y Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 384 | lcd.refresh(); // set flag in ISR |
sesa514652 | 10:42e70b596099 | 385 | } |
sesa514652 | 11:1dec05b7d1c1 | 386 | //Button Start event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 387 | void buttonStart_isr() |
sesa514652 | 11:1dec05b7d1c1 | 388 | { |
sesa514652 | 11:1dec05b7d1c1 | 389 | g_buttonStart_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 390 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 391 | lcd.printString("Button Start Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 392 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 393 | } |
sesa514652 | 11:1dec05b7d1c1 | 394 | //Button Back event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 395 | void buttonBack_isr() |
sesa514652 | 11:1dec05b7d1c1 | 396 | { |
sesa514652 | 11:1dec05b7d1c1 | 397 | g_buttonBack_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 398 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 399 | lcd.printString("Button Back Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 400 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 401 | } |
sesa514652 | 11:1dec05b7d1c1 | 402 | //Button Left event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 403 | void buttonLeft_isr() |
sesa514652 | 11:1dec05b7d1c1 | 404 | { |
sesa514652 | 11:1dec05b7d1c1 | 405 | g_buttonLeft_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 406 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 407 | lcd.printString("Button Left Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 408 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 409 | } |
sesa514652 | 11:1dec05b7d1c1 | 410 | //Button Right event-triggered interrupt |
sesa514652 | 11:1dec05b7d1c1 | 411 | void buttonRight_isr() |
sesa514652 | 11:1dec05b7d1c1 | 412 | { |
sesa514652 | 11:1dec05b7d1c1 | 413 | g_buttonRight_flag = 1; // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 414 | lcd.clear(); |
sesa514652 | 11:1dec05b7d1c1 | 415 | lcd.printString("Button Right Pressed",0,0); |
sesa514652 | 11:1dec05b7d1c1 | 416 | lcd.refresh(); // set flag in ISR |
sesa514652 | 11:1dec05b7d1c1 | 417 | } |