4180 Lab 4 v1.0

Dependencies:   4DGL-uLCD-SE mbed

Fork of MPR121_Demo by jim hamblen

Committer:
jsobchuk3
Date:
Mon Oct 20 23:54:30 2014 +0000
Revision:
1:288da1e5d10b
Parent:
0:e09703934ff4
Touchpad Game v1.0 (Lab 4)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:e09703934ff4 1 /*
jsobchuk3 1:288da1e5d10b 2 Dhruvin Desai and Joseph Sobchuk
4180_1 0:e09703934ff4 3
jsobchuk3 1:288da1e5d10b 4 fallInterrupt() and API for mpr121 written by Anthony Buckton in 2011
4180_1 0:e09703934ff4 5 */
4180_1 0:e09703934ff4 6
4180_1 0:e09703934ff4 7 #include <mbed.h>
jsobchuk3 1:288da1e5d10b 8 #include <mpr121.h> // Thanks to Anthony Buckton
jsobchuk3 1:288da1e5d10b 9 #include "uLCD_4DGL.h"
jsobchuk3 1:288da1e5d10b 10
jsobchuk3 1:288da1e5d10b 11 uLCD_4DGL uLCD(p28,p27,p29);
4180_1 0:e09703934ff4 12
4180_1 0:e09703934ff4 13 // Create the interrupt receiver object on pin 26
4180_1 0:e09703934ff4 14 InterruptIn interrupt(p26);
4180_1 0:e09703934ff4 15
jsobchuk3 1:288da1e5d10b 16 // Setup the i2c bus on pins 9 and 10
4180_1 0:e09703934ff4 17 I2C i2c(p9, p10);
4180_1 0:e09703934ff4 18
4180_1 0:e09703934ff4 19 // Setup the Mpr121:
4180_1 0:e09703934ff4 20 // constructor(i2c object, i2c address of the mpr121)
4180_1 0:e09703934ff4 21 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
4180_1 0:e09703934ff4 22
jsobchuk3 1:288da1e5d10b 23 int on [9] = {0,0,0,0,0,0,0,0,0};
jsobchuk3 1:288da1e5d10b 24 int key;
jsobchuk3 1:288da1e5d10b 25 int radius = 15;
jsobchuk3 1:288da1e5d10b 26 int initpos = 23; // Initial x and y shift from the edge of the display
jsobchuk3 1:288da1e5d10b 27 int shift = 40; // Space in between each circle
4180_1 0:e09703934ff4 28
jsobchuk3 1:288da1e5d10b 29 // Function to invert any specified circle
jsobchuk3 1:288da1e5d10b 30 void invert(int on [9], int circleN) {
jsobchuk3 1:288da1e5d10b 31 switch (circleN) {
jsobchuk3 1:288da1e5d10b 32
jsobchuk3 1:288da1e5d10b 33 case 1:
jsobchuk3 1:288da1e5d10b 34 if (on[0] == 0) { // If circle 1 is off then Fill 1st Circle
jsobchuk3 1:288da1e5d10b 35 uLCD.filled_circle(initpos, initpos, radius, RED); // Fill 1st Circle
jsobchuk3 1:288da1e5d10b 36 on[0] = 1; // Then change status of circle 1 to on
4180_1 0:e09703934ff4 37 }
jsobchuk3 1:288da1e5d10b 38 else if (on[0] == 1) { // If circle 1 is on then erase 1st circle
jsobchuk3 1:288da1e5d10b 39 uLCD.filled_circle(initpos, initpos, radius, BLACK);
jsobchuk3 1:288da1e5d10b 40 uLCD.circle(initpos, initpos, radius, RED); // Erase 1st Circle
jsobchuk3 1:288da1e5d10b 41 on[0] = 0;
jsobchuk3 1:288da1e5d10b 42 }
jsobchuk3 1:288da1e5d10b 43 break;
jsobchuk3 1:288da1e5d10b 44
jsobchuk3 1:288da1e5d10b 45 case 2:
jsobchuk3 1:288da1e5d10b 46 if (on[1] == 0) {
jsobchuk3 1:288da1e5d10b 47 uLCD.filled_circle(initpos + shift, initpos, radius, RED); // Fill 2nd Circle
jsobchuk3 1:288da1e5d10b 48 on[1] = 1;
jsobchuk3 1:288da1e5d10b 49 }
jsobchuk3 1:288da1e5d10b 50
jsobchuk3 1:288da1e5d10b 51 else if (on[1] == 1) {
jsobchuk3 1:288da1e5d10b 52 uLCD.filled_circle(initpos + shift, initpos, radius, BLACK);
jsobchuk3 1:288da1e5d10b 53 uLCD.circle(initpos + shift, initpos, radius, RED); // Erase 2nd Circle
jsobchuk3 1:288da1e5d10b 54 on[1] = 0;
jsobchuk3 1:288da1e5d10b 55 }
jsobchuk3 1:288da1e5d10b 56 break;
jsobchuk3 1:288da1e5d10b 57
jsobchuk3 1:288da1e5d10b 58
jsobchuk3 1:288da1e5d10b 59 case 3:
jsobchuk3 1:288da1e5d10b 60 if (on[2] == 0) {
jsobchuk3 1:288da1e5d10b 61 uLCD.filled_circle(initpos + 2*shift, initpos, radius, RED);
jsobchuk3 1:288da1e5d10b 62 on[2] = 1;
jsobchuk3 1:288da1e5d10b 63 }
jsobchuk3 1:288da1e5d10b 64 else if (on[2] == 1) {
jsobchuk3 1:288da1e5d10b 65 uLCD.filled_circle(initpos + 2*shift, initpos, radius, BLACK);
jsobchuk3 1:288da1e5d10b 66 uLCD.circle(initpos + 2*shift, initpos, radius, RED);
jsobchuk3 1:288da1e5d10b 67 on[2] = 0;
jsobchuk3 1:288da1e5d10b 68 }
jsobchuk3 1:288da1e5d10b 69 break;
jsobchuk3 1:288da1e5d10b 70
jsobchuk3 1:288da1e5d10b 71
jsobchuk3 1:288da1e5d10b 72 case 4:
jsobchuk3 1:288da1e5d10b 73 if (on[3] == 0) {
jsobchuk3 1:288da1e5d10b 74 uLCD.filled_circle(initpos, initpos + shift, radius, RED); // Fill 4th Circle
jsobchuk3 1:288da1e5d10b 75 on[3] = 1;
jsobchuk3 1:288da1e5d10b 76 }
jsobchuk3 1:288da1e5d10b 77
jsobchuk3 1:288da1e5d10b 78 else if (on[3] == 1) {
jsobchuk3 1:288da1e5d10b 79 uLCD.filled_circle(initpos, initpos + shift, radius, BLACK);
jsobchuk3 1:288da1e5d10b 80 uLCD.circle(initpos, initpos + shift, radius, RED); // Erase 4th Circle
jsobchuk3 1:288da1e5d10b 81 on[3] = 0;
jsobchuk3 1:288da1e5d10b 82 }
jsobchuk3 1:288da1e5d10b 83 break;
jsobchuk3 1:288da1e5d10b 84
jsobchuk3 1:288da1e5d10b 85 case 5:
jsobchuk3 1:288da1e5d10b 86 if (on[4] == 0) {
jsobchuk3 1:288da1e5d10b 87 uLCD.filled_circle(initpos + shift, initpos + shift, radius, RED); // Fill 5th Circle
jsobchuk3 1:288da1e5d10b 88 on[4] = 1;
jsobchuk3 1:288da1e5d10b 89 }
jsobchuk3 1:288da1e5d10b 90
jsobchuk3 1:288da1e5d10b 91 else if (on[4] == 1) {
jsobchuk3 1:288da1e5d10b 92 uLCD.filled_circle(initpos + shift, initpos + shift, radius, BLACK);
jsobchuk3 1:288da1e5d10b 93 uLCD.circle(initpos + shift, initpos + shift, radius, RED); // Erase 5th Circle
jsobchuk3 1:288da1e5d10b 94 on[4] = 0;
jsobchuk3 1:288da1e5d10b 95 }
jsobchuk3 1:288da1e5d10b 96 break;
jsobchuk3 1:288da1e5d10b 97
jsobchuk3 1:288da1e5d10b 98 case 6:
jsobchuk3 1:288da1e5d10b 99 if (on[5] == 0) {
jsobchuk3 1:288da1e5d10b 100 uLCD.filled_circle(initpos + 2*shift, initpos + shift, radius, RED); // Fill 6th Circle
jsobchuk3 1:288da1e5d10b 101 on[5] = 1;
jsobchuk3 1:288da1e5d10b 102 }
jsobchuk3 1:288da1e5d10b 103
jsobchuk3 1:288da1e5d10b 104 else if (on[5] == 1) {
jsobchuk3 1:288da1e5d10b 105 uLCD.filled_circle(initpos + 2*shift, initpos + shift, radius, BLACK);
jsobchuk3 1:288da1e5d10b 106 uLCD.circle(initpos + 2*shift, initpos + shift, radius, RED); // Erase 6th Circle
jsobchuk3 1:288da1e5d10b 107 on[5] = 0;
jsobchuk3 1:288da1e5d10b 108 }
jsobchuk3 1:288da1e5d10b 109 break;
jsobchuk3 1:288da1e5d10b 110
jsobchuk3 1:288da1e5d10b 111
jsobchuk3 1:288da1e5d10b 112
jsobchuk3 1:288da1e5d10b 113 case 7:
jsobchuk3 1:288da1e5d10b 114 if (on[6] == 0) {
jsobchuk3 1:288da1e5d10b 115 uLCD.filled_circle(initpos, initpos + 2*shift, radius, RED); // Fill 7th Circle
jsobchuk3 1:288da1e5d10b 116 on[6] = 1;
jsobchuk3 1:288da1e5d10b 117 }
jsobchuk3 1:288da1e5d10b 118
jsobchuk3 1:288da1e5d10b 119 else if (on[6] == 1) {
jsobchuk3 1:288da1e5d10b 120 uLCD.filled_circle(initpos, initpos + 2*shift, radius, BLACK); // Erase 7th Circle
jsobchuk3 1:288da1e5d10b 121 uLCD.circle(initpos, initpos + 2*shift, radius, RED);
jsobchuk3 1:288da1e5d10b 122 on[6] = 0;
jsobchuk3 1:288da1e5d10b 123 }
jsobchuk3 1:288da1e5d10b 124 break;
jsobchuk3 1:288da1e5d10b 125
jsobchuk3 1:288da1e5d10b 126
jsobchuk3 1:288da1e5d10b 127
jsobchuk3 1:288da1e5d10b 128 case 8:
jsobchuk3 1:288da1e5d10b 129 if (on[7] == 0) {
jsobchuk3 1:288da1e5d10b 130 uLCD.filled_circle(initpos + shift, initpos + 2*shift, radius, RED); // Fill 8th Circle
jsobchuk3 1:288da1e5d10b 131 on[7] = 1;
jsobchuk3 1:288da1e5d10b 132 }
jsobchuk3 1:288da1e5d10b 133
jsobchuk3 1:288da1e5d10b 134 else if (on[7] == 1) {
jsobchuk3 1:288da1e5d10b 135 uLCD.filled_circle(initpos + shift, initpos + 2*shift, radius, BLACK); // Erase 8th Circle
jsobchuk3 1:288da1e5d10b 136 uLCD.circle(initpos + shift, initpos + 2*shift, radius, RED);
jsobchuk3 1:288da1e5d10b 137 on[7] = 0;
jsobchuk3 1:288da1e5d10b 138 }
jsobchuk3 1:288da1e5d10b 139 break;
jsobchuk3 1:288da1e5d10b 140
jsobchuk3 1:288da1e5d10b 141
jsobchuk3 1:288da1e5d10b 142
jsobchuk3 1:288da1e5d10b 143 case 9:
jsobchuk3 1:288da1e5d10b 144 if (on[8] == 0) {
jsobchuk3 1:288da1e5d10b 145 uLCD.filled_circle(initpos + 2*shift, initpos + 2*shift, radius, RED); // Fill 9th Circle
jsobchuk3 1:288da1e5d10b 146 on[8] = 1;
jsobchuk3 1:288da1e5d10b 147 }
jsobchuk3 1:288da1e5d10b 148
jsobchuk3 1:288da1e5d10b 149 else if (on[8] == 1) {
jsobchuk3 1:288da1e5d10b 150 uLCD.filled_circle(initpos + 2*shift, initpos + 2*shift, radius, BLACK);
jsobchuk3 1:288da1e5d10b 151 uLCD.circle(initpos + 2*shift, initpos + 2*shift, radius, RED); // Erase 9th Circle
jsobchuk3 1:288da1e5d10b 152 on[8] = 0;
jsobchuk3 1:288da1e5d10b 153 }
jsobchuk3 1:288da1e5d10b 154 break;
jsobchuk3 1:288da1e5d10b 155
jsobchuk3 1:288da1e5d10b 156
jsobchuk3 1:288da1e5d10b 157 }
jsobchuk3 1:288da1e5d10b 158 }
4180_1 0:e09703934ff4 159
jsobchuk3 1:288da1e5d10b 160 void pushButton(int on [9],int key_code) {
jsobchuk3 1:288da1e5d10b 161 // The winning combination of buttons is 0,2,4,8 (1,3,5,9 in key_codes)
jsobchuk3 1:288da1e5d10b 162 // The rest of the buttons are programmed to give bogus combinations to confuse the player, but one of them might coincidently win the game.
jsobchuk3 1:288da1e5d10b 163 switch (key_code) {
jsobchuk3 1:288da1e5d10b 164
jsobchuk3 1:288da1e5d10b 165 case 2:
jsobchuk3 1:288da1e5d10b 166 invert(on,7);
jsobchuk3 1:288da1e5d10b 167 invert(on,4);
jsobchuk3 1:288da1e5d10b 168 invert(on,5);
jsobchuk3 1:288da1e5d10b 169 invert(on,8);
jsobchuk3 1:288da1e5d10b 170 break;
jsobchuk3 1:288da1e5d10b 171
jsobchuk3 1:288da1e5d10b 172 case 3:
jsobchuk3 1:288da1e5d10b 173 invert(on,7);
jsobchuk3 1:288da1e5d10b 174 invert(on,4);
jsobchuk3 1:288da1e5d10b 175 invert(on,1);
jsobchuk3 1:288da1e5d10b 176 break;
jsobchuk3 1:288da1e5d10b 177
jsobchuk3 1:288da1e5d10b 178 case 4:
jsobchuk3 1:288da1e5d10b 179 invert(on,2);
jsobchuk3 1:288da1e5d10b 180 invert(on,4);
jsobchuk3 1:288da1e5d10b 181 invert(on,5);
jsobchuk3 1:288da1e5d10b 182 invert(on,1);
jsobchuk3 1:288da1e5d10b 183 break;
jsobchuk3 1:288da1e5d10b 184
jsobchuk3 1:288da1e5d10b 185 case 6:
jsobchuk3 1:288da1e5d10b 186 invert(on,9);
jsobchuk3 1:288da1e5d10b 187 invert(on,7);
jsobchuk3 1:288da1e5d10b 188 invert(on,8);
jsobchuk3 1:288da1e5d10b 189 break;
jsobchuk3 1:288da1e5d10b 190
jsobchuk3 1:288da1e5d10b 191 case 7:
jsobchuk3 1:288da1e5d10b 192 invert(on,2);
jsobchuk3 1:288da1e5d10b 193 invert(on,5);
jsobchuk3 1:288da1e5d10b 194 invert(on,4);
jsobchuk3 1:288da1e5d10b 195 invert(on,8);
jsobchuk3 1:288da1e5d10b 196 invert(on,6);
jsobchuk3 1:288da1e5d10b 197 break;
jsobchuk3 1:288da1e5d10b 198
jsobchuk3 1:288da1e5d10b 199 case 8:
jsobchuk3 1:288da1e5d10b 200 invert(on,1);
jsobchuk3 1:288da1e5d10b 201 invert(on,2);
jsobchuk3 1:288da1e5d10b 202 invert(on,3);
jsobchuk3 1:288da1e5d10b 203 break;
jsobchuk3 1:288da1e5d10b 204
jsobchuk3 1:288da1e5d10b 205 case 10:
jsobchuk3 1:288da1e5d10b 206 invert(on,8);
jsobchuk3 1:288da1e5d10b 207 invert(on,9);
jsobchuk3 1:288da1e5d10b 208 invert(on,5);
jsobchuk3 1:288da1e5d10b 209 invert(on,6);
jsobchuk3 1:288da1e5d10b 210 break;
jsobchuk3 1:288da1e5d10b 211
jsobchuk3 1:288da1e5d10b 212 case 11:
jsobchuk3 1:288da1e5d10b 213 invert(on,9);
jsobchuk3 1:288da1e5d10b 214 invert(on,6);
jsobchuk3 1:288da1e5d10b 215 invert(on,3);
jsobchuk3 1:288da1e5d10b 216 break;
jsobchuk3 1:288da1e5d10b 217
jsobchuk3 1:288da1e5d10b 218 case 12:
jsobchuk3 1:288da1e5d10b 219 invert(on,2);
jsobchuk3 1:288da1e5d10b 220 invert(on,3);
jsobchuk3 1:288da1e5d10b 221 invert(on,6);
jsobchuk3 1:288da1e5d10b 222 invert(on,5);
jsobchuk3 1:288da1e5d10b 223 break;
jsobchuk3 1:288da1e5d10b 224 }
4180_1 0:e09703934ff4 225 }
4180_1 0:e09703934ff4 226
4180_1 0:e09703934ff4 227
4180_1 0:e09703934ff4 228
jsobchuk3 1:288da1e5d10b 229
jsobchuk3 1:288da1e5d10b 230
jsobchuk3 1:288da1e5d10b 231 // Key hit/release interrupt routine thanks to Anthony Buckton
jsobchuk3 1:288da1e5d10b 232 void fallInterrupt() {
jsobchuk3 1:288da1e5d10b 233 int key_code = 0;
jsobchuk3 1:288da1e5d10b 234 int i = 0;
jsobchuk3 1:288da1e5d10b 235 int value = mpr121.read(0x00);
jsobchuk3 1:288da1e5d10b 236 value += mpr121.read(0x01) << 8;
jsobchuk3 1:288da1e5d10b 237 // LED demo mod
jsobchuk3 1:288da1e5d10b 238 i = 0;
jsobchuk3 1:288da1e5d10b 239 // puts key number out to LEDs for demo
jsobchuk3 1:288da1e5d10b 240 for (i=0; i<12; i++) {
jsobchuk3 1:288da1e5d10b 241 if (((value>>i)&0x01)==1) {
jsobchuk3 1:288da1e5d10b 242 key_code=i+1;
jsobchuk3 1:288da1e5d10b 243 }
jsobchuk3 1:288da1e5d10b 244 }
jsobchuk3 1:288da1e5d10b 245 key = key_code;
jsobchuk3 1:288da1e5d10b 246 }
jsobchuk3 1:288da1e5d10b 247
jsobchuk3 1:288da1e5d10b 248
jsobchuk3 1:288da1e5d10b 249
jsobchuk3 1:288da1e5d10b 250 int main() {
jsobchuk3 1:288da1e5d10b 251 int j;
jsobchuk3 1:288da1e5d10b 252 uLCD.rectangle(0,0,127,127,WHITE); // Draw border
jsobchuk3 1:288da1e5d10b 253 // Draw empty circles in correct positions
jsobchuk3 1:288da1e5d10b 254 uLCD.circle(initpos, initpos, radius, RED);
jsobchuk3 1:288da1e5d10b 255 uLCD.circle(initpos + shift, initpos, radius, RED);
jsobchuk3 1:288da1e5d10b 256 uLCD.circle(initpos + 2*shift, initpos, radius, RED);
jsobchuk3 1:288da1e5d10b 257 uLCD.circle(initpos, initpos + shift, radius, RED);
jsobchuk3 1:288da1e5d10b 258 uLCD.circle(initpos + shift, initpos + shift, radius, RED);
jsobchuk3 1:288da1e5d10b 259 uLCD.circle(initpos + 2*shift, initpos + shift, radius, RED);
jsobchuk3 1:288da1e5d10b 260 uLCD.circle(initpos, initpos + 2*shift, radius, RED);
jsobchuk3 1:288da1e5d10b 261 uLCD.circle(initpos + shift, initpos + 2*shift, radius, RED);
jsobchuk3 1:288da1e5d10b 262 uLCD.circle(initpos + 2*shift, initpos + 2*shift, radius, RED);
jsobchuk3 1:288da1e5d10b 263
jsobchuk3 1:288da1e5d10b 264 interrupt.fall(&fallInterrupt);
jsobchuk3 1:288da1e5d10b 265 interrupt.mode(PullUp);
jsobchuk3 1:288da1e5d10b 266 while (1) {
jsobchuk3 1:288da1e5d10b 267 // Call pushButton once button is pressed by passing the key of the button pushed and the status of the circles
jsobchuk3 1:288da1e5d10b 268 if(key != 0) {
jsobchuk3 1:288da1e5d10b 269 pushButton(on, key);
jsobchuk3 1:288da1e5d10b 270 if(key != 1)key = 0;
jsobchuk3 1:288da1e5d10b 271 }
jsobchuk3 1:288da1e5d10b 272 // If all circles are filled, then print You win!!! Reset the mbed
jsobchuk3 1:288da1e5d10b 273 if (key == 1 || (on[0] && on[1] && on[2] && on[3] && on[4] && on[5] && on[6] && on[7] && on[8])) {
jsobchuk3 1:288da1e5d10b 274 uLCD.locate(5,5);
jsobchuk3 1:288da1e5d10b 275 if(key != 1) uLCD.printf("You Win!!!");
jsobchuk3 1:288da1e5d10b 276 uLCD.locate(0,7);
jsobchuk3 1:288da1e5d10b 277 uLCD.printf("Resetting the game");
jsobchuk3 1:288da1e5d10b 278 wait(3);
jsobchuk3 1:288da1e5d10b 279
jsobchuk3 1:288da1e5d10b 280 for (j = 0; j < 9; j++) {
jsobchuk3 1:288da1e5d10b 281 on[j] = rand() % 2;
jsobchuk3 1:288da1e5d10b 282 }
jsobchuk3 1:288da1e5d10b 283 uLCD.filled_rectangle(0,0,127,127,BLACK);// Erase Everything
jsobchuk3 1:288da1e5d10b 284
jsobchuk3 1:288da1e5d10b 285 uLCD.rectangle(0,0,127,127,WHITE); // Draw border
jsobchuk3 1:288da1e5d10b 286
jsobchuk3 1:288da1e5d10b 287
jsobchuk3 1:288da1e5d10b 288 // Draw empty circles in correct positions
jsobchuk3 1:288da1e5d10b 289 uLCD.circle(initpos, initpos, radius, RED);
jsobchuk3 1:288da1e5d10b 290 uLCD.circle(initpos + shift, initpos, radius, RED);
jsobchuk3 1:288da1e5d10b 291 uLCD.circle(initpos + 2*shift, initpos, radius, RED);
jsobchuk3 1:288da1e5d10b 292 uLCD.circle(initpos, initpos + shift, radius, RED);
jsobchuk3 1:288da1e5d10b 293 uLCD.circle(initpos + shift, initpos + shift, radius, RED);
jsobchuk3 1:288da1e5d10b 294 uLCD.circle(initpos + 2*shift, initpos + shift, radius, RED);
jsobchuk3 1:288da1e5d10b 295 uLCD.circle(initpos, initpos + 2*shift, radius, RED);
jsobchuk3 1:288da1e5d10b 296 uLCD.circle(initpos + shift, initpos + 2*shift, radius, RED);
jsobchuk3 1:288da1e5d10b 297 uLCD.circle(initpos + 2*shift, initpos + 2*shift, radius, RED);
jsobchuk3 1:288da1e5d10b 298
jsobchuk3 1:288da1e5d10b 299 //Make sure they match the array
jsobchuk3 1:288da1e5d10b 300 for(j = 0; j < 9; j++) {
jsobchuk3 1:288da1e5d10b 301 invert(on,j);
jsobchuk3 1:288da1e5d10b 302 }
jsobchuk3 1:288da1e5d10b 303
jsobchuk3 1:288da1e5d10b 304 }
jsobchuk3 1:288da1e5d10b 305 //wait(0.2); // Used wait to eliminate multiple presses of button. <- Done somewhere else
jsobchuk3 1:288da1e5d10b 306 }
jsobchuk3 1:288da1e5d10b 307 }