an mbed tile music game using a capacitive touchpad and uLCD

Dependencies:   SDFileSystem mbed wave_player

Committer:
clu67
Date:
Mon Mar 14 00:26:24 2016 +0000
Revision:
0:a1c374b9a4fe
Initial Release

Who changed what in which revision?

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