music player, led, motor

Dependencies:   LSM9DS1_Library_cal RPCInterface final mbed

Fork of final_mbed by Team X

Committer:
mikebenq
Date:
Mon May 01 11:05:37 2017 +0000
Revision:
2:55495227fcca
Parent:
1:f54bee9d59de
Final_mbed

Who changed what in which revision?

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