Lab that has to do with basic IO on mbed.

Dependencies:   DebounceIn mbed PinDetect

Committer:
Jesse Baker
Date:
Sun Jan 24 16:45:15 2016 -0500
Revision:
53:1c8235c49b70
Parent:
46:7775a1ff6915
JB is about to test expansion

Who changed what in which revision?

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