111

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

Fork of 4180_proj by ECE4180proj

Committer:
hanjiex
Date:
Mon Dec 07 18:41:29 2015 +0000
Revision:
13:4cec0e446def
Parent:
12:b6265952fb06
111

Who changed what in which revision?

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