Bop-It Game

Dependencies:   TextLCD mbed ADXL345 VS1053b

Committer:
jdumond3
Date:
Wed Oct 19 18:04:48 2011 +0000
Revision:
0:d16ac399135a

        

Who changed what in which revision?

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