mmm
Dependencies: PinDetect TFT_fonts TouchADS7843 mbed
Fork of SPI_TFT_ILI9341 by
main.cpp@14:94bc37f34b32, 2017-04-23 (annotated)
- Committer:
- cgscmm
- Date:
- Sun Apr 23 15:16:47 2017 +0000
- Revision:
- 14:94bc37f34b32
???
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cgscmm | 14:94bc37f34b32 | 1 | |
cgscmm | 14:94bc37f34b32 | 2 | #include "PinDetect.h" |
cgscmm | 14:94bc37f34b32 | 3 | #include "stdio.h" |
cgscmm | 14:94bc37f34b32 | 4 | #include "mbed.h" |
cgscmm | 14:94bc37f34b32 | 5 | #include "SPI_TFT_ILI9341.h" |
cgscmm | 14:94bc37f34b32 | 6 | #include "string" |
cgscmm | 14:94bc37f34b32 | 7 | #include "Arial12x12.h" |
cgscmm | 14:94bc37f34b32 | 8 | #include "Arial24x23.h" |
cgscmm | 14:94bc37f34b32 | 9 | #include "Arial28x28.h" |
cgscmm | 14:94bc37f34b32 | 10 | #include "font_big.h" |
cgscmm | 14:94bc37f34b32 | 11 | #include "Touch.h" |
cgscmm | 14:94bc37f34b32 | 12 | // ********************************************************************** |
cgscmm | 14:94bc37f34b32 | 13 | #define TOUCH_X_MIN 30 |
cgscmm | 14:94bc37f34b32 | 14 | #define TOUCH_X_MAX 230 |
cgscmm | 14:94bc37f34b32 | 15 | #define TOUCH_Y_MIN 30 |
cgscmm | 14:94bc37f34b32 | 16 | #define TOUCH_Y_MAX 240 |
cgscmm | 14:94bc37f34b32 | 17 | SPI_TFT_ILI9341 TFT(p5, p6, p7, p8, p9, p10,"TFT"); |
cgscmm | 14:94bc37f34b32 | 18 | |
cgscmm | 14:94bc37f34b32 | 19 | PinDetect pb1(p26); |
cgscmm | 14:94bc37f34b32 | 20 | |
cgscmm | 14:94bc37f34b32 | 21 | int score = 0; |
cgscmm | 14:94bc37f34b32 | 22 | int wallSpeed = 1; |
cgscmm | 14:94bc37f34b32 | 23 | int wall1x1 = 80; |
cgscmm | 14:94bc37f34b32 | 24 | int wall1x2 = 100; |
cgscmm | 14:94bc37f34b32 | 25 | int wall1y1 = 0; |
cgscmm | 14:94bc37f34b32 | 26 | int wall1y2 = 40; |
cgscmm | 14:94bc37f34b32 | 27 | int wall2x1 = 80; |
cgscmm | 14:94bc37f34b32 | 28 | int wall2x2 = 100; |
cgscmm | 14:94bc37f34b32 | 29 | int wall2y1 = 88; |
cgscmm | 14:94bc37f34b32 | 30 | int wall2y2 = 128; |
cgscmm | 14:94bc37f34b32 | 31 | |
cgscmm | 14:94bc37f34b32 | 32 | int ballxpos = 30; |
cgscmm | 14:94bc37f34b32 | 33 | int ballypos = 64; |
cgscmm | 14:94bc37f34b32 | 34 | int ballrad = 3; |
cgscmm | 14:94bc37f34b32 | 35 | |
cgscmm | 14:94bc37f34b32 | 36 | int volatile ballVel = 1; |
cgscmm | 14:94bc37f34b32 | 37 | |
cgscmm | 14:94bc37f34b32 | 38 | enum gameState{ |
cgscmm | 14:94bc37f34b32 | 39 | begin, |
cgscmm | 14:94bc37f34b32 | 40 | playing, |
cgscmm | 14:94bc37f34b32 | 41 | over}; |
cgscmm | 14:94bc37f34b32 | 42 | |
cgscmm | 14:94bc37f34b32 | 43 | int volatile state; |
cgscmm | 14:94bc37f34b32 | 44 | |
cgscmm | 14:94bc37f34b32 | 45 | TOUCH_DATA Coordenadas; |
cgscmm | 14:94bc37f34b32 | 46 | |
cgscmm | 14:94bc37f34b32 | 47 | TouchScreenADS7843 MyTouch(p11,p12,p13,p14,p15); // ADS7843-> SDO,SDI,SCK,CS,IRQ |
cgscmm | 14:94bc37f34b32 | 48 | |
cgscmm | 14:94bc37f34b32 | 49 | void vISRTouchIRQ(void){ |
cgscmm | 14:94bc37f34b32 | 50 | UINT_COORDxy Temp; |
cgscmm | 14:94bc37f34b32 | 51 | |
cgscmm | 14:94bc37f34b32 | 52 | MyTouch.vTestTouchPanel(&Coordenadas); |
cgscmm | 14:94bc37f34b32 | 53 | if((Coordenadas.x>TOUCH_X_MIN && Coordenadas.x<TOUCH_X_MAX) && (Coordenadas.y>TOUCH_Y_MIN && Coordenadas.y<TOUCH_Y_MAX)){ |
cgscmm | 14:94bc37f34b32 | 54 | // Cambio de coordenada y escala: |
cgscmm | 14:94bc37f34b32 | 55 | Temp=Coordenadas.y; |
cgscmm | 14:94bc37f34b32 | 56 | Coordenadas.y=((float)((255-TOUCH_X_MIN)-Coordenadas.x)*(160.0/(TOUCH_X_MAX-TOUCH_X_MIN))); |
cgscmm | 14:94bc37f34b32 | 57 | Coordenadas.x=((float)(Temp-TOUCH_Y_MIN)*(240.0/(TOUCH_Y_MAX-TOUCH_Y_MIN))); |
cgscmm | 14:94bc37f34b32 | 58 | |
cgscmm | 14:94bc37f34b32 | 59 | } |
cgscmm | 14:94bc37f34b32 | 60 | } |
cgscmm | 14:94bc37f34b32 | 61 | |
cgscmm | 14:94bc37f34b32 | 62 | |
cgscmm | 14:94bc37f34b32 | 63 | |
cgscmm | 14:94bc37f34b32 | 64 | void pb1_hit_callback() { |
cgscmm | 14:94bc37f34b32 | 65 | |
cgscmm | 14:94bc37f34b32 | 66 | switch (state) { |
cgscmm | 14:94bc37f34b32 | 67 | case begin: |
cgscmm | 14:94bc37f34b32 | 68 | state = playing; |
cgscmm | 14:94bc37f34b32 | 69 | break; |
cgscmm | 14:94bc37f34b32 | 70 | case playing: |
cgscmm | 14:94bc37f34b32 | 71 | ballVel = ballVel-10; |
cgscmm | 14:94bc37f34b32 | 72 | break; |
cgscmm | 14:94bc37f34b32 | 73 | case over: |
cgscmm | 14:94bc37f34b32 | 74 | state = begin; |
cgscmm | 14:94bc37f34b32 | 75 | break; |
cgscmm | 14:94bc37f34b32 | 76 | } |
cgscmm | 14:94bc37f34b32 | 77 | } |
cgscmm | 14:94bc37f34b32 | 78 | int main() { |
cgscmm | 14:94bc37f34b32 | 79 | |
cgscmm | 14:94bc37f34b32 | 80 | state = begin; |
cgscmm | 14:94bc37f34b32 | 81 | int ready = 0; |
cgscmm | 14:94bc37f34b32 | 82 | TFT.claim(stdout); // send stdout to the TFT display |
cgscmm | 14:94bc37f34b32 | 83 | //TFT.claim(stderr); // send stderr to the TFT display |
cgscmm | 14:94bc37f34b32 | 84 | TFT.set_orientation(2); |
cgscmm | 14:94bc37f34b32 | 85 | TFT.background(Black); // set background to black |
cgscmm | 14:94bc37f34b32 | 86 | TFT.foreground(White); // set chars to white |
cgscmm | 14:94bc37f34b32 | 87 | TFT.cls(); |
cgscmm | 14:94bc37f34b32 | 88 | pb1.mode(PullUp); |
cgscmm | 14:94bc37f34b32 | 89 | wait(.01); |
cgscmm | 14:94bc37f34b32 | 90 | pb1.attach_deasserted(&pb1_hit_callback); |
cgscmm | 14:94bc37f34b32 | 91 | pb1.setSampleFrequency(); |
cgscmm | 14:94bc37f34b32 | 92 | TFT.cls(); |
cgscmm | 14:94bc37f34b32 | 93 | TFT.fillrect(0,0,240,320,Blue); |
cgscmm | 14:94bc37f34b32 | 94 | TFT.fillcircle(ballxpos, ballypos, ballrad, Red); |
cgscmm | 14:94bc37f34b32 | 95 | TFT.fillrect(wall1x1, wall1y1, wall1x2, wall1y2, Green); |
cgscmm | 14:94bc37f34b32 | 96 | TFT.fillrect(wall2x1, wall2y1, wall2x2, wall2y2, Green); |
cgscmm | 14:94bc37f34b32 | 97 | TFT.set_font((unsigned char*) Arial12x12); |
cgscmm | 14:94bc37f34b32 | 98 | TFT.locate(14,0); |
cgscmm | 14:94bc37f34b32 | 99 | TFT.printf("%04d", score); |
cgscmm | 14:94bc37f34b32 | 100 | |
cgscmm | 14:94bc37f34b32 | 101 | int go = 1; |
cgscmm | 14:94bc37f34b32 | 102 | int scoreWrite = 1; |
cgscmm | 14:94bc37f34b32 | 103 | while(go) { |
cgscmm | 14:94bc37f34b32 | 104 | |
cgscmm | 14:94bc37f34b32 | 105 | switch (state) { |
cgscmm | 14:94bc37f34b32 | 106 | case begin: |
cgscmm | 14:94bc37f34b32 | 107 | |
cgscmm | 14:94bc37f34b32 | 108 | TFT.locate(4,2); |
cgscmm | 14:94bc37f34b32 | 109 | TFT.foreground(White); |
cgscmm | 14:94bc37f34b32 | 110 | TFT.background(Blue); |
cgscmm | 14:94bc37f34b32 | 111 | // TFT.text_mode(OPAQUE); |
cgscmm | 14:94bc37f34b32 | 112 | TFT.set_font((unsigned char*) Arial12x12); |
cgscmm | 14:94bc37f34b32 | 113 | TFT.printf("Flappy mbed"); |
cgscmm | 14:94bc37f34b32 | 114 | |
cgscmm | 14:94bc37f34b32 | 115 | TFT.locate(1,30); |
cgscmm | 14:94bc37f34b32 | 116 | TFT.printf("Press PB to Start"); |
cgscmm | 14:94bc37f34b32 | 117 | ready = 0; |
cgscmm | 14:94bc37f34b32 | 118 | |
cgscmm | 14:94bc37f34b32 | 119 | |
cgscmm | 14:94bc37f34b32 | 120 | break; |
cgscmm | 14:94bc37f34b32 | 121 | |
cgscmm | 14:94bc37f34b32 | 122 | case playing: |
cgscmm | 14:94bc37f34b32 | 123 | wait(0.05); |
cgscmm | 14:94bc37f34b32 | 124 | if (!ready) |
cgscmm | 14:94bc37f34b32 | 125 | { |
cgscmm | 14:94bc37f34b32 | 126 | TFT.cls(); |
cgscmm | 14:94bc37f34b32 | 127 | |
cgscmm | 14:94bc37f34b32 | 128 | wall1x1 = 80; |
cgscmm | 14:94bc37f34b32 | 129 | wall1x2 = 100; |
cgscmm | 14:94bc37f34b32 | 130 | wall1y1 = 0; |
cgscmm | 14:94bc37f34b32 | 131 | wall1y2 = 40; |
cgscmm | 14:94bc37f34b32 | 132 | wall2x1 = 80; |
cgscmm | 14:94bc37f34b32 | 133 | wall2x2 = 100; |
cgscmm | 14:94bc37f34b32 | 134 | wall2y1 = 88; |
cgscmm | 14:94bc37f34b32 | 135 | wall2y2 = 128; |
cgscmm | 14:94bc37f34b32 | 136 | |
cgscmm | 14:94bc37f34b32 | 137 | ballxpos = 30; |
cgscmm | 14:94bc37f34b32 | 138 | ballypos = 64; |
cgscmm | 14:94bc37f34b32 | 139 | ballrad = 3; |
cgscmm | 14:94bc37f34b32 | 140 | |
cgscmm | 14:94bc37f34b32 | 141 | TFT.fillrect(0,0,128,128,Blue); |
cgscmm | 14:94bc37f34b32 | 142 | TFT.fillcircle(ballxpos, ballypos, ballrad, Red); |
cgscmm | 14:94bc37f34b32 | 143 | TFT.fillrect(wall1x1, wall1y1, wall1x2, wall1y2, Green); |
cgscmm | 14:94bc37f34b32 | 144 | TFT.fillrect(wall2x1, wall2y1, wall2x2, wall2y2, Green); |
cgscmm | 14:94bc37f34b32 | 145 | ready = 1; |
cgscmm | 14:94bc37f34b32 | 146 | } |
cgscmm | 14:94bc37f34b32 | 147 | |
cgscmm | 14:94bc37f34b32 | 148 | wall1x1--; |
cgscmm | 14:94bc37f34b32 | 149 | wall2x1--; |
cgscmm | 14:94bc37f34b32 | 150 | wall1x2--; |
cgscmm | 14:94bc37f34b32 | 151 | wall2x2--; |
cgscmm | 14:94bc37f34b32 | 152 | |
cgscmm | 14:94bc37f34b32 | 153 | if (wall1x2 < -1) |
cgscmm | 14:94bc37f34b32 | 154 | { |
cgscmm | 14:94bc37f34b32 | 155 | wall1x2 = 148; |
cgscmm | 14:94bc37f34b32 | 156 | wall1x1 = 128; |
cgscmm | 14:94bc37f34b32 | 157 | wall2x2 = 148; |
cgscmm | 14:94bc37f34b32 | 158 | wall2x1 = 128; |
cgscmm | 14:94bc37f34b32 | 159 | |
cgscmm | 14:94bc37f34b32 | 160 | wall1y2 = rand() % (73) + 8; |
cgscmm | 14:94bc37f34b32 | 161 | wall2y1 = wall1y2 + 48; |
cgscmm | 14:94bc37f34b32 | 162 | scoreWrite = 1; |
cgscmm | 14:94bc37f34b32 | 163 | } |
cgscmm | 14:94bc37f34b32 | 164 | |
cgscmm | 14:94bc37f34b32 | 165 | if(wall1x2 < 95 && scoreWrite == 1) |
cgscmm | 14:94bc37f34b32 | 166 | { |
cgscmm | 14:94bc37f34b32 | 167 | TFT.locate(14,0); |
cgscmm | 14:94bc37f34b32 | 168 | TFT.printf("%04d", score); |
cgscmm | 14:94bc37f34b32 | 169 | scoreWrite = 0; |
cgscmm | 14:94bc37f34b32 | 170 | } |
cgscmm | 14:94bc37f34b32 | 171 | |
cgscmm | 14:94bc37f34b32 | 172 | |
cgscmm | 14:94bc37f34b32 | 173 | TFT.fillrect(wall1x1, wall1y1, wall1x2, wall1y2, Green); |
cgscmm | 14:94bc37f34b32 | 174 | TFT.fillrect(wall2x1, wall2y1, wall2x2, wall2y2, Green); |
cgscmm | 14:94bc37f34b32 | 175 | TFT.line(wall1x2+1,wall1y1,wall1x2+1,wall1y2+1,Blue); |
cgscmm | 14:94bc37f34b32 | 176 | TFT.line(wall1x2+1,wall2y1,wall1x2+1,wall2y2+1,Blue); |
cgscmm | 14:94bc37f34b32 | 177 | |
cgscmm | 14:94bc37f34b32 | 178 | if (ballxpos == (wall1x1 + 10)) |
cgscmm | 14:94bc37f34b32 | 179 | { |
cgscmm | 14:94bc37f34b32 | 180 | score++; |
cgscmm | 14:94bc37f34b32 | 181 | TFT.locate(14,0); |
cgscmm | 14:94bc37f34b32 | 182 | TFT.printf("%04d", score); |
cgscmm | 14:94bc37f34b32 | 183 | } |
cgscmm | 14:94bc37f34b32 | 184 | |
cgscmm | 14:94bc37f34b32 | 185 | TFT.fillcircle(ballxpos, ballypos, ballrad, Blue); |
cgscmm | 14:94bc37f34b32 | 186 | if (ballVel < 2) |
cgscmm | 14:94bc37f34b32 | 187 | ballVel++; |
cgscmm | 14:94bc37f34b32 | 188 | |
cgscmm | 14:94bc37f34b32 | 189 | ballypos = ballypos + ballVel; |
cgscmm | 14:94bc37f34b32 | 190 | TFT.fillcircle(ballxpos, ballypos, ballrad, Red); |
cgscmm | 14:94bc37f34b32 | 191 | |
cgscmm | 14:94bc37f34b32 | 192 | if (ballypos - 2 < wall1y2) { |
cgscmm | 14:94bc37f34b32 | 193 | if (ballxpos+2 > wall1x1 && ballxpos-2 < wall1x2) { |
cgscmm | 14:94bc37f34b32 | 194 | state = over; |
cgscmm | 14:94bc37f34b32 | 195 | } |
cgscmm | 14:94bc37f34b32 | 196 | } |
cgscmm | 14:94bc37f34b32 | 197 | else if (ballypos + 2 > wall2y1) { |
cgscmm | 14:94bc37f34b32 | 198 | if (ballxpos+2 > wall2x1 && ballxpos-2 < wall2x2) { |
cgscmm | 14:94bc37f34b32 | 199 | state = over; |
cgscmm | 14:94bc37f34b32 | 200 | } |
cgscmm | 14:94bc37f34b32 | 201 | } |
cgscmm | 14:94bc37f34b32 | 202 | |
cgscmm | 14:94bc37f34b32 | 203 | if (ballypos > 125) { |
cgscmm | 14:94bc37f34b32 | 204 | state = over; |
cgscmm | 14:94bc37f34b32 | 205 | } |
cgscmm | 14:94bc37f34b32 | 206 | |
cgscmm | 14:94bc37f34b32 | 207 | break; |
cgscmm | 14:94bc37f34b32 | 208 | |
cgscmm | 14:94bc37f34b32 | 209 | case over: |
cgscmm | 14:94bc37f34b32 | 210 | //open wav file |
cgscmm | 14:94bc37f34b32 | 211 | if (ready) |
cgscmm | 14:94bc37f34b32 | 212 | { |
cgscmm | 14:94bc37f34b32 | 213 | // Thread::wait(300); |
cgscmm | 14:94bc37f34b32 | 214 | // fclose(wave_file); |
cgscmm | 14:94bc37f34b32 | 215 | // FILE *wave_file2; |
cgscmm | 14:94bc37f34b32 | 216 | // wave_file2=fopen("/sd/sfx_hit.wav","r"); |
cgscmm | 14:94bc37f34b32 | 217 | |
cgscmm | 14:94bc37f34b32 | 218 | //play wav file |
cgscmm | 14:94bc37f34b32 | 219 | // waver.play(wave_file2); |
cgscmm | 14:94bc37f34b32 | 220 | |
cgscmm | 14:94bc37f34b32 | 221 | //close wav file |
cgscmm | 14:94bc37f34b32 | 222 | // fclose(wave_file2); |
cgscmm | 14:94bc37f34b32 | 223 | |
cgscmm | 14:94bc37f34b32 | 224 | if (ballypos < 125) |
cgscmm | 14:94bc37f34b32 | 225 | { |
cgscmm | 14:94bc37f34b32 | 226 | //wave_file2=fopen("/sd/sfx_die.wav","r"); |
cgscmm | 14:94bc37f34b32 | 227 | |
cgscmm | 14:94bc37f34b32 | 228 | //play wav file |
cgscmm | 14:94bc37f34b32 | 229 | //waver.play(wave_file2); |
cgscmm | 14:94bc37f34b32 | 230 | |
cgscmm | 14:94bc37f34b32 | 231 | //close wav file |
cgscmm | 14:94bc37f34b32 | 232 | //fclose(wave_file2); |
cgscmm | 14:94bc37f34b32 | 233 | |
cgscmm | 14:94bc37f34b32 | 234 | // thread2.signal_set(0x1); |
cgscmm | 14:94bc37f34b32 | 235 | |
cgscmm | 14:94bc37f34b32 | 236 | for (int i = ballypos; i < 125; i++) |
cgscmm | 14:94bc37f34b32 | 237 | { |
cgscmm | 14:94bc37f34b32 | 238 | wait(0.03); |
cgscmm | 14:94bc37f34b32 | 239 | TFT.fillcircle(ballxpos, i, ballrad, Blue); |
cgscmm | 14:94bc37f34b32 | 240 | TFT.fillcircle(ballxpos, i+1, ballrad, Red); |
cgscmm | 14:94bc37f34b32 | 241 | } |
cgscmm | 14:94bc37f34b32 | 242 | } |
cgscmm | 14:94bc37f34b32 | 243 | |
cgscmm | 14:94bc37f34b32 | 244 | TFT.locate(5,8); |
cgscmm | 14:94bc37f34b32 | 245 | TFT.foreground(White); |
cgscmm | 14:94bc37f34b32 | 246 | TFT.background(Blue); |
cgscmm | 14:94bc37f34b32 | 247 | //TFT.text_mode(OPAQUE); |
cgscmm | 14:94bc37f34b32 | 248 | TFT.set_font((unsigned char*) Arial12x12); |
cgscmm | 14:94bc37f34b32 | 249 | TFT.printf("Game Over"); |
cgscmm | 14:94bc37f34b32 | 250 | |
cgscmm | 14:94bc37f34b32 | 251 | TFT.locate(14,0); |
cgscmm | 14:94bc37f34b32 | 252 | TFT.printf("%04d", score); |
cgscmm | 14:94bc37f34b32 | 253 | ready = 0; |
cgscmm | 14:94bc37f34b32 | 254 | score = 0; |
cgscmm | 14:94bc37f34b32 | 255 | } |
cgscmm | 14:94bc37f34b32 | 256 | break; |
cgscmm | 14:94bc37f34b32 | 257 | } |
cgscmm | 14:94bc37f34b32 | 258 | } |
cgscmm | 14:94bc37f34b32 | 259 | } |