Sehoon Shon and Tanvir Iqbal's mbed mini project

Dependencies:   Motordriver TextLCD mbed

Fork of TextLCD_HelloWorld by Simon Ford

Committer:
luckybug38
Date:
Thu Oct 11 20:59:05 2012 +0000
Revision:
3:a59397561c9d
PROJECT3;

Who changed what in which revision?

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