Simple implementation of Guitar Hero with mbed.
Dependencies: 4DGL-uLCD-SE MCP23S17 SDFileSystem mbed-rtos mbed wave_player
Revision 0:58d424fe40b9, committed 2016-03-14
- Comitter:
- gboggs3
- Date:
- Mon Mar 14 17:59:32 2016 +0000
- Commit message:
- Original working version that received checkoff. 3/14/2016
Changed in this revision
diff -r 000000000000 -r 58d424fe40b9 4DGL-uLCD-SE.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Mon Mar 14 17:59:32 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/gboggs3/code/4DGL-uLCD-SE/#2e9823adbfd8
diff -r 000000000000 -r 58d424fe40b9 MCP23S17.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MCP23S17.lib Mon Mar 14 17:59:32 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/romilly/code/MCP23S17/#068b1e8909bb
diff -r 000000000000 -r 58d424fe40b9 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Mon Mar 14 17:59:32 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/neilt6/code/SDFileSystem/#3fa5eaf48e81
diff -r 000000000000 -r 58d424fe40b9 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Mar 14 17:59:32 2016 +0000 @@ -0,0 +1,533 @@ +/*=================================================================== +--------------------- Guitar Hero mbed Program ---------------------- + +This program recreates the classic Guitar Hero game using the mbed +processor, along with various other peripherals, such as capacitive +touch sensors, an IR sensor, and a uLCD display. + +Written for: + Georgia Institute of Technology + ECE 4180, Lab 4, Section B + Dr. James Hamblen + +Authors: + Garren Boggs + Anthony Jones +===================================================================*/ + +#include "mbed.h" +#include "rtos.h" //For threading +#include "uLCD_4DGL.h" //Display +#include "SDFileSystem.h" //SD card functionality +#include "wave_player.h" //For playing music +#include "MCP23S17.h" //Serial PC for debugging +#include <mpr121.h> //Capacitive touch sensors +#include <stdlib.h> +#include <time.h> + +#define NOTE_START 24 +#define NOTE_HEIGHT 9 +#define NOTE_WIDTH 27 +#define MISS 128 +#define SPEED 3 +#define HIT_TOP 98 +#define HIT_BOT 125 +#define VICTORY_THRESHOLD 50 + +bool paused = false; //If paused, stop gameplay and show on screen +bool gameOver = false; //Occurs if HP reaches zero +bool victory = false; +bool resetDisplay = false; //After resetting game or unpausing + +bool gHit = false; +bool rHit = false; +bool yHit = false; +bool bHit = false; + +int hp = 260; //Hitpoint count +int score = 0; //Score count +int combo = 0; //Combo count +int notesCreated = 0; //Victory after threshold + +/* Note locations. The values are the x and y positions respectively. + A value of -1 for these coordinates indicates that the specific + note is currently not on the screen. */ +int gNote[2]= {0,-1}; //Current GREEN note location. +int rNote[2]= {NOTE_WIDTH+1,-1}; //Current RED note location. +int yNote[2]= {2*NOTE_WIDTH+2,-1}; //Current YELLOW note location. +int bNote[2]= {3*NOTE_WIDTH+3,-1}; //Current BLUE note location. + +//------------------------------------------------------------------- +/* Instantiates the capacitive touch sensors. These will be used for + determining which notes have been pressed by the user. */ + +/* Setup the i2c bus on pins 9 and 10 */ + I2C i2c(p9, p10); + +/* Setup the Mpr121: */ + Mpr121 notes(&i2c, Mpr121::ADD_VSS); +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +/* Create an interrupt receiver to detect when the user has + strummed. */ + InterruptIn interruptStrum(p26); +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +/* Initialize SD Card Reader to import song to be played during the + game. */ + SDFileSystem sd(p5, p6, p7, p8, "sd"); + +/* Initializes the wave player to actually play the song. */ + AnalogOut DACout (p18); + wave_player player(&DACout); +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +/* Initializes the uLCD display as a UI for the game */ + uLCD_4DGL uLCD(p28,p27,p30); + +/* Create an interrupt to allow the user to pause the game, this + freezing the game UI. */ + InterruptIn interruptPause(p16); +//------------------------------------------------------------------- + +//------------------------------------------------------------------- +/* Initialize the mbed LEDs for testing and debugging purposes. */ + DigitalOut led1(LED1); + DigitalOut led2(LED2); + DigitalOut led3(LED3); + DigitalOut led4(LED4); + +/* Setup the Serial to the PC for debugging */ + Serial pc(USBTX, USBRX); + DigitalIn test(p24); +//------------------------------------------------------------------- + +/* +* Detects when the user has strum in order to play a note. This occurs +* when the user waves a hand close to the IR sensor. If close enough, +* the defined threshold will be passes and registered as a strum. +*/ +void strumInterrupt() +{ + //For resetting a game + if(gameOver || victory) + { + gNote[1] = -1; + rNote[1] = -1; + yNote[1] = -1; + bNote[1] = -1; + notesCreated = 0; + score = 0; + combo = 0; + hp = 260; + + gHit = false; + rHit = false; + yHit = false; + bHit = false; + + resetDisplay = true; + paused = false; + gameOver = false; + victory = false; + return; + } + + //Read values of selected notes off of capacitive touch sensor + int value = notes.read(0x00); + value += notes.read(0x01)<<8; + + //Determine if a note or combination of notes is being pressed + if((value == 1 || value == 3 || value == 5 || value == 7 || value == 9 || value == 11 || value == 13 || value == 15) && + gNote[1] < HIT_BOT && gNote[1] > HIT_TOP) + gHit = true; + if((value == 2 || value == 3 || value == 6 || value == 7 || value == 10 || value == 11 || value == 14 || value == 15) && + rNote[1] < HIT_BOT && rNote[1] > HIT_TOP) + rHit = true; + if((value == 4 || value == 5 || value == 6 || value == 7 || value == 12 || value == 13 || value == 14 || value == 15) && + yNote[1] < HIT_BOT && yNote[1] > HIT_TOP) + yHit = true; + if((value == 8 || value == 9 || value == 10 || value == 11 || value == 12 || value == 13 || value == 14 || value == 15) && + bNote[1] < HIT_BOT && bNote[1] > HIT_TOP) + bHit = true; + +} + +/* +* Updates the game display's life bar counter. If life bar drops below zero, +* game ends. +*/ +void updateLifeBar() +{ + int i; + int offset = 0; + + //Creates life bars on the game UI for the user to know how well they are doing. + //If the user loses HP, the bars will decrease, and as the user repleneshes their + //HP, the bars will begin to rise. + for(i = 0; i < 8; i++) + { + if(hp < (260 - i*10)) + uLCD.filled_rectangle(4*NOTE_WIDTH+6,3*i+offset,124,3*(i+1)+offset,BLACK); + else + uLCD.filled_rectangle(4*NOTE_WIDTH+6,3*i+offset,124,3*(i+1)+offset,GREEN); + offset += 2; + } + for(i = 8; i < 19; i++) + { + if(hp < (260 - i*10)) + uLCD.filled_rectangle(4*NOTE_WIDTH+6,3*i+offset,124,3*(i+1)+offset,BLACK); + else + uLCD.filled_rectangle(4*NOTE_WIDTH+6,3*i+offset,124,3*(i+1)+offset,YELLOW); + offset += 2; + } + for(i = 19; i < 26; i++) + { + if(hp < (260 - i*10)) + uLCD.filled_rectangle(4*NOTE_WIDTH+6,3*i+offset,124,3*(i+1)+offset,BLACK); + else + uLCD.filled_rectangle(4*NOTE_WIDTH+6,3*i+offset,124,3*(i+1)+offset,RED); + offset += 2; + } +} + +/* +* Initializes the game UI with the predefined background. There are +* four specific notes the user can play, and thus there will be four +* distinct lanes that are drawn initially. +*/ +void setupDisplay() +{ + //Background display + uLCD.cls(); + uLCD.background_color(BLACK); + uLCD.line(NOTE_WIDTH,NOTE_START-4,NOTE_WIDTH,127,WHITE); + uLCD.line(2*NOTE_WIDTH+1,NOTE_START-4,2*NOTE_WIDTH+1,127,WHITE); + uLCD.line(3*NOTE_WIDTH+2,NOTE_START-4,3*NOTE_WIDTH+2,127,WHITE); + uLCD.filled_rectangle(4*NOTE_WIDTH+3,0,4*NOTE_WIDTH+4,127,WHITE); + uLCD.filled_rectangle(126,0,127,127,WHITE); + uLCD.line(0,NOTE_START-4,4*NOTE_WIDTH+3,NOTE_START-4,WHITE); + uLCD.rectangle(0,127-(NOTE_HEIGHT+2),NOTE_WIDTH-1,127,GREEN); + uLCD.rectangle(NOTE_WIDTH+1,127-(NOTE_HEIGHT+2),2*NOTE_WIDTH,127,RED); + uLCD.rectangle(2*NOTE_WIDTH+2,127-(NOTE_HEIGHT+2),3*NOTE_WIDTH+1,127,YELLOW); + uLCD.rectangle(3*NOTE_WIDTH+3,127-(NOTE_HEIGHT+2),4*NOTE_WIDTH+2,127,BLUE); + + //Scoreboard + uLCD.printf("SCORE: %d\nCOMBO: %d",score,combo); + + //Initialize life bar + updateLifeBar(); +} + +/* +* Calculates the dimensions for a specific note, based on its +* current position. +*/ +void calculateNoteDims(int pos[], int dims[]) +{ + //If currently not on screen, do nothing. + if(pos[1] < 0 || pos[1] > MISS) + { + dims[0] = 0; + dims[1] = 0; + dims[2] = 0; + dims[3] = 0; + return; + } + + //Otherwise calculate dimensions + dims[0] = pos[0]; //x-left dimension + dims[2] = pos[0] + NOTE_WIDTH - 1; //x-right dimension + + dims[1] = (pos[1] - (NOTE_HEIGHT - 1)) > 0 ? pos[1] : 0; //y-bottom dimension + dims[3] = (pos[1] - (NOTE_HEIGHT - 1)) > 0 ? dims[1] + (NOTE_HEIGHT - 1) : pos[1]; //y-top dimension + + return; +} + +/* +* Pauses the game. +*/ +void pauseGame() +{ + if(paused) + { + resetDisplay = true; + } + paused = !paused; + wait(0.2); +} + +/* +* The game loop. This continuously updates the display with the notes, and +* acts accordingly if the user has strum, and a note has been hit. If the +* game is paused, the notes will cease movement, and a text displaying so +* will appear on the screen. +*/ +void playGame(void const *args) +{ + int gDims[4]; + int rDims[4]; + int yDims[4]; + int bDims[4]; + + while(1) + { + if(!gameOver && !victory && !paused) + { + //Resets display if game has been reset or unpaused + if(resetDisplay) + { + resetDisplay = false; + setupDisplay(); + } + + //Update life points + updateLifeBar(); + + //Calculate respective positions and draw accordingly if notes + //are going on or off screen. + calculateNoteDims(gNote, gDims); + calculateNoteDims(rNote, rDims); + calculateNoteDims(yNote, yDims); + calculateNoteDims(bNote, bDims); + + //Draw the notes and erase old positions + if(gNote[1] >= 0 && gNote[1] < MISS) + { + if(gHit) + { + gHit = false; + uLCD.filled_rectangle(gNote[0],gNote[1]-SPEED,gNote[0]+NOTE_WIDTH-1,gNote[1]-1,BLACK); + uLCD.filled_rectangle(gDims[0],gDims[1],gDims[2],gDims[3],BLACK); + gNote[1] = -1; + score += 30; + combo++; + hp = (hp + (combo >= 10 ? 10 : combo)) >= 260 ? 260 : (hp + (combo >= 10 ? 10 : combo)); + } + else + { + uLCD.filled_rectangle(gNote[0],gNote[1]-SPEED,gNote[0]+NOTE_WIDTH-1,gNote[1]-1,BLACK); + uLCD.filled_rectangle(gDims[0],gDims[1],gDims[2],gDims[3],GREEN); + } + } + + if(rNote[1] >= 0 && rNote[1] < MISS) + { + if(rHit) + { + rHit = false; + uLCD.filled_rectangle(rNote[0],rNote[1]-SPEED,rNote[0]+NOTE_WIDTH-1,rNote[1]-1,BLACK); + uLCD.filled_rectangle(rDims[0],rDims[1],rDims[2],rDims[3],BLACK); + rNote[1] = -1; + score += 30; + combo++; + hp = (hp + (combo >= 10 ? 10 : combo)) >= 260 ? 260 : (hp + (combo >= 10 ? 10 : combo)); + } + else + { + uLCD.filled_rectangle(rNote[0],rNote[1]-SPEED,rNote[0]+NOTE_WIDTH-1,rNote[1]-1,BLACK); + uLCD.filled_rectangle(rDims[0],rDims[1],rDims[2],rDims[3],RED); + } + } + + if(yNote[1] >= 0 && yNote[1] < MISS) + { + if(yHit) + { + yHit = false; + uLCD.filled_rectangle(yNote[0],yNote[1]-SPEED,yNote[0]+NOTE_WIDTH-1,yNote[1]-1,BLACK); + uLCD.filled_rectangle(yDims[0],yDims[1],yDims[2],yDims[3],BLACK); + yNote[1] = -1; + score += 30; + combo++; + hp = (hp + (combo >= 10 ? 10 : combo)) >= 260 ? 260 : (hp + (combo >= 10 ? 10 : combo)); + } + else + { + uLCD.filled_rectangle(yNote[0],yNote[1]-SPEED,yNote[0]+NOTE_WIDTH-1,yNote[1]-1,BLACK); + uLCD.filled_rectangle(yDims[0],yDims[1],yDims[2],yDims[3],YELLOW); + } + } + + if(bNote[1] >= 0 && bNote[1] < MISS) + { + if(bHit) + { + bHit = false; + uLCD.filled_rectangle(bNote[0],bNote[1]-SPEED,bNote[0]+NOTE_WIDTH-1,bNote[1]-1,BLACK); + uLCD.filled_rectangle(bDims[0],bDims[1],bDims[2],bDims[3],BLACK); + bNote[1] = -1; + score += 30; + combo++; + hp = (hp + (combo >= 10 ? 10 : combo)) >= 260 ? 260 : (hp + (combo >= 10 ? 10 : combo)); + } + else + { + uLCD.filled_rectangle(bNote[0],bNote[1]-SPEED,bNote[0]+NOTE_WIDTH-1,bNote[1]-1,BLACK); + uLCD.filled_rectangle(bDims[0],bDims[1],bDims[2],bDims[3],BLUE); + } + + } + + //Redraw target boxes at bottom of screen, incase notes are drawn over them + uLCD.rectangle(0,127-(NOTE_HEIGHT+2),NOTE_WIDTH-1,127,GREEN); + uLCD.rectangle(NOTE_WIDTH+1,127-(NOTE_HEIGHT+2),2*NOTE_WIDTH,127,RED); + uLCD.rectangle(2*NOTE_WIDTH+2,127-(NOTE_HEIGHT+2),3*NOTE_WIDTH+1,127,YELLOW); + uLCD.rectangle(3*NOTE_WIDTH+3,127-(NOTE_HEIGHT+2),4*NOTE_WIDTH+2,127,BLUE); + + //Check if each note is still on the screen. If it is, increment the position + gNote[1] = gNote[1] < 0 ? (rand()%100 > 95 ? NOTE_START : -2) : gNote[1] >= MISS ? -1 : (gNote[1] + SPEED); + if(gNote[1] == -1) + { + hp -= 10; + combo = 0; + } + if(gNote[1] == NOTE_START) + { + notesCreated++; + } + + rNote[1] = rNote[1] < 0 ? (rand()%100 > 95 ? NOTE_START : -2) : rNote[1] >= MISS ? -1 : (rNote[1] + SPEED); + if(rNote[1] == -1) + { + hp -= 10; + combo = 0; + } + if(rNote[1] == NOTE_START) + { + notesCreated++; + } + + yNote[1] = yNote[1] < 0 ? (rand()%100 > 95 ? NOTE_START : -2) : yNote[1] >= MISS ? -1 : (yNote[1] + SPEED); + if(yNote[1] == -1) + { + hp -= 10; + combo = 0; + } + if(yNote[1] == NOTE_START) + { + notesCreated++; + } + + bNote[1] = bNote[1] < 0 ? (rand()%100 > 95 ? NOTE_START : -2) : bNote[1] >= MISS ? -1 : (bNote[1] + SPEED); + if(bNote[1] == -1) + { + hp -= 10; + combo = 0; + } + if(bNote[1] == NOTE_START) + { + notesCreated++; + } + + //Check to make sure HP is above zero, otherwise, Game Over. + if(hp <= 0) + gameOver = true; + + //Check to see if user has won. + if(notesCreated >= VICTORY_THRESHOLD) + victory = true; + + uLCD.locate(0,0); + uLCD.printf("SCORE: %d\nCOMBO: %d",score,combo); + } + else if(gameOver) + { + //Sets up the display for a game over. + uLCD.cls(); + uLCD.background_color(BLACK); + uLCD.text_width(2); + uLCD.text_height(2); + uLCD.locate(0,0); + uLCD.printf("\n\nGAME OVER"); + uLCD.locate(0,6); + uLCD.text_width(1.5); + uLCD.text_height(1.5); + uLCD.printf("\n\r Score: %d\n\r Combo: %d",score,combo); + uLCD.text_width(1); + uLCD.text_height(1); + uLCD.locate(0,11); + uLCD.printf("\n\r Strum to reset"); + uLCD.locate(0,0); + while(gameOver) + { + wait(0.1); + } + } + else if(victory) + { + //Sets up the display for a victory. + uLCD.cls(); + uLCD.background_color(BLACK); + uLCD.text_width(2); + uLCD.text_height(2); + uLCD.locate(0,0); + uLCD.printf("\n\nYOU ROCK!"); + uLCD.locate(0,6); + uLCD.text_width(1.5); + uLCD.text_height(1.5); + uLCD.printf("\n\r Score: %d\n\r Combo: %d",score,combo); + uLCD.text_width(1); + uLCD.text_height(1); + uLCD.locate(0,11); + uLCD.printf("\n\r Strum to reset"); + uLCD.locate(0,0); + while(victory) + { + wait(0.1); + } + } + else + { + //Sets up the display for a paused game. + uLCD.cls(); + uLCD.background_color(BLACK); + uLCD.text_width(2); + uLCD.text_height(2); + uLCD.locate(0,0); + uLCD.printf("\n\n\n PAUSED!"); + uLCD.text_width(1); + uLCD.text_height(1); + uLCD.locate(0,0); + while(paused) + { + wait(0.1); + } + } + } +} + +int main() +{ + //Intialize display to show game UI + setupDisplay(); + + //Initialize the strum interrupt to act on a rising edge + interruptStrum.rise(&strumInterrupt); + + //Initialize the pause interrupt + interruptPause.rise(&pauseGame); + + //Initialize game loop + Thread gl(playGame); + + //Play the song + FILE *fp = fopen("/sd/sound/smoke_on_the_water.wav", "r"); + if(fp == NULL) + { + pc.printf("SD Card could not be read!"); + } + else + { + // player.play(fp); + fclose(fp); + } + + //Tasks for main loop (mainly debugging) + while(1) + { } +}
diff -r 000000000000 -r 58d424fe40b9 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Mon Mar 14 17:59:32 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#dfc27975e193
diff -r 000000000000 -r 58d424fe40b9 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Mar 14 17:59:32 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/f141b2784e32 \ No newline at end of file
diff -r 000000000000 -r 58d424fe40b9 mpr121.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.cpp Mon Mar 14 17:59:32 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 58d424fe40b9 mpr121.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.h Mon Mar 14 17:59:32 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 58d424fe40b9 wave_player.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Mon Mar 14 17:59:32 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/sravet/code/wave_player/#acc3e18e77ad