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 jim hamblen

Revision:
1:89fd0d713ffc
Parent:
0:e09703934ff4
Child:
2:d85c5ce4d397
--- a/main.cpp	Wed Mar 16 01:49:13 2011 +0000
+++ b/main.cpp	Mon Mar 14 03:28:28 2016 +0000
@@ -23,23 +23,40 @@
 #include <mbed.h>
 #include <string>
 #include <list>
+#include "uLCD_4DGL.h"
+#include <mpr121.h>
+#include "rtos.h"
 
-#include <mpr121.h>
+uLCD_4DGL lcd(p9, p10, p30); // tx, rx, reset
 
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
+AnalogIn   Ain(p15);
 
 // Create the interrupt receiver object on pin 26
 InterruptIn interrupt(p26);
-
+Mutex POS_MUTTEX;
 // Setup the Serial to the PC for debugging
 Serial pc(USBTX, USBRX);
 
 // Setup the i2c bus on pins 28 and 27
-I2C i2c(p9, p10);
+I2C i2c(p28, p27);
 
+int pos1;
+int pos2;
+int pos3;
+int pos4;
+int pos5;
+int pos6;
+int score = 0;
+int hole1 = 0x100;
+int hole2 = 0x10;
+int hole3 = 0x1;
+int hole4 = 0x200;
+int hole5 = 0x20;
+int hole6 = 0x2;
 // Setup the Mpr121:
 // constructor(i2c object, i2c address of the mpr121)
 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
@@ -50,18 +67,72 @@
     int value=mpr121.read(0x00);
     value +=mpr121.read(0x01)<<8;
     // LED demo mod by J. Hamblen
-    //pc.printf("MPR value: %x \r\n", value);
-    i=0;
+    POS_MUTEX.lock;
+    if (value==hole1&&pos1==1)
+    {
+        score++;
+    }
+    POS_MUTEX.lock;
+    pc.printf("MPR value: %x \r\n", value);
+    /*if (value == 1)
+    {
+        led1 = !led1;
+    }
+        if (value == 2)
+    {
+        led2 = !led2;
+    }
+        if (value == 16)
+    {
+        led3 = !led3;
+    }
+        if (value == 32)
+    {
+        led4 = !led4;
+    }
+    
+            if (value == 256)
+    {
+        led3 = !led3;
+    }
+            if (value == 512)
+    {
+        led4 = !led4;
+    }*/
+    //i=0;
     // puts key number out to LEDs for demo
-    for (i=0; i<12; i++) {
+   /* 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;
+    led1=(key_code>>3) & 0x01;*/
 }
+float arand;
+unsigned int state;
+
 int main() {
+lcd.baudrate(300000);
+    
+int out = 0xFF0000;// outline color
+int fill = 0x0000;// fill color. aka no mole
+int col1 = 21;
+int col2 = 64;
+int col3=107;
+int row1=107;
+int row2=64;
+int row3=21; 
+int r = 20;
+int rmole=18;//radius mole
+int mole= 0xD3D3D3;//mole color like gray
+
+lcd.circle(col1, row1 , r, out);// all empty
+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");
 
@@ -114,12 +185,309 @@
 
     interrupt.fall(&fallInterrupt);
     interrupt.mode(PullUp);
-
-    while (1) {
-        wait(5);
-        pc.printf(".");
+    int counter = 0;
+    while (counter < 5) {
+        arand = Ain.read()*1000000;
+        state = arand;
+        srand(state);
+        state = rand()%15;
+        pc.printf("%i\n\r",state);
+        switch(state)
+        {
+            case 0:
+            Mutex POS_MUTEX.lock;
+            pos1=1;
+            pos2=0;
+            pos3=0;
+            pos4=0;
+            pos5=0;
+            pos6=0;
+            Mutex 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);
+            break;
+            case 1:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=1;
+            pos3=0;
+            pos4=0;
+            pos5=0;
+            pos6=0;
+            Mutex 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);
+            break;
+            case 2:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=0;
+            pos3=1;
+            pos4=0;
+            pos5=0;
+            pos6=0;
+            Mutex 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);
+            break;
+            case 3:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=0;
+            pos3=0;
+            pos4=1;
+            pos5=0;
+            pos6=0;
+            Mutex 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);
+            break;
+            case 4:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=0;
+            pos3=0;
+            pos4=0;
+            pos5=1;
+            pos6=0;
+            Mutex 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);
+            break;
+            case 5:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=0;
+            pos3=0;
+            pos4=0;
+            pos5=0;
+            pos6=1;
+            Mutex 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);
+            break;
+            case 6:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=1;
+            pos3=0;
+            pos4=0;
+            pos5=0;
+            pos6=1;
+            Mutex 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);
+            break;
+            case 7:
+            Mutex POS_MUTEX.lock;
+            pos1=1;
+            pos2=0;
+            pos3=1;
+            pos4=0;
+            pos5=0;
+            pos6=0;
+            Mutex 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);
+            break;
+            case 8:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=1;
+            pos3=0;
+            pos4=1;
+            pos5=0;
+            pos6=1;
+            Mutex 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);
+            break;
+            case 9:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=0;
+            pos3=1;
+            pos4=1;
+            pos5=0;
+            pos6=1;
+            Mutex 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);
+            break;
+            case 10:
+            Mutex POS_MUTEX.lock;
+            pos1=1;
+            pos2=0;
+            pos3=0;
+            pos4=0;
+            pos5=0;
+            pos6=1;
+            Mutex 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);
+            break;
+            case 11:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=1;
+            pos3=1;
+            pos4=0;
+            pos5=1;
+            pos6=1;
+            Mutex 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);
+            break;
+            case 12:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=0;
+            pos3=1;
+            pos4=0;
+            pos5=1;
+            pos6=0;
+            Mutex 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);
+            break;
+            case 13:
+            Mutex POS_MUTEX.lock;
+            pos1=1;
+            pos2=0;
+            pos3=0;
+            pos4=1;
+            pos5=1;
+            pos6=0;
+            Mutex 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);
+            break;
+            case 14:
+            Mutex POS_MUTEX.lock;
+            pos1=0;
+            pos2=1;
+            pos3=0;
+            pos4=0;
+            pos5=1;
+            pos6=0;
+            Mutex 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);
+            break;
+            }
+        wait(2);
+        counter++;
+        }
+        pc.printf("you scored %i \n\r",score);
     }
-}
 
 
 
+