This is a MIDI synthesizer with touchpads used for playing the notes. mbed will capture the touchpad press and release events and sends the appropriate MIDI commands to the PC. By using the C# windows forms application created by us, one can compose songs as though they were composed on a piano. We support all types of standard MIDI piano types.

Dependencies:   mbed

Committer:
thejasvi3
Date:
Thu Oct 13 06:17:30 2011 +0000
Revision:
0:e33f18af0471

        

Who changed what in which revision?

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