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