Revenge of the Mouse

Dependencies:   4DGL-uLCD-SE EthernetInterface Game_Synchronizer LCD_fonts MMA8452 SDFileSystem mbed-rtos mbed wave_player

Fork of 2035_Tanks_Shell by ECE2035 Spring 2015 TA

Committer:
eriklomas
Date:
Tue Nov 01 18:59:29 2016 +0000
Revision:
28:fdaa7ecfbd80
4180;

Who changed what in which revision?

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