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