update

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

Fork of Lab3 by Azra Ismail

Committer:
aismail1997
Date:
Thu Oct 13 10:06:25 2016 +0000
Revision:
0:8af0a4200bd0
updates

Who changed what in which revision?

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