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