Tap Tap Revenge Mbed Game

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

Committer:
bricecroxton
Date:
Wed Mar 16 19:13:19 2016 +0000
Revision:
0:71e79407f6d5
Tap Tap Revenge Mbed

Who changed what in which revision?

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