Whack-A-Mole game created using the mbed, capacitive touchpad, and a vibration motor.

Dependencies:   4DGL-uLCD-SE mbed

Committer:
richsua
Date:
Wed Oct 21 18:41:01 2015 +0000
Revision:
0:12260ef28a24
Final Version

Who changed what in which revision?

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