Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE mbed
Fork of MPR121_Demo by
main.cpp
00001 /* 00002 Dhruvin Desai and Joseph Sobchuk 00003 00004 fallInterrupt() and API for mpr121 written by Anthony Buckton in 2011 00005 */ 00006 00007 #include <mbed.h> 00008 #include <mpr121.h> // Thanks to Anthony Buckton 00009 #include "uLCD_4DGL.h" 00010 00011 uLCD_4DGL uLCD(p28,p27,p29); 00012 00013 // Create the interrupt receiver object on pin 26 00014 InterruptIn interrupt(p26); 00015 00016 // Setup the i2c bus on pins 9 and 10 00017 I2C i2c(p9, p10); 00018 00019 // Setup the Mpr121: 00020 // constructor(i2c object, i2c address of the mpr121) 00021 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); 00022 00023 int on [9] = {0,0,0,0,0,0,0,0,0}; 00024 int key; 00025 int radius = 15; 00026 int initpos = 23; // Initial x and y shift from the edge of the display 00027 int shift = 40; // Space in between each circle 00028 00029 // Function to invert any specified circle 00030 void invert(int on [9], int circleN) { 00031 switch (circleN) { 00032 00033 case 1: 00034 if (on[0] == 0) { // If circle 1 is off then Fill 1st Circle 00035 uLCD.filled_circle(initpos, initpos, radius, RED); // Fill 1st Circle 00036 on[0] = 1; // Then change status of circle 1 to on 00037 } 00038 else if (on[0] == 1) { // If circle 1 is on then erase 1st circle 00039 uLCD.filled_circle(initpos, initpos, radius, BLACK); 00040 uLCD.circle(initpos, initpos, radius, RED); // Erase 1st Circle 00041 on[0] = 0; 00042 } 00043 break; 00044 00045 case 2: 00046 if (on[1] == 0) { 00047 uLCD.filled_circle(initpos + shift, initpos, radius, RED); // Fill 2nd Circle 00048 on[1] = 1; 00049 } 00050 00051 else if (on[1] == 1) { 00052 uLCD.filled_circle(initpos + shift, initpos, radius, BLACK); 00053 uLCD.circle(initpos + shift, initpos, radius, RED); // Erase 2nd Circle 00054 on[1] = 0; 00055 } 00056 break; 00057 00058 00059 case 3: 00060 if (on[2] == 0) { 00061 uLCD.filled_circle(initpos + 2*shift, initpos, radius, RED); 00062 on[2] = 1; 00063 } 00064 else if (on[2] == 1) { 00065 uLCD.filled_circle(initpos + 2*shift, initpos, radius, BLACK); 00066 uLCD.circle(initpos + 2*shift, initpos, radius, RED); 00067 on[2] = 0; 00068 } 00069 break; 00070 00071 00072 case 4: 00073 if (on[3] == 0) { 00074 uLCD.filled_circle(initpos, initpos + shift, radius, RED); // Fill 4th Circle 00075 on[3] = 1; 00076 } 00077 00078 else if (on[3] == 1) { 00079 uLCD.filled_circle(initpos, initpos + shift, radius, BLACK); 00080 uLCD.circle(initpos, initpos + shift, radius, RED); // Erase 4th Circle 00081 on[3] = 0; 00082 } 00083 break; 00084 00085 case 5: 00086 if (on[4] == 0) { 00087 uLCD.filled_circle(initpos + shift, initpos + shift, radius, RED); // Fill 5th Circle 00088 on[4] = 1; 00089 } 00090 00091 else if (on[4] == 1) { 00092 uLCD.filled_circle(initpos + shift, initpos + shift, radius, BLACK); 00093 uLCD.circle(initpos + shift, initpos + shift, radius, RED); // Erase 5th Circle 00094 on[4] = 0; 00095 } 00096 break; 00097 00098 case 6: 00099 if (on[5] == 0) { 00100 uLCD.filled_circle(initpos + 2*shift, initpos + shift, radius, RED); // Fill 6th Circle 00101 on[5] = 1; 00102 } 00103 00104 else if (on[5] == 1) { 00105 uLCD.filled_circle(initpos + 2*shift, initpos + shift, radius, BLACK); 00106 uLCD.circle(initpos + 2*shift, initpos + shift, radius, RED); // Erase 6th Circle 00107 on[5] = 0; 00108 } 00109 break; 00110 00111 00112 00113 case 7: 00114 if (on[6] == 0) { 00115 uLCD.filled_circle(initpos, initpos + 2*shift, radius, RED); // Fill 7th Circle 00116 on[6] = 1; 00117 } 00118 00119 else if (on[6] == 1) { 00120 uLCD.filled_circle(initpos, initpos + 2*shift, radius, BLACK); // Erase 7th Circle 00121 uLCD.circle(initpos, initpos + 2*shift, radius, RED); 00122 on[6] = 0; 00123 } 00124 break; 00125 00126 00127 00128 case 8: 00129 if (on[7] == 0) { 00130 uLCD.filled_circle(initpos + shift, initpos + 2*shift, radius, RED); // Fill 8th Circle 00131 on[7] = 1; 00132 } 00133 00134 else if (on[7] == 1) { 00135 uLCD.filled_circle(initpos + shift, initpos + 2*shift, radius, BLACK); // Erase 8th Circle 00136 uLCD.circle(initpos + shift, initpos + 2*shift, radius, RED); 00137 on[7] = 0; 00138 } 00139 break; 00140 00141 00142 00143 case 9: 00144 if (on[8] == 0) { 00145 uLCD.filled_circle(initpos + 2*shift, initpos + 2*shift, radius, RED); // Fill 9th Circle 00146 on[8] = 1; 00147 } 00148 00149 else if (on[8] == 1) { 00150 uLCD.filled_circle(initpos + 2*shift, initpos + 2*shift, radius, BLACK); 00151 uLCD.circle(initpos + 2*shift, initpos + 2*shift, radius, RED); // Erase 9th Circle 00152 on[8] = 0; 00153 } 00154 break; 00155 00156 00157 } 00158 } 00159 00160 void pushButton(int on [9],int key_code) { 00161 // The winning combination of buttons is 0,2,4,8 (1,3,5,9 in key_codes) 00162 // The rest of the buttons are programmed to give bogus combinations to confuse the player, but one of them might coincidently win the game. 00163 switch (key_code) { 00164 00165 case 2: 00166 invert(on,7); 00167 invert(on,4); 00168 invert(on,5); 00169 invert(on,8); 00170 break; 00171 00172 case 3: 00173 invert(on,7); 00174 invert(on,4); 00175 invert(on,1); 00176 break; 00177 00178 case 4: 00179 invert(on,2); 00180 invert(on,4); 00181 invert(on,5); 00182 invert(on,1); 00183 break; 00184 00185 case 6: 00186 invert(on,9); 00187 invert(on,7); 00188 invert(on,8); 00189 break; 00190 00191 case 7: 00192 invert(on,2); 00193 invert(on,5); 00194 invert(on,4); 00195 invert(on,8); 00196 invert(on,6); 00197 break; 00198 00199 case 8: 00200 invert(on,1); 00201 invert(on,2); 00202 invert(on,3); 00203 break; 00204 00205 case 10: 00206 invert(on,8); 00207 invert(on,9); 00208 invert(on,5); 00209 invert(on,6); 00210 break; 00211 00212 case 11: 00213 invert(on,9); 00214 invert(on,6); 00215 invert(on,3); 00216 break; 00217 00218 case 12: 00219 invert(on,2); 00220 invert(on,3); 00221 invert(on,6); 00222 invert(on,5); 00223 break; 00224 } 00225 } 00226 00227 00228 00229 00230 00231 // Key hit/release interrupt routine thanks to Anthony Buckton 00232 void fallInterrupt() { 00233 int key_code = 0; 00234 int i = 0; 00235 int value = mpr121.read(0x00); 00236 value += mpr121.read(0x01) << 8; 00237 // LED demo mod 00238 i = 0; 00239 // puts key number out to LEDs for demo 00240 for (i=0; i<12; i++) { 00241 if (((value>>i)&0x01)==1) { 00242 key_code=i+1; 00243 } 00244 } 00245 key = key_code; 00246 } 00247 00248 00249 00250 int main() { 00251 int j; 00252 uLCD.rectangle(0,0,127,127,WHITE); // Draw border 00253 // Draw empty circles in correct positions 00254 uLCD.circle(initpos, initpos, radius, RED); 00255 uLCD.circle(initpos + shift, initpos, radius, RED); 00256 uLCD.circle(initpos + 2*shift, initpos, radius, RED); 00257 uLCD.circle(initpos, initpos + shift, radius, RED); 00258 uLCD.circle(initpos + shift, initpos + shift, radius, RED); 00259 uLCD.circle(initpos + 2*shift, initpos + shift, radius, RED); 00260 uLCD.circle(initpos, initpos + 2*shift, radius, RED); 00261 uLCD.circle(initpos + shift, initpos + 2*shift, radius, RED); 00262 uLCD.circle(initpos + 2*shift, initpos + 2*shift, radius, RED); 00263 00264 interrupt.fall(&fallInterrupt); 00265 interrupt.mode(PullUp); 00266 while (1) { 00267 // Call pushButton once button is pressed by passing the key of the button pushed and the status of the circles 00268 if(key != 0) { 00269 pushButton(on, key); 00270 if(key != 1)key = 0; 00271 } 00272 // If all circles are filled, then print You win!!! Reset the mbed 00273 if (key == 1 || (on[0] && on[1] && on[2] && on[3] && on[4] && on[5] && on[6] && on[7] && on[8])) { 00274 uLCD.locate(5,5); 00275 if(key != 1) uLCD.printf("You Win!!!"); 00276 uLCD.locate(0,7); 00277 uLCD.printf("Resetting the game"); 00278 wait(3); 00279 00280 for (j = 0; j < 9; j++) { 00281 on[j] = rand() % 2; 00282 } 00283 uLCD.filled_rectangle(0,0,127,127,BLACK);// Erase Everything 00284 00285 uLCD.rectangle(0,0,127,127,WHITE); // Draw border 00286 00287 00288 // Draw empty circles in correct positions 00289 uLCD.circle(initpos, initpos, radius, RED); 00290 uLCD.circle(initpos + shift, initpos, radius, RED); 00291 uLCD.circle(initpos + 2*shift, initpos, radius, RED); 00292 uLCD.circle(initpos, initpos + shift, radius, RED); 00293 uLCD.circle(initpos + shift, initpos + shift, radius, RED); 00294 uLCD.circle(initpos + 2*shift, initpos + shift, radius, RED); 00295 uLCD.circle(initpos, initpos + 2*shift, radius, RED); 00296 uLCD.circle(initpos + shift, initpos + 2*shift, radius, RED); 00297 uLCD.circle(initpos + 2*shift, initpos + 2*shift, radius, RED); 00298 00299 //Make sure they match the array 00300 for(j = 0; j < 9; j++) { 00301 invert(on,j); 00302 } 00303 00304 } 00305 //wait(0.2); // Used wait to eliminate multiple presses of button. <- Done somewhere else 00306 } 00307 }
Generated on Fri Jul 15 2022 02:23:14 by
1.7.2
