Digital Sound Synthesizer for lab 4
Dependencies: LSM9DS1_Library_cal PinDetect mbed-rtos mbed
Revision 0:0d977b83a68d, committed 2016-03-15
- Comitter:
- nsloth
- Date:
- Tue Mar 15 21:27:30 2016 +0000
- Commit message:
- Final Submission for ECE4180 Lab 4
Changed in this revision
diff -r 000000000000 -r 0d977b83a68d LSM9DS1_Library_cal.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LSM9DS1_Library_cal.lib Tue Mar 15 21:27:30 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/4180_1/code/LSM9DS1_Library_cal/#36abf8e18ade
diff -r 000000000000 -r 0d977b83a68d PinDetect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Tue Mar 15 21:27:30 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
diff -r 000000000000 -r 0d977b83a68d main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Mar 15 21:27:30 2016 +0000 @@ -0,0 +1,207 @@ +#include "mbed.h" +#include "rtos.h" +#include "mpr121.h" +#include "PinDetect.h" +#include "LSM9DS1.h" + +// Sound waveform synthesizer +// The program precomputes 128 data points on one wave cycle +// for triangular, sawtooth, and sine waves. These are stored +// and outout to the PWM to vary the PWM duty cycle. A very high +// PWM frequency is used and the frequency of the output soundwave +// is changed by changing the sampling rate. The program uses the +// IMU to shift frequencies, the keypad to choose center frequencies, +// the RGB LED to visually display pitch, and a pushbutton to switch between wave modes. + +#define PI 3.141592653589793238462 + +InterruptIn irq(p11); +I2C i2c1(p9,p10); +Mpr121 touchpad(&i2c1, Mpr121::ADD_VSS); +PwmOut PWM(p26); +Serial pc(USBTX,USBRX); +LSM9DS1 IMU(p28, p27, 0xD6, 0x3C); +Mutex stdio_mutex; + +PwmOut RGBLED_r(p21); +PwmOut RGBLED_g(p22); +PwmOut RGBLED_b(p23); + +PinDetect pb1(p17); +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +//Global variables used by interrupt routines and threads +volatile int i=0; +volatile int j=0; +float sineInit[128]; +float sawInit[128]; +float triInit[128]; +volatile float baseFreq = 440; +float maxDeltaFreq = 200; +float maxDeltaXAxis = 15; +volatile float freq = 900; +volatile float deltaFreq = 0; +volatile float deltaXAxis = 0; +volatile float deltaXAxisOld = 0; +float frequencyScales[12] = {261.626, 293.665, 329.628, 349.228, 391.995, 440.0, + 493.883, 523.251, 587.33, 659.255, 783.991, 880}; + +enum Waveforms {sine=0,sawTooth,triangle}; +Waveforms wave = sine; + +//Samples IMU every .5 seconds and adjusts frequencies accordingly +void sample_IMU(void const *args) +{ + while(1) { + if(IMU.accelAvailable()) { + stdio_mutex.lock(); + deltaXAxisOld = deltaXAxis; + deltaXAxis = (0.0024*IMU.readAccel(X_AXIS)); //Measures difference of IMU from base value of 0 + stdio_mutex.unlock(); + } + Thread::wait(500); + } +} + +//Plays the current waveform at the current frequency +void playSineWave() { + while(j<150) { //Controls how many cycles. Plays about 3 full cycles + switch(wave) { + case sine: + PWM = (sineInit[i]); + break; + case sawTooth: + PWM = sawInit[i]; + break; + case triangle: + PWM = triInit[i]; + break; + default: + led2 = 0; + led3 = 0; + led4 = 0; + break; + } + //(1.0 + sin((float(i)*freq*6.28318530717959/(128.0))))/2.0); + // increment pointer and wrap around back to 0 at 128 + if(i>60) { + j++; + } + i = (i+1) & 0x07F; + wait_us(1000000.0/((freq+deltaFreq)*128)); + } + + //At the end of 3 cycles, reset counters + i=0; + j=0; + + int value=touchpad.read(0x00); + value +=touchpad.read(0x01)<<8; + if (value == 0) { + //Reset values if keypad is not being touched + PWM = 0; + deltaFreq = 0; + deltaXAxis = 0; + RGBLED_r = 0; + RGBLED_g = 0; + RGBLED_b = 0; + } else { + //If the keypad has a nonzero value, update the frequency and play more cycles + stdio_mutex.lock(); + float diff = (abs(deltaXAxis - deltaXAxisOld) > 2) ? deltaXAxis : deltaXAxisOld; //Makes the delta frequency resistant to noise in the IMU + stdio_mutex.unlock(); + + //Calculates delta frequency from center frequency based off of IMU accelerometer + if(abs(diff) > maxDeltaXAxis) + deltaFreq = maxDeltaFreq; + else { + deltaFreq = diff/maxDeltaXAxis*maxDeltaFreq; + } + + RGBLED_r = abs(freq-deltaFreq-220)/880; + RGBLED_g = abs(freq-220)/880; + RGBLED_b = abs(freq+deltaFreq-220)/880;; + playSineWave(); + } +} + +//Called when the touchpad is tapped +void fallInterrupt() { + int key_code=0; + int n=0; + int value=touchpad.read(0x00); + value +=touchpad.read(0x01)<<8; + for (n=0; n<12; n++) { + if (((value>>n)&0x01)==1) { + key_code=n+1; + } + } + //sets frequency based on the tapped value + freq = frequencyScales[key_code-1]; + if (value != 0) { + playSineWave(); + } +} + +//Switches the current waveform and leds +void pb1_released_callback(void){ + if (wave == triangle) { + led2 = 1; + led3 = 0; + led4 = 0; + wave = sine; + } else if (wave == sine) { + led2 = 0; + led3 = 1; + led4 = 0; + wave = sawTooth; + } else { + led2 = 0; + led3 = 0; + led4 = 1; + wave = triangle; + } +} + +int main() +{ + pc.printf("Program starting."); + PWM.period(1.0/200000.0); + led2 = 1; + led3 = 0; + led4 = 0; + + // Precompute 128 sample points on one wave cycle for three wave forms + // used for continuous wave output later + for(int k=0; k<128; k++) { + sineInit[k] = 1.0*((1.0 + sin((float(k)/128.0*2*PI)))/2.0); + sawInit[k] = (1.0 + ((-2.0*1.0/PI)*atan(cos(float(k)*PI/128.0)/sin(float(k)*PI/128.0))))/2.0; + triInit[k] = (1.0 + ((2.0*1.0/PI)*asin(sin(2.0*PI*float(k)/128.0))))/2.0; + // scale the waves from 0.0 to 1.0 - as needed for AnalogOut arg + } + + //Configure the IMU + IMU.begin(); + if (!IMU.begin()) { + pc.printf("Failed to communicate with LSM9DS1.\n"); + } + IMU.calibrate(1); + + //Configure the Push button + pb1.mode(PullUp); + wait(0.01); + pb1.attach_deasserted(&pb1_released_callback); + pb1.setSampleFrequency(); + + //Set up the I2C Touch keypad interrupt + irq.fall(&fallInterrupt); + irq.mode(PullUp); + + //And the IMU Sample thread + Thread t1(sample_IMU); + + while(1) {} +} \ No newline at end of file
diff -r 000000000000 -r 0d977b83a68d mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Tue Mar 15 21:27:30 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#dfc27975e193
diff -r 000000000000 -r 0d977b83a68d mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Mar 15 21:27:30 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb \ No newline at end of file
diff -r 000000000000 -r 0d977b83a68d mpr121.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.cpp Tue Mar 15 21:27:30 2016 +0000 @@ -0,0 +1,221 @@ +/* +Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include <mbed.h> +#include <sstream> +#include <string> +#include <list> + +#include <mpr121.h> + +Mpr121::Mpr121(I2C *i2c, Address i2cAddress) +{ + this->i2c = i2c; + + address = i2cAddress; + + // Configure the MPR121 settings to default + this->configureSettings(); +} + + +void Mpr121::configureSettings() +{ + // Put the MPR into setup mode + this->write(ELE_CFG,0x00); + + // Electrode filters for when data is > baseline + unsigned char gtBaseline[] = { + 0x01, //MHD_R + 0x01, //NHD_R + 0x00, //NCL_R + 0x00 //FDL_R + }; + + writeMany(MHD_R,gtBaseline,4); + + // Electrode filters for when data is < baseline + unsigned char ltBaseline[] = { + 0x01, //MHD_F + 0x01, //NHD_F + 0xFF, //NCL_F + 0x02 //FDL_F + }; + + writeMany(MHD_F,ltBaseline,4); + + // Electrode touch and release thresholds + unsigned char electrodeThresholds[] = { + E_THR_T, // Touch Threshhold + E_THR_R // Release Threshold + }; + + for(int i=0; i<12; i++){ + int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2); + } + + // Proximity Settings + unsigned char proximitySettings[] = { + 0xff, //MHD_Prox_R + 0xff, //NHD_Prox_R + 0x00, //NCL_Prox_R + 0x00, //FDL_Prox_R + 0x01, //MHD_Prox_F + 0x01, //NHD_Prox_F + 0xFF, //NCL_Prox_F + 0xff, //FDL_Prox_F + 0x00, //NHD_Prox_T + 0x00, //NCL_Prox_T + 0x00 //NFD_Prox_T + }; + writeMany(MHDPROXR,proximitySettings,11); + + unsigned char proxThresh[] = { + PROX_THR_T, // Touch Threshold + PROX_THR_R // Release Threshold + }; + writeMany(EPROXTTH,proxThresh,2); + + this->write(FIL_CFG,0x04); + + // Set the electrode config to transition to active mode + this->write(ELE_CFG,0x0c); +} + +void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){ + + if(electrode > 11) return; + + // Get the current mode + unsigned char mode = this->read(ELE_CFG); + + // Put the MPR into setup mode + this->write(ELE_CFG,0x00); + + // Write the new threshold + this->write((ELE0_T+(electrode*2)), touch); + this->write((ELE0_T+(electrode*2)+1), release); + + //Restore the operating mode + this->write(ELE_CFG, mode); +} + + +unsigned char Mpr121::read(int key){ + + unsigned char data[2]; + + //Start the command + i2c->start(); + + // Address the target (Write mode) + int ack1= i2c->write(address); + + // Set the register key to read + int ack2 = i2c->write(key); + + // Re-start for read of data + i2c->start(); + + // Re-send the target address in read mode + int ack3 = i2c->write(address+1); + + // Read in the result + data[0] = i2c->read(0); + + // Reset the bus + i2c->stop(); + + return data[0]; +} + + +int Mpr121::write(int key, unsigned char value){ + + //Start the command + i2c->start(); + + // Address the target (Write mode) + int ack1= i2c->write(address); + + // Set the register key to write + int ack2 = i2c->write(key); + + // Read in the result + int ack3 = i2c->write(value); + + // Reset the bus + i2c->stop(); + + return (ack1+ack2+ack3)-3; +} + + +int Mpr121::writeMany(int start, unsigned char* dataSet, int length){ + //Start the command + i2c->start(); + + // Address the target (Write mode) + int ack= i2c->write(address); + if(ack!=1){ + return -1; + } + + // Set the register key to write + ack = i2c->write(start); + if(ack!=1){ + return -1; + } + + // Write the date set + int count = 0; + while(ack==1 && (count < length)){ + ack = i2c->write(dataSet[count]); + count++; + } + // Stop the cmd + i2c->stop(); + + return count; +} + + +bool Mpr121::getProximityMode(){ + if(this->read(ELE_CFG) > 0x0c) + return true; + else + return false; +} + +void Mpr121::setProximityMode(bool mode){ + this->write(ELE_CFG,0x00); + if(mode){ + this->write(ELE_CFG,0x30); //Sense proximity from ALL pads + } else { + this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active. + } +} + + +int Mpr121::readTouchData(){ + return this->read(0x00); +} \ No newline at end of file
diff -r 000000000000 -r 0d977b83a68d mpr121.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.h Tue Mar 15 21:27:30 2016 +0000 @@ -0,0 +1,157 @@ +/* +Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au) + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + Parts written by Jim Lindblom of Sparkfun + Ported to mbed by A.Buckton, Feb 2011 +*/ + +#ifndef MPR121_H +#define MPR121_H + +//using namespace std; + +class Mpr121 +{ + +public: + // i2c Addresses, bit-shifted + enum Address { ADD_VSS = 0xb4,// ADD->VSS = 0x5a <-wiring on Sparkfun board + ADD_VDD = 0xb6,// ADD->VDD = 0x5b + ADD_SCL = 0xb8,// ADD->SDA = 0x5c + ADD_SDA = 0xba // ADD->SCL = 0x5d + }; + + // Real initialiser, takes the i2c address of the device. + Mpr121(I2C *i2c, Address i2cAddress); + + bool getProximityMode(); + + void setProximityMode(bool mode); + + int readTouchData(); + + unsigned char read(int key); + + int write(int address, unsigned char value); + int writeMany(int start, unsigned char* dataSet, int length); + + void setElectrodeThreshold(int electrodeId, unsigned char touchThreshold, unsigned char releaseThreshold); + +protected: + // Configures the MPR with standard settings. This is permitted to be overwritten by sub-classes. + void configureSettings(); + +private: + // The I2C bus instance. + I2C *i2c; + + // i2c address of this mpr121 + Address address; +}; + + +// MPR121 Register Defines +#define MHD_R 0x2B +#define NHD_R 0x2C +#define NCL_R 0x2D +#define FDL_R 0x2E +#define MHD_F 0x2F +#define NHD_F 0x30 +#define NCL_F 0x31 +#define FDL_F 0x32 +#define NHDT 0x33 +#define NCLT 0x34 +#define FDLT 0x35 +// Proximity sensing controls +#define MHDPROXR 0x36 +#define NHDPROXR 0x37 +#define NCLPROXR 0x38 +#define FDLPROXR 0x39 +#define MHDPROXF 0x3A +#define NHDPROXF 0x3B +#define NCLPROXF 0x3C +#define FDLPROXF 0x3D +#define NHDPROXT 0x3E +#define NCLPROXT 0x3F +#define FDLPROXT 0x40 +// Electrode Touch/Release thresholds +#define ELE0_T 0x41 +#define ELE0_R 0x42 +#define ELE1_T 0x43 +#define ELE1_R 0x44 +#define ELE2_T 0x45 +#define ELE2_R 0x46 +#define ELE3_T 0x47 +#define ELE3_R 0x48 +#define ELE4_T 0x49 +#define ELE4_R 0x4A +#define ELE5_T 0x4B +#define ELE5_R 0x4C +#define ELE6_T 0x4D +#define ELE6_R 0x4E +#define ELE7_T 0x4F +#define ELE7_R 0x50 +#define ELE8_T 0x51 +#define ELE8_R 0x52 +#define ELE9_T 0x53 +#define ELE9_R 0x54 +#define ELE10_T 0x55 +#define ELE10_R 0x56 +#define ELE11_T 0x57 +#define ELE11_R 0x58 +// Proximity Touch/Release thresholds +#define EPROXTTH 0x59 +#define EPROXRTH 0x5A +// Debounce configuration +#define DEB_CFG 0x5B +// AFE- Analogue Front End configuration +#define AFE_CFG 0x5C +// Filter configuration +#define FIL_CFG 0x5D +// Electrode configuration - transistions to "active mode" +#define ELE_CFG 0x5E + +#define GPIO_CTRL0 0x73 +#define GPIO_CTRL1 0x74 +#define GPIO_DATA 0x75 +#define GPIO_DIR 0x76 +#define GPIO_EN 0x77 +#define GPIO_SET 0x78 +#define GPIO_CLEAR 0x79 +#define GPIO_TOGGLE 0x7A +// Auto configration registers +#define AUTO_CFG_0 0x7B +#define AUTO_CFG_U 0x7D +#define AUTO_CFG_L 0x7E +#define AUTO_CFG_T 0x7F + +// Threshold defaults +// Electrode touch threshold +#define E_THR_T 0x0F +// Electrode release threshold +#define E_THR_R 0x0A +// Prox touch threshold +#define PROX_THR_T 0x02 +// Prox release threshold +#define PROX_THR_R 0x02 + +#endif