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