a

Dependencies:   mbed

Fork of MPR121_Demo by jim hamblen

Committer:
4180_1
Date:
Wed Mar 16 01:49:13 2011 +0000
Revision:
0:e09703934ff4

        

Who changed what in which revision?

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