CodeShare

Dependencies:   mbed

Committer:
jeremycai3721
Date:
Sun Sep 25 20:20:04 2016 +0000
Revision:
0:42cceb842d76
CodeShare

Who changed what in which revision?

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