Poker code for Secondary Mbed

Dependencies:   NokiaLCD mbed

Committer:
wjenkins7
Date:
Wed Dec 11 07:07:19 2013 +0000
Revision:
0:1324145f6904
Mbed Poker Secondary

Who changed what in which revision?

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