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