Simple implementation of Guitar Hero with mbed.

Dependencies:   4DGL-uLCD-SE MCP23S17 SDFileSystem mbed-rtos mbed wave_player

Committer:
gboggs3
Date:
Mon Mar 14 17:59:32 2016 +0000
Revision:
0:58d424fe40b9
Original working version that received checkoff. 3/14/2016

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gboggs3 0:58d424fe40b9 1 /*
gboggs3 0:58d424fe40b9 2 Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
gboggs3 0:58d424fe40b9 3
gboggs3 0:58d424fe40b9 4 Permission is hereby granted, free of charge, to any person obtaining a copy
gboggs3 0:58d424fe40b9 5 of this software and associated documentation files (the "Software"), to deal
gboggs3 0:58d424fe40b9 6 in the Software without restriction, including without limitation the rights
gboggs3 0:58d424fe40b9 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
gboggs3 0:58d424fe40b9 8 copies of the Software, and to permit persons to whom the Software is
gboggs3 0:58d424fe40b9 9 furnished to do so, subject to the following conditions:
gboggs3 0:58d424fe40b9 10
gboggs3 0:58d424fe40b9 11 The above copyright notice and this permission notice shall be included in
gboggs3 0:58d424fe40b9 12 all copies or substantial portions of the Software.
gboggs3 0:58d424fe40b9 13
gboggs3 0:58d424fe40b9 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
gboggs3 0:58d424fe40b9 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
gboggs3 0:58d424fe40b9 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
gboggs3 0:58d424fe40b9 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
gboggs3 0:58d424fe40b9 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gboggs3 0:58d424fe40b9 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
gboggs3 0:58d424fe40b9 20 THE SOFTWARE.
gboggs3 0:58d424fe40b9 21 */
gboggs3 0:58d424fe40b9 22
gboggs3 0:58d424fe40b9 23 #include <mbed.h>
gboggs3 0:58d424fe40b9 24 #include <sstream>
gboggs3 0:58d424fe40b9 25 #include <string>
gboggs3 0:58d424fe40b9 26 #include <list>
gboggs3 0:58d424fe40b9 27
gboggs3 0:58d424fe40b9 28 #include <mpr121.h>
gboggs3 0:58d424fe40b9 29
gboggs3 0:58d424fe40b9 30 Mpr121::Mpr121(I2C *i2c, Address i2cAddress)
gboggs3 0:58d424fe40b9 31 {
gboggs3 0:58d424fe40b9 32 this->i2c = i2c;
gboggs3 0:58d424fe40b9 33
gboggs3 0:58d424fe40b9 34 address = i2cAddress;
gboggs3 0:58d424fe40b9 35
gboggs3 0:58d424fe40b9 36 // Configure the MPR121 settings to default
gboggs3 0:58d424fe40b9 37 this->configureSettings();
gboggs3 0:58d424fe40b9 38 }
gboggs3 0:58d424fe40b9 39
gboggs3 0:58d424fe40b9 40
gboggs3 0:58d424fe40b9 41 void Mpr121::configureSettings()
gboggs3 0:58d424fe40b9 42 {
gboggs3 0:58d424fe40b9 43 // Put the MPR into setup mode
gboggs3 0:58d424fe40b9 44 this->write(ELE_CFG,0x00);
gboggs3 0:58d424fe40b9 45
gboggs3 0:58d424fe40b9 46 // Electrode filters for when data is > baseline
gboggs3 0:58d424fe40b9 47 unsigned char gtBaseline[] = {
gboggs3 0:58d424fe40b9 48 0x01, //MHD_R
gboggs3 0:58d424fe40b9 49 0x01, //NHD_R
gboggs3 0:58d424fe40b9 50 0x00, //NCL_R
gboggs3 0:58d424fe40b9 51 0x00 //FDL_R
gboggs3 0:58d424fe40b9 52 };
gboggs3 0:58d424fe40b9 53
gboggs3 0:58d424fe40b9 54 writeMany(MHD_R,gtBaseline,4);
gboggs3 0:58d424fe40b9 55
gboggs3 0:58d424fe40b9 56 // Electrode filters for when data is < baseline
gboggs3 0:58d424fe40b9 57 unsigned char ltBaseline[] = {
gboggs3 0:58d424fe40b9 58 0x01, //MHD_F
gboggs3 0:58d424fe40b9 59 0x01, //NHD_F
gboggs3 0:58d424fe40b9 60 0xFF, //NCL_F
gboggs3 0:58d424fe40b9 61 0x02 //FDL_F
gboggs3 0:58d424fe40b9 62 };
gboggs3 0:58d424fe40b9 63
gboggs3 0:58d424fe40b9 64 writeMany(MHD_F,ltBaseline,4);
gboggs3 0:58d424fe40b9 65
gboggs3 0:58d424fe40b9 66 // Electrode touch and release thresholds
gboggs3 0:58d424fe40b9 67 unsigned char electrodeThresholds[] = {
gboggs3 0:58d424fe40b9 68 E_THR_T, // Touch Threshhold
gboggs3 0:58d424fe40b9 69 E_THR_R // Release Threshold
gboggs3 0:58d424fe40b9 70 };
gboggs3 0:58d424fe40b9 71
gboggs3 0:58d424fe40b9 72 for(int i=0; i<12; i++){
gboggs3 0:58d424fe40b9 73 int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2);
gboggs3 0:58d424fe40b9 74 }
gboggs3 0:58d424fe40b9 75
gboggs3 0:58d424fe40b9 76 // Proximity Settings
gboggs3 0:58d424fe40b9 77 unsigned char proximitySettings[] = {
gboggs3 0:58d424fe40b9 78 0xff, //MHD_Prox_R
gboggs3 0:58d424fe40b9 79 0xff, //NHD_Prox_R
gboggs3 0:58d424fe40b9 80 0x00, //NCL_Prox_R
gboggs3 0:58d424fe40b9 81 0x00, //FDL_Prox_R
gboggs3 0:58d424fe40b9 82 0x01, //MHD_Prox_F
gboggs3 0:58d424fe40b9 83 0x01, //NHD_Prox_F
gboggs3 0:58d424fe40b9 84 0xFF, //NCL_Prox_F
gboggs3 0:58d424fe40b9 85 0xff, //FDL_Prox_F
gboggs3 0:58d424fe40b9 86 0x00, //NHD_Prox_T
gboggs3 0:58d424fe40b9 87 0x00, //NCL_Prox_T
gboggs3 0:58d424fe40b9 88 0x00 //NFD_Prox_T
gboggs3 0:58d424fe40b9 89 };
gboggs3 0:58d424fe40b9 90 writeMany(MHDPROXR,proximitySettings,11);
gboggs3 0:58d424fe40b9 91
gboggs3 0:58d424fe40b9 92 unsigned char proxThresh[] = {
gboggs3 0:58d424fe40b9 93 PROX_THR_T, // Touch Threshold
gboggs3 0:58d424fe40b9 94 PROX_THR_R // Release Threshold
gboggs3 0:58d424fe40b9 95 };
gboggs3 0:58d424fe40b9 96 writeMany(EPROXTTH,proxThresh,2);
gboggs3 0:58d424fe40b9 97
gboggs3 0:58d424fe40b9 98 this->write(FIL_CFG,0x04);
gboggs3 0:58d424fe40b9 99
gboggs3 0:58d424fe40b9 100 // Set the electrode config to transition to active mode
gboggs3 0:58d424fe40b9 101 this->write(ELE_CFG,0x0c);
gboggs3 0:58d424fe40b9 102 }
gboggs3 0:58d424fe40b9 103
gboggs3 0:58d424fe40b9 104 void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){
gboggs3 0:58d424fe40b9 105
gboggs3 0:58d424fe40b9 106 if(electrode > 11) return;
gboggs3 0:58d424fe40b9 107
gboggs3 0:58d424fe40b9 108 // Get the current mode
gboggs3 0:58d424fe40b9 109 unsigned char mode = this->read(ELE_CFG);
gboggs3 0:58d424fe40b9 110
gboggs3 0:58d424fe40b9 111 // Put the MPR into setup mode
gboggs3 0:58d424fe40b9 112 this->write(ELE_CFG,0x00);
gboggs3 0:58d424fe40b9 113
gboggs3 0:58d424fe40b9 114 // Write the new threshold
gboggs3 0:58d424fe40b9 115 this->write((ELE0_T+(electrode*2)), touch);
gboggs3 0:58d424fe40b9 116 this->write((ELE0_T+(electrode*2)+1), release);
gboggs3 0:58d424fe40b9 117
gboggs3 0:58d424fe40b9 118 //Restore the operating mode
gboggs3 0:58d424fe40b9 119 this->write(ELE_CFG, mode);
gboggs3 0:58d424fe40b9 120 }
gboggs3 0:58d424fe40b9 121
gboggs3 0:58d424fe40b9 122
gboggs3 0:58d424fe40b9 123 unsigned char Mpr121::read(int key){
gboggs3 0:58d424fe40b9 124
gboggs3 0:58d424fe40b9 125 unsigned char data[2];
gboggs3 0:58d424fe40b9 126
gboggs3 0:58d424fe40b9 127 //Start the command
gboggs3 0:58d424fe40b9 128 i2c->start();
gboggs3 0:58d424fe40b9 129
gboggs3 0:58d424fe40b9 130 // Address the target (Write mode)
gboggs3 0:58d424fe40b9 131 int ack1= i2c->write(address);
gboggs3 0:58d424fe40b9 132
gboggs3 0:58d424fe40b9 133 // Set the register key to read
gboggs3 0:58d424fe40b9 134 int ack2 = i2c->write(key);
gboggs3 0:58d424fe40b9 135
gboggs3 0:58d424fe40b9 136 // Re-start for read of data
gboggs3 0:58d424fe40b9 137 i2c->start();
gboggs3 0:58d424fe40b9 138
gboggs3 0:58d424fe40b9 139 // Re-send the target address in read mode
gboggs3 0:58d424fe40b9 140 int ack3 = i2c->write(address+1);
gboggs3 0:58d424fe40b9 141
gboggs3 0:58d424fe40b9 142 // Read in the result
gboggs3 0:58d424fe40b9 143 data[0] = i2c->read(0);
gboggs3 0:58d424fe40b9 144
gboggs3 0:58d424fe40b9 145 // Reset the bus
gboggs3 0:58d424fe40b9 146 i2c->stop();
gboggs3 0:58d424fe40b9 147
gboggs3 0:58d424fe40b9 148 return data[0];
gboggs3 0:58d424fe40b9 149 }
gboggs3 0:58d424fe40b9 150
gboggs3 0:58d424fe40b9 151
gboggs3 0:58d424fe40b9 152 int Mpr121::write(int key, unsigned char value){
gboggs3 0:58d424fe40b9 153
gboggs3 0:58d424fe40b9 154 //Start the command
gboggs3 0:58d424fe40b9 155 i2c->start();
gboggs3 0:58d424fe40b9 156
gboggs3 0:58d424fe40b9 157 // Address the target (Write mode)
gboggs3 0:58d424fe40b9 158 int ack1= i2c->write(address);
gboggs3 0:58d424fe40b9 159
gboggs3 0:58d424fe40b9 160 // Set the register key to write
gboggs3 0:58d424fe40b9 161 int ack2 = i2c->write(key);
gboggs3 0:58d424fe40b9 162
gboggs3 0:58d424fe40b9 163 // Read in the result
gboggs3 0:58d424fe40b9 164 int ack3 = i2c->write(value);
gboggs3 0:58d424fe40b9 165
gboggs3 0:58d424fe40b9 166 // Reset the bus
gboggs3 0:58d424fe40b9 167 i2c->stop();
gboggs3 0:58d424fe40b9 168
gboggs3 0:58d424fe40b9 169 return (ack1+ack2+ack3)-3;
gboggs3 0:58d424fe40b9 170 }
gboggs3 0:58d424fe40b9 171
gboggs3 0:58d424fe40b9 172
gboggs3 0:58d424fe40b9 173 int Mpr121::writeMany(int start, unsigned char* dataSet, int length){
gboggs3 0:58d424fe40b9 174 //Start the command
gboggs3 0:58d424fe40b9 175 i2c->start();
gboggs3 0:58d424fe40b9 176
gboggs3 0:58d424fe40b9 177 // Address the target (Write mode)
gboggs3 0:58d424fe40b9 178 int ack= i2c->write(address);
gboggs3 0:58d424fe40b9 179 if(ack!=1){
gboggs3 0:58d424fe40b9 180 return -1;
gboggs3 0:58d424fe40b9 181 }
gboggs3 0:58d424fe40b9 182
gboggs3 0:58d424fe40b9 183 // Set the register key to write
gboggs3 0:58d424fe40b9 184 ack = i2c->write(start);
gboggs3 0:58d424fe40b9 185 if(ack!=1){
gboggs3 0:58d424fe40b9 186 return -1;
gboggs3 0:58d424fe40b9 187 }
gboggs3 0:58d424fe40b9 188
gboggs3 0:58d424fe40b9 189 // Write the date set
gboggs3 0:58d424fe40b9 190 int count = 0;
gboggs3 0:58d424fe40b9 191 while(ack==1 && (count < length)){
gboggs3 0:58d424fe40b9 192 ack = i2c->write(dataSet[count]);
gboggs3 0:58d424fe40b9 193 count++;
gboggs3 0:58d424fe40b9 194 }
gboggs3 0:58d424fe40b9 195 // Stop the cmd
gboggs3 0:58d424fe40b9 196 i2c->stop();
gboggs3 0:58d424fe40b9 197
gboggs3 0:58d424fe40b9 198 return count;
gboggs3 0:58d424fe40b9 199 }
gboggs3 0:58d424fe40b9 200
gboggs3 0:58d424fe40b9 201
gboggs3 0:58d424fe40b9 202 bool Mpr121::getProximityMode(){
gboggs3 0:58d424fe40b9 203 if(this->read(ELE_CFG) > 0x0c)
gboggs3 0:58d424fe40b9 204 return true;
gboggs3 0:58d424fe40b9 205 else
gboggs3 0:58d424fe40b9 206 return false;
gboggs3 0:58d424fe40b9 207 }
gboggs3 0:58d424fe40b9 208
gboggs3 0:58d424fe40b9 209 void Mpr121::setProximityMode(bool mode){
gboggs3 0:58d424fe40b9 210 this->write(ELE_CFG,0x00);
gboggs3 0:58d424fe40b9 211 if(mode){
gboggs3 0:58d424fe40b9 212 this->write(ELE_CFG,0x30); //Sense proximity from ALL pads
gboggs3 0:58d424fe40b9 213 } else {
gboggs3 0:58d424fe40b9 214 this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active.
gboggs3 0:58d424fe40b9 215 }
gboggs3 0:58d424fe40b9 216 }
gboggs3 0:58d424fe40b9 217
gboggs3 0:58d424fe40b9 218
gboggs3 0:58d424fe40b9 219 int Mpr121::readTouchData(){
gboggs3 0:58d424fe40b9 220 return this->read(0x00);
gboggs3 0:58d424fe40b9 221 }