Laser

Dependencies:   USBMIDI mbed

Fork of USBMIDI_HelloWorld by Simon Ford

Committer:
ywang627
Date:
Tue Dec 09 00:03:45 2014 +0000
Revision:
2:21f0ae3bef8a
Parent:
1:2b551cb862fe
Code for Laser Harp Implementation with MBed.

Who changed what in which revision?

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