A Card Matching Game using uLCD and Touchpad

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

Committer:
mtaylor33
Date:
Wed Oct 22 00:12:42 2014 +0000
Revision:
0:d77e1909cdc7
Card Match Game

Who changed what in which revision?

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