4180 Design project

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Committer:
asims35
Date:
Tue Dec 08 16:30:32 2015 +0000
Revision:
0:be32bcd5f29d
Design project for ECE4180

Who changed what in which revision?

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