Project

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

Fork of Coffee_Roaster by Eric Patterson

Committer:
ericspatterson
Date:
Thu Nov 20 04:01:51 2014 +0000
Revision:
7:8a0223a951c1
Parent:
3:0f0e79eff4a7
a bit slow, but the controls work now...

Who changed what in which revision?

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