Capacitive Touch Sensor Controlling an MP3 Player
Dependencies: 4DGL-uLCD-SE SDFileSystem TextLCD mbed wave_player
Revision 0:1cb83aab3f9a, committed 2016-12-08
- Comitter:
- maxmbrown
- Date:
- Thu Dec 08 20:20:09 2016 +0000
- Commit message:
- Capacitive Touch Sensor Controlling an MP3 Player
Changed in this revision
diff -r 000000000000 -r 1cb83aab3f9a 4DGL-uLCD-SE.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Thu Dec 08 20:20:09 2016 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
diff -r 000000000000 -r 1cb83aab3f9a SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Thu Dec 08 20:20:09 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/maxmbrown/code/SDFileSystem/#f563c8b9df99
diff -r 000000000000 -r 1cb83aab3f9a Speaker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Thu Dec 08 20:20:09 2016 +0000 @@ -0,0 +1,19 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the Speaker Constructor + } +// class method to play a note based on PwmOut class + void PlayNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); + _pin = volume/2.0; + wait(duration); + _pin = 0.0; + } + +private: + PwmOut _pin; +}; \ No newline at end of file
diff -r 000000000000 -r 1cb83aab3f9a TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Thu Dec 08 20:20:09 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/benyun/code/TextLCD/#7dd9751172e1
diff -r 000000000000 -r 1cb83aab3f9a VS1002.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VS1002.cpp Thu Dec 08 20:20:09 2016 +0000 @@ -0,0 +1,288 @@ +#include "VS1002.h" +#include "mbed.h" +#include "uLCD_4DGL.h" + +//Serial pc(USBTX, USBRX); +uLCD_4DGL uLCD2(p28, p27, p29); + +/* ================================================================== + * Constructor + * =================================================================*/ + +VS1002::VS1002(PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name, PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, PinName dreq, PinName dcs, PinName vol) + : _sd(mmosi, mmiso, ssck, ccs, name), _spi(mosi, miso, sck), _CS(cs), _RST(rst), _DREQ(dreq), _DCS(dcs), _VOL(vol) { + + } + +/*=================================================================== + * Functions + *==================================================================*/ + +void VS1002::cs_low(void) +{ + _CS = 0; +} +void VS1002::cs_high(void) +{ + _CS = 1; +} +void VS1002::dcs_low(void) +{ + _DCS = 0; +} +void VS1002::dcs_high(void) +{ + _DCS = 1; +} +void VS1002::sci_en(void) //SCI enable +{ + cs_high(); + dcs_high(); + cs_low(); +} +void VS1002::sci_dis(void) //SCI disable +{ + cs_high(); +} +void VS1002::sdi_en(void) //SDI enable +{ + dcs_high(); + cs_high(); + dcs_low(); +} +void VS1002::sdi_dis(void) //SDI disable +{ + dcs_high(); +} +void VS1002::reset(void) //hardware reset +{ + wait(0.01); + _RST = 0; + wait(0.01); + _RST = 1; + wait(0.10); +} +void VS1002::power_down(void) //hardware and software reset +{ + cs_low(); + reset(); + sci_write(0x00, SM_PDOWN); + wait(0.01); + reset(); +} +void VS1002::sci_initialise(void) +{ + _RST = 1; //no reset + _spi.format(8,0); //spi 8bit interface, steady state low + _spi.frequency(1000000); //rising edge data record, freq. 1Mhz + + cs_low(); + for(int i=0; i<4; i++) + { + _spi.write(0xFF); //clock the chip a bit + } + cs_high(); + dcs_high(); + wait_us(5); +} +void VS1002::sdi_initialise(void) +{ + _spi.format(8,0); + _spi.frequency(7000000); //set to 7MHz + + cs_high(); + dcs_high(); +} +void VS1002::sci_write(unsigned char address, unsigned short int data) +{ + sci_en(); //enables SCI/disables SDI + + while(!_DREQ); //wait unitl data request is high + _spi.write(0x02); //SCI write + _spi.write(address); //register address + _spi.write((data >> 8) & 0xFF); //write out first half of data word + _spi.write(data & 0xFF); //write out second half of data word + + sci_dis(); //enables SDI/disables SCI + wait_us(5); +} +void VS1002::sdi_write(unsigned char datum) +{ + sdi_en(); + + while(!_DREQ); + _spi.write(datum); + + sci_dis(); +} +unsigned short int VS1002::read(unsigned short int address) +{ + cs_low(); //enables SCI/disables SDI + + while(!_DREQ); //wait unitl data request is high + _spi.write(0x03); //SCI write + _spi.write(address); //register address + unsigned short int received = _spi.write(0x00); //write out dummy byte + received <<= 8; + received += _spi.write(0x00); //write out dummy byte + + cs_high(); //enables SDI/disables SCI + + return received; //return received word +} +void VS1002::sine_test_activate(unsigned char wave) +{ + cs_high(); //enables SDI/disables SCI + + while(!_DREQ); //wait unitl data request is high + _spi.write(0x53); //SDI write + _spi.write(0xEF); //SDI write + _spi.write(0x6E); //SDI write + _spi.write(wave); //SDI write + _spi.write(0x00); //filler byte + _spi.write(0x00); //filler byte + _spi.write(0x00); //filler byte + _spi.write(0x00); //filler byte + + cs_low(); //enables SCI/disables SDI +} +void VS1002::sine_test_deactivate(void) +{ + cs_high(); + + while(!_DREQ); + _spi.write(0x45); //SDI write + _spi.write(0x78); //SDI write + _spi.write(0x69); //SDI write + _spi.write(0x74); //SDI write + _spi.write(0x00); //filler byte + _spi.write(0x00); //filler byte + _spi.write(0x00); //filler byte + _spi.write(0x00); //filler byte +} + + +void VS1002::volume(signed int left, signed int right) +{ + while(_DREQ == 0); + unsigned short int _left = -left; //convert the decibel values into a format + unsigned short int _right = -right; //readable by the chip cf. datasheet p.32 subsection 8.6.11 + _left *= 2; + _right *= 2; + unsigned short int attenuation = ((256 * _left) + _right); + cs_low(); + sci_write(0x0B, attenuation); //writeout these values + cs_high(); +} + +void VS1002::update_LCD(string print_file) +{ + uLCD2.cls(); + if(pause){ + uLCD2.printf("Paused "); + uLCD2.printf(print_file.c_str());} + if(mute){ + uLCD2.printf("Muted "); + uLCD2.printf(print_file.c_str());} + if(!mute && !pause){ + uLCD2.printf("Playing "); + uLCD2.printf(print_file.c_str()); } + + uLCD2.locate(0,4); + if(fade==0) + uLCD2.printf("Fade: %s ",print_fade); + else + uLCD2.printf("Fade: %s %i % ",print_fade,fade_percent); + if(t!=oldt) + { + uLCD2.locate(0,8); + if(t<10) + uLCD2.printf("%i:0%i",m,t); + else + uLCD2.printf("%i:%i",m,t); + oldt=t; + } + //uLCD2.printf("\n %d %s",new_song_number,song_name[new_song_number-1]); +} + +void VS1002::play_song(string song, string print_file, int song_number) +{ + /*====== Song Select ======*/ + + FILE *song2play; + unsigned char array[512]; //array for reading data from file + bool play_new=false; // Variable to see if new_song has be assigned or not + + song2play = fopen(song.c_str(), "r"); // Open the music file in read mode + + //Printing to LCD the present status + update_LCD(print_file); + t=0; + m=0; + if(!song2play) + { + uLCD2.printf("\n \n Error!!"); + exit(1); + } + while(!feof(song2play)) + { + if(!pause) + { + fread(&array, 1, 512, song2play); + for(int i=0; i<512; i++) + { + sdi_write(array[i]); + } + volume(volume_left,volume_right); + } + if(t!=oldt) + { + uLCD2.locate(0,8); + if(t<10) + uLCD2.printf("%i:0%i",m,t); + else + uLCD2.printf("%i:%i",m,t); + oldt=t; + } + if(fade!=old_fade) + { + uLCD2.locate(0,4); + if(fade==0) + uLCD2.printf("Fade: %s ",print_fade); + else + uLCD2.printf("Fade: %s %i % ",print_fade,fade_percent); + + old_fade=fade; + } + if(stop) + { + play_new=true; + break; + } + if((pause!=old_pause)||(mute!=old_mute)){ + old_pause=pause; + old_mute=mute; + update_LCD(print_file); + uLCD2.locate(0,8); + if(t<10) + uLCD2.printf("%i:0%i",m,t); + else + uLCD2.printf("%i:%i",m,t); + } + if(new_song_number!=song_number) + { + play_new=true; + break; + } + } + fclose(song2play); //close the file + + if(!play_new) + { + new_song_number+=1; // Goto Next song on completion of one song + if(new_song_number==size) + new_song_number=0; + play_new=false; + } + +} \ No newline at end of file
diff -r 000000000000 -r 1cb83aab3f9a VS1002.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VS1002.h Thu Dec 08 20:20:09 2016 +0000 @@ -0,0 +1,90 @@ +#ifndef VS1002_H +#define VS1002_H + +#include "mbed.h" +#include "SDFileSystem.h" +#include "string" +#include "string.h" +#include "TextLCD.h" + + +//SCI_MODE register bits as of p.26 of the datasheet +#define SM_DIFF 0x0001 +#define SM_SETTOZERO 0x0002 +#define SM_RESET 0x0004 +#define SM_OUTOFWAV 0x0008 +#define SM_PDOWN 0x0010 +#define SM_TESTS 0x0020 +#define SM_STREAM 0x0040 +#define SM_PLUSV 0x0080 +#define SM_DACT 0x0100 +#define SM_SDIORD 0x0200 +#define SM_SDISHARE 0x0400 +#define SM_SDINEW 0x0800 +#define SM_ADPCM 0x1000 +#define SM_ADPCM_HP 0x2000 + +extern int new_song_number; +extern int volume_left; +extern int volume_right; +extern int size; +extern bool pause; +extern bool old_pause; +extern bool mute; +extern bool old_mute; +extern bool stop; +extern int t; +extern int oldt; +extern int m; +extern int fade; +extern int old_fade; +extern string print_fade; +extern int fade_percent; + +extern char * song_name[6]; + + +class VS1002 { + +public: + + VS1002(PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name, PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, PinName dreq, PinName dcs, PinName vol); + + void cs_low(void); + void cs_high(void); + void dcs_low(void); + void dcs_high(void); + void sci_en(void); + void sci_dis(void); + void sdi_en(void); + void sdi_dis(void); + + void sci_initialise(void); + void sdi_initialise(void); + void reset(void); + void power_down(void); + + void sci_write(unsigned char, unsigned short int); + void sdi_write(unsigned char); + unsigned short int read(unsigned short int); + void sine_test_activate(unsigned char); + void volume(signed int,signed int); + void sine_test_deactivate(void); + void play_song(string, string, int); + void update_LCD(string); + + int num_of_files; + + DigitalIn _DREQ; + DigitalOut _RST; + AnalogIn _VOL; + +protected: + + SPI _spi; + DigitalOut _CS; + DigitalOut _DCS; + SDFileSystem _sd; + +}; +#endif \ No newline at end of file
diff -r 000000000000 -r 1cb83aab3f9a main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Dec 08 20:20:09 2016 +0000 @@ -0,0 +1,359 @@ +#include "mbed.h" +#include "SDFileSystem.h" +#include "wave_player.h" +#include "Speaker.h" +#include <vector> +#include <string> +#include "uLCD_4DGL.h" +#include "VS1002.h" +#include "mpr121.h" + +FILE *wave_file; +string current_file; +string print_file; +string song; +string new_song; +Serial pc(USBTX, USBRX); +SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card +AnalogOut DACout(p18); +uLCD_4DGL uLCD(p28, p27, p29); +Ticker play_time; + +vector<string> filenames; //filenames are stored in a vector string + +int startStop = 0; + +// OutKast, Kanye, Kenny Rogers, Sinatra, Odesza, DMX, 21 Pilots, Eminem +int array[] = { 0x000066, 0x000044, 0x000011, 0x000055, 0x000000, 0x000033, 0x000022, 0x000077}; +vector<int> artwork(array, array + 8); +//below code goes in song playing function +//uLCD.media_init(); +//uLCD.set_sector_address(0x0000, artwork[selected]); +//uLCD.display_image(0,0); + +void read_file_names(char *dir) +{ + DIR *dp; + struct dirent *dirp; + dp = opendir(dir); + //read all directory and file names in current directory into filename vector + while((dirp = readdir(dp)) != NULL) { + filenames.push_back(string(dirp->d_name)); + } +} + +VS1002 mp3(p5, p6, p7, p8,"sd",p11, p12 ,p13, p14, p23, p22, p21, p15); //Setup Audio decoder. Name is VS1002 even though VS1053 is used. +InterruptIn interrupt(p26); // Create the interrupt receiver object on pin 26 +I2C i2c(p9, p10); // Setup the i2c bus on pins 9 and 10 +Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); //Setup Mpr121 + +/* Global Variables to store Status*/ +int new_song_number=0; //Variable to store the Song Number +int old_song_number=0; +int volume_left = -15; //Variable to store the Volume +int volume_right = -15; +int previous_volume_left; //Variable to store the volume when muted +int previous_volume_right; +bool stop=true; +bool pause=false; +bool old_pause=false; //Variable to store the status of Pause button +bool mute=false; +bool old_mute=false; //Variable to store the status of mute button +int size; +int first; +int t=0; +int m=0; +int oldt=0; +int selected = 0; +int check = 0; //Capacitative touch generates interrupt on both press and release. This variable tracks this and updates only on press. +int fade=0; +int old_fade=0; +int n=0; +int fade_percent; +string print_fade="Center"; + +void counter(void){ + if(!pause) + { + t++; + if(t%60==0) + { + t=0; + m++; + } + } +} + +void fade_left(void) +{ + fade--; + //pc.printf("fade left\n\rfade=%i\n\r",fade); + if(fade<-5) + fade=-5; + fade_percent = abs(fade)*20; + if(fade==0) + { + volume_left=volume_right; + print_fade=("Center"); + } + if(fade<0) + { + n=(volume_left+60)/5; + volume_right=volume_left+fade*n; + print_fade="Left"; + // pc.printf("fade<0 volume_left=%i volume_right=%i n=%i\n\r",volume_left,volume_right,n); + } + if(fade>0) + { + n=(volume_right+60)/5; + volume_left=volume_right-fade*n; + print_fade="Right"; + // pc.printf("fade>0 volume_left=%i volume_right=%i n=%i\n\r",volume_left,volume_right,n); + } +} + +void fade_right(void) +{ + fade++; + pc.printf("fade right\n\rfade=%i\n\r",fade); + if(fade>5) + fade=5; + fade_percent = abs(fade)*20; + if(fade==0) + { + volume_right=volume_left; + print_fade=("Center"); + } + if(fade<0) + { + n=(volume_left+60)/5; + volume_right=volume_left+fade*n; + print_fade="Left"; + // pc.printf("fade<0 volume_left=%i volume_right=%i n=%i\n\r",volume_left,volume_right,n); + } + if(fade>0) + { + n=(volume_right+60)/5; + volume_left=volume_right-fade*n; + print_fade="Right"; + // pc.printf("fade>0 volume_left=%i volume_right=%i n=%i\n\r",volume_left,volume_right,n); + } +} + +void volume_up(void) +{ + if(fade==0) + { + if(volume_right!=0) + volume_right+=5; + if(volume_left!=0) + volume_left+=5; + } + if(fade<0) + { + if(volume_left!=0) + { + volume_left+=5; + n=(volume_left+60)/5; + volume_right=volume_left+fade*n; + } + } + if(fade>0) + { + if(volume_right!=0) + { + volume_right+=5; + n=(volume_right+60)/5; + volume_left=volume_right-fade*n; + } + } +} + +void volume_down(void) +{ + if(fade==0) + { + if(volume_right!=-60) + volume_right-=5; + if(volume_left!=-60) + volume_left-=5; + } + if(fade<0) + { + if(volume_left!=-60) + { + volume_left-=5; + n=(volume_left+60)/5; + volume_right=volume_left+fade*n; + if(volume_right<-60) + volume_right=-60; + } + } + if(fade>0) + { + if(volume_right!=-60) + { + volume_right-=5; + n=(volume_right+60)/5; + volume_left=volume_right-fade*n; + if(volume_left<-60) + volume_left=-60; + } + } +} + +void fallInterrupt() +{ + int key_code=0; + int i=0; + int value; + value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + + i=0; + if(check) + check=0; + else + check=1; + if(check) { + for (i=0; i<12; i++) { + if (((value>>i)&0x01)==1) key_code=i+1; //Convert value into decimal + } + + switch(key_code) { // Different cases depending on key press + case 0: + break; // Invalid entry . Valid 1-12 + case 1: + if(stop) + { + new_song_number+=1; // Move selecter down + if(new_song_number==filenames.size()) + new_song_number=0; // go back to beginning of vector + } + break; + case 2: + //play/stop button + stop=!stop; + first=0; + break; + case 3: + fade_right(); + break; + case 4: + fade_left(); + break; + case 5: + if(stop) + { + new_song_number-=1; // move selecetor up + if(new_song_number==-1) + new_song_number=filenames.size()-1; // Go to end of vector + } + break; + case 6: + break; + case 7: + new_song_number+=1; // Otherwise Next song + if(new_song_number==filenames.size()){ + new_song_number=0; // go back to beginning of vector + } + break; + case 8: + new_song_number-=1; // Previous Song + if(new_song_number==-1){ + new_song_number=filenames.size()-1; // Go to end of vector + } + break; + case 9: + old_pause=pause; + pause=!pause; // Pause/Play button + //stop=false; + //first=0; + break; + case 10: + volume_up(); + break; + case 11: + volume_down(); + break; + case 12: + old_mute=mute; + mute=!mute; //Mute/Unmute + if(mute) { + previous_volume_left = volume_left; // Attenuation of -55 db is small enough to not hear anything + previous_volume_right = volume_right; + volume_left = -60; + volume_right = -60; + } else { + volume_left=previous_volume_left; + volume_right = previous_volume_right; + } + break; + default: + uLCD.cls(); + uLCD.printf("error"); // exit on error + exit(1); + } + } +} +int main () +{ + read_file_names("/sd/myMusic"); // read file names into vector of strings + + + /*============================================================ + * MP3 Initialising + *==========================================================*/ + + mp3._RST = 1; + mp3.cs_high(); //chip disabled + mp3.sci_initialise(); //initialise MBED + mp3.sci_write(0x00,(SM_SDINEW+SM_STREAM+SM_DIFF)); + mp3.sci_write(0x03, 0x9800); + mp3.sdi_initialise(); + + + /* Touch Pad setup */ + interrupt.fall(&fallInterrupt); + interrupt.mode(PullUp); + play_time.attach(&counter, 1.0); + + while(1) { + if(stop) + { + if(first==0) + { + uLCD.cls(); + int i=0; + // print filename strings from vector using an iterator + for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++) + { + print_file = (*it).substr(0, ((*it).size() - 4)); + uLCD.locate(1,i); + uLCD.printf("%s\n",print_file.c_str()); + i++; + } + size = filenames.size(); + first++; + } + uLCD.locate(0,new_song_number); + uLCD.printf("*"); + if(old_song_number!=new_song_number) + { + uLCD.locate(0,old_song_number); + uLCD.printf(" "); + old_song_number=new_song_number; + } + } + if(!stop) + { + current_file = filenames[new_song_number]; + print_file = current_file.substr(0, (current_file.size() - 4)); + string song = "/sd/myMusic/" + current_file; + mp3.play_song(song, print_file, new_song_number); + } + } + +} + +
diff -r 000000000000 -r 1cb83aab3f9a mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Dec 08 20:20:09 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
diff -r 000000000000 -r 1cb83aab3f9a mpr121.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.cpp Thu Dec 08 20:20:09 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 1cb83aab3f9a mpr121.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.h Thu Dec 08 20:20:09 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
diff -r 000000000000 -r 1cb83aab3f9a wave_player.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Thu Dec 08 20:20:09 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/maxmbrown/code/wave_player/#87acbc2d0023