ECE 4180 Final

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

Committer:
yqin70
Date:
Sun Dec 08 02:18:30 2019 +0000
Revision:
21:cbcbb3480cad
Parent:
20:7d56cdcbc9a5
somewhat done(pushbutton added, has an issue where if you spam the touchpad, it'll increase your score even if there is no bubble).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yqin70 0:4f77ae831ee7 1 #include "mbed.h"
jcrane32 7:15fdc55dbf66 2 #include "rtos.h"
jcrane32 19:d65f9fb1023b 3 #include "stdio.h"
jcrane32 20:7d56cdcbc9a5 4 #include <stdlib.h>
jcrane32 20:7d56cdcbc9a5 5 #include <time.h>
yqin70 0:4f77ae831ee7 6 #include "SDFileSystem.h"
yqin70 0:4f77ae831ee7 7 #include "wave_player.h"
ShelbyC22 16:13b65f6139be 8 #include "mpr121.h"
jcrane32 6:cd24147b5e50 9
yqin70 0:4f77ae831ee7 10 #include "uLCD_4DGL.h"
yqin70 0:4f77ae831ee7 11 #include "Small_6.h"
yqin70 0:4f77ae831ee7 12 #include "Small_7.h"
yqin70 0:4f77ae831ee7 13 #include "Arial_9.h"
yqin70 0:4f77ae831ee7 14 #include "C12832_lcd.h"
yqin70 0:4f77ae831ee7 15
jcrane32 19:d65f9fb1023b 16 #include "globals.h"
jcrane32 19:d65f9fb1023b 17
jcrane32 20:7d56cdcbc9a5 18 //#include "bubbles.h"
yqin70 11:362e25f659a5 19 #include "stacys_mom.h"
jcrane32 19:d65f9fb1023b 20 //#include "the_middle.h"
yqin70 11:362e25f659a5 21 #include "fireflies.h"
yqin70 11:362e25f659a5 22 #include "sins.h"
jcrane32 7:15fdc55dbf66 23 #include "LED.hpp"
jcrane32 6:cd24147b5e50 24
jcrane32 19:d65f9fb1023b 25 #include "Lane.hpp"
jcrane32 19:d65f9fb1023b 26 #include "Bubble.hpp"
jcrane32 19:d65f9fb1023b 27
jcrane32 19:d65f9fb1023b 28
yqin70 0:4f77ae831ee7 29 //Setup RGB led using PWM pins and class
ShelbyC22 4:59b73f321c0e 30 RGBLed myRGBled(p24,p23,p22); //RGB PWM pins
yqin70 0:4f77ae831ee7 31 char bred=0;
yqin70 0:4f77ae831ee7 32 char bgreen=0;
yqin70 0:4f77ae831ee7 33 char bblue=0;
yqin70 0:4f77ae831ee7 34
yqin70 0:4f77ae831ee7 35 volatile bool songselect = false;
yqin70 0:4f77ae831ee7 36 volatile bool homescreen = true;
ShelbyC22 5:b2cf15651d4e 37 volatile bool songchange = false;
jcrane32 7:15fdc55dbf66 38 volatile bool playing = false;
yqin70 21:cbcbb3480cad 39 Serial pc(USBTX, USBRX);
jcrane32 7:15fdc55dbf66 40 uLCD_4DGL uLCD(p28,p27,p30);
yqin70 0:4f77ae831ee7 41 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
jcrane32 19:d65f9fb1023b 42 AnalogIn joy_pot(p16);
yqin70 0:4f77ae831ee7 43 DigitalOut myled(LED1);
yqin70 0:4f77ae831ee7 44 DigitalIn pb1(p20);
yqin70 0:4f77ae831ee7 45 DigitalIn pb2(p19);
yqin70 1:a1aa9a79070a 46 AnalogOut DACout(p18);
ShelbyC22 16:13b65f6139be 47 // Create the interrupt receiver object on pin 26 for touch pad IRQ
ShelbyC22 16:13b65f6139be 48 InterruptIn interrupt(p26);
ShelbyC22 16:13b65f6139be 49 // Setup the i2c bus on pins 9 and 10
ShelbyC22 16:13b65f6139be 50 I2C i2c(p9, p10);
ShelbyC22 16:13b65f6139be 51 // Setup the Mpr121:
ShelbyC22 16:13b65f6139be 52 // constructor(i2c object, i2c address of the mpr121)
ShelbyC22 16:13b65f6139be 53 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
ShelbyC22 16:13b65f6139be 54
ShelbyC22 16:13b65f6139be 55 // Key hit/release interrupt routine
jcrane32 20:7d56cdcbc9a5 56 //void fallInterrupt() {
jcrane32 20:7d56cdcbc9a5 57 // int key_code=0;
jcrane32 20:7d56cdcbc9a5 58 // int i=0;
jcrane32 20:7d56cdcbc9a5 59 // int value=mpr121.read(0x00);
jcrane32 20:7d56cdcbc9a5 60 // value +=mpr121.read(0x01)<<8;
jcrane32 20:7d56cdcbc9a5 61 // for (i=0; i<12; i++) {
jcrane32 20:7d56cdcbc9a5 62 // if (((value>>i)&0x01)==1) {
jcrane32 20:7d56cdcbc9a5 63 // key_code=i+1;
jcrane32 20:7d56cdcbc9a5 64 // }
jcrane32 20:7d56cdcbc9a5 65 // }
jcrane32 20:7d56cdcbc9a5 66 //}
ShelbyC22 16:13b65f6139be 67
yqin70 1:a1aa9a79070a 68 wave_player waver(&DACout);
jcrane32 19:d65f9fb1023b 69 Thread thread1, thread2, thread3, thread4;
jcrane32 19:d65f9fb1023b 70
jcrane32 7:15fdc55dbf66 71 Ticker nextsample;
jcrane32 19:d65f9fb1023b 72 volatile int songNdx = 0;
jcrane32 19:d65f9fb1023b 73 unsigned char *playingSong = NULL;
yqin70 0:4f77ae831ee7 74
yqin70 0:4f77ae831ee7 75 // mutex to make the lcd lib thread safe
yqin70 0:4f77ae831ee7 76 Mutex lcd_mutex;
yqin70 0:4f77ae831ee7 77 int songnum = 1;
jcrane32 19:d65f9fb1023b 78
jcrane32 19:d65f9fb1023b 79 unsigned short **currentBubbles = NULL;
jcrane32 19:d65f9fb1023b 80 int bubbleNdx = 0;
yqin70 21:cbcbb3480cad 81 volatile float bubblesDrawn = 0;
yqin70 21:cbcbb3480cad 82 volatile float bubblesMissed = 0;
yqin70 21:cbcbb3480cad 83 volatile float bubblesHit = 0;
yqin70 21:cbcbb3480cad 84 int key_code;
yqin70 21:cbcbb3480cad 85 int prev_key_code = 0;
yqin70 21:cbcbb3480cad 86 volatile bool touchpadPressed = false;
jcrane32 19:d65f9fb1023b 87
jcrane32 6:cd24147b5e50 88
yqin70 0:4f77ae831ee7 89 // Thread 1
yqin70 1:a1aa9a79070a 90 // print homescreen to LCD
jcrane32 6:cd24147b5e50 91 void homescreen_thread()
yqin70 0:4f77ae831ee7 92 {
jcrane32 19:d65f9fb1023b 93 while (true) { // thread loop
ShelbyC22 5:b2cf15651d4e 94 lcd_mutex.lock();
yqin70 0:4f77ae831ee7 95 if (homescreen){
yqin70 0:4f77ae831ee7 96 uLCD.cls();
ShelbyC22 5:b2cf15651d4e 97 uLCD.text_height(1.3);
yqin70 2:765774781bb1 98 uLCD.text_width(1.9);
yqin70 0:4f77ae831ee7 99 uLCD.color(WHITE);
ShelbyC22 5:b2cf15651d4e 100 uLCD.locate(5,0);
ShelbyC22 5:b2cf15651d4e 101 uLCD.printf("Tap Tap");
ShelbyC22 5:b2cf15651d4e 102 uLCD.locate(4,1);
ShelbyC22 5:b2cf15651d4e 103 uLCD.printf("Revolution");
ShelbyC22 5:b2cf15651d4e 104 uLCD.locate(0,3);
jcrane32 7:15fdc55dbf66 105 uLCD.printf("Pick a song!");
ShelbyC22 5:b2cf15651d4e 106 uLCD.text_height(1.3);
yqin70 2:765774781bb1 107 uLCD.text_width(1.9);
ShelbyC22 5:b2cf15651d4e 108 uLCD.locate(3,5);
ShelbyC22 4:59b73f321c0e 109 uLCD.printf("Fireflies");
ShelbyC22 5:b2cf15651d4e 110 uLCD.locate(3,7);
jcrane32 19:d65f9fb1023b 111 uLCD.printf("Stacy's Mom");
ShelbyC22 5:b2cf15651d4e 112 uLCD.locate(3,9);
jcrane32 7:15fdc55dbf66 113 uLCD.printf("I Write Sins \n \t Not Tragedies");
jcrane32 19:d65f9fb1023b 114 uLCD.filled_circle(6, 43, 4, WHITE); //new selection with circle instead of rectangle
jcrane32 19:d65f9fb1023b 115 homescreen = false;
jcrane32 19:d65f9fb1023b 116 }
jcrane32 19:d65f9fb1023b 117 if (songchange) {
jcrane32 19:d65f9fb1023b 118 for (int i = 0; i < 3; i++){
jcrane32 19:d65f9fb1023b 119 uLCD.filled_circle(6, i*15+43, 4,BLACK);
ShelbyC22 5:b2cf15651d4e 120 }
jcrane32 19:d65f9fb1023b 121 uLCD.filled_circle(6, (songnum-1)*15+43, 4,WHITE);
ShelbyC22 5:b2cf15651d4e 122 songchange = false;
jcrane32 19:d65f9fb1023b 123 }
jcrane32 19:d65f9fb1023b 124
jcrane32 19:d65f9fb1023b 125 lcd_mutex.unlock();
jcrane32 20:7d56cdcbc9a5 126 Thread::wait(500);
yqin70 0:4f77ae831ee7 127 }
yqin70 0:4f77ae831ee7 128 }
yqin70 0:4f77ae831ee7 129
jcrane32 6:cd24147b5e50 130 // Thread 2
jcrane32 7:15fdc55dbf66 131 // joystick control for song selection during homescreen
jcrane32 19:d65f9fb1023b 132 void joystick_thread() {
jcrane32 19:d65f9fb1023b 133 while (true) {
jcrane32 19:d65f9fb1023b 134 if (!playing) { // we dont want to be able to change the song if we're playing
yqin70 0:4f77ae831ee7 135 if ((joy_pot <= (1.4/3.3)) && songnum>1) {
jcrane32 19:d65f9fb1023b 136 songnum--;
jcrane32 19:d65f9fb1023b 137 songchange = true;
jcrane32 19:d65f9fb1023b 138 } else if ((joy_pot >= (1.8/3.3)) && songnum<3){
yqin70 0:4f77ae831ee7 139 songnum++;
ShelbyC22 5:b2cf15651d4e 140 songchange = true;
jcrane32 19:d65f9fb1023b 141 }
jcrane32 19:d65f9fb1023b 142 Thread::wait(250);
jcrane32 19:d65f9fb1023b 143 }
jcrane32 19:d65f9fb1023b 144
yqin70 0:4f77ae831ee7 145 }
yqin70 0:4f77ae831ee7 146 }
yqin70 0:4f77ae831ee7 147
yqin70 0:4f77ae831ee7 148 // Thread 3
jcrane32 19:d65f9fb1023b 149 // reads pushbuttons to control menu
jcrane32 6:cd24147b5e50 150 void pbcontrol_thread()
yqin70 0:4f77ae831ee7 151 {
jcrane32 19:d65f9fb1023b 152 // The pb variables will be zero when the pushbutton is pressed
jcrane32 19:d65f9fb1023b 153 pb1.mode(PullUp); //pb1 is to select song
jcrane32 19:d65f9fb1023b 154 pb2.mode(PullUp); //pb2 is to return to homescreen
yqin70 0:4f77ae831ee7 155 while(true) { // thread loop
jcrane32 19:d65f9fb1023b 156 if (!pb2) {
yqin70 0:4f77ae831ee7 157 homescreen = true;
yqin70 0:4f77ae831ee7 158 songselect = false;
jcrane32 19:d65f9fb1023b 159 playing = false;
jcrane32 19:d65f9fb1023b 160 }
jcrane32 19:d65f9fb1023b 161 if (!pb1) {
yqin70 0:4f77ae831ee7 162 songselect = true;
jcrane32 19:d65f9fb1023b 163 }
jcrane32 19:d65f9fb1023b 164 Thread::wait(100);
yqin70 0:4f77ae831ee7 165 }
yqin70 0:4f77ae831ee7 166 }
yqin70 0:4f77ae831ee7 167
jcrane32 7:15fdc55dbf66 168 // Thread 4
jcrane32 19:d65f9fb1023b 169 // control LED effects
jcrane32 19:d65f9fb1023b 170 void LED_thread() {
jcrane32 19:d65f9fb1023b 171 while (true) {
jcrane32 19:d65f9fb1023b 172 if (!playing) { //spin LED colors at start menu
jcrane32 19:d65f9fb1023b 173 myRGBled = red;
jcrane32 19:d65f9fb1023b 174 Thread::wait(200);
jcrane32 19:d65f9fb1023b 175 myRGBled = yellow;
jcrane32 19:d65f9fb1023b 176 Thread::wait(200);
jcrane32 19:d65f9fb1023b 177 myRGBled = orange;
jcrane32 19:d65f9fb1023b 178 Thread::wait(200);
jcrane32 19:d65f9fb1023b 179 myRGBled = blue;
jcrane32 19:d65f9fb1023b 180 Thread::wait(200);
jcrane32 19:d65f9fb1023b 181 myRGBled = green;
jcrane32 19:d65f9fb1023b 182 Thread::wait(200);
jcrane32 19:d65f9fb1023b 183 } else if (playing) {
jcrane32 20:7d56cdcbc9a5 184 //lcd_mutex.lock();
jcrane32 20:7d56cdcbc9a5 185 // uLCD.printf("%d", bubblesMissed);
jcrane32 20:7d56cdcbc9a5 186 // lcd_mutex.unlock();
yqin70 21:cbcbb3480cad 187 if (bubblesHit <= 1) {
yqin70 21:cbcbb3480cad 188 myRGBled = red;
yqin70 21:cbcbb3480cad 189 } else if (bubblesHit <= 3) {
jcrane32 20:7d56cdcbc9a5 190 myRGBled = blue;
jcrane32 19:d65f9fb1023b 191 } else {
yqin70 21:cbcbb3480cad 192 myRGBled = green;
jcrane32 19:d65f9fb1023b 193 }
jcrane32 19:d65f9fb1023b 194 Thread::wait(300);
jcrane32 19:d65f9fb1023b 195
jcrane32 19:d65f9fb1023b 196 }
jcrane32 19:d65f9fb1023b 197 }
jcrane32 19:d65f9fb1023b 198 }
jcrane32 19:d65f9fb1023b 199
jcrane32 19:d65f9fb1023b 200 // Thread 5
jcrane32 7:15fdc55dbf66 201 // this thread plays music using the ticker class
jcrane32 7:15fdc55dbf66 202 void playsound() {
jcrane32 7:15fdc55dbf66 203 if (playing) {
jcrane32 19:d65f9fb1023b 204 DACout.write(playingSong[songNdx++] / 255.0);
jcrane32 19:d65f9fb1023b 205 if (songNdx>120000) {
jcrane32 19:d65f9fb1023b 206 playing = false;
jcrane32 19:d65f9fb1023b 207 songNdx = 0;
jcrane32 19:d65f9fb1023b 208 }
jcrane32 7:15fdc55dbf66 209 } else {
jcrane32 19:d65f9fb1023b 210 DACout.write(0.0);
jcrane32 7:15fdc55dbf66 211 songNdx = 0;
jcrane32 7:15fdc55dbf66 212 }
jcrane32 7:15fdc55dbf66 213 }
jcrane32 7:15fdc55dbf66 214
jcrane32 19:d65f9fb1023b 215 void pickSong() {
jcrane32 19:d65f9fb1023b 216 switch (songnum) {
jcrane32 19:d65f9fb1023b 217 case 1:
jcrane32 19:d65f9fb1023b 218 {
jcrane32 19:d65f9fb1023b 219 playingSong = (unsigned char *) fireflies;
jcrane32 19:d65f9fb1023b 220 playing = true;
jcrane32 19:d65f9fb1023b 221 break;
jcrane32 19:d65f9fb1023b 222 }
jcrane32 19:d65f9fb1023b 223
jcrane32 19:d65f9fb1023b 224 case 2:
jcrane32 19:d65f9fb1023b 225 {
jcrane32 19:d65f9fb1023b 226 playingSong = (unsigned char *) stacys_mom;
jcrane32 19:d65f9fb1023b 227 playing = true;
jcrane32 19:d65f9fb1023b 228 break;
jcrane32 19:d65f9fb1023b 229 }
jcrane32 19:d65f9fb1023b 230 case 3:
jcrane32 19:d65f9fb1023b 231 {
jcrane32 19:d65f9fb1023b 232 playingSong = (unsigned char *) sins;
jcrane32 19:d65f9fb1023b 233 playing = true;
jcrane32 19:d65f9fb1023b 234 break;
jcrane32 19:d65f9fb1023b 235 }
jcrane32 19:d65f9fb1023b 236 default:
jcrane32 19:d65f9fb1023b 237 break;
jcrane32 19:d65f9fb1023b 238 }
jcrane32 19:d65f9fb1023b 239 }
jcrane32 19:d65f9fb1023b 240
jcrane32 19:d65f9fb1023b 241
jcrane32 19:d65f9fb1023b 242
jcrane32 19:d65f9fb1023b 243 void showScore () {
yqin70 21:cbcbb3480cad 244
yqin70 21:cbcbb3480cad 245 lcd_mutex.lock();
yqin70 21:cbcbb3480cad 246 uLCD.cls();
jcrane32 19:d65f9fb1023b 247 uLCD.color(0x00FF00); // Change text color and set up menu
jcrane32 19:d65f9fb1023b 248 uLCD.text_bold(ON);
jcrane32 19:d65f9fb1023b 249 uLCD.locate(1,1);
jcrane32 19:d65f9fb1023b 250 uLCD.text_width(2); //4X size text
jcrane32 19:d65f9fb1023b 251 uLCD.text_height(2);
jcrane32 19:d65f9fb1023b 252 uLCD.printf("Thanks for playing Tap Tap Revolution!");
jcrane32 19:d65f9fb1023b 253 // winSound();
jcrane32 19:d65f9fb1023b 254 wait(1.5);
yqin70 21:cbcbb3480cad 255 uLCD.cls();
jcrane32 19:d65f9fb1023b 256 uLCD.text_width(1); //4X size text
jcrane32 19:d65f9fb1023b 257 uLCD.text_height(1);
jcrane32 19:d65f9fb1023b 258 uLCD.text_bold(OFF);
jcrane32 19:d65f9fb1023b 259 uLCD.color(0xFFFFFF);
jcrane32 19:d65f9fb1023b 260 uLCD.locate(2,8);
jcrane32 19:d65f9fb1023b 261 uLCD.printf("Your Score:");
jcrane32 19:d65f9fb1023b 262 uLCD.color(0x00FF00);
jcrane32 19:d65f9fb1023b 263 uLCD.locate(2,10);
jcrane32 19:d65f9fb1023b 264
yqin70 21:cbcbb3480cad 265 float scorePercent = ((bubblesHit) / bubblesDrawn) * 100.0;
yqin70 21:cbcbb3480cad 266 uLCD.printf("Bubbles Hit %4.2f \n", bubblesHit);
yqin70 21:cbcbb3480cad 267 uLCD.printf("Bubbles Drawn %4.2f \n", bubblesDrawn);
yqin70 21:cbcbb3480cad 268 uLCD.printf("Your Score: %4.2f % \n", scorePercent);
yqin70 21:cbcbb3480cad 269 bubblesHit = 0;
yqin70 21:cbcbb3480cad 270 bubblesDrawn = 0;
yqin70 21:cbcbb3480cad 271 //uLCD.printf("%d %", scorePercent);
yqin70 21:cbcbb3480cad 272 wait(10);
yqin70 14:6f5432bb44c0 273 uLCD.cls();
jcrane32 19:d65f9fb1023b 274 uLCD.locate(5,0);
yqin70 21:cbcbb3480cad 275 lcd_mutex.unlock();
jcrane32 19:d65f9fb1023b 276 Thread::wait(2000);
jcrane32 19:d65f9fb1023b 277 }
yqin70 21:cbcbb3480cad 278 //touchpad interrupt code
yqin70 21:cbcbb3480cad 279 void fallInterrupt()
yqin70 21:cbcbb3480cad 280 {
yqin70 21:cbcbb3480cad 281 prev_key_code = key_code;
yqin70 21:cbcbb3480cad 282 key_code=0;
yqin70 21:cbcbb3480cad 283 int i=0;
yqin70 21:cbcbb3480cad 284 int value=mpr121.read(0x00);
yqin70 21:cbcbb3480cad 285 value +=mpr121.read(0x01)<<8;
yqin70 21:cbcbb3480cad 286 i=0;
yqin70 21:cbcbb3480cad 287 //acquires value
yqin70 21:cbcbb3480cad 288 for (i=0; i<12; i++) {
yqin70 21:cbcbb3480cad 289 if (((value>>i)&0x01)==1) key_code=i+1;
yqin70 21:cbcbb3480cad 290 }
yqin70 21:cbcbb3480cad 291 touchpadPressed = true;
yqin70 21:cbcbb3480cad 292 }
jcrane32 20:7d56cdcbc9a5 293 int main() {
jcrane32 20:7d56cdcbc9a5 294 //<<<<<<< working copy
jcrane32 20:7d56cdcbc9a5 295 //
yqin70 21:cbcbb3480cad 296 uLCD.baudrate(3000000); // set baud rate to max
jcrane32 20:7d56cdcbc9a5 297 //
jcrane32 20:7d56cdcbc9a5 298 //=======
jcrane32 20:7d56cdcbc9a5 299 // interrupt.fall(&fallInterrupt);
jcrane32 20:7d56cdcbc9a5 300 // interrupt.mode(PullUp); //for touch keypad
jcrane32 20:7d56cdcbc9a5 301 // lcd_mutex.lock();
jcrane32 20:7d56cdcbc9a5 302 // uLCD.baudrate(3000000);
yqin70 21:cbcbb3480cad 303 uLCD.cls();
yqin70 21:cbcbb3480cad 304 uLCD.media_init();
yqin70 21:cbcbb3480cad 305 uLCD.set_sector_address(0x0077, 0x4002);
yqin70 21:cbcbb3480cad 306 uLCD.display_video(0,0);
jcrane32 20:7d56cdcbc9a5 307 // wait(4);
jcrane32 20:7d56cdcbc9a5 308 // lcd_mutex.unlock();
jcrane32 20:7d56cdcbc9a5 309 //>>>>>>> merge rev
jcrane32 20:7d56cdcbc9a5 310 // seed random
jcrane32 20:7d56cdcbc9a5 311 srand (time(NULL));
yqin70 21:cbcbb3480cad 312
jcrane32 6:cd24147b5e50 313 thread1.start(homescreen_thread);
jcrane32 7:15fdc55dbf66 314 thread2.start(joystick_thread);
jcrane32 6:cd24147b5e50 315 thread3.start(pbcontrol_thread);
jcrane32 19:d65f9fb1023b 316 thread4.start(LED_thread);
jcrane32 19:d65f9fb1023b 317 nextsample.attach(&playsound, 1.0/8000.0);
jcrane32 19:d65f9fb1023b 318
jcrane32 19:d65f9fb1023b 319 Lane *bubbleLanes[3];
jcrane32 19:d65f9fb1023b 320 // these positions may need to be adjusted as I assume they're positioned incorrectly...
jcrane32 20:7d56cdcbc9a5 321 bubbleLanes[0] = new Lane(20,15);
jcrane32 20:7d56cdcbc9a5 322 bubbleLanes[1] = new Lane(60,15);
jcrane32 20:7d56cdcbc9a5 323 bubbleLanes[2] = new Lane(100,15);
yqin70 21:cbcbb3480cad 324 interrupt.fall(&fallInterrupt);
yqin70 21:cbcbb3480cad 325 interrupt.mode(PullUp);
jcrane32 19:d65f9fb1023b 326 //startup sound?
jcrane32 19:d65f9fb1023b 327
jcrane32 19:d65f9fb1023b 328 while(true) {
jcrane32 19:d65f9fb1023b 329 if (songselect) {
jcrane32 19:d65f9fb1023b 330 homescreen = false;
jcrane32 19:d65f9fb1023b 331 // lcd_mutex.lock();
jcrane32 19:d65f9fb1023b 332 // uLCD.cls();
jcrane32 19:d65f9fb1023b 333 // uLCD.printf("You selected song %2d", songnum);
jcrane32 19:d65f9fb1023b 334 // lcd_mutex.unlock();
jcrane32 19:d65f9fb1023b 335 pickSong();
jcrane32 20:7d56cdcbc9a5 336 homescreen = false;
jcrane32 19:d65f9fb1023b 337
jcrane32 19:d65f9fb1023b 338 // "playing" is updated by playsound() thread
jcrane32 20:7d56cdcbc9a5 339 bubbleLanes[0]->add();
jcrane32 20:7d56cdcbc9a5 340 // bubbleLanes[1]->add();
jcrane32 20:7d56cdcbc9a5 341 // bubbleLanes[2]->add();
jcrane32 20:7d56cdcbc9a5 342 bubbleNdx = 0;
jcrane32 20:7d56cdcbc9a5 343 bool runninggame = true;
jcrane32 20:7d56cdcbc9a5 344 bubblesMissed = 0;
jcrane32 20:7d56cdcbc9a5 345 lcd_mutex.lock();
jcrane32 20:7d56cdcbc9a5 346 uLCD.cls();
jcrane32 20:7d56cdcbc9a5 347 lcd_mutex.unlock();
jcrane32 20:7d56cdcbc9a5 348 while (runninggame) {
jcrane32 20:7d56cdcbc9a5 349
jcrane32 19:d65f9fb1023b 350 // draw unufilled circles at the bottom of the screen where the user should "tap"
yqin70 1:a1aa9a79070a 351 lcd_mutex.lock();
jcrane32 20:7d56cdcbc9a5 352 uLCD.circle(20,110, 10, GREEN);
jcrane32 20:7d56cdcbc9a5 353 uLCD.circle(60,110, 10, GREEN);
jcrane32 20:7d56cdcbc9a5 354 uLCD.circle(100,110, 10, GREEN);
yqin70 1:a1aa9a79070a 355 lcd_mutex.unlock();
jcrane32 19:d65f9fb1023b 356
jcrane32 19:d65f9fb1023b 357 // add bubbles to lanes
jcrane32 20:7d56cdcbc9a5 358 // uLCD.printf("%d", fireflies_bubbles[bubbleNdx][0]);
jcrane32 20:7d56cdcbc9a5 359 /*if (songNdx > fireflies_bubbles[bubbleNdx][0]) { // check if it's time to load a bubble
jcrane32 19:d65f9fb1023b 360 bool addedbubbles = false;
jcrane32 20:7d56cdcbc9a5 361 if (fireflies_bubbles[bubbleNdx][2]) { // check if add bubble at ndx
jcrane32 19:d65f9fb1023b 362 bubbleLanes[0]->add();
jcrane32 19:d65f9fb1023b 363 addedbubbles = true;
jcrane32 19:d65f9fb1023b 364 }
jcrane32 19:d65f9fb1023b 365
jcrane32 20:7d56cdcbc9a5 366 if (fireflies_bubbles[bubbleNdx][3]) { // check if add bubble at ndx
jcrane32 19:d65f9fb1023b 367 bubbleLanes[1]->add();
jcrane32 19:d65f9fb1023b 368 addedbubbles = true;
jcrane32 19:d65f9fb1023b 369 }
jcrane32 19:d65f9fb1023b 370
jcrane32 20:7d56cdcbc9a5 371 if (fireflies_bubbles[bubbleNdx][4]) { // check if add bubble at ndx
jcrane32 19:d65f9fb1023b 372 bubbleLanes[2]->add();
jcrane32 19:d65f9fb1023b 373 addedbubbles = true;
jcrane32 19:d65f9fb1023b 374 }
jcrane32 19:d65f9fb1023b 375
jcrane32 20:7d56cdcbc9a5 376 if (addedbubbles) ++bubbleNdx;
jcrane32 20:7d56cdcbc9a5 377 }*/
jcrane32 20:7d56cdcbc9a5 378
jcrane32 20:7d56cdcbc9a5 379 if (playing && ((rand() % 99) < 20)) { // 20% chance of inserting a bubble
jcrane32 20:7d56cdcbc9a5 380 bubbleLanes[rand() % 3]->add();
ShelbyC22 4:59b73f321c0e 381 }
jcrane32 19:d65f9fb1023b 382
jcrane32 19:d65f9fb1023b 383 // draw all bubbles
jcrane32 20:7d56cdcbc9a5 384 lcd_mutex.lock();
jcrane32 19:d65f9fb1023b 385 bubbleLanes[0]->draw();
jcrane32 19:d65f9fb1023b 386 bubbleLanes[1]->draw();
jcrane32 19:d65f9fb1023b 387 bubbleLanes[2]->draw();
yqin70 21:cbcbb3480cad 388 uLCD.locate(0,0);
yqin70 21:cbcbb3480cad 389 uLCD.printf("%4.2f", bubblesHit);
jcrane32 20:7d56cdcbc9a5 390 lcd_mutex.unlock();
jcrane32 19:d65f9fb1023b 391
yqin70 21:cbcbb3480cad 392
jcrane32 19:d65f9fb1023b 393 // TODO: check for hits with capacitive touchpad
jcrane32 19:d65f9fb1023b 394 // if there is a hit, delete the bubble from the dynamic array
jcrane32 20:7d56cdcbc9a5 395 // replace "true" in the next 3 lines with your logic of when to check for a hit
yqin70 21:cbcbb3480cad 396 //if touchpad has been pressed, checks if bubble in right spot
yqin70 21:cbcbb3480cad 397 if(touchpadPressed){
yqin70 21:cbcbb3480cad 398 //lcd_mutex.lock();
yqin70 21:cbcbb3480cad 399 //uLCD.printf("%d", key_code);
yqin70 21:cbcbb3480cad 400 //lcd_mutex.unlock();
yqin70 21:cbcbb3480cad 401 //pc.printf("Pressed/n");
yqin70 21:cbcbb3480cad 402 if (key_code == 12) bubblesHit += bubbleLanes[0]->checkHit();
yqin70 21:cbcbb3480cad 403 if (key_code == 8) bubblesHit += bubbleLanes[1]->checkHit();
yqin70 21:cbcbb3480cad 404 if (key_code == 4) bubblesHit += bubbleLanes[2]->checkHit();
yqin70 21:cbcbb3480cad 405 }
yqin70 21:cbcbb3480cad 406 touchpadPressed = false;
jcrane32 19:d65f9fb1023b 407
jcrane32 19:d65f9fb1023b 408 // move down all the bubbles, deleting bubbles that are out of bounds if required
jcrane32 19:d65f9fb1023b 409 bubbleLanes[0]->moveDown();
jcrane32 19:d65f9fb1023b 410 bubbleLanes[1]->moveDown();
jcrane32 19:d65f9fb1023b 411 bubbleLanes[2]->moveDown();
jcrane32 19:d65f9fb1023b 412
jcrane32 20:7d56cdcbc9a5 413 if (!playing && bubbleLanes[0]->isEmpty() && bubbleLanes[1]->isEmpty() && bubbleLanes[2]->isEmpty()) {
jcrane32 20:7d56cdcbc9a5 414 runninggame = false;
ShelbyC22 4:59b73f321c0e 415 }
jcrane32 20:7d56cdcbc9a5 416
jcrane32 19:d65f9fb1023b 417 //this wait delay may need to be tuned
jcrane32 19:d65f9fb1023b 418 Thread::wait(100);
ShelbyC22 4:59b73f321c0e 419 }
jcrane32 19:d65f9fb1023b 420
jcrane32 19:d65f9fb1023b 421 // uncomment this when gameplay works
yqin70 21:cbcbb3480cad 422 showScore();
jcrane32 19:d65f9fb1023b 423
jcrane32 19:d65f9fb1023b 424 // return to homescreen when
jcrane32 19:d65f9fb1023b 425 songselect = false;
jcrane32 19:d65f9fb1023b 426 homescreen = true;
yqin70 0:4f77ae831ee7 427 }
jcrane32 19:d65f9fb1023b 428 Thread::wait(100);
jcrane32 19:d65f9fb1023b 429 }
yqin70 0:4f77ae831ee7 430 }