IoT for mbed1

Dependencies:   4DGL-uLCD-SE IoTsecuritySys PinDetect mbed-rtos mbed

Fork of IoT by Tal Landes

Committer:
jsmith352
Date:
Tue Nov 24 20:15:53 2015 +0000
Revision:
0:04dcbfb4388c
Touch pad door lock with two phase authentication

Who changed what in which revision?

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