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

Files at this revision

API Documentation at this revision

Comitter:
tpettet3
Date:
Mon Mar 14 05:36:09 2016 +0000
Parent:
4:4cb02cde783c
Child:
6:ca03b9c94c88
Commit message:
before speaker;

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
SongPlayer.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Mon Mar 14 05:36:09 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SongPlayer.h	Mon Mar 14 05:36:09 2016 +0000
@@ -0,0 +1,42 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class SongPlayer
+{
+public:
+    SongPlayer(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlaySong(float frequency[], float duration[], float volume=1.0) {
+        vol = volume;
+        notecount = 0;
+        _pin.period(1.0/frequency[notecount]);
+        _pin = volume/2.0;
+        noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
+        // setup timer to interrupt for next note to play
+        frequencyptr = frequency;
+        durationptr = duration;
+        //returns after first note starts to play
+    }
+    void nextnote();
+private:
+    Timeout noteduration;
+    PwmOut _pin;
+    int notecount;
+    float vol;
+    float * frequencyptr;
+    float * durationptr;
+};
+//Interrupt Routine to play next note
+void SongPlayer::nextnote()
+{
+    _pin = 0.0;
+    notecount++; //setup next note in song
+    if (durationptr[notecount]!=0.0) {
+        _pin.period(1.0/frequencyptr[notecount]);
+        noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
+        _pin = vol/2.0;
+    } else
+        _pin = 0.0; //turn off on last note
+}
+   
\ No newline at end of file
--- a/main.cpp	Mon Mar 14 04:44:41 2016 +0000
+++ b/main.cpp	Mon Mar 14 05:36:09 2016 +0000
@@ -25,7 +25,8 @@
 #include <list>
 #include "uLCD_4DGL.h"
 #include <mpr121.h>
-
+#include "SDFileSystem.h"
+#include "SongPlayer.h"
 uLCD_4DGL lcd(p9, p10, p30); // tx, rx, reset
 
 DigitalOut led1(LED1);
@@ -38,10 +39,11 @@
 InterruptIn interrupt(p26);
 // Setup the Serial to the PC for debugging
 Serial pc(USBTX, USBRX);
-
+SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
 // Setup the i2c bus on pins 28 and 27
 I2C i2c(p28, p27);
-
+float note[1]= {1568.0};
+float duration[1]= {0.1};
 int pos1;
 int pos2;
 int pos3;
@@ -55,6 +57,7 @@
 int hole4 = 0x200;
 int hole5 = 0x20;
 int hole6 = 0x2;
+char str [80];
 // Setup the Mpr121:
 // constructor(i2c object, i2c address of the mpr121)
 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
@@ -506,6 +509,31 @@
         //lcd.text_string("What does...", 1, 1, FONT_7X8, GREEN);
         //lcd.text_string("What does...", 1, 8, FONT_7X8, BLUE);
         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");
+        }
+        fprintf(fp, "Hello fun SD Card World!");
+        fclose(fp);*/
+            static const char SampleString[] = "Hello World\n";
+    mkdir("/sd/write_test", 0777);
+    FILE *fp = fopen("/sd/write_test/out.txt", "w");
+    //FILE* fp = fopen("/local/out.txt", "w");
+    if(fp == NULL) 
+    {
+        error("Could not open file for write\n");
+    }
+    fprintf(fp, "Hello from SD Card World!");    
+    fclose(fp); 
+    FILE *fp1 = fopen("/sd/mydir/sdtest.txt", "r"); 
+    if(fp1 == NULL) 
+    { 
+    error("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);
     }