music mixer code

Dependencies:   mbed wave_player mbed-rtos SimpleRGB 4DGL-uLCD-SE PinDetect

Committer:
vamsatoor
Date:
Mon Dec 09 16:43:04 2019 +0000
Revision:
10:5f5e199e1b9c
commit 1;

Who changed what in which revision?

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