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