I simplified the library "ILI9225_TFT" provided by Arman Safikhani to better suit my needs in implementing a simple sliding puzzle game.
Effects.h
00001 #include "ILI9225.h" 00002 #include <time.h> 00003 00004 //Initialize pins for MBED LPC1768: RST, RS, CS, SDI(MOSI), CLK(SCK), LED. 00005 ILI9225 tft(P2_4, P2_3, P2_5, P0_9, P0_7, P0_6); 00006 DigitalOut led1(LED1); 00007 DigitalOut led2(LED2); 00008 DigitalOut led3(LED3); 00009 DigitalOut led4(LED4); 00010 00011 //Initialize pins for LPCXPRESSO LPC1769: RST, RS, CS, SDI(MOSI), CLK(SCK), LED. 00012 //ILI9225 tft(P0_5, P0_10, P0_4, P0_18, P0_15, P0_16); 00013 00014 //???b used out of 256kb available. 00015 //uint16_t picture[13986] = {0}; 00016 00017 uint16_t *picture = (uint16_t *)(0x2007C000); 00018 00019 uint8_t puzzState[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 00020 15}; 00021 00022 uint8_t cursorIndex = 0; //Refers to current index (-1 < cursorIndex < 16). 00023 00024 Timer timer; //Timer used to implement wireless input 00025 InterruptIn wirelessInput(p18); //Interrupt used to implement wireless input 00026 float wirelessInput_Freq = 0; //Global variable used to implement wireless input 00027 00028 void flip() { 00029 //Convert period (in us) to frequency (Hz) 00030 wirelessInput_Freq = (1/(float)timer.read_us())*1000000; 00031 //Reset timer and wait for next interrupt 00032 timer.reset(); 00033 } 00034 00035 int getPlayerInput(float freq) { 00036 int specifier = (((int)freq) - (((int)freq)%1000))/1000; 00037 wait_ms(1); 00038 if(specifier == 1) { 00039 //Turn on LED indicator(s): controller is in range. 00040 led1 = led2 = led3 = led4 = 1; 00041 return 0; 00042 } 00043 else if(specifier > 0 && specifier < 8) { 00044 //Turn on LED indicator(s): controller is in range. 00045 led1 = led2 = led3 = led4 = 1; 00046 return specifier; 00047 } 00048 else { 00049 //Turn off LED indicator(s): controller is NOT in range. 00050 led1 = led2 = led3 = led4 = 0; 00051 return 0; 00052 } 00053 } 00054 00055 void printNumber(int x, int y, uint8_t number) { 00056 //tft.fillRectangle(0, 0, 175, 91, COLOR_BLACK); 00057 tft.setFont(Terminal12x16); 00058 00059 if(number == 0) 00060 tft.drawText(x, y, "0", COLOR_WHITE); 00061 else if(number == 1) 00062 tft.drawText(x, y, "1", COLOR_WHITE); 00063 else if(number == 2) 00064 tft.drawText(x, y, "2", COLOR_WHITE); 00065 else if(number == 3) 00066 tft.drawText(x, y, "3", COLOR_WHITE); 00067 else if(number == 4) 00068 tft.drawText(x, y, "4", COLOR_WHITE); 00069 else if(number == 5) 00070 tft.drawText(x, y, "5", COLOR_WHITE); 00071 else if(number == 6) 00072 tft.drawText(x, y, "6", COLOR_WHITE); 00073 else if(number == 7) 00074 tft.drawText(x, y, "7", COLOR_WHITE); 00075 else if(number == 8) 00076 tft.drawText(x, y, "8", COLOR_WHITE); 00077 else if(number == 9) 00078 tft.drawText(x, y, "9", COLOR_WHITE); 00079 else if(number == 10) 00080 tft.drawText(x, y, "10", COLOR_WHITE); 00081 else if(number == 11) 00082 tft.drawText(x, y, "11", COLOR_WHITE); 00083 else if(number == 12) 00084 tft.drawText(x, y, "12", COLOR_WHITE); 00085 else if(number == 13) 00086 tft.drawText(x, y, "13", COLOR_WHITE); 00087 else if(number == 14) 00088 tft.drawText(x, y, "14", COLOR_WHITE); 00089 else if(number == 15) 00090 tft.drawText(x, y, "15", COLOR_WHITE); 00091 else 00092 tft.drawText(x, y, "?", COLOR_WHITE); 00093 } 00094 00095 void printElementsInPicture() { 00096 tft.fillRectangle(0, 0, 175, 91, COLOR_BLACK); 00097 for(int i = 0; i < 16; i++) 00098 printNumber((40*(i%4)) + 8, (20*((i - (i%4))/4)) + 8, puzzState[i]); 00099 } 00100 00101 //Put contents of pictureN at target index on GLCD specified by 'index'. 00102 void printPictureN(int index, int N) { 00103 int c = index%4; 00104 int r = (index - c)/4; 00105 00106 if(N > 13) { 00107 tft.fillRectangle((39*c) + 11, (29*r) + 95, 00108 (39*c) + 47, (29*r) + 121, COLOR_BLACK); 00109 return; 00110 } 00111 for(int x = (39*c) + 11; x <= (39*c) + 47; x++) { 00112 for(int y = (29*r) + 95; y <= (29*r) + 121; y++) 00113 tft.drawPixel(x, y, picture[(999*N) + (4*y) + x]); 00114 } 00115 } 00116 00117 void updatePuzzle() { 00118 for(int N = 0; N < 16; N++) 00119 printPictureN(N, puzzState[N]); 00120 } 00121 00122 int getNextPixel_index = 0; 00123 00124 //**TODO** replace the contents of the following method with the code necessary 00125 //to get the next pixel from the OV7670. The method, in its current state, is 00126 //good for testing purposes. You should also be able to remove the 00127 //'getNextPixel_index' variable if you correctly implement this new method. 00128 uint16_t getNextPixel() { 00129 uint16_t value = 0; 00130 00131 int x, y; 00132 00133 if(getNextPixel_index > 19199) { 00134 getNextPixel_index = 0; 00135 x = 0; 00136 } 00137 else 00138 x = getNextPixel_index%160; 00139 y = (getNextPixel_index - x)/160; 00140 00141 //Is pixel in one of 4 outer pieces (corners)? 00142 if((x >= 0 && x <= 40) && (y >= 0 && y <= 30)) //Upper left corner. 00143 value = 65535; 00144 if((x >= 119 && x <= 159) && (y >= 0 && y <= 30)) //Upper right corner. 00145 value = 52428; 00146 if((x >= 0 && x <= 40) && (y >= 89 && y <= 119)) //Lower left corner. 00147 value = 13107; 00148 if((x >= 119 && x <= 159) && (y >= 89 && y <= 119)) //Lower right corner. 00149 value = 0; 00150 00151 //Is pixel in one of 4 outer pieces (left and right)? 00152 if((x >= 0 && x <= 40) && (y >= 31 && y <= 59)) //Upper left side. 00153 value = 48059; 00154 if((x >= 0 && x <= 40) && (y >= 60 && y <= 88)) //Lower left side. 00155 value = 30583; 00156 if((x >= 119 && x <= 159) && (y >= 31 && y <= 59)) //Upper right side. 00157 value = 34952; 00158 if((x >= 119 && x <= 159) && (y >= 60 && y <= 88)) //Lower right side. 00159 value = 17476; 00160 00161 //Is pixel in one of 4 outer pieces (top and bottom)? 00162 if((x >= 41 && x <= 79) && (y >= 0 && y <= 30)) //Left top side. 00163 value = 61166; 00164 if((x >= 80 && x <= 118) && (y >= 0 && y <= 30)) //Right top side. 00165 value = 56797; 00166 if((x >= 41 && x <= 79) && (y >= 89 && y <= 119)) //Left bottom side. 00167 value = 8738; 00168 if((x >= 80 && x <= 118) && (y >= 89 && y <= 119)) //Right bottom side. 00169 value = 0; 00170 00171 //Is pixel in one of 4 inner pieces? 00172 if((x >= 41 && x <= 79) && (y >= 31 && y <= 59)) //Upper left inner piece. 00173 value = 43690; 00174 if((x >= 80 && x <= 118) && (y >= 31 && y <= 59)) //Upper right inner piece. 00175 value = 39321; 00176 if((x >= 41 && x <= 79) && (y >= 60 && y <= 88)) //Lower left inner piece. 00177 value = 26214; 00178 if((x >= 80 && x <= 118) && (y >= 60 && y <= 88)) //Lower right inner piece. 00179 value = 21845; 00180 00181 getNextPixel_index++; 00182 00183 return value; 00184 } 00185 00186 void handlePixel(int index, uint16_t pixel) { 00187 int x = index%160; 00188 int y = (index - x)/160; 00189 if((x >= 3 && x <= 39) && (y >= 3 && y <= 29)) 00190 picture[37*y + x - 114] = pixel; 00191 else if((x >= 42 && x <= 78) && (y >= 3 && y <= 29)) 00192 picture[37*y + x - 153 + 999] = pixel; 00193 else if((x >= 81 && x <= 117) && (y >= 3 && y <= 29)) 00194 picture[37*y + x - 192 + 1998] = pixel; 00195 else if((x >= 120 && x <= 156) && (y >= 3 && y <= 29)) 00196 picture[37*y + x - 231 + 2997] = pixel; 00197 else if((x >= 3 && x <= 39) && (y >= 32 && y <= 58)) 00198 picture[37*y + x - 1187 + 3996] = pixel; 00199 else if((x >= 42 && x <= 78) && (y >= 32 && y <= 58)) 00200 picture[37*y + x - 1226 + 4995] = pixel; 00201 else if((x >= 81 && x <= 117) && (y >= 32 && y <= 58)) 00202 picture[37*y + x - 1265 + 5994] = pixel; 00203 else if((x >= 120 && x <= 156) && (y >= 32 && y <= 58)) 00204 picture[37*y + x - 1304 + 6993] = pixel; 00205 else if((x >= 3 && x <= 39) && (y >= 61 && y <= 87)) 00206 picture[37*y + x - 2260 + 7992] = pixel; 00207 else if((x >= 42 && x <= 78) && (y >= 61 && y <= 87)) 00208 picture[37*y + x - 2299 + 8991] = pixel; 00209 else if((x >= 81 && x <= 117) && (y >= 61 && y <= 87)) 00210 picture[37*y + x - 2338 + 9990] = pixel; 00211 else if((x >= 120 && x <= 156) && (y >= 61 && y <= 87)) 00212 picture[37*y + x - 2377 + 10989] = pixel; 00213 else if((x >= 3 && x <= 39) && (y >= 90 && y <= 116)) 00214 picture[37*y + x - 3333 + 11988] = pixel; 00215 else if((x >= 42 && x <= 78) && (y >= 90 && y <= 116)) 00216 picture[37*y + x - 3372 + 12987] = pixel; 00217 } 00218 00219 //A utility function to swap 2 numbers 00220 void swap(uint8_t *a, uint8_t *b) { 00221 uint8_t temp = *a; 00222 *a = *b; 00223 *b = temp; 00224 } 00225 00226 void shuffleGridIndices() { 00227 int n = sizeof(puzzState)/sizeof(puzzState[0]); 00228 00229 //Use a different seed value so that we don't get same result each time we 00230 //run this program. 00231 srand(time(NULL)); 00232 00233 //Start from the last element and swap one by one. We don't need to run for 00234 //the first element, which is why i > 0. 00235 for (int i = n-1; i > 0; i--) { 00236 int j = rand()%(i+1); //Pick a random index b.w. 0 & i. 00237 swap(&puzzState[i], &puzzState[j]); //Swap w| element @ random index. 00238 } 00239 } 00240 00241 //This method puts a picture on the screen that is/was helpful during testing. 00242 void putTestPicture() { 00243 //Add 4 outer pieces (corners). 00244 tft.fillRectangle(8, 92, 48, 122, 65535); //Upper left corner. 00245 tft.fillRectangle(127, 92, 167, 122, 52428); //Upper right corner. 00246 tft.fillRectangle(8, 181, 48, 211, 13107); //Lower left corner. 00247 tft.fillRectangle(127, 181, 167, 211, 0); //Lower right corner. 00248 00249 //Add 4 outer pieces (left and right). 00250 tft.fillRectangle(8, 123, 48, 151, 48059); //Upper left side. 00251 tft.fillRectangle(8, 152, 48, 180, 30583); //Lower left side. 00252 tft.fillRectangle(127, 123, 167, 151, 34952); //Upper right side. 00253 tft.fillRectangle(127, 152, 167, 180, 17476); //Lower right side. 00254 00255 //Add 4 outer pieces (top and bottom). 00256 tft.fillRectangle(49, 92, 87, 122, 61166); //Left top side. 00257 tft.fillRectangle(88, 92, 126, 122, 56797); //Right top side. 00258 tft.fillRectangle(49, 181, 87, 211, 8738); //Left bottom side. 00259 tft.fillRectangle(88, 181, 126, 211, 0); //Right bottom side. 00260 00261 //Add 4 inner pieces. 00262 tft.fillRectangle(49, 123, 87, 151, 43690); //Upper left inner piece. 00263 tft.fillRectangle(88, 123, 126, 151, 39321); //Upper right inner piece. 00264 tft.fillRectangle(49, 152, 87, 180, 26214); //Lower left inner piece. 00265 tft.fillRectangle(88, 152, 126, 180, 21845); //Lower right inner piece. 00266 } 00267 00268 void putGrid() { 00269 tft.drawRectangle(8, 92, 167, 211, COLOR_WHITE); 00270 tft.drawRectangle(9, 93, 166, 210, COLOR_WHITE); 00271 00272 for(int r = 0; r < 4; r++) { 00273 for(int c = 0; c < 4; c++) 00274 tft.drawRectangle(39*c + 10, 29*r + 94, 39*c + 48, 29*r + 122, 00275 COLOR_WHITE); 00276 } 00277 } 00278 00279 void highlightGridIndex(uint8_t index, bool hasSelected) { 00280 int c = index%4; 00281 int r = (index - c)/4; 00282 putGrid(); 00283 if(hasSelected) //Red highlight means player has selected an index. 00284 tft.drawRectangle(39*c + 10, 29*r + 94, 39*c + 48, 29*r + 122, 00285 COLOR_RED); 00286 if(!hasSelected) //Green highlight means player has not selected an index. 00287 tft.drawRectangle(39*c + 10, 29*r + 94, 39*c + 48, 29*r + 122, 00288 COLOR_GREEN); 00289 cursorIndex = index; 00290 } 00291 00292 bool selectionMode_indexVerified(uint8_t index) { 00293 //index must satisfy 2 characteristics for method to return true. 00294 00295 //1) Target puzzle piece is NOT one of the 2 missing puzzle pieces. 00296 if(puzzState[index] > 13) 00297 return false; 00298 00299 //2) Target puzzle piece is NOT locked on all 4 sides. 00300 if(index - 4 > -1) { //NOT out of bounds above. 00301 if(puzzState[index - 4] > 13) //Empty space above? 00302 return true; 00303 } 00304 if(index + 4 < 16) { //NOT out of bounds below. 00305 if(puzzState[index + 4] > 13) //Empty space below? 00306 return true; 00307 } 00308 if((index%4) - 1 > -1) { //NOT out of bounds to the left. 00309 if(puzzState[index - 1] > 13) //Empty space to the left? 00310 return true; 00311 } 00312 if((index%4) + 1 < 4) { //NOT out of bounds to the right. 00313 if(puzzState[index + 1] > 13) //Empty space to the right? 00314 return true; 00315 } 00316 00317 return false; 00318 }
Generated on Thu Jul 14 2022 22:45:10 by
1.7.2