Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem

Committer:
rushib1
Date:
Sun Apr 26 00:47:37 2020 +0000
Revision:
2:acd4656312d8
Parent:
0:5bb514318c64
Child:
3:6e41a5ce16c2
Combine game features

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rushib1 0:5bb514318c64 1 #include "mbed.h"
rushib1 0:5bb514318c64 2 #include "uLCD_4DGL.h"
rushib1 0:5bb514318c64 3 #include "rgb.h"
rushib1 0:5bb514318c64 4 #include "SDFileSystem.h"
rushib1 0:5bb514318c64 5 #include "wave_player.h"
rushib1 0:5bb514318c64 6 #include "rtos.h"
rushib1 0:5bb514318c64 7
rushib1 0:5bb514318c64 8 #include <string>
rushib1 0:5bb514318c64 9 #include <iostream>
rushib1 0:5bb514318c64 10 #include <fstream>
rushib1 2:acd4656312d8 11 #include <algorithm>
rushib1 0:5bb514318c64 12
rushib1 0:5bb514318c64 13 using namespace std;
rushib1 0:5bb514318c64 14
rushib1 0:5bb514318c64 15 /*
rushib1 0:5bb514318c64 16 ------------------CONSTS
rushib1 0:5bb514318c64 17 */
rushib1 0:5bb514318c64 18
rushib1 0:5bb514318c64 19 volatile bool homescreen = true;
rushib1 0:5bb514318c64 20 volatile bool diff_selected = false;
rushib1 0:5bb514318c64 21 volatile bool boot_vid = true;
rushib1 2:acd4656312d8 22 bool timeout = true;
rushib1 2:acd4656312d8 23 bool game_is_over = false;
rushib1 0:5bb514318c64 24
rushib1 0:5bb514318c64 25 /*
rushib1 0:5bb514318c64 26 ------------------I/O VARS
rushib1 0:5bb514318c64 27 */
rushib1 0:5bb514318c64 28 SDFileSystem sd(p5, p6, p7, p8, "sd");
rushib1 0:5bb514318c64 29 RawSerial bluemod(p28, p27);
rushib1 0:5bb514318c64 30 Serial pc(USBTX, USBRX);
rushib1 0:5bb514318c64 31 uLCD_4DGL uLCD(p9, p10, p11);
rushib1 0:5bb514318c64 32 DigitalIn pb(p12);
rushib1 0:5bb514318c64 33
rushib1 0:5bb514318c64 34 /*
rushib1 0:5bb514318c64 35 ------------------LED VARS
rushib1 0:5bb514318c64 36 */
rushib1 2:acd4656312d8 37
rushib1 2:acd4656312d8 38 DigitalOut life[] = {(p16), (p19), (p20)};
rushib1 0:5bb514318c64 39 RGBLed myRGBled(p21, p24, p23);
rushib1 0:5bb514318c64 40 DigitalOut onboard_led(LED1);
rushib1 0:5bb514318c64 41
rushib1 0:5bb514318c64 42 /*
rushib1 0:5bb514318c64 43 ------------------AUDIO VARS
rushib1 0:5bb514318c64 44 */
rushib1 0:5bb514318c64 45
rushib1 0:5bb514318c64 46 FILE *wave_file;
rushib1 0:5bb514318c64 47 AnalogOut DACount(p18);
rushib1 0:5bb514318c64 48 wave_player waver(&DACount);
rushib1 0:5bb514318c64 49
rushib1 0:5bb514318c64 50 /*
rushib1 0:5bb514318c64 51 ------------------GENERAL VARS
rushib1 0:5bb514318c64 52 */
rushib1 0:5bb514318c64 53 int difficulty = 0;
rushib1 0:5bb514318c64 54 string scores;
rushib1 0:5bb514318c64 55 int score_val[3];
rushib1 0:5bb514318c64 56 volatile char bnum;
rushib1 0:5bb514318c64 57 int ans[4];
rushib1 2:acd4656312d8 58 int corr_ans;
rushib1 0:5bb514318c64 59 char sign;
rushib1 2:acd4656312d8 60 int live_left = 3;
rushib1 0:5bb514318c64 61
rushib1 0:5bb514318c64 62 Mutex lcd_mutex;
rushib1 0:5bb514318c64 63 Mutex blue_mutex;
rushib1 0:5bb514318c64 64 Thread thread1, thread2, thread3;
rushib1 0:5bb514318c64 65
rushib1 0:5bb514318c64 66 void main_screen()
rushib1 0:5bb514318c64 67 {
rushib1 0:5bb514318c64 68
rushib1 0:5bb514318c64 69 //while(true){
rushib1 0:5bb514318c64 70 // if(homescreen){
rushib1 0:5bb514318c64 71 pc.printf("MAIN_SCREEN\r\n");
rushib1 0:5bb514318c64 72 lcd_mutex.lock();
rushib1 0:5bb514318c64 73 uLCD.cls();
rushib1 0:5bb514318c64 74 uLCD.text_height(1.9);
rushib1 0:5bb514318c64 75 uLCD.text_width(1.9);
rushib1 0:5bb514318c64 76 uLCD.color(WHITE);
rushib1 0:5bb514318c64 77 uLCD.locate(6, 1);
rushib1 0:5bb514318c64 78 uLCD.printf("MATH FUN");
rushib1 0:5bb514318c64 79 //MAKE MATH FUN BLINK
rushib1 0:5bb514318c64 80 uLCD.locate(1, 4);
rushib1 0:5bb514318c64 81 uLCD.text_height(1.3);
rushib1 0:5bb514318c64 82 uLCD.printf("Difficulty");
rushib1 0:5bb514318c64 83 uLCD.text_height(1.3);
rushib1 0:5bb514318c64 84 uLCD.text_width(1.9);
rushib1 0:5bb514318c64 85 uLCD.locate(3, 6);
rushib1 0:5bb514318c64 86 uLCD.color(GREEN);
rushib1 0:5bb514318c64 87 uLCD.printf("1) Easy");
rushib1 0:5bb514318c64 88 uLCD.locate(3, 8);
rushib1 0:5bb514318c64 89 uLCD.color(0xFFFF00);
rushib1 0:5bb514318c64 90 uLCD.printf("2) Not as Easy");
rushib1 0:5bb514318c64 91 uLCD.locate(3, 10);
rushib1 0:5bb514318c64 92 uLCD.color(RED);
rushib1 0:5bb514318c64 93 uLCD.printf("3) Very Uneasy");
rushib1 0:5bb514318c64 94 uLCD.color(BLUE);
rushib1 0:5bb514318c64 95 uLCD.locate(1, 13);
rushib1 0:5bb514318c64 96 uLCD.printf("4) High Scores");
rushib1 0:5bb514318c64 97 lcd_mutex.unlock();
rushib1 0:5bb514318c64 98 //pc.printf("thread 1\r\n");
rushib1 0:5bb514318c64 99 homescreen = false;
rushib1 0:5bb514318c64 100 // Thread::wait(100);
rushib1 0:5bb514318c64 101 // }
rushib1 0:5bb514318c64 102 // else{
rushib1 0:5bb514318c64 103 // Thread::yield();
rushib1 0:5bb514318c64 104 // }
rushib1 0:5bb514318c64 105 //}
rushib1 0:5bb514318c64 106 }
rushib1 0:5bb514318c64 107
rushib1 2:acd4656312d8 108 bool count_distinct(int arr[], int n)
rushib1 2:acd4656312d8 109 {
rushib1 2:acd4656312d8 110
rushib1 2:acd4656312d8 111 int res = 1;
rushib1 0:5bb514318c64 112
rushib1 2:acd4656312d8 113 // Pick all elements one by one
rushib1 2:acd4656312d8 114 for (int i = 1; i < n; i++)
rushib1 2:acd4656312d8 115 {
rushib1 2:acd4656312d8 116 int j = 0;
rushib1 2:acd4656312d8 117 for (j = 0; j < i; j++)
rushib1 2:acd4656312d8 118 if (arr[i] == arr[j])
rushib1 2:acd4656312d8 119 break;
rushib1 2:acd4656312d8 120
rushib1 2:acd4656312d8 121 // If not printed earlier, then print it
rushib1 2:acd4656312d8 122 if (i == j)
rushib1 2:acd4656312d8 123 res++;
rushib1 0:5bb514318c64 124 }
rushib1 0:5bb514318c64 125
rushib1 2:acd4656312d8 126 if (res == 4)
rushib1 2:acd4656312d8 127 {
rushib1 2:acd4656312d8 128 return false;
rushib1 2:acd4656312d8 129 }
rushib1 2:acd4656312d8 130 else
rushib1 2:acd4656312d8 131 {
rushib1 2:acd4656312d8 132 return true;
rushib1 2:acd4656312d8 133 }
rushib1 2:acd4656312d8 134 }
rushib1 0:5bb514318c64 135
rushib1 2:acd4656312d8 136 void gen_ans(int &num1, int &num2, int sign_val)
rushib1 2:acd4656312d8 137 {
rushib1 0:5bb514318c64 138
rushib1 2:acd4656312d8 139 switch (sign_val)
rushib1 2:acd4656312d8 140 {
rushib1 2:acd4656312d8 141 case 1:
rushib1 2:acd4656312d8 142 sign = '*';
rushib1 2:acd4656312d8 143 ans[0] = num1 * num2;
rushib1 2:acd4656312d8 144 ans[1] = (num1 + (rand() % (10 + 1 - 1) + 1)) * num2;
rushib1 2:acd4656312d8 145 ans[2] = (num1 * num2) + (rand() % (20 + 1 - 1) + 1);
rushib1 2:acd4656312d8 146 ans[3] = (num1 * num2) + (rand() % (20 + 1 - 1) + 1);
rushib1 2:acd4656312d8 147 break;
rushib1 2:acd4656312d8 148 case 2:
rushib1 2:acd4656312d8 149 sign = '+';
rushib1 2:acd4656312d8 150 ans[0] = num1 + num2;
rushib1 2:acd4656312d8 151 ans[1] = (num1 + (rand() % (10 + 1 - 1) + 1)) + num2;
rushib1 2:acd4656312d8 152 ans[2] = (num1 + num2) - (rand() % ((num1 + num2) + 1 - 1) + 1);
rushib1 2:acd4656312d8 153 ans[3] = (num1 * num2) + (rand() % (10 + 1 - 1) + 1);
rushib1 2:acd4656312d8 154 break;
rushib1 2:acd4656312d8 155 case 3:
rushib1 2:acd4656312d8 156 sign = '-';
rushib1 2:acd4656312d8 157 //rand()%(max-min + 1) + min;
rushib1 2:acd4656312d8 158 while (num1 == num2)
rushib1 2:acd4656312d8 159 {
rushib1 2:acd4656312d8 160 num1 = (rand() % ((num1 + 2) - 1 + 1) + 1);
rushib1 0:5bb514318c64 161 }
rushib1 2:acd4656312d8 162 ans[0] = num1 - num2;
rushib1 2:acd4656312d8 163 ans[1] = (num1 - num2) * -1 + (rand() % (10 + 1 - 1) + 1);
rushib1 2:acd4656312d8 164 ans[2] = num1 + num2 - (rand() % (10 + 1 - 1) + 1);
rushib1 2:acd4656312d8 165 ans[3] = num1 * num2 - (rand() % (20 + 1 - 1) + 1);
rushib1 2:acd4656312d8 166 break;
rushib1 2:acd4656312d8 167 case 4:
rushib1 2:acd4656312d8 168 sign = '/';
rushib1 2:acd4656312d8 169 ans[0] = num1 - num2;
rushib1 2:acd4656312d8 170 break;
rushib1 2:acd4656312d8 171 }
rushib1 0:5bb514318c64 172 }
rushib1 0:5bb514318c64 173
rushib1 0:5bb514318c64 174 void game_questions()
rushib1 0:5bb514318c64 175 {
rushib1 0:5bb514318c64 176 int gen1 = rand() % (10 + 1 - 0) + 0;
rushib1 0:5bb514318c64 177 int gen2 = rand() % (10 + 1 - 1) + 1;
rushib1 0:5bb514318c64 178 int gen_sign = rand() % (3 + 1 - 1) + 1;
rushib1 0:5bb514318c64 179 gen_ans(gen1, gen2, gen_sign);
rushib1 0:5bb514318c64 180 bool reroll = count_distinct(ans, 4);
rushib1 2:acd4656312d8 181 while (reroll)
rushib1 2:acd4656312d8 182 {
rushib1 0:5bb514318c64 183 gen_ans(gen1, gen2, gen_sign);
rushib1 0:5bb514318c64 184 reroll = count_distinct(ans, 4);
rushib1 2:acd4656312d8 185 //pc.printf("reroll\r\n");
rushib1 0:5bb514318c64 186 }
rushib1 2:acd4656312d8 187
rushib1 2:acd4656312d8 188 corr_ans = ans[0];
rushib1 2:acd4656312d8 189 random_shuffle(&ans[0], &ans[3]);
rushib1 2:acd4656312d8 190
rushib1 0:5bb514318c64 191 uLCD.cls();
rushib1 0:5bb514318c64 192 uLCD.locate(2, 1);
rushib1 0:5bb514318c64 193 uLCD.text_height(2);
rushib1 0:5bb514318c64 194 uLCD.text_width(2);
rushib1 0:5bb514318c64 195 uLCD.color(0xFC766A);
rushib1 0:5bb514318c64 196 uLCD.printf("Q1");
rushib1 0:5bb514318c64 197 uLCD.text_height(1);
rushib1 0:5bb514318c64 198 uLCD.text_width(1);
rushib1 0:5bb514318c64 199
rushib1 2:acd4656312d8 200 uLCD.locate(9, 1);
rushib1 2:acd4656312d8 201 uLCD.printf("TIME:");
rushib1 2:acd4656312d8 202 uLCD.line(55, 0, 55, 27, 0xA89C94);
rushib1 2:acd4656312d8 203
rushib1 0:5bb514318c64 204 uLCD.rectangle(0, 0, 127, 127, 0xA89C94);
rushib1 0:5bb514318c64 205 uLCD.line(0, 27, 127, 27, 0xA89C94);
rushib1 2:acd4656312d8 206 uLCD.line(0, 50, 127, 50, 0xA89C94);
rushib1 0:5bb514318c64 207
rushib1 0:5bb514318c64 208 uLCD.color(0x669DB2);
rushib1 0:5bb514318c64 209 uLCD.locate(2, 4);
rushib1 0:5bb514318c64 210 uLCD.text_height(2);
rushib1 0:5bb514318c64 211 uLCD.text_width(2);
rushib1 0:5bb514318c64 212 uLCD.printf("%i%c%i=", gen1, sign, gen2);
rushib1 0:5bb514318c64 213 uLCD.text_height(1);
rushib1 0:5bb514318c64 214 uLCD.text_width(1);
rushib1 0:5bb514318c64 215
rushib1 0:5bb514318c64 216 uLCD.color(0xFC766A);
rushib1 2:acd4656312d8 217 uLCD.locate(2, 7);
rushib1 0:5bb514318c64 218 uLCD.printf("1)");
rushib1 0:5bb514318c64 219
rushib1 0:5bb514318c64 220 uLCD.color(0x669DB2);
rushib1 2:acd4656312d8 221 uLCD.locate(8, 7);
rushib1 2:acd4656312d8 222 uLCD.printf("%i", ans[0]);
rushib1 0:5bb514318c64 223
rushib1 0:5bb514318c64 224 uLCD.color(0xFC766A);
rushib1 2:acd4656312d8 225 uLCD.locate(2, 9);
rushib1 0:5bb514318c64 226 uLCD.printf("2)");
rushib1 0:5bb514318c64 227
rushib1 0:5bb514318c64 228 uLCD.color(0x669DB2);
rushib1 2:acd4656312d8 229 uLCD.locate(8, 9);
rushib1 2:acd4656312d8 230 uLCD.printf("%i", ans[1]);
rushib1 0:5bb514318c64 231
rushib1 0:5bb514318c64 232 uLCD.color(0xFC766A);
rushib1 2:acd4656312d8 233 uLCD.locate(2, 11);
rushib1 0:5bb514318c64 234 uLCD.printf("3)");
rushib1 0:5bb514318c64 235
rushib1 0:5bb514318c64 236 uLCD.color(0x669DB2);
rushib1 2:acd4656312d8 237 uLCD.locate(8, 11);
rushib1 2:acd4656312d8 238 uLCD.printf("%i", ans[2]);
rushib1 0:5bb514318c64 239
rushib1 0:5bb514318c64 240 uLCD.color(0xFC766A);
rushib1 2:acd4656312d8 241 uLCD.locate(2, 13);
rushib1 0:5bb514318c64 242 uLCD.printf("4)");
rushib1 0:5bb514318c64 243
rushib1 0:5bb514318c64 244 uLCD.color(0x669DB2);
rushib1 2:acd4656312d8 245 uLCD.locate(8, 13);
rushib1 2:acd4656312d8 246 uLCD.printf("%i", ans[3]);
rushib1 0:5bb514318c64 247
rushib1 2:acd4656312d8 248 uLCD.line(0, 115, 127, 115, 0xA89C94);
rushib1 2:acd4656312d8 249 }
rushib1 2:acd4656312d8 250 void game_over()
rushib1 2:acd4656312d8 251 {
rushib1 2:acd4656312d8 252 uLCD.cls();
rushib1 2:acd4656312d8 253 uLCD.locate(0, 7);
rushib1 2:acd4656312d8 254 uLCD.text_height(2);
rushib1 2:acd4656312d8 255 uLCD.text_width(2);
rushib1 2:acd4656312d8 256 uLCD.color(0xFC776A);
rushib1 2:acd4656312d8 257 uLCD.printf("GAME OVER");
rushib1 2:acd4656312d8 258 game_is_over = true;
rushib1 2:acd4656312d8 259 wait(2);
rushib1 2:acd4656312d8 260 }
rushib1 2:acd4656312d8 261
rushib1 2:acd4656312d8 262 void life_count_check()
rushib1 2:acd4656312d8 263 {
rushib1 2:acd4656312d8 264 life[0] = 0;
rushib1 2:acd4656312d8 265 life[1] = 0;
rushib1 2:acd4656312d8 266 life[2] = 0;
rushib1 2:acd4656312d8 267
rushib1 2:acd4656312d8 268 for (int i = 0; i < live_left; i++)
rushib1 2:acd4656312d8 269 {
rushib1 2:acd4656312d8 270 life[i] = 1;
rushib1 2:acd4656312d8 271 }
rushib1 2:acd4656312d8 272 if (live_left == 0)
rushib1 2:acd4656312d8 273 {
rushib1 2:acd4656312d8 274 game_over();
rushib1 2:acd4656312d8 275 }
rushib1 0:5bb514318c64 276 }
rushib1 0:5bb514318c64 277
rushib1 2:acd4656312d8 278 void check_correct_ans()
rushib1 0:5bb514318c64 279 {
rushib1 2:acd4656312d8 280 if (timeout)
rushib1 0:5bb514318c64 281 {
rushib1 2:acd4656312d8 282 //pc.printf("TIMEOUT\r\n");
rushib1 2:acd4656312d8 283 uLCD.cls();
rushib1 2:acd4656312d8 284 uLCD.locate(5, 7);
rushib1 2:acd4656312d8 285 uLCD.text_height(2);
rushib1 2:acd4656312d8 286 uLCD.text_width(2);
rushib1 2:acd4656312d8 287 uLCD.color(0xF0F6F7);
rushib1 2:acd4656312d8 288 uLCD.printf("TIME");
rushib1 2:acd4656312d8 289 live_left--;
rushib1 2:acd4656312d8 290 //wait(2);
rushib1 2:acd4656312d8 291 }
rushib1 2:acd4656312d8 292 else
rushib1 2:acd4656312d8 293 {
rushib1 2:acd4656312d8 294 //pc.printf("%i\r\n", corr_ans);
rushib1 2:acd4656312d8 295 //pc.printf("%i\r\n", ans[(bnum-'0' -1)]);
rushib1 2:acd4656312d8 296
rushib1 2:acd4656312d8 297 if (corr_ans == ans[(bnum - '0') - 1])
rushib1 0:5bb514318c64 298 {
rushib1 0:5bb514318c64 299
rushib1 2:acd4656312d8 300 uLCD.cls();
rushib1 2:acd4656312d8 301 uLCD.locate(2, 7);
rushib1 2:acd4656312d8 302 uLCD.text_height(2);
rushib1 2:acd4656312d8 303 uLCD.text_width(2);
rushib1 2:acd4656312d8 304 uLCD.color(GREEN);
rushib1 2:acd4656312d8 305 uLCD.printf("CORRECT");
rushib1 2:acd4656312d8 306 //wait(2);
rushib1 2:acd4656312d8 307 //pc.printf("CORRECT\r\n");
rushib1 2:acd4656312d8 308 }
rushib1 2:acd4656312d8 309 else
rushib1 2:acd4656312d8 310 {
rushib1 2:acd4656312d8 311 //pc.printf("INCORRECT\r\n");
rushib1 2:acd4656312d8 312 uLCD.cls();
rushib1 2:acd4656312d8 313 uLCD.locate(0, 7);
rushib1 2:acd4656312d8 314 uLCD.text_height(2);
rushib1 2:acd4656312d8 315 uLCD.text_width(2);
rushib1 2:acd4656312d8 316 uLCD.color(RED);
rushib1 2:acd4656312d8 317 uLCD.printf("INCORRECT");
rushib1 2:acd4656312d8 318 live_left--;
rushib1 2:acd4656312d8 319 //wait(2);
rushib1 2:acd4656312d8 320 }
rushib1 2:acd4656312d8 321 }
rushib1 2:acd4656312d8 322 }
rushib1 2:acd4656312d8 323
rushib1 2:acd4656312d8 324
rushib1 2:acd4656312d8 325 bool get_bluetooth_button()
rushib1 2:acd4656312d8 326 {
rushib1 2:acd4656312d8 327 pc.printf("BLUETOOTH\r\n");
rushib1 2:acd4656312d8 328 blue_mutex.lock();
rushib1 2:acd4656312d8 329 if (bluemod.getc() == '!')
rushib1 2:acd4656312d8 330 {
rushib1 2:acd4656312d8 331 if (bluemod.getc() == 'B')
rushib1 2:acd4656312d8 332 {
rushib1 2:acd4656312d8 333 //button number
rushib1 2:acd4656312d8 334 bnum = bluemod.getc();
rushib1 2:acd4656312d8 335 //button data
rushib1 2:acd4656312d8 336 char bhit = bluemod.getc();
rushib1 2:acd4656312d8 337 if (bluemod.getc() == char(~('!' + 'B' + bnum + bhit)))
rushib1 0:5bb514318c64 338 {
rushib1 2:acd4656312d8 339 if (bhit == '1')
rushib1 0:5bb514318c64 340 {
rushib1 2:acd4656312d8 341 pc.printf("%c\r\n", bnum);
rushib1 2:acd4656312d8 342 blue_mutex.unlock();
rushib1 2:acd4656312d8 343 return false;
rushib1 0:5bb514318c64 344 }
rushib1 2:acd4656312d8 345 }
rushib1 2:acd4656312d8 346 }
rushib1 2:acd4656312d8 347 }
rushib1 2:acd4656312d8 348 blue_mutex.unlock();
rushib1 2:acd4656312d8 349 return true;
rushib1 2:acd4656312d8 350 }
rushib1 2:acd4656312d8 351
rushib1 2:acd4656312d8 352 void get_button()
rushib1 2:acd4656312d8 353 {
rushib1 2:acd4656312d8 354 int timer = 10;
rushib1 2:acd4656312d8 355 timeout = true;
rushib1 2:acd4656312d8 356 while (timer != 0)
rushib1 2:acd4656312d8 357 {
rushib1 2:acd4656312d8 358 uLCD.locate(15, 1);
rushib1 2:acd4656312d8 359 uLCD.color(0xF0F6F7);
rushib1 2:acd4656312d8 360 uLCD.printf("%i", timer);
rushib1 2:acd4656312d8 361 if (bluemod.readable())
rushib1 2:acd4656312d8 362 {
rushib1 2:acd4656312d8 363 timeout = get_bluetooth_button();
rushib1 2:acd4656312d8 364 if(timeout == false){
rushib1 2:acd4656312d8 365 break;
rushib1 0:5bb514318c64 366 }
rushib1 0:5bb514318c64 367 }
rushib1 2:acd4656312d8 368 wait(1);
rushib1 2:acd4656312d8 369 uLCD.locate(15, 1);
rushib1 2:acd4656312d8 370 uLCD.color(BLACK);
rushib1 2:acd4656312d8 371 uLCD.printf("%i", timer);
rushib1 2:acd4656312d8 372 timer = timer - 1;
rushib1 2:acd4656312d8 373 //pc.printf("%i\r\n", x);
rushib1 2:acd4656312d8 374 }
rushib1 2:acd4656312d8 375 }
rushib1 2:acd4656312d8 376
rushib1 2:acd4656312d8 377
rushib1 2:acd4656312d8 378
rushib1 2:acd4656312d8 379 void bluetooth_thread()
rushib1 2:acd4656312d8 380 {
rushib1 2:acd4656312d8 381 while (true)
rushib1 2:acd4656312d8 382 {
rushib1 2:acd4656312d8 383 if (bluemod.readable())
rushib1 2:acd4656312d8 384 {
rushib1 2:acd4656312d8 385 bool n_timeout = get_bluetooth_button();
rushib1 2:acd4656312d8 386 }
rushib1 2:acd4656312d8 387 else
rushib1 2:acd4656312d8 388 {
rushib1 2:acd4656312d8 389 Thread::yield();
rushib1 2:acd4656312d8 390 }
rushib1 2:acd4656312d8 391 Thread::wait(100);
rushib1 0:5bb514318c64 392 }
rushib1 0:5bb514318c64 393 }
rushib1 0:5bb514318c64 394
rushib1 0:5bb514318c64 395 void rgb_led_difficulty()
rushib1 0:5bb514318c64 396 {
rushib1 0:5bb514318c64 397 difficulty = bnum - 48;
rushib1 0:5bb514318c64 398 //pc.printf("%i\r\n",difficulty);
rushib1 0:5bb514318c64 399 if (difficulty == 1)
rushib1 0:5bb514318c64 400 {
rushib1 0:5bb514318c64 401 myRGBled.write(0.0, 1.0, 0.0); //green
rushib1 0:5bb514318c64 402 diff_selected = true;
rushib1 0:5bb514318c64 403 }
rushib1 0:5bb514318c64 404 else if (difficulty == 2)
rushib1 0:5bb514318c64 405 {
rushib1 0:5bb514318c64 406 myRGBled.write(1.0, 0.2, 0.0); //yellow = red + some green
rushib1 0:5bb514318c64 407 diff_selected = true;
rushib1 0:5bb514318c64 408 }
rushib1 0:5bb514318c64 409 else if (difficulty == 3)
rushib1 0:5bb514318c64 410 {
rushib1 0:5bb514318c64 411 myRGBled.write(1.0, 0.0, 0.0); //red
rushib1 0:5bb514318c64 412 diff_selected = true;
rushib1 0:5bb514318c64 413 }
rushib1 0:5bb514318c64 414 }
rushib1 0:5bb514318c64 415
rushib1 0:5bb514318c64 416 void wav_thread()
rushib1 0:5bb514318c64 417 {
rushib1 0:5bb514318c64 418 while (true)
rushib1 0:5bb514318c64 419 {
rushib1 0:5bb514318c64 420 pc.printf("AUDIO\r\n");
rushib1 0:5bb514318c64 421 wave_file = fopen("/sd/audio/intro.wav", "r");
rushib1 0:5bb514318c64 422 waver.play(wave_file);
rushib1 0:5bb514318c64 423 fclose(wave_file);
rushib1 0:5bb514318c64 424 }
rushib1 0:5bb514318c64 425 }
rushib1 0:5bb514318c64 426
rushib1 0:5bb514318c64 427 void boot_video_thread()
rushib1 0:5bb514318c64 428 {
rushib1 0:5bb514318c64 429 while (true)
rushib1 0:5bb514318c64 430 {
rushib1 2:acd4656312d8 431 //pc.printf("BootVID\r\n");
rushib1 0:5bb514318c64 432 //PLAY VIDEO BOOT
rushib1 0:5bb514318c64 433 if (boot_vid)
rushib1 0:5bb514318c64 434 {
rushib1 0:5bb514318c64 435 //lcd_mutex.lock();
rushib1 0:5bb514318c64 436 uLCD.media_init();
rushib1 0:5bb514318c64 437 uLCD.set_sector_address(0x0, 0x0);
rushib1 0:5bb514318c64 438 uLCD.display_video(0, 0);
rushib1 0:5bb514318c64 439 boot_vid = false;
rushib1 0:5bb514318c64 440 //lcd_mutex.unlock();
rushib1 0:5bb514318c64 441 }
rushib1 0:5bb514318c64 442 else
rushib1 0:5bb514318c64 443 {
rushib1 0:5bb514318c64 444 Thread::yield();
rushib1 0:5bb514318c64 445 }
rushib1 0:5bb514318c64 446 }
rushib1 0:5bb514318c64 447 }
rushib1 0:5bb514318c64 448
rushib1 2:acd4656312d8 449 void init()
rushib1 2:acd4656312d8 450 {
rushib1 2:acd4656312d8 451 uLCD.baudrate(3000000);
rushib1 2:acd4656312d8 452 srand(time(NULL));
rushib1 2:acd4656312d8 453 life[0] = 1;
rushib1 2:acd4656312d8 454 life[1] = 1;
rushib1 2:acd4656312d8 455 life[2] = 1;
rushib1 2:acd4656312d8 456 thread1.start(boot_video_thread);
rushib1 2:acd4656312d8 457 }
rushib1 2:acd4656312d8 458
rushib1 0:5bb514318c64 459 int main()
rushib1 0:5bb514318c64 460 {
rushib1 0:5bb514318c64 461
rushib1 2:acd4656312d8 462 init();
rushib1 0:5bb514318c64 463 pc.printf("MAIN_INIT\r\n\n");
rushib1 0:5bb514318c64 464 thread2.start(wav_thread);
rushib1 0:5bb514318c64 465 while (true)
rushib1 0:5bb514318c64 466 {
rushib1 0:5bb514318c64 467 //pc.printf("MAIN_LOOP\r\n");
rushib1 0:5bb514318c64 468 //ONCE VIDEO STOPS PLAYING
rushib1 0:5bb514318c64 469 if (boot_vid == false)
rushib1 0:5bb514318c64 470 {
rushib1 0:5bb514318c64 471 thread1.terminate();
rushib1 0:5bb514318c64 472 //IF HOMESCREEN PRINTED ONCE DONT PRINT AGAIN
rushib1 0:5bb514318c64 473 if (homescreen)
rushib1 0:5bb514318c64 474 {
rushib1 0:5bb514318c64 475 main_screen();
rushib1 0:5bb514318c64 476 }
rushib1 0:5bb514318c64 477 //pc.printf("MAIN_PRINT\r\n");
rushib1 2:acd4656312d8 478 //break;
rushib1 0:5bb514318c64 479 if (diff_selected == false)
rushib1 0:5bb514318c64 480 {
rushib1 2:acd4656312d8 481 thread3.start(bluetooth_thread);
rushib1 0:5bb514318c64 482 rgb_led_difficulty();
rushib1 0:5bb514318c64 483 }
rushib1 2:acd4656312d8 484 else
rushib1 2:acd4656312d8 485 {
rushib1 0:5bb514318c64 486 thread3.terminate();
rushib1 2:acd4656312d8 487 break;
rushib1 0:5bb514318c64 488 }
rushib1 0:5bb514318c64 489 }
rushib1 0:5bb514318c64 490 Thread::wait(200);
rushib1 0:5bb514318c64 491 }
rushib1 2:acd4656312d8 492 while (!game_is_over)
rushib1 2:acd4656312d8 493 {
rushib1 2:acd4656312d8 494 game_questions();
rushib1 2:acd4656312d8 495 get_button();
rushib1 2:acd4656312d8 496 check_correct_ans();
rushib1 2:acd4656312d8 497 wait(2);
rushib1 2:acd4656312d8 498 life_count_check();
rushib1 2:acd4656312d8 499 }
rushib1 2:acd4656312d8 500 }