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