Whack a Mole game! Features: - LCD graphics display - Touch pad input - Speaker effects through a class D audio amplifier\ - A high score page maintained by the SD card file system - Analog noise used to seed random numbers
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed
Fork of MPR121_Demo by
Diff: main.cpp
- Revision:
- 9:867dde4be88a
- Parent:
- 8:e6e1e5b3f2a9
- Child:
- 10:a31802412ab6
diff -r e6e1e5b3f2a9 -r 867dde4be88a main.cpp --- a/main.cpp Mon Mar 14 16:23:47 2016 +0000 +++ b/main.cpp Mon Mar 14 18:50:39 2016 +0000 @@ -27,18 +27,22 @@ #include <mpr121.h> #include "SDFileSystem.h" #include "SongPlayer.h" + +//initialize lcd uLCD_4DGL lcd(p9, p10, p30); // tx, rx, reset DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); + +//setup analog in pin to read noise for random number generation AnalogIn Ain(p15); //Setup class D audio output on pwm out pin SongPlayer mySpeaker(p21); -// Create the interrupt receiver object on pin 26 +//Create the interrupt receiver object on pin 26 InterruptIn interrupt(p26); //Setup the Serial to the PC for debugging @@ -90,8 +94,8 @@ //Check to see if the a mole is hit after an interupt if (value==256&&pos1==1) { - mySpeaker.PlaySong(note,duration); - score++; + mySpeaker.PlaySong(note,duration);//play note to indicate successful hit + score++;//increment score by one pos1 = 0;//set the position to 0 after succesful hit to prevent multiple hits } if (value==16&&pos2==1) @@ -124,22 +128,12 @@ score++; pos6 = 0; } - //pc.printf("MPR value: %x \r\n", value); //debug code to the COM port that prints MPR value - //i=0; - // puts key number out to LEDs for demo - /* for (i=0; i<12; i++) { - if (((value>>i)&0x01)==1) key_code=i+1; - } - led4=key_code & 0x01; - led3=(key_code>>1) & 0x01; - led2=(key_code>>2) & 0x01; - led1=(key_code>>3) & 0x01;*/ } -float arand; -unsigned int state; +float arand;// variable to store analog noise +unsigned int state; // global state variable int main() { -lcd.baudrate(300000); +lcd.baudrate(300000);// set lcd baudrate int out = 0xFF0000;// outline color int fill = 0x0000;// fill color. aka no mole @@ -153,21 +147,22 @@ int rmole=18;//radius mole int mole= 0xD3D3D3;//mole color like gray -lcd.circle(col1, row1 , r, out);// all empty +lcd.circle(col1, row1 , r, out);// draws 6 circles to the lcd screen to show where the moles will appear lcd.circle(col1, row2 , r, out); lcd.circle(col2, row1 , r, out); lcd.circle(col2, row2 , r, out); lcd.circle(col3, row1 , r, out); lcd.circle(col3, row2 , r, out); - pc.printf("\nHello from the mbed & mpr121\n\r"); +// Begin test code for mpr121 +pc.printf("\nHello from the mbed & mpr121\n\r"); - unsigned char dataArray[2]; - int key; - int count = 0; +unsigned char dataArray[2]; +int key; +int count = 0; - pc.printf("Test 1: read a value: \r\n"); - dataArray[0] = mpr121.read(AFE_CFG); +pc.printf("Test 1: read a value: \r\n"); +dataArray[0] = mpr121.read(AFE_CFG); pc.printf("Read value=%x\r\n\n",dataArray[0]); pc.printf("Test 2: read a value: \r\n"); @@ -208,20 +203,31 @@ // mpr121.setProximityMode(true); pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG)); +// end test code of mpr121 - interrupt.fall(&fallInterrupt); + + interrupt.fall(&fallInterrupt);//set up the interrupt mode interrupt.mode(PullUp); - int counter = 0; - srand(state); + + int counter = 0;// initialize the variable to count the number of transitions per game + + //read in a psuedo random noise reading and multiply so that it can later be cast to int by truncation + arand = Ain.read()*1000000; + + //seed rand with noise reading + srand(arand); + + //game runs for transitions while (counter < 30) { - arand = Ain.read()*1000000; - state = arand; + //determine state using rand() state = rand()%15; - pc.printf("%i\n\r",state); + + //switch the value of state to determine the output to the lcd switch(state) { case 0: + //mutex functionality can be incorperated my uncommenting //POS_MUTEX.un/lock() //POS_MUTEX.lock(); pos1=1; pos2=0; @@ -231,13 +237,13 @@ pos6=0; //POS_MUTEX.unlock(); pc.printf("%i should be 0 \n\r",state); -lcd.filled_circle(col1, row1 , rmole, fill);// 1 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col1, row1 , rmole, mole); + lcd.filled_circle(col1, row1 , rmole, fill);// 1 + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col1, row1 , rmole, mole); break; case 1: //POS_MUTEX.lock(); @@ -249,13 +255,13 @@ pos6=0; //POS_MUTEX.unlock(); pc.printf("%i should be 1 \n\r",state); -lcd.filled_circle(col1, row1 , rmole, fill);// 2 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, mole); + lcd.filled_circle(col1, row1 , rmole, fill);// 2 + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, mole); break; case 2: //POS_MUTEX.lock(); @@ -268,12 +274,12 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 2 \n\r",state); lcd.filled_circle(col1, row1 , rmole, fill);// 3 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, mole); break; case 3: //POS_MUTEX.lock(); @@ -286,12 +292,12 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 3 \n\r",state); lcd.filled_circle(col1, row1 , rmole, fill);// 4 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col1, row2 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col1, row2 , rmole, mole); break; case 4: //POS_MUTEX.lock(); @@ -304,12 +310,12 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 4 \n\r",state); lcd.filled_circle(col1, row1 , rmole, fill);// 5 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, mole); break; case 5: //POS_MUTEX.lock(); @@ -322,13 +328,13 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 5 \n\r",state); -lcd.filled_circle(col1, row1 , rmole, fill);// 6 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, mole); + lcd.filled_circle(col1, row1 , rmole, fill);// 6 + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, mole); break; case 6: //POS_MUTEX.lock(); @@ -341,13 +347,13 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 6 \n\r",state); lcd.filled_circle(col1, row1 , rmole, fill);// 2,6 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, mole); -lcd.filled_circle(col3, row2 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, mole); + lcd.filled_circle(col3, row2 , rmole, mole); break; case 7: //POS_MUTEX.lock(); @@ -360,13 +366,13 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 7 \n\r",state); lcd.filled_circle(col1, row1 , rmole, fill);// 1,3 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col1, row1 , rmole, mole); -lcd.filled_circle(col3, row1 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col1, row1 , rmole, mole); + lcd.filled_circle(col3, row1 , rmole, mole); break; case 8: //POS_MUTEX.lock(); @@ -379,15 +385,15 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 8 \n\r",state); -lcd.filled_circle(col1, row1 , rmole, fill);// 2,4,6 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, mole); -lcd.filled_circle(col2, row2 , rmole, mole); -lcd.filled_circle(col3, row2 , rmole, mole); + lcd.filled_circle(col1, row1 , rmole, fill);// 2,4,6 + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, mole); + lcd.filled_circle(col3, row2 , rmole, mole); break; case 9: //POS_MUTEX.lock(); @@ -400,14 +406,14 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 9 \n\r",state); lcd.filled_circle(col1, row1 , rmole, fill);// 3,4,6 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, mole); -lcd.filled_circle(col1, row2 , rmole, mole); -lcd.filled_circle(col3, row2 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, mole); + lcd.filled_circle(col3, row2 , rmole, mole); break; case 10: //POS_MUTEX.lock(); @@ -420,13 +426,13 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 10 \n\r",state); lcd.filled_circle(col1, row1 , rmole, fill);// 1,6 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col1, row1 , rmole, mole); -lcd.filled_circle(col3, row2 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col1, row1 , rmole, mole); + lcd.filled_circle(col3, row2 , rmole, mole); break; case 11: //POS_MUTEX.lock(); @@ -439,15 +445,15 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 11 \n\r",state); lcd.filled_circle(col1, row1 , rmole, fill);// 2,3,5,6 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, mole); -lcd.filled_circle(col3, row1 , rmole, mole); -lcd.filled_circle(col2, row2 , rmole, mole); -lcd.filled_circle(col3, row2 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, mole); + lcd.filled_circle(col3, row1 , rmole, mole); + lcd.filled_circle(col2, row2 , rmole, mole); + lcd.filled_circle(col3, row2 , rmole, mole); break; case 12: //POS_MUTEX.lock(); @@ -460,13 +466,13 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 12 \n\r",state); lcd.filled_circle(col1, row1 , rmole, fill);// 3,5 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, mole); -lcd.filled_circle(col2, row2 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, mole); + lcd.filled_circle(col2, row2 , rmole, mole); break; case 13: //POS_MUTEX.lock(); @@ -479,14 +485,14 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 13 \n\r",state); lcd.filled_circle(col1, row1 , rmole, fill);// 1,4,5 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col1, row1 , rmole, mole); -lcd.filled_circle(col1, row2 , rmole, mole); -lcd.filled_circle(col2, row2 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col1, row1 , rmole, mole); + lcd.filled_circle(col1, row2 , rmole, mole); + lcd.filled_circle(col2, row2 , rmole, mole); break; case 14: //POS_MUTEX.lock(); @@ -499,55 +505,24 @@ //POS_MUTEX.unlock(); pc.printf("%i should be 14 \n\r",state); -lcd.filled_circle(col1, row1 , rmole, fill);// 2,5 -lcd.filled_circle(col1, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, fill); -lcd.filled_circle(col2, row2 , rmole, fill); -lcd.filled_circle(col3, row1 , rmole, fill); -lcd.filled_circle(col3, row2 , rmole, fill); -lcd.filled_circle(col2, row1 , rmole, mole); -lcd.filled_circle(col2, row2 , rmole, mole); + lcd.filled_circle(col1, row1 , rmole, fill);// 2,5 + lcd.filled_circle(col1, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, fill); + lcd.filled_circle(col2, row2 , rmole, fill); + lcd.filled_circle(col3, row1 , rmole, fill); + lcd.filled_circle(col3, row2 , rmole, fill); + lcd.filled_circle(col2, row1 , rmole, mole); + lcd.filled_circle(col2, row2 , rmole, mole); break; } wait(0.5); - counter++; - } - lcd.cls(); - wait(3); - lcd.cls(); - /* mkdir("/sd/mydir", 0777); - FILE *fp = fopen("/sd/mydir/highscore.txt", "w"); - pc.printf("Could not open file for write\n"); - - fprintf(fp,"titties"); - fclose(fp); - - lcd.printf("The high schore is %s, - lcd.printf("you scored %D",score); - FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); - if(fp == NULL) { - pc.printf("Could not open file for write\n"); + counter++; // increment counter and run the "state machine again" } - fprintf(fp, "Hello fun SD Card World!"); - fclose(fp);*//* - char str [80]; - mkdir("/sd/mydir", 0777); - FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); - if(fp == NULL) { - pc.printf("Could not open file for write\n"); - } - fprintf(fp, "%d is the high score score); - fclose(fp); FILE *fp1 = fopen("/sd/mydir/sdtest.txt", "r"); - if(fp1 == NULL) - { - pc.printf("Could not open file for read\n"); - } - pc.printf("Opened file for read\n\r"); - fscanf (fp1, "%s", str); - pc.printf ("I have read: %s \n",str); - fclose(fp1); - - pc.printf("Goodbye World!\n");*/ + // clear the lcd screen + lcd.cls(); + wait(0.5); + lcd.cls(); + // int variable to read current high score int please; mkdir("/sd/mydir", 0777); FILE *fp1 = fopen("/sd/mydir/sdtest.txt", "r"); @@ -584,27 +559,11 @@ lcd.printf("You did not set\n\n"); lcd.printf("A new high score\n\n"); lcd.printf("Press reset to\n\n"); - lcd.printf("Try again)"); + lcd.printf("Try again"); } - -/* - FILE *fp2 = fopen("/sd/mydir/sdtest.txt", "r"); - if(fp2 == NULL) - { - error("Could not open file for read\n"); - } - pc.printf("Opened file for read\n\r"); - fscanf (fp2, "%s", str); - sscanf(str, "%d", &please); - pc.printf ("I have read: %s \n",str); - fclose(fp2); - if (please == 5) - { - pc.printf("we got it\n"); - }*/ - pc.printf("Goodbye World!\n"); - }//end +pc.printf("Goodbye World!\n"); +}//end