The purpose of this project was to create a system that would allow users to monitor a locked device using a Bluetooth device. This Bluetooth device will show the last user that unlocked the device, and also allows the user to unlock the device using the Bluetooth device. This device can be physically unlocked using a capacitive touch keypad sensor.

Dependencies:   mbed Motor Servo

Fork of SerialPassthrough_LPC1768 by jim hamblen

Committer:
ewilliams61
Date:
Sat Apr 30 19:47:54 2016 +0000
Revision:
9:c216d7b63f92
Parent:
7:79d0b30fedb4
Final working code for IOT mailbox

Who changed what in which revision?

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