Digital Sound Synthesizer for lab 4

Dependencies:   LSM9DS1_Library_cal PinDetect mbed-rtos mbed

Committer:
nsloth
Date:
Tue Mar 15 21:27:30 2016 +0000
Revision:
0:0d977b83a68d
Final Submission for ECE4180 Lab 4

Who changed what in which revision?

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