Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE SDFileSystem
Revision 0:eb011b288325, committed 2021-05-04
- Comitter:
- alanjko9
- Date:
- Tue May 04 20:38:25 2021 +0000
- Commit message:
- Piano_Tile_Racer_Source_Code
Changed in this revision
diff -r 000000000000 -r eb011b288325 4DGL-uLCD-SE.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Tue May 04 20:38:25 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/4180_1/code/4DGL-uLCD-SE/#2cb1845d7681
diff -r 000000000000 -r eb011b288325 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Tue May 04 20:38:25 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/neilt6/code/SDFileSystem/#e4d2567200db
diff -r 000000000000 -r eb011b288325 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue May 04 20:38:25 2021 +0000 @@ -0,0 +1,769 @@ +#include "mbed.h" +#include "SDFileSystem.h" +#include "uLCD_4DGL.h" +#include <string> +#include <list> +#include <mpr121.h> +#include "rtos.h" +#include <sstream> + +// new class to play a note on Speaker based on PwmOut class +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the Speaker Constructor + } +// class method to play a note based on PwmOut class + void playNote(float frequency) { + _pin.period(1.0/frequency); + _pin = 0.1; + } + void off() { + _pin = 0.0; + } + +private: +// sets up specified pin for PWM using PwmOut class + PwmOut _pin; +}; + + +//set up sd card for storing high scores +SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card +//leds to correspond to the key presses for debugging +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); +// Create the interrupt receiver object on pin 26 +InterruptIn interrupt(p26); +// Setup the i2c bus on pins 9 and 10 +I2C i2c(p9, p10); +// Setup the Mpr121: +// constructor(i2c object, i2c address of the mpr121) +Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); +//set up uLCD +uLCD_4DGL uLCD(p28,p27,p11); // serial tx, serial rx, reset pin; +//set up speaker on pin 21 +Speaker speaker(p21); + +//GLOBAL VARIABLES HERE +//keycode for touchpad press +volatile int key_code = 0; +volatile int keyPressed = -1; +//bool variables indicating when to update game and screen +volatile bool updateGame = false; +//state stores the current game state +enum State {MENU, PLAYING_GAME, GAME_OVER, SET_MUSIC, SET_DURATION, HIGH_SCORES} state; +//the total game duration (default 30 sec. can be 15, 30, 45, 60, 120 sec) +float gameDuration = 30.0; +//stores the randomly generated tiles in the game (value of 0 is far left, 3 is far right) +int tileRow0; +int tileRow1; +int tileRow2; +int tileRow3; +//keeps track of the current game score +int scoreCount; +//Note frequencies for songs +float C=261.63, CX=277.18, D=293.66, DX=311.13, E=329.63, F=349.23, FX=369.99, G=392, GX=415.3, A=440, AX=466.16, B=493.88, C1=523.25; +//different song arrays +float soundOfMusic[] = {C, D, E, C, E, C, E, D, E, F, F, E, D, F, E, F, G, E, G, E, G, F, G, A, A, G, F, A, G, C, D, E, F, G, A, A, D, E, F, G, A, B, B, E, F, G, A, B, C1, C1, B, A, F, B, G, C1, G, E, D}; +float threeBlindMice[] = {E, D, C, E, D, C, G, F, E, G, F, E, G, C1, C1, B, A, B, C1, G, G, G, C1, C1, B, A, B, C1, G, G, G, C1, C1, B, A, B, C1, G, G, F}; +float flyMeToTheMoon[] = {C1, B, A, G, F, G, A, C1, B, A, G, F, E, A, G, F, E, D, E, F, A, GX, F, E, D, C, C, D, A, A, C1, B, B, G, C, C, F, F, A, G, E, E}; +float halfStepScale[] = {C, CX, D, DX, E, F, FX, G, GX, A, AX, B, C1, C1, B, AX, A, GX, G, FX, F, E, DX, D, CX, C}; +//int used to determine which song is chosen (0-3) +int soundToggle = 0; +//bool to play song or not +bool isSoundOn = false; +//int used to toggle through highscores by game duration (0-4) corresponding to increasing time durations) +int highScoreToggle = 0; +//high score arrays pulled from SD card storage. only need top 5 scores +std::list<int> hs15; +std::list<int> hs30; +std::list<int> hs45; +std::list<int> hs60; +std::list<int> hs120; +//game timer +Timer t; +//threads +Thread gameTimerThread; +Thread playSoundThread; +Mutex m; + +//below are various draw fuctions to draw on the uLCD +//draw the white grid for the piano tiles +void refreshGrid() +{ + uLCD.filled_rectangle(0, 0, 80, 127, 0xFFFFFF); +} +//draw the 4 black tiles corresponding to tileRow0-3 +void drawTiles() { + uLCD.filled_rectangle(20 * tileRow0, 96, 20 * tileRow0 + 20, 127, 0x000000); + uLCD.filled_rectangle(20 * tileRow1, 64, 20 * tileRow1 + 20, 95, 0x000000); + uLCD.filled_rectangle(20 * tileRow2, 32, 20 * tileRow2 + 20, 63, 0x000000); + uLCD.filled_rectangle(20 * tileRow3, 0, 20 * tileRow3 + 20, 31, 0x000000); +} +//draw the initialized game with all components +void drawInitGame() { + m.lock(); + uLCD.cls(); + refreshGrid(); + drawTiles(); + uLCD.color(WHITE); + uLCD.text_height(2); + uLCD.locate(12,0); + uLCD.printf("Timer:"); + uLCD.locate(12, 2); + uLCD.printf(" %.0f ", gameDuration); + uLCD.locate(12,4); + uLCD.printf("Score:"); + uLCD.locate(12,6); + uLCD.printf(" %d ", scoreCount); + m.unlock(); +} +//draw only the updated components during gameplay +void drawPlayingGame() { + m.lock(); + refreshGrid(); + drawTiles();/* + uLCD.locate(12, 2); + uLCD.printf(" %.0f ", gameDuration - t.read());*/ + uLCD.locate(12,6); + uLCD.printf(" %d ", scoreCount); + m.unlock(); +} +//draw the game over screen +void drawGameOver() { + m.lock(); + + uLCD.cls(); + + uLCD.text_height(2); + uLCD.text_width(2); + uLCD.printf("Game Over\n\n"); + + uLCD.text_height(1); + uLCD.text_width(1); + uLCD.printf("\nYour score: %d\n", scoreCount); + uLCD.printf("\nPress 4 to return to menu"); + + m.unlock(); +} +//draw static menu screen +void drawMenu() { + m.lock(); + + uLCD.cls(); + uLCD.color(WHITE); + uLCD.text_height(2); + uLCD.printf(" Piano Tile Racer \n"); + + uLCD.color(16757350); + uLCD.text_height(1); + uLCD.printf("\n\n0. Start Game\n"); + uLCD.color(6730495); + uLCD.printf("\n1. Change Song\n"); + uLCD.color(6736896); + uLCD.printf("\n2. Change Duration\n"); + uLCD.color(16751001); + uLCD.printf("3. See Highscores\n"); + uLCD.color(13408767); + uLCD.printf("\nSound: Duration:\n"); + if(isSoundOn) { + uLCD.printf("[On] [%.0f]", gameDuration); + } else { + uLCD.printf("[Off] [%.0f]", gameDuration); + } + m.unlock(); +} +//draw static song selection screen +void drawSetMusic() { + m.lock(); + + uLCD.cls(); + + uLCD.color(6730495); + uLCD.text_height(2); + uLCD.printf(" Song Selection \n"); + + uLCD.text_height(1); + uLCD.printf("\n0. Back\n"); + uLCD.printf("\n1. No Music\n"); + uLCD.printf("\n2. Sound of Music\n"); + uLCD.printf("\n3. 3 Blind Mice\n"); + uLCD.printf("\n4. Fly Me to the Moon\n"); + uLCD.printf("\n5. Half Step Scale\n"); + m.unlock(); +} +//draw static set duration screen +void drawSetDuration() { + m.lock(); + + uLCD.cls(); + + uLCD.color(6736896); + uLCD.text_height(2); + uLCD.printf(" Game Duration \n"); + + uLCD.text_height(1); + uLCD.printf("\n\n0. Back\n"); + uLCD.printf("\n1. 15 seconds\n"); + uLCD.printf("\n2. 30 seconds\n"); + uLCD.printf("\n3. 45 seconds\n"); + uLCD.printf("\n4. 60 seocnds\n"); + uLCD.printf("\n5. 120 seconds\n"); + m.unlock(); +} +//draw highscores screen +void drawHighScores() { + m.lock(); + uLCD.cls(); + uLCD.color(16751001); + uLCD.text_height(2); + uLCD.printf(" High Scores \n"); + uLCD.text_height(1); + uLCD.printf("\n0. Back\n"); + uLCD.printf("\n1. Toggle Duration\n"); + uLCD.text_height(2); + std::list<int>::iterator it; + if (highScoreToggle == 0) { + uLCD.printf(" 15 Seconds: \n"); + uLCD.locate(0,5); + for (it=hs15.begin(); it!=hs15.end(); ++it) { + printf("%d, ", *it); + uLCD.printf("%d, ", *it); + } + } else if (highScoreToggle == 1) { + uLCD.printf(" 30 Seconds: \n"); + uLCD.locate(0,5); + for (it=hs30.begin(); it!=hs30.end(); ++it) { + printf("%d, ", *it); + uLCD.printf("%d, ", *it); + } + } else if (highScoreToggle == 2) { + uLCD.printf(" 45 Seconds: \n"); + uLCD.locate(0,5); + for (it=hs45.begin(); it!=hs45.end(); ++it) { + printf("%d, ", *it); + uLCD.printf("%d, ", *it); + } + } else if (highScoreToggle == 3) { + uLCD.printf(" 60 Seconds: \n"); + uLCD.locate(0,5); + for (it=hs60.begin(); it!=hs60.end(); ++it) { + printf("%d, ", *it); + uLCD.printf("%d, ", *it); + } + } else if (highScoreToggle == 4) { + uLCD.printf(" 120 Seconds: \n"); + uLCD.locate(0,5); + for (it=hs120.begin(); it!=hs120.end(); ++it) { + printf("%d, ", *it); + uLCD.printf("%d, ", *it); + } + } + printf("\n"); + m.unlock(); +} +//thread called in main to continuously update game timer +void gameTimer() { + t.reset(); + t.start(); + while(1) { + //The game duration is reached and the game is over + if (t.read() >= gameDuration) { + speaker.off(); + state = GAME_OVER; + drawGameOver(); + gameTimerThread.terminate(); + playSoundThread.terminate(); + } + m.lock(); + uLCD.locate(12, 2); + uLCD.printf(" %.0f ", gameDuration - t.read()); + m.unlock(); + Thread::wait(100); + } +} +//Thread called in main to play sound +void playSound() { + int prevScoreCount = 0; + Timer s; + while(1) { + //means correct key has been pressed and next note should play + if (prevScoreCount != scoreCount) { + if (soundToggle == 0) + speaker.playNote(soundOfMusic[(scoreCount-1) % 59]); + else if (soundToggle == 1) + speaker.playNote(threeBlindMice[(scoreCount-1) % 40]); + else if (soundToggle == 2) + speaker.playNote(flyMeToTheMoon[(scoreCount-1) % 42]); + else if (soundToggle == 3) + speaker.playNote(halfStepScale[(scoreCount-1) % 26]); + s.reset(); + s.start(); + prevScoreCount = scoreCount; + } + //only allow each note to play a max of 0.3 seconds + else if (s.read() > 0.3) { + speaker.off(); + s.stop(); + } + Thread::wait(100); + } +} + +//function called inside updateHighScoreArrays to update SD card +//dur specifies which duration's highscores have been updated +void updateHighScoreSD(int dur) { + //mkdir("/sd/PianoTileRacer", 0777); + std::list<int>::iterator iterator; + if (dur == 15) { + FILE *fp = fopen("/sd/PianoTileRacer/highScores15.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + } + for (iterator = hs15.begin(); iterator != hs15.end(); ++iterator) { + fprintf(fp, "%d ", *iterator); + } + fclose(fp); + return; + } else if (dur == 30) { + FILE *fp = fopen("/sd/PianoTileRacer/highScores30.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + } + for (iterator = hs30.begin(); iterator != hs30.end(); ++iterator) { + fprintf(fp, "%d ", *iterator); + } + fclose(fp); + return; + } else if (dur == 45) { + FILE *fp = fopen("/sd/PianoTileRacer/highScores45.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + } + for (iterator = hs45.begin(); iterator != hs45.end(); ++iterator) { + fprintf(fp, "%d ", *iterator); + } + fclose(fp); + return; + } else if (dur == 60) { + FILE *fp = fopen("/sd/PianoTileRacer/highScores60.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + } + for (iterator = hs60.begin(); iterator != hs60.end(); ++iterator) { + fprintf(fp, "%d ", *iterator); + } + fclose(fp); + return; + } else if (dur == 120) { + FILE *fp = fopen("/sd/PianoTileRacer/highScores120.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + } + for (iterator = hs120.begin(); iterator != hs120.end(); ++iterator) { + fprintf(fp, "%d ", *iterator); + } + fclose(fp); + return; + } +} + +//update highscores to arrays +void updateHighScoreArrays(int score) { + std::list<int>::iterator iterator; + if (gameDuration == 15.0) { + //empty list case + if (hs15.empty()) { + hs15.push_front(score); + updateHighScoreSD(15); + return; + } + //insert somewhere in the middle case + for (iterator = hs15.begin(); iterator != hs15.end(); ++iterator) { + if(score > *iterator) { + hs15.insert(iterator, score); + //make sure list is a max size of 5 + if (hs15.size() > 5) { + hs15.pop_back(); + } + updateHighScoreSD(15); + return; + } + } + //insert at the end of list case + if (hs15.size() < 5) { + hs15.push_back(score); + updateHighScoreSD(15); + } + return; + } else if (gameDuration == 30.0) { + //empty list case + if (hs30.empty()) { + hs30.push_front(score); + updateHighScoreSD(30); + return; + } + //insert somewhere in the middle case + for (iterator = hs30.begin(); iterator != hs30.end(); ++iterator) { + if(score > *iterator) { + hs30.insert(iterator, score); + //make sure list is a max size of 5 + if (hs30.size() > 5) { + hs30.pop_back(); + } + updateHighScoreSD(30); + return; + } + } + //insert at the end of list case + if (hs30.size() < 5) { + hs30.push_back(score); + updateHighScoreSD(30); + } + return; + } else if (gameDuration == 45.0) { + //empty list case + if (hs45.empty()) { + hs45.push_front(score); + updateHighScoreSD(45); + return; + } + //insert somewhere in the middle case + for (iterator = hs45.begin(); iterator != hs45.end(); ++iterator) { + if(score > *iterator) { + hs45.insert(iterator, score); + //make sure list is a max size of 5 + if (hs45.size() > 5) { + hs45.pop_back(); + } + updateHighScoreSD(45); + return; + } + } + //insert at the end of list case + if (hs45.size() < 5) { + hs45.push_back(score); + updateHighScoreSD(45); + } + return; + } else if (gameDuration == 60.0) { + //empty list case + if (hs60.empty()) { + hs60.push_front(score); + updateHighScoreSD(60); + return; + } + //insert somewhere in the middle case + for (iterator = hs60.begin(); iterator != hs60.end(); ++iterator) { + if(score > *iterator) { + hs60.insert(iterator, score); + //make sure list is a max size of 5 + if (hs60.size() > 5) { + hs60.pop_back(); + } + updateHighScoreSD(60); + return; + } + } + //insert at the end of list case + if (hs60.size() < 5) { + hs60.push_back(score); + updateHighScoreSD(60); + } + return; + } else if (gameDuration == 120.0) { + //empty list case + if (hs120.empty()) { + hs120.push_front(score); + updateHighScoreSD(120); + return; + } + //insert somewhere in the middle case + for (iterator = hs120.begin(); iterator != hs120.end(); ++iterator) { + if(score > *iterator) { + hs120.insert(iterator, score); + //make sure list is a max size of 5 + if (hs120.size() > 5) { + hs120.pop_back(); + } + updateHighScoreSD(120); + return; + } + } + //insert at the end of list case + if (hs120.size() < 5) { + hs120.push_back(score); + updateHighScoreSD(120); + } + return; + } +} + +//reads in highscores to the arrays. This is only called once in the very beginning of main +void readInHighScores() { + int tempScore; + char c; + std::string scoreStr = ""; + //15 second highscores + FILE *fp = fopen("/sd/PianoTileRacer/highScores15.txt", "r"); + if(fp == NULL) { + error("Could not open file for read 15\n"); + } + while (!feof(fp)){ // while not end of file + c = fgetc(fp); + if (c != ' ') { + scoreStr = scoreStr + c; + } else { + stringstream ss(scoreStr); + ss >> tempScore; + hs15.push_back(tempScore); + scoreStr = ""; + } + } + fclose(fp); + //30 second highscores + tempScore = -1; + c = NULL; + scoreStr = ""; + fp = fopen("/sd/PianoTileRacer/highScores30.txt", "r"); + if(fp == NULL) { + error("Could not open file for read 30\n"); + } + while (!feof(fp)){ // while not end of file + c = fgetc(fp); + if (c != ' ') { + scoreStr = scoreStr + c; + } else { + stringstream ss(scoreStr); + ss >> tempScore; + hs30.push_back(tempScore); + scoreStr = ""; + } + } + fclose(fp); + //45 second highscores + tempScore = -1; + c = NULL; + scoreStr = ""; + fp = fopen("/sd/PianoTileRacer/highScores45.txt", "r"); + if(fp == NULL) { + error("Could not open file for read 45\n"); + } + while (!feof(fp)){ // while not end of file + c = fgetc(fp); + if (c != ' ') { + scoreStr = scoreStr + c; + } else { + stringstream ss(scoreStr); + ss >> tempScore; + hs45.push_back(tempScore); + scoreStr = ""; + } + } + fclose(fp); + //60 second highscores + tempScore = -1; + c = NULL; + scoreStr = ""; + fp = fopen("/sd/PianoTileRacer/highScores60.txt", "r"); + if(fp == NULL) { + error("Could not open file for read 60\n"); + } + while (!feof(fp)){ // while not end of file + c = fgetc(fp); + if (c != ' ') { + scoreStr = scoreStr + c; + } else { + stringstream ss(scoreStr); + ss >> tempScore; + hs60.push_back(tempScore); + scoreStr = ""; + } + } + fclose(fp); + //120 second highscores + tempScore = -1; + c = NULL; + scoreStr = ""; + fp = fopen("/sd/PianoTileRacer/highScores120.txt", "r"); + if(fp == NULL) { + error("Could not open file for read 120\n"); + } + while (!feof(fp)){ // while not end of file + c = fgetc(fp); + if (c != ' ') { + scoreStr = scoreStr + c; + } else { + stringstream ss(scoreStr); + ss >> tempScore; + hs120.push_back(tempScore); + scoreStr = ""; + } + } + fclose(fp); + m.unlock(); +} + +//function called upon touchpad press +//this function is responsible for managing backend updates and calling the correct draw functions +//returning 1 means the actual game is starting and starts the game timer thread +int updateGameInfo() { + //playing game state + if(state == PLAYING_GAME) { + //if the right key is pressed + if (keyPressed == tileRow0) { + //increase score count + m.lock(); + scoreCount++; + m.unlock(); + //shift down the rows and generate a new row + tileRow0 = tileRow1; + tileRow1 = tileRow2; + tileRow2 = tileRow3; + tileRow3 = rand() % 4; + drawPlayingGame(); + return 0; + } else { //if the wrong key is pressed + speaker.off(); + state = GAME_OVER; + gameTimerThread.terminate(); + playSoundThread.terminate(); + drawGameOver(); + return 0; + } + } + //game over state, keypress 4 returns to menu and writes highscores to arrays and SD card + else if (state == GAME_OVER && keyPressed == 4) { + updateHighScoreArrays(scoreCount); + state = MENU; + drawMenu(); + return 0; + } + //menu state + else if (state == MENU) { + if (keyPressed == 0){ + //initialize tile rows + srand (time(NULL)); + tileRow0 = rand() % 4; + tileRow1 = rand() % 4; + tileRow2 = rand() % 4; + tileRow3 = rand() % 4; + scoreCount = 0; + state = PLAYING_GAME; + drawInitGame(); + return 1; + } else if (keyPressed == 1) { + state = SET_MUSIC; + drawSetMusic(); + return 0; + } else if (keyPressed == 2) { + state = SET_DURATION; + drawSetDuration(); + return 0; + } else if (keyPressed == 3) { + state = HIGH_SCORES; + drawHighScores(); + return 0; + } else if (keyPressed == 11) { + readInHighScores(); + drawMenu(); + return 0; + } + } + //changing music screen + else if (state == SET_MUSIC) { + if (keyPressed == 1) { + isSoundOn = false; + } else if (keyPressed == 2) { + isSoundOn = true; + soundToggle = 0; + } else if (keyPressed == 3) { + isSoundOn = true; + soundToggle = 1; + } else if (keyPressed == 4) { + isSoundOn = true; + soundToggle = 2; + } else if (keyPressed == 5) { + isSoundOn = true; + soundToggle = 3; + } + state = MENU; + drawMenu(); + return 0; + } + //changing duration screen + else if (state == SET_DURATION) { + if (keyPressed == 1) { + gameDuration = 15.0; + } else if (keyPressed == 2) { + gameDuration = 30.0; + } else if (keyPressed == 3) { + gameDuration = 45.0; + } else if (keyPressed == 4) { + gameDuration = 60.0; + } else if (keyPressed == 5) { + gameDuration = 120.0; + } + state = MENU; + drawMenu(); + return 0; + } + //viewing highscores screen + else if (state == HIGH_SCORES) { + if (keyPressed == 0) { + highScoreToggle = 0; + state = MENU; + drawMenu(); + return 0; + } else if (keyPressed == 1) { + highScoreToggle = (highScoreToggle + 1) % 5; + drawHighScores(); + return 0; + } + } + return 0; +} + +// Key hit/release interrupt routine +void fallInterrupt() { + key_code = 0; + int i=0; + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + // LED demo mod + i=0; + // puts key number out to LEDs for demo + for (i=0; i<12; i++) { + if (((value>>i)&0x01)==1) key_code=i+1; + } + if (key_code != 0) { + updateGame = true; + } + led4=key_code & 0x01; + led3=(key_code>>1) & 0x01; + led2=(key_code>>2) & 0x01; + led1=(key_code>>3) & 0x01; +} + +int main() { + interrupt.fall(&fallInterrupt); + interrupt.mode(PullUp); + state = MENU; + while (1) { + if (updateGame) { + keyPressed = key_code - 1; + if (updateGameInfo() == 1) { + gameTimerThread.start(gameTimer); + if(isSoundOn) { + playSoundThread.start(playSound); + //playSoundThread.set_priority(osPriorityHigh); + } + } + updateGame = false; + } + } +}
diff -r 000000000000 -r eb011b288325 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Tue May 04 20:38:25 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed-rtos/#58563e6cba1e
diff -r 000000000000 -r eb011b288325 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue May 04 20:38:25 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/0ab6a29f35bf \ No newline at end of file
diff -r 000000000000 -r eb011b288325 mpr121.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.cpp Tue May 04 20:38:25 2021 +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 eb011b288325 mpr121.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.h Tue May 04 20:38:25 2021 +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