Tap Tap Revenge Mbed Game
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Revision 0:71e79407f6d5, committed 2016-03-16
- Comitter:
- bricecroxton
- Date:
- Wed Mar 16 19:13:19 2016 +0000
- Commit message:
- Tap Tap Revenge Mbed
Changed in this revision
diff -r 000000000000 -r 71e79407f6d5 4DGL-uLCD-SE.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Wed Mar 16 19:13:19 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#2cb1845d7681
diff -r 000000000000 -r 71e79407f6d5 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Wed Mar 16 19:13:19 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/bricecroxton/code/SDFileSystem/#209ed001ffc8
diff -r 000000000000 -r 71e79407f6d5 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Mar 16 19:13:19 2016 +0000 @@ -0,0 +1,319 @@ +#include "mbed.h" +#include "rtos.h" +#include "uLCD_4DGL.h" +#include "SDFileSystem.h" +#include "wave_player.h" +#include "mpr121.h" + +//Arrays for the notes each button will play +int L1[300]; +int L2[300]; +int L3[300]; +int L4[300]; + +int timespace = -6; //count for drawing the circles + +//Flips the LEDs when you hit one of the buttons +DigitalOut myled(LED1); +DigitalOut myled2(LED2); +DigitalOut myled3(LED3); +DigitalOut myled4(LED4); + +AnalogOut DACout(p18); //speaker analog out + +uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; + +SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board + +FILE *wav_file; // the song to play +wave_player player(&DACout); // the player + +SPI spi(p11, p12, p13); //for the ShiftBrite +DigitalOut latch(p15); +DigitalOut enable(p16); + +Mutex lcdMutex; //LCD mutex lock + +InterruptIn interrupt(p21); //interrupt signal for keypad + +// keypad declaration +I2C i2c(p9, p10); +Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); + +int value = 0; //keypad input value +int points = 0; //points +int red = 0; // global values for changing ShiftBrite +int green = 0; + +//function to output to ShiftBrite +void RGB_LED(int red, int green, int blue) { + unsigned int low_color=0; + unsigned int high_color=0; + high_color=(blue<<4)|((red&0x3C0)>>6); + low_color=(((red&0x3F)<<10)|(green)); + spi.write(high_color); + spi.write(low_color); + latch=1; + latch=0; +} + +//Interrupt function +void fallInterrupt(){ + if(timespace > -1){ + value = mpr121.read(0x00); //read keypad + if(((value>>0) & 0x01) == 1) + { + myled = 1 - myled; + if(L4[timespace] == 1) //compare value + { + points+=5; //increment points if correct + red = 0; green = 255; // ShiftBrite is green + } + else + { + red = 255; green = 0; //ShiftBrite is red + } + } + + if(((value>>1) & 0x01) == 1) + { + myled2 = 1 - myled2; + if(L3[timespace] == 1) + { + points+=5; + red = 0; green = 255; + } + else + { + red = 255; green = 0; + } + } + + if(((value>>2) & 0x01) == 1) + { + myled3 = 1 - myled3; + if(L2[timespace] == 1) + { + points+=5; + red = 0; green = 255; + } + else + { + red = 255; green = 0; + } + } + + if(((value>>3) & 0x01) == 1) + { + myled4 = 1 - myled4; + if(L1[timespace] == 1) + { + red = 0; green = 255; + points+=5; + } + else + { + red = 255; green = 0; + } + } + } +} + +void lcdThread1(void const *args) { + while (true) { + lcdMutex.lock(); + timespace += 1; + if (timespace < 256) //if song notes are not done, + { + //delete any circles in the yellow circles + uLCD.filled_circle(110, 20, 12, 0x000000); + uLCD.filled_circle(110, 50, 12, 0x000000); + uLCD.filled_circle(110, 80, 12, 0x000000); + uLCD.filled_circle(110, 110, 12, 0x000000); + + //draw the path of the circles + if (timespace >= 0){ + if (L1[timespace] == 1){ + uLCD.filled_circle(86, 20, 10, 0x000000); + uLCD.filled_circle(110, 20, 12, 0x0000FF);} + if (L2[timespace] == 1){ + uLCD.filled_circle(86, 50, 10, 0x000000); + uLCD.filled_circle(110, 50, 12, 0x00FFFF);} + if (L3[timespace] == 1){ + uLCD.filled_circle(86, 80, 10, 0x000000); + uLCD.filled_circle(110, 80, 12, 0x00FF00);} + if (L4[timespace] == 1){ + uLCD.filled_circle(86, 110, 10, 0x000000); + uLCD.filled_circle(110, 110, 12, 0x008000);} + } + if (timespace >= -1){ + if (L1[timespace+1] == 1){ + uLCD.filled_circle(66, 20, 8, 0x000000); + uLCD.filled_circle(86, 20, 10, 0x0000FF);} + if (L2[timespace+1] == 1){ + uLCD.filled_circle(66, 50, 8, 0x000000); + uLCD.filled_circle(86, 50, 10, 0x00FFFF);} + if (L3[timespace+1] == 1){ + uLCD.filled_circle(66, 80, 8, 0x000000); + uLCD.filled_circle(86, 80, 10, 0x00FF00);} + if (L4[timespace+1] == 1){ + uLCD.filled_circle(66, 110, 8, 0x000000); + uLCD.filled_circle(86, 110, 10, 0x008000);} + } + if (timespace >= -2){ + if (L1[timespace+2] == 1){ + uLCD.filled_circle(50, 20, 6, 0x000000); + uLCD.filled_circle(66, 20, 8, 0x0000FF);} + if (L2[timespace+2] == 1){ + uLCD.filled_circle(50, 50, 6, 0x000000); + uLCD.filled_circle(66, 50, 8, 0x00FFFF);} + if (L3[timespace+2] == 1){ + uLCD.filled_circle(50, 80, 6, 0x000000); + uLCD.filled_circle(66, 80, 8, 0x00FF00);} + if (L4[timespace+2] == 1){ + uLCD.filled_circle(50, 110, 6, 0x000000); + uLCD.filled_circle(66, 110, 8, 0x008000);} + } + if (timespace >= -3){ + if (L1[timespace+3] == 1){ + uLCD.filled_circle(38, 20, 4, 0x000000); + uLCD.filled_circle(50, 20, 6, 0x0000FF);} + if (L2[timespace+3] == 1){ + uLCD.filled_circle(38, 50, 4, 0x000000); + uLCD.filled_circle(50, 50, 6, 0x00FFFF);} + if (L3[timespace+3] == 1){ + uLCD.filled_circle(38, 80, 4, 0x000000); + uLCD.filled_circle(50, 80, 6, 0x00FF00);} + if (L4[timespace+3] == 1){ + uLCD.filled_circle(38, 110, 4, 0x000000); + uLCD.filled_circle(50, 110, 6, 0x008000);} + } + if (timespace >= -4){ + if (L1[timespace+4] == 1){ + uLCD.filled_circle(30, 20, 2, 0x000000); + uLCD.filled_circle(38, 20, 4, 0x0000FF);} + if (L2[timespace+4] == 1){ + uLCD.filled_circle(30, 50, 2, 0x000000); + uLCD.filled_circle(38, 50, 4, 0x00FFFF);} + if (L3[timespace+4] == 1){ + uLCD.filled_circle(30, 80, 2, 0x000000); + uLCD.filled_circle(38, 80, 4, 0x00FF00);} + if (L4[timespace+4] == 1){ + uLCD.filled_circle(30, 110, 2, 0x000000); + uLCD.filled_circle(38, 110, 4, 0x008000);} + } + if (L1[timespace+5] == 1){ + uLCD.filled_circle(30, 20, 2, 0x0000FF);} + if (L2[timespace+5] == 1){ + uLCD.filled_circle(30, 50, 2, 0x00FFFF);} + if (L3[timespace+5] == 1){ + uLCD.filled_circle(30, 80, 2, 0x00FF00);} + if (L4[timespace+5] == 1){ + uLCD.filled_circle(30, 110, 2, 0x008000);} + } + else //when the game is over + { + uLCD.cls(); + uLCD.printf("POINTS: %d\n", points); + if(points > 1000) + { + uLCD.printf("GOOD JOB!"); + } + else + { + uLCD.printf("YOU ARE BAD AT THIS GAME"); + } + } + //draw yellow circles + uLCD.circle(110,20,12, 0xFFFF00); + uLCD.circle(110,50,12, 0xFFFF00); + uLCD.circle(110,80,12, 0xFFFF00); + uLCD.circle(110,110,12, 0xFFFF00); + + lcdMutex.unlock(); + Thread::wait(250); + + } +} + +//Function that plays song. It is declared as a thread in main. +void wavThread(void const *args) { + Thread::wait(1200); + wav_file = fopen("/sd/wavfiles/MyHouseLowQ.wav", "r"); + player.play(wav_file); + while (true) { + Thread::wait(100); + } +} + +//Function that reads the note text file from the SD +void textRead(){ + FILE *fp = fopen("/sd/mydir/myHouse.txt", "r"); + char line[200]; + char one[2]="1"; + char two[2]="2"; + char three[2]="3"; + char four[2]="4"; + + //Generate the lists. There are no two notes that play at once. + int index = 0; + while (fgets(line, 200, fp)) { + char firstLet[sizeof(line)]; + strncpy(firstLet, line,1); + firstLet[1] = 0; + if (strcmp(firstLet,one) == 0){ + L1[index] = 1; + L2[index] = 0; + L3[index] = 0; + L4[index] = 0; + } + else if(strcmp(firstLet,two) == 0){ + L1[index] = 0; + L2[index] = 1; + L3[index] = 0; + L4[index] = 0; + + } + else if(strcmp(firstLet,three) == 0){ + L1[index] = 0; + L2[index] = 0; + L3[index] = 1; + L4[index] = 0; + + } + else if(strcmp(firstLet,four) == 0){ + L1[index] = 0; + L2[index] = 0; + L3[index] = 0; + L4[index] = 1; + + } + index++; + } +} + +int main() { + + textRead(); //read the notes text file + uLCD.baudrate(3000000); //increase the baudrate for sick graphics + Thread thread3(wavThread); //thread that plays the song + Thread thread1(lcdThread1); //thread that displays the circles + + //outputs for ShiftBrite + enable=0; + latch=0; + + //declare interrupt + interrupt.mode(PullUp); + interrupt.fall(&fallInterrupt); + + spi.format(16,0); + spi.frequency(500000); + + while (true) { + Thread::wait(100); + //update the ShiftBrite + RGB_LED(red, green, 0); + + } +}
diff -r 000000000000 -r 71e79407f6d5 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Wed Mar 16 19:13:19 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#07314541bd12
diff -r 000000000000 -r 71e79407f6d5 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Mar 16 19:13:19 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/252557024ec3 \ No newline at end of file
diff -r 000000000000 -r 71e79407f6d5 mpr121.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.cpp Wed Mar 16 19:13:19 2016 +0000 @@ -0,0 +1,221 @@ +/* +Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include <mbed.h> +#include <sstream> +#include <string> +#include <list> + +#include <mpr121.h> + +Mpr121::Mpr121(I2C *i2c, Address i2cAddress) +{ + this->i2c = i2c; + + address = i2cAddress; + + // Configure the MPR121 settings to default + this->configureSettings(); +} + + +void Mpr121::configureSettings() +{ + // Put the MPR into setup mode + this->write(ELE_CFG,0x00); + + // Electrode filters for when data is > baseline + unsigned char gtBaseline[] = { + 0x01, //MHD_R + 0x01, //NHD_R + 0x00, //NCL_R + 0x00 //FDL_R + }; + + writeMany(MHD_R,gtBaseline,4); + + // Electrode filters for when data is < baseline + unsigned char ltBaseline[] = { + 0x01, //MHD_F + 0x01, //NHD_F + 0xFF, //NCL_F + 0x02 //FDL_F + }; + + writeMany(MHD_F,ltBaseline,4); + + // Electrode touch and release thresholds + unsigned char electrodeThresholds[] = { + E_THR_T, // Touch Threshhold + E_THR_R // Release Threshold + }; + + for(int i=0; i<12; i++){ + int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2); + } + + // Proximity Settings + unsigned char proximitySettings[] = { + 0xff, //MHD_Prox_R + 0xff, //NHD_Prox_R + 0x00, //NCL_Prox_R + 0x00, //FDL_Prox_R + 0x01, //MHD_Prox_F + 0x01, //NHD_Prox_F + 0xFF, //NCL_Prox_F + 0xff, //FDL_Prox_F + 0x00, //NHD_Prox_T + 0x00, //NCL_Prox_T + 0x00 //NFD_Prox_T + }; + writeMany(MHDPROXR,proximitySettings,11); + + unsigned char proxThresh[] = { + PROX_THR_T, // Touch Threshold + PROX_THR_R // Release Threshold + }; + writeMany(EPROXTTH,proxThresh,2); + + this->write(FIL_CFG,0x04); + + // Set the electrode config to transition to active mode + this->write(ELE_CFG,0x0c); +} + +void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){ + + if(electrode > 11) return; + + // Get the current mode + unsigned char mode = this->read(ELE_CFG); + + // Put the MPR into setup mode + this->write(ELE_CFG,0x00); + + // Write the new threshold + this->write((ELE0_T+(electrode*2)), touch); + this->write((ELE0_T+(electrode*2)+1), release); + + //Restore the operating mode + this->write(ELE_CFG, mode); +} + + +unsigned char Mpr121::read(int key){ + + unsigned char data[2]; + + //Start the command + i2c->start(); + + // Address the target (Write mode) + int ack1= i2c->write(address); + + // Set the register key to read + int ack2 = i2c->write(key); + + // Re-start for read of data + i2c->start(); + + // Re-send the target address in read mode + int ack3 = i2c->write(address+1); + + // Read in the result + data[0] = i2c->read(0); + + // Reset the bus + i2c->stop(); + + return data[0]; +} + + +int Mpr121::write(int key, unsigned char value){ + + //Start the command + i2c->start(); + + // Address the target (Write mode) + int ack1= i2c->write(address); + + // Set the register key to write + int ack2 = i2c->write(key); + + // Read in the result + int ack3 = i2c->write(value); + + // Reset the bus + i2c->stop(); + + return (ack1+ack2+ack3)-3; +} + + +int Mpr121::writeMany(int start, unsigned char* dataSet, int length){ + //Start the command + i2c->start(); + + // Address the target (Write mode) + int ack= i2c->write(address); + if(ack!=1){ + return -1; + } + + // Set the register key to write + ack = i2c->write(start); + if(ack!=1){ + return -1; + } + + // Write the date set + int count = 0; + while(ack==1 && (count < length)){ + ack = i2c->write(dataSet[count]); + count++; + } + // Stop the cmd + i2c->stop(); + + return count; +} + + +bool Mpr121::getProximityMode(){ + if(this->read(ELE_CFG) > 0x0c) + return true; + else + return false; +} + +void Mpr121::setProximityMode(bool mode){ + this->write(ELE_CFG,0x00); + if(mode){ + this->write(ELE_CFG,0x30); //Sense proximity from ALL pads + } else { + this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active. + } +} + + +int Mpr121::readTouchData(){ + return this->read(0x00); +} \ No newline at end of file
diff -r 000000000000 -r 71e79407f6d5 mpr121.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.h Wed Mar 16 19:13:19 2016 +0000 @@ -0,0 +1,157 @@ +/* +Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au) + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + Parts written by Jim Lindblom of Sparkfun + Ported to mbed by A.Buckton, Feb 2011 +*/ + +#ifndef MPR121_H +#define MPR121_H + +//using namespace std; + +class Mpr121 +{ + +public: + // i2c Addresses, bit-shifted + enum Address { ADD_VSS = 0xb4,// ADD->VSS = 0x5a <-wiring on Sparkfun board + ADD_VDD = 0xb6,// ADD->VDD = 0x5b + ADD_SCL = 0xb8,// ADD->SDA = 0x5c + ADD_SDA = 0xba // ADD->SCL = 0x5d + }; + + // Real initialiser, takes the i2c address of the device. + Mpr121(I2C *i2c, Address i2cAddress); + + bool getProximityMode(); + + void setProximityMode(bool mode); + + int readTouchData(); + + unsigned char read(int key); + + int write(int address, unsigned char value); + int writeMany(int start, unsigned char* dataSet, int length); + + void setElectrodeThreshold(int electrodeId, unsigned char touchThreshold, unsigned char releaseThreshold); + +protected: + // Configures the MPR with standard settings. This is permitted to be overwritten by sub-classes. + void configureSettings(); + +private: + // The I2C bus instance. + I2C *i2c; + + // i2c address of this mpr121 + Address address; +}; + + +// MPR121 Register Defines +#define MHD_R 0x2B +#define NHD_R 0x2C +#define NCL_R 0x2D +#define FDL_R 0x2E +#define MHD_F 0x2F +#define NHD_F 0x30 +#define NCL_F 0x31 +#define FDL_F 0x32 +#define NHDT 0x33 +#define NCLT 0x34 +#define FDLT 0x35 +// Proximity sensing controls +#define MHDPROXR 0x36 +#define NHDPROXR 0x37 +#define NCLPROXR 0x38 +#define FDLPROXR 0x39 +#define MHDPROXF 0x3A +#define NHDPROXF 0x3B +#define NCLPROXF 0x3C +#define FDLPROXF 0x3D +#define NHDPROXT 0x3E +#define NCLPROXT 0x3F +#define FDLPROXT 0x40 +// Electrode Touch/Release thresholds +#define ELE0_T 0x41 +#define ELE0_R 0x42 +#define ELE1_T 0x43 +#define ELE1_R 0x44 +#define ELE2_T 0x45 +#define ELE2_R 0x46 +#define ELE3_T 0x47 +#define ELE3_R 0x48 +#define ELE4_T 0x49 +#define ELE4_R 0x4A +#define ELE5_T 0x4B +#define ELE5_R 0x4C +#define ELE6_T 0x4D +#define ELE6_R 0x4E +#define ELE7_T 0x4F +#define ELE7_R 0x50 +#define ELE8_T 0x51 +#define ELE8_R 0x52 +#define ELE9_T 0x53 +#define ELE9_R 0x54 +#define ELE10_T 0x55 +#define ELE10_R 0x56 +#define ELE11_T 0x57 +#define ELE11_R 0x58 +// Proximity Touch/Release thresholds +#define EPROXTTH 0x59 +#define EPROXRTH 0x5A +// Debounce configuration +#define DEB_CFG 0x5B +// AFE- Analogue Front End configuration +#define AFE_CFG 0x5C +// Filter configuration +#define FIL_CFG 0x5D +// Electrode configuration - transistions to "active mode" +#define ELE_CFG 0x5E + +#define GPIO_CTRL0 0x73 +#define GPIO_CTRL1 0x74 +#define GPIO_DATA 0x75 +#define GPIO_DIR 0x76 +#define GPIO_EN 0x77 +#define GPIO_SET 0x78 +#define GPIO_CLEAR 0x79 +#define GPIO_TOGGLE 0x7A +// Auto configration registers +#define AUTO_CFG_0 0x7B +#define AUTO_CFG_U 0x7D +#define AUTO_CFG_L 0x7E +#define AUTO_CFG_T 0x7F + +// Threshold defaults +// Electrode touch threshold +#define E_THR_T 0x0F +// Electrode release threshold +#define E_THR_R 0x0A +// Prox touch threshold +#define PROX_THR_T 0x02 +// Prox release threshold +#define PROX_THR_R 0x02 + +#endif
diff -r 000000000000 -r 71e79407f6d5 wave_player.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Wed Mar 16 19:13:19 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/bricecroxton/code/wave_player/#f8dce50a1fd6