Final Game. Have fun!!! :-)
Dependencies: 4DGL-uLCD-SE PinDetect mbed-rtos mbed
Revision 6:f99b7d1fd770, committed 2018-05-03
- Comitter:
- aamin33
- Date:
- Thu May 03 01:22:51 2018 +0000
- Parent:
- 5:ee8e44d75775
- Commit message:
- Final Game. Have fun!!! :-)
Changed in this revision
diff -r ee8e44d75775 -r f99b7d1fd770 DebounceIn.lib --- a/DebounceIn.lib Wed May 02 08:49:43 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://os.mbed.com/users/AjK/code/DebounceIn/#31ae5cfb44a4
diff -r ee8e44d75775 -r f99b7d1fd770 PinDetect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Thu May 03 01:22:51 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
diff -r ee8e44d75775 -r f99b7d1fd770 main.cpp --- a/main.cpp Wed May 02 08:49:43 2018 +0000 +++ b/main.cpp Thu May 03 01:22:51 2018 +0000 @@ -3,22 +3,28 @@ #include "uLCD_4DGL.h" #include "SDFileSystem.h" #include "wave_player.h" +#include "PinDetect.h" #include <string> //***** HW and signal setup *****// -Serial bluemod(p28,p27); //serial tx, serial rx +Serial bluemod(p28,p27); //bluetooth setup -//Bus for buttons -BusIn nibble(p21, p22, p23, p24); +//Pushbuttons +PinDetect pb1(p24); +PinDetect pb2(p23); +PinDetect pb3(p22); +PinDetect pb4(p21); - //Setup RGB led using PWM pins and class +int pb1Score = 0; +int pb2Score = 0; +int pb3Score = 0; +int pb4Score = 0; + +//Setup RGB led using PWM pins and class DigitalOut redLed(p19); DigitalOut greenLed(p20); -uLCD_4DGL uLCD(p13,p14,p12); // main screen for game -//uLCD_4DGL uLCD1(p13, p14, p12); // second screen for score - -Mutex ledMutex; +uLCD_4DGL uLCD(p13, p14, p12); // main screen for game SDFileSystem sd(p5, p6, p7, p8, "sd"); @@ -26,15 +32,8 @@ AnalogOut DACout(p18); wave_player waver(&DACout, &speakerPWM); -//*********************// - - - //***** Constants *****// -const int songLength = 50; //adjust as needed - -//*********************// - +const int songLength = 100; //adjust as needed //Arrays for notes int L1[songLength]; @@ -42,261 +41,207 @@ int L3[songLength]; int L4[songLength]; -int timespace = -6; //count for drawing the circles -int value = 0; //keypad input value -int score = 0; //score -int playGame = 1; //game state toggle to start game -int restartGame = 1; //game state toggle to restart or quit +int circleAligner = -6; //count for drawing the circles +int value = 0; //keypad input value +int score = 0; //score +int playGame = 1; //game state toggle to start game +int restartGame = 1; //game state toggle to restart or quit -int red = 0; -int green = 0; - -char blueIn = '0'; //Bluetooth setup +char blueIn = '0'; //Bluetooth setup int selectedSong = 0; -FILE *wav_file; // song file +FILE *wav_file; // song file + +//----------------------------------------------------------------------------------------------------------------------- +void pb1_hit_interrupt(void) { + if (circleAligner > -1) { + if (L4[circleAligner] == 1) { + pb1Score = pb1Score + 1; //increment points if tapped correctly + redLed = 0; + greenLed = 1; + } else { + redLed = 1; + greenLed = 0; + } + } + wait(.05); + redLed = 1; + greenLed = 0; +} -void mainMenu() -{ - uLCD.display_control(PORTRAIT_R); - uLCD.locate(5,0); - uLCD.printf("Welcome!"); - uLCD.locate(2,2); - uLCD.printf("Choose a song\n\n"); - uLCD.printf("1: GTShake\n\n"); - uLCD.printf("2: DarkHorse\n\n"); - uLCD.printf("3: OneRepublic\n\n"); - uLCD.printf("4: ShakeItOff\n\n"); - - int startGame = 0; - playGame = 1; - timespace = -6; - blueIn = '0'; - score = 0; - selectedSong = 0; - - while(!startGame){ //Loop until a valid song is selected - while(bluemod.readable()){ - blueIn = bluemod.getc(); //Read bluetooth input and select song. - switch(blueIn) { - case '1': - selectedSong = 1; - break; - case '2': - selectedSong = 2; - break; - case '3': - selectedSong = 3; - break; - case '4': - selectedSong = 4; - break; - } - break; //Only read 1 character at a time +void pb2_hit_interrupt(void) { + if (circleAligner > -1) { + if (L3[circleAligner] == 1) { + pb2Score = pb2Score + 1; + redLed = 0; + greenLed = 1; + } else { + redLed = 1; + greenLed = 0; } - - if(selectedSong >= 1 && selectedSong <= 4) //Start game once song is selected - startGame = 1; + } + wait(.05); + redLed = 1; + greenLed = 0; +} + +void pb3_hit_interrupt(void) { + if (circleAligner > -1) { + if (L2[circleAligner] == 1) { + pb3Score = pb3Score + 1; + redLed = 0; + greenLed = 1; + } else { + redLed = 1; + greenLed = 0; + } } - - uLCD.cls(); + wait(.05); + redLed = 1; + greenLed = 0; +} + +void pb4_hit_interrupt(void) { + if (circleAligner > -1) { + if (L1[circleAligner] == 1) { + pb4Score = pb4Score + 1; + redLed = 0; + greenLed = 1; + } else { + redLed = 1; + greenLed = 0; + } + } + wait(.05); + redLed = 1; + greenLed = 0; } -void endScreen() -{ - uLCD.cls(); - uLCD.display_control(PORTRAIT_R); - uLCD.printf("Score: %d\n\n", score); - - uLCD.printf("1: Play Again\n"); - uLCD.printf("2: Quit."); - - blueIn = '0'; - while(restartGame < 0) //Loop waiting for bluetooth input - { - while(bluemod.readable()){ - blueIn = bluemod.getc(); //Read bluetooth input and select song. - - if(blueIn == '1'){ - restartGame = 1; //if 1 go back to main menu - break; +//----------------------------------------------------------------------------------------------------------------------- + +void lcdThread1(void const *args) { + while(playGame) { + circleAligner++; + uLCD.filled_circle(110, 20, 12, BLACK); + uLCD.filled_circle(110, 50, 12, BLACK); + uLCD.filled_circle(110, 80, 12, BLACK); + uLCD.filled_circle(110, 110, 12, BLACK); + if (circleAligner >= 0) { //draw the path of the circles + if (L1[circleAligner]) { + uLCD.filled_circle(86, 20, 10, BLACK); + uLCD.filled_circle(110, 20, 12, BLUE); + } + if (L2[circleAligner]) { + uLCD.filled_circle(86, 50, 10, BLACK); + uLCD.filled_circle(110, 50, 12, RED); } - else if(blueIn == '2'){ - restartGame = 0; //if 2 end game - break; + if (L3[circleAligner]) { + uLCD.filled_circle(86, 80, 10, BLACK); + uLCD.filled_circle(110, 80, 12, GREEN); + } + if (L4[circleAligner]) { + uLCD.filled_circle(86, 110, 10, BLACK); + uLCD.filled_circle(110, 110, 12, LGREY); + } + } + if (circleAligner >= -1) { + if (L1[circleAligner + 1]) { + uLCD.filled_circle(66, 20, 8, BLACK); + uLCD.filled_circle(86, 20, 10, BLUE);} + if (L2[circleAligner + 1]) { + uLCD.filled_circle(66, 50, 8, BLACK); + uLCD.filled_circle(86, 50, 10, RED);} + if (L3[circleAligner + 1]) { + uLCD.filled_circle(66, 80, 8, BLACK); + uLCD.filled_circle(86, 80, 10, GREEN); + } + if (L4[circleAligner + 1]) { + uLCD.filled_circle(66, 110, 8, BLACK); + uLCD.filled_circle(86, 110, 10, LGREY); } } + if (circleAligner >= -2) { + if (L1[circleAligner + 2]) { + uLCD.filled_circle(50, 20, 6, BLACK); + uLCD.filled_circle(66, 20, 8, BLUE); + } + if (L2[circleAligner + 2]) { + uLCD.filled_circle(50, 50, 6, BLACK); + uLCD.filled_circle(66, 50, 8, RED); + } + if (L3[circleAligner + 2]) { + uLCD.filled_circle(50, 80, 6, BLACK); + uLCD.filled_circle(66, 80, 8, GREEN); + } + if (L4[circleAligner + 2]) { + uLCD.filled_circle(50, 110, 6, BLACK); + uLCD.filled_circle(66, 110, 8, LGREY); + } + } + if (circleAligner >= -3) { + if (L1[circleAligner + 3]) { + uLCD.filled_circle(38, 20, 4, BLACK); + uLCD.filled_circle(50, 20, 6, BLUE); + } + if (L2[circleAligner + 3]) { + uLCD.filled_circle(38, 50, 4, BLACK); + uLCD.filled_circle(50, 50, 6, RED); + } + if (L3[circleAligner + 3]) { + uLCD.filled_circle(38, 80, 4, BLACK); + uLCD.filled_circle(50, 80, 6, GREEN); + } + if (L4[circleAligner + 3]) { + uLCD.filled_circle(38, 110, 4, BLACK); + uLCD.filled_circle(50, 110, 6, LGREY); + } + } + if (circleAligner >= -4) { + if (L1[circleAligner + 4]) { + uLCD.filled_circle(30, 20, 2, BLACK); + uLCD.filled_circle(38, 20, 4, BLUE); + } + if (L2[circleAligner + 4]) { + uLCD.filled_circle(30, 50, 2, BLACK); + uLCD.filled_circle(38, 50, 4, RED); + } + if (L3[circleAligner + 4]) { + uLCD.filled_circle(30, 80, 2, BLACK); + uLCD.filled_circle(38, 80, 4, GREEN); + } + if (L4[circleAligner + 4]) { + uLCD.filled_circle(30, 110, 2, BLACK); + uLCD.filled_circle(38, 110, 4, LGREY); + } + } + if (L1[circleAligner + 5]) { + uLCD.filled_circle(30, 20, 2, BLUE); + } + if (L2[circleAligner + 5]) { + uLCD.filled_circle(30, 50, 2, RED); + } + if (L3[circleAligner + 5]) { + uLCD.filled_circle(30, 80, 2, GREEN); + } + if (L4[circleAligner + 5]) { + uLCD.filled_circle(30, 110, 2, LGREY); + } + uLCD.circle(110, 20, 12, WHITE); //draw circles at base + uLCD.circle(110, 50, 12, WHITE); + uLCD.circle(110, 80, 12, WHITE); + uLCD.circle(110, 110, 12, WHITE); + if(circleAligner >= songLength) { //stop game if game ends before songs ends + playGame = 0; + } + Thread::wait(250); } } - -void buttonsThread(void const* args) { - while (playGame) { - if (timespace > -1) { - switch(nibble) { - case 0x8: - if (L4[timespace] == 1) { - score = score + 1; - redLed = 0; - greenLed = 1; - } else { - redLed = 1; - greenLed = 0; - } - break; - case 0x4: - if (L3[timespace] == 1) { - score = score + 1; - redLed = 0; - greenLed = 1; - } else { - redLed = 1; - greenLed = 0; - } - break; - case 0x2: - if (L2[timespace] == 1) { - score = score + 1; - redLed = 0; - greenLed = 1; - } else { - redLed = 1; - greenLed = 0; - } - break; - case 0x1: - if (L1[timespace] == 1) { - score = score + 1; - redLed = 0; - greenLed = 1; - } else { - redLed = 1; - greenLed = 0; - } - break; - default: - redLed = 1; - break; - } - while(nibble) {} - } - } -} +//----------------------------------------------------------------------------------------------------------------------- -void lcdThread1(void const *args) { - - while(playGame) { - timespace++; - //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); - - uLCD.locate(5, 0); - uLCD.printf("%d", score); - //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);} - - - - //draw circles at base - uLCD.circle(110,20,12, 0xFFFF00); - uLCD.circle(110,50,12, 0xFFFF00); - uLCD.circle(110,80,12, 0xFFFF00); - uLCD.circle(110,110,12, 0xFFFF00); - - Thread::wait(100); - - if(timespace >= songLength) - playGame = 0; - } - -} - -//Thread to play song, song is selected in mainMenu() -void musicThread(void const *args) { - - speakerPWM.period(1.0/400000.0); - +void musicThread(void const *args) { //Thread to play song, song is selected in mainMenu() + speakerPWM.period(1.0 / 400000.0); switch(selectedSong) { case 1: wav_file = fopen("/sd/GTShake.wav", "r"); @@ -314,41 +259,20 @@ wav_file = fopen("/sd/ShakeItOff.wav", "r"); break; } - waver.play(wav_file); - while (playGame) { - Thread::wait(50); + Thread::wait(100); } fclose(wav_file); - } -//Test method for writing notes to a specified file -//Example usage: writeFile("/sd/", "test"); writes data to /sd/test.txt -void writeFile(const string path, const string fileName) -{ - char name[64]; - snprintf(name, sizeof(name), "%s%s.txt", path.c_str(), fileName.c_str()); - printf("%s\n", name); - FILE *fp = fopen(name, "w"); - - if(fp == NULL) { - error("Could not open file for write\n"); - } else { - for(int i = 0; i < songLength; i++) { - fprintf(fp, "%d\n", i%4 + 1); - } - } - fclose(fp); -} +//----------------------------------------------------------------------------------------------------------------------- //Function to read notes from a text file into arrays //parameters: path of file (string), name of file with no suffix (string) //Example: readFile("/sd/", "test"); gets data from file /sd/test.txt // -void readFile(const string path, const string fileName) -{ +void readFile(const string path, const string fileName) { char name[64]; snprintf(name, sizeof(name), "%s%s.txt", path.c_str(), fileName.c_str()); FILE *fp = fopen(name, "r"); @@ -359,7 +283,7 @@ do { c = getc(fp); if(c != '\n') { - switch (c){ + switch (c) { case '1': L1[index] = 1; L2[index] = 0; @@ -387,21 +311,13 @@ } index++; } - } while ((c!=EOF) && (index < songLength)); fclose(fp); - - FILE *arr = fopen("/sd/arr.txt", "w"); - - for(int i = 0; i < index - 1; i++) - fprintf(arr, "%d%d%d%d\n", L1[i], L2[i], L3[i], L4[i]); - - fclose(arr); } -void readSong(int selection) -{ +//Gets data for selected song +void readSong(int selection) { switch(selection) { case 1: readFile("/sd/", "GTShake"); @@ -420,36 +336,124 @@ break; } } - -int main() { + +//----------------------------------------------------------------------------------------------------------------------- + +void bootMainMenu() { + uLCD.display_control(PORTRAIT_R); + uLCD.locate(5,0); + uLCD.printf("Welcome!"); + uLCD.locate(2,2); + uLCD.printf("Choose a song\n\n"); + uLCD.printf("1: GTShake\n\n"); + uLCD.printf("2: DarkHorse\n\n"); + uLCD.printf("3: OneRepublic\n\n"); + uLCD.printf("4: ShakeItOff\n\n"); + + int startGame = 0; + + //reset variables used in game + playGame = 1; + circleAligner = -6; + blueIn = '0'; + score = 0; + pb1Score = 0; + pb2Score = 0; + pb3Score = 0; + pb4Score = 0; + + selectedSong = 0; - uLCD.baudrate(3000000); //Set up Display - - while(restartGame) - { - mainMenu(); //Game stays at menu until player chooses song + while (!startGame) { //Loop until a valid song is selected + while (bluemod.readable()) { + blueIn = bluemod.getc(); //Read bluetooth input and select song. + switch(blueIn) { + case '1': + selectedSong = 1; + break; + case '2': + selectedSong = 2; + break; + case '3': + selectedSong = 3; + break; + case '4': + selectedSong = 4; + break; + } + break; //Only read 1 character at a time + } + if(selectedSong >= 1 && selectedSong <= 4) { //Start game once song is selected + startGame = 1; + } + } + uLCD.cls(); +} + +//----------------------------------------------------------------------------------------------------------------------- + +void bootEndScreen() { + uLCD.cls(); + uLCD.display_control(PORTRAIT_R); + score = pb1Score + pb2Score + pb3Score + pb4Score; + uLCD.printf("Score: %d\n", score); + uLCD.printf("Percent Correct:\n%04.1f\n\n", (float) score / (float) songLength * 100); + uLCD.printf("1: Play Again\n"); + uLCD.printf("2: Quit."); + blueIn = '0'; + while (restartGame < 0) { //Loop waiting for bluetooth input + while (bluemod.readable()) { + blueIn = bluemod.getc(); //Read bluetooth input and select song. + if (blueIn == '1') { + restartGame = 1; //if 1 go back to main menu + break; + } else if (blueIn == '2') { + restartGame = 0; //if 2 end game + break; + } + } + } +} + +//----------------------------------------------------------------------------------------------------------------------- + +int main() { + uLCD.baudrate(3000000); + + pb1.mode(PullUp); + pb2.mode(PullUp); + pb3.mode(PullUp); + pb4.mode(PullUp); + wait(0.001); + pb1.attach_deasserted(&pb1_hit_interrupt); + pb2.attach_deasserted(&pb2_hit_interrupt); + pb3.attach_deasserted(&pb3_hit_interrupt); + pb4.attach_deasserted(&pb4_hit_interrupt); + pb1.setSampleFrequency(); + pb2.setSampleFrequency(); + pb3.setSampleFrequency(); + pb4.setSampleFrequency(); + + while(restartGame) { + bootMainMenu(); //Game stays at menu until player chooses song through bluetooth + readSong(selectedSong); //create note arrays based on chosen song - readSong(selectedSong); //create note arrays based on chosen song - - //start threads - uLCD.display_control(LANDSCAPE_R); + uLCD.display_control(LANDSCAPE_R); //Orient the game in landscape mode. - Thread thread1(lcdThread1); //thread that displays the circles - Thread thread2(musicThread); //thread that plays the song - Thread thread3(buttonsThread); //thread to get user input - + Thread thread1(lcdThread1); //thread that displays the circles + Thread thread2(musicThread); //thread that plays the song + while (playGame) { Thread::wait(10); } - //End Threads - thread2.terminate(); + thread2.terminate(); //End music thread - //reset game state switches - playGame = 1; + playGame = 1; //reset game state switches restartGame = -1; - endScreen(); //Game stays at end screen until player restarts or quits + bootEndScreen(); //Game stays at end screen until player restarts or quits + uLCD.cls(); } -} +} \ No newline at end of file
diff -r ee8e44d75775 -r f99b7d1fd770 mpr121.cpp --- a/mpr121.cpp Wed May 02 08:49:43 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,221 +0,0 @@ -/* -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 ee8e44d75775 -r f99b7d1fd770 mpr121.h --- a/mpr121.h Wed May 02 08:49:43 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,157 +0,0 @@ -/* -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 ee8e44d75775 -r f99b7d1fd770 test.txt --- a/test.txt Wed May 02 08:49:43 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,4 +0,0 @@ -1 -2 -3 -4 \ No newline at end of file