Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed-rtos mbed wave_player
Fork of app-board-RTOS-Threads by
Revision 5:36b6ce2faf88, committed 2015-10-20
- Comitter:
- ethan_wireless
- Date:
- Tue Oct 20 17:24:08 2015 +0000
- Parent:
- 4:79863d2ea5a0
- Commit message:
- NA
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Tue Oct 20 17:24:08 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
--- a/C12832_lcd.lib Sun Sep 22 17:57:46 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/dreschpe/code/C12832_lcd/#c9afe58d786a
--- a/LCD_fonts.lib Sun Sep 22 17:57:46 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/dreschpe/code/LCD_fonts/#d0b7d7bf1f56
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Tue Oct 20 17:24:08 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Tue Oct 20 17:24:08 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
--- a/main.cpp Sun Sep 22 17:57:46 2013 +0000 +++ b/main.cpp Tue Oct 20 17:24:08 2015 +0000 @@ -1,164 +1,392 @@ -// example to test the mbed Lab Board lcd lib with the mbed rtos -// Pot1 changes the contrast -// Pot2 changes the speed of the sin wave +#include "mbed.h" +#include "uLCD_4DGL.h" +#include "SDFileSystem.h" +#include "wave_player.h" +#include "PinDetect.h" +#include <mpr121.h> + +Serial pc(USBTX, USBRX); -#include "mbed.h" -#include "rtos.h" -#include "Small_6.h" -#include "Small_7.h" -#include "Arial_9.h" -#include "stdio.h" -#include "C12832_lcd.h" +uLCD_4DGL lcd(p9, p10, p8); +SDFileSystem sd(p5, p6, p7, p14, "sd"); //SD card +int key = 0; +int *keyptr = &key; +int turn = 0; +int *turnptr = &turn; +char won = 'n'; +char start = 'n'; + +//Speaker +AnalogOut DACout(p18); +wave_player waver(&DACout); -C12832_LCD LCD; -AnalogIn Pot1(p19); -AnalogIn Pot2(p20); -PwmOut Speaker(p26); -PwmOut RGBLED_r(p23); -PwmOut RGBLED_g(p24); -PwmOut RGBLED_b(p25); -DigitalIn joyfire(p14); -BusIn joy(p15,p12,p13,p16); -BusOut leds(LED1,LED2,LED3,LED4); - -// mutex to make the lcd lib thread safe -Mutex lcd_mutex; +FILE *wave_file; +void drawerase() +{ + wave_file=fopen("/sd/erase16.wav","r"); + waver.play(wave_file); + fclose(wave_file); +} +void drawcircle() +{ + wave_file=fopen("/sd/circle16.wav","r"); + waver.play(wave_file); + fclose(wave_file); +} +void drawcross() +{ + wave_file=fopen("/sd/cross16.wav","r"); + waver.play(wave_file); + fclose(wave_file); +} -// Thread 1 -// print counter into first line and wait for 1 s -void thread1(void const *args) +//shift brite +DigitalOut latch(p15); +DigitalOut enable(p16); +SPI spi(p11, p12, p13); +void RGB_LED(int red, int green, int blue) { - int i; - while(true) { // thread loop - lcd_mutex.lock(); - LCD.locate(0,0); - LCD.set_font((unsigned char*) Small_6); - LCD.printf("Thread1 count: %d",i); - lcd_mutex.unlock(); - i++; - Thread::wait(1000); + unsigned int low_color=0; + unsigned int high_color=0; + high_color=(blue<<4)|((red&0x3C0)>>6); + low_color=(((red&0x3F)<<10)|(green)); + spi.write(high_color); + spi.write(low_color); + latch=1; + latch=0; +} +void winningLED() +{ + while(key!=4) { + RGB_LED( 100, 0, 0); + wait(.05); + RGB_LED( 0, 100, 0); + wait(.05); + RGB_LED( 0, 0, 100); + wait(.05); } } +void tieLED() +{ + RGB_LED(100, 100, 100); +} -// Thread 2 -// print counter into third line and wait for 0,5s -void thread2(void const *args) +DigitalOut myled1(LED1); +DigitalOut myled2(LED2); +DigitalOut myled3(LED3); +DigitalOut myled4(LED4); + +int pressed[12]={0}; + +//touch pad +InterruptIn interrupt(p26); +// Setup the i2c bus on pins 9 and 10 +I2C i2c(p28, p27); +// Setup the Mpr121: +// constructor(i2c object, i2c address of the mpr121) +Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); + +// Key hit/release interrupt routine +void fallInterrupt() { + int key_code=0; + int i=0; + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + // LED demo mod + i=0; + // puts key number out to LEDs for demo + for (i=0; i<12; i++) { + if (((value>>i)&0x01)==1) key_code=i+1; + } + myled4=key_code & 0x01; + myled3=(key_code>>1) & 0x01; + myled2=(key_code>>2) & 0x01; + myled1=(key_code>>3) & 0x01; + *keyptr = key_code; + start = 'y'; +} + +void drawBKG() +{ + lcd.cls(); + lcd.background_color(0x254542); + drawerase(); + lcd.line(2, 44, 126, 44, WHITE); + lcd.line(2, 85, 126, 85, WHITE); + lcd.line(44, 2, 44, 126, WHITE); + lcd.line(85, 2, 85, 126, WHITE); +} + +void winnercheck() { - int k; - while(true) { // thread loop - lcd_mutex.lock(); - LCD.locate(0,20); - LCD.set_font((unsigned char*) Arial_9); - LCD.printf("Thread 2 count : %d",k); - lcd_mutex.unlock(); - k++; - Thread::wait(500); // wait 0.5s + if (pressed[3]==pressed[7]&&pressed[7]==pressed[11]&&pressed[3]!=0) { + if(pressed[3]==1) { + lcd.locate(4,0); + lcd.printf("circle wins"); + } else if(pressed[3]==2) { + lcd.locate(5,0); + lcd.printf("cross wins"); + } + lcd.line(23,23,107,23, WHITE); + won = 'w'; + winningLED(); + } else if (pressed[2]==pressed[6]&&pressed[6]==pressed[10]&&pressed[2]!=0) { + if(pressed[2]==1) { + lcd.locate(4,0); + lcd.printf("circle wins"); + } else if(pressed[2]==2) { + lcd.locate(5,0); + lcd.printf("cross wins"); + } + lcd.line(23,65,107,65, WHITE); + won = 'w'; + winningLED(); + } else if (pressed[1]==pressed[5]&&pressed[5]==pressed[9]&&pressed[1]!=0) { + if(pressed[1]==1) { + lcd.locate(4,0); + lcd.printf("circle wins"); + } else if(pressed[1]==2) { + lcd.locate(5,0); + lcd.printf("cross wins"); + } + lcd.line(23,107,107,107, WHITE); + won = 'w'; + winningLED(); + } else if (pressed[3]==pressed[2]&&pressed[2]==pressed[1]&&pressed[3]!=0) { + if(pressed[3]==1) { + lcd.locate(4,0); + lcd.printf("circle wins"); + } else if(pressed[3]==2) { + lcd.locate(5,0); + lcd.printf("cross wins"); + } + lcd.line(23,23,23,107, WHITE); + won = 'w'; + winningLED(); + } else if (pressed[7]==pressed[6]&&pressed[6]==pressed[5]&&pressed[7]!=0) { + if(pressed[7]==1) { + lcd.locate(4,0); + lcd.printf("circle wins"); + } else if(pressed[7]==2) { + lcd.locate(5,0); + lcd.printf("cross wins"); + } + lcd.line(65,23,65,107, WHITE); + won = 'w'; + winningLED(); + } else if (pressed[11]==pressed[10]&&pressed[10]==pressed[9]&&pressed[11]!=0) { + if(pressed[11]==1) { + lcd.locate(4,0); + lcd.printf("circle wins"); + } else if(pressed[11]==2) { + lcd.locate(5,0); + lcd.printf("cross wins"); + } + lcd.line(107,23,107,107, WHITE); + won = 'w'; + winningLED(); + } else if (pressed[3]==pressed[6]&&pressed[6]==pressed[9]&&pressed[3]!=0) { + if(pressed[3]==1) { + lcd.locate(4,0); + lcd.printf("circle wins"); + } else if(pressed[3]==2) { + lcd.locate(5,0); + lcd.printf("cross wins"); + } + lcd.line(23,23,107,107, WHITE); + won = 'w'; + winningLED(); + } else if (pressed[11]==pressed[6]&&pressed[6]==pressed[1]&&pressed[11]!=0) { + if(pressed[11]==1) { + lcd.locate(4,0); + lcd.printf("circle wins"); + } else if(pressed[11]==2) { + lcd.locate(5,0); + lcd.printf("cross wins"); + } + lcd.line(107,23,23,107, WHITE); + won = 'w'; + winningLED(); + } else if (pressed[1]!=0 && pressed[2]!=0 &&pressed[3]!=0 && pressed[5]!=0 && pressed[6]!=0 && pressed[7]!=0 && pressed[9]!=0 && pressed[10]!=0 && pressed[11]!=0) { + lcd.locate(7,0); + lcd.printf("Tie"); + won = 'w'; + tieLED(); } } -// Thread 3 -// print a sin function in a small window -// the value of pot 1 changes the speed of the sine wave -void thread3(void const *args) -{ - int i,k,v; - double s,a; - k = 1; - lcd_mutex.lock(); - LCD.rect(89,0,127,17,1); - lcd_mutex.unlock(); - while(true) { // thread loop - v = Pot1.read_u16(); // get value of pot 1 - lcd_mutex.lock(); - for (i=90; i<127; i++) { - s = 8 * sin((long double)(i+k) /5); // pixel to print - a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase - LCD.pixel(i,9 + (int)a ,0); // erase pixel - LCD.pixel(i,9 + (int)s ,1); // print pixel - } - LCD.copy_to_lcd(); // LCD.pixel does not update the lcd - lcd_mutex.unlock(); - k++; - Thread::wait(v/100); // value of pot1 / 100 +void resetgame(){ + drawBKG(); + int i; + for (i =0; i<=11;i++)pressed[i]=0; + *turnptr = 0; + won = 'n'; + RGB_LED(100,0,0); + } + +int main() +{ + spi.format(16,0); + spi.frequency(500000); + enable=0; + latch=0; + + interrupt.fall(&fallInterrupt); + interrupt.mode(PullUp); + + //welcome + lcd.baudrate(3000000); + lcd.text_width(2); + lcd.text_height(2); + lcd.locate(0,2); + lcd.text_mode(TRANSPARENT); + lcd.color(WHITE); + lcd.printf(" Tic\n Tac\n Toe"); + wait(3); + lcd.cls(); + lcd.color(RED); + lcd.printf("Use lower area of the keypad to draw a circle or cross\n\nKey 3 to reset the game\n\nLED indicates next player\n\nPress any key to continue..."); + while(start!='y') { + wait(1); } -} - -// Thread 4 -// input pot 2 and change the contrast of LCD -void thread4(void const *args) -{ - int k; - while(true) { // thread loop - k = Pot2.read_u16(); // get the value of poti 2 - k = k >> 10; // need only 6 bits for contrast - lcd_mutex.lock(); - LCD.set_contrast(k); - lcd_mutex.unlock(); - Thread::wait(500); // wait 0.5s - } -} -// Thread 5 -// RGB LED -void thread5(void const *args) -{ - while(true) { // thread loop - RGBLED_r = 0.5 + (rand() % 11)/20.0; - RGBLED_g = 0.5 + (rand() % 11)/20.0; - RGBLED_b = 0.5 + (rand() % 11)/20.0; - Thread::wait(1667); // wait 1.5s + + lcd.cls(); + lcd.background_color(0x254542); + resetgame(); + lcd.textbackground_color(0x254542); + while (1) { + if (key == 4) { + resetgame(); + } + else if (pressed[key]==0 && won == 'n') { + if (key == 1) { + if (turn == 0) { + lcd.circle(23, 107, 10, RED); + drawcircle(); + RGB_LED(0,0,100); + } else if(turn == 1) { + lcd.line(18, 100, 28, 114, BLUE); + lcd.line(18, 114, 28, 100, BLUE); + drawcross(); + RGB_LED(100,0,0); + } + pressed[1]=turn+1; + *turnptr = (turn+1)%2; + winnercheck(); + } else if (key == 2) { + if (turn == 0) { + lcd.circle(23, 65, 10, RED); + drawcircle(); + RGB_LED(0,0,100); + } else if(turn == 1) { + lcd.line(18, 58, 28, 72, BLUE); + lcd.line(18, 72, 28, 58, BLUE); + drawcross(); + RGB_LED(100,0,0); + } + pressed[2]=turn+1; + *turnptr = (turn+1)%2; + winnercheck(); + } else if (key == 3) { + if (turn == 0) { + lcd.circle(23, 23, 10, RED); + drawcircle(); + RGB_LED(0,0,100); + } else if(turn == 1) { + lcd.line(18, 16, 28, 30, BLUE); + lcd.line(18, 30, 28, 16, BLUE); + drawcross(); + RGB_LED(100,0,0); + } + pressed[3]=turn+1; + *turnptr = (turn+1)%2; + winnercheck(); + } else if (key == 5) { + if (turn == 0) { + lcd.circle(65, 107, 10, RED); + drawcircle(); + RGB_LED(0,0,100); + } else if(turn == 1) { + lcd.line(60, 100, 70, 114, BLUE); + lcd.line(60, 114, 70, 100, BLUE); + drawcross(); + RGB_LED(100,0,0); + } + pressed[5]=turn+1; + *turnptr = (turn+1)%2; + winnercheck(); + } else if (key == 6) { + if (turn == 0) { + lcd.circle(65, 65, 10, RED); + drawcircle(); + RGB_LED(0,0,100); + } else if(turn == 1) { + lcd.line(60, 58, 70, 72, BLUE); + lcd.line(60, 72, 70, 58, BLUE); + drawcross(); + RGB_LED(100,0,0); + } + pressed[6]=turn+1; + *turnptr = (turn+1)%2; + winnercheck(); + } else if (key == 7) { + if (turn == 0) { + lcd.circle(65, 23, 10, RED); + drawcircle(); + RGB_LED(0,0,100); + } else if(turn == 1) { + lcd.line(60, 16, 70, 30, BLUE); + lcd.line(60, 30, 70, 16, BLUE); + drawcross(); + RGB_LED(100,0,0); + } + pressed[7]=turn+1; + *turnptr = (turn+1)%2; + winnercheck(); + } else if (key == 9) { + if (turn == 0) { + lcd.circle(107, 107, 10, RED); + drawcircle(); + RGB_LED(0,0,100); + } else if(turn == 1) { + lcd.line(102, 100, 112, 114, BLUE); + lcd.line(102, 114, 112, 100, BLUE); + drawcross(); + RGB_LED(100,0,0); + } + pressed[9]=turn+1; + *turnptr = (turn+1)%2; + winnercheck(); + } else if (key == 10) { + if (turn == 0) { + lcd.circle(107, 65, 10, RED); + drawcircle(); + RGB_LED(0,0,100); + } else if(turn == 1) { + lcd.line(102, 58, 112, 72, BLUE); + lcd.line(102, 72, 112, 58, BLUE); + drawcross(); + RGB_LED(100,0,0); + } + pressed[10]=turn+1; + *turnptr = (turn+1)%2; + winnercheck(); + } else if (key == 11) { + if (turn == 0) { + lcd.circle(107, 23, 10, RED); + drawcircle(); + RGB_LED(0,0,100); + } else if(turn == 1) { + lcd.line(102, 16, 112, 30, BLUE); + lcd.line(102, 30, 112, 16, BLUE); + drawcross(); + RGB_LED(100,0,0); + } + pressed[11]=turn+1; + *turnptr = (turn+1)%2; + winnercheck(); + } + } } } -// Thread 6 -// Speaker -void thread6(void const *args) -{ - while(true) { // thread loop - Speaker.period(1.0/800.0); - Speaker = 0.01; - Thread::wait(1000); // wait 1.0s - Speaker.period(1.0/969.0); - Speaker = 0.01; - Thread::wait(1000); // wait 1.0s - } -} - -// Thread 7 -// Joystick controls onboard mbed LEDs -void thread7(void const *args) -{ - while(true) { // thread loop - if (joyfire) { - leds = 0xf; - } else { - leds = joy; - } - Thread::wait(200); // wait 0.25s - } -} - - - -int main() -{ - int j; - LCD.cls(); - - Thread t1(thread1); //start thread1 - Thread t2(thread2); //start thread2 - Thread t3(thread3); //start thread3 - Thread t4(thread4); //start thread4 - Thread t5(thread5); //start thread5 - Thread t6(thread6); //start thread6 - Thread t7(thread7); //start thread7 - - while(true) { // main is the next thread - lcd_mutex.lock(); - LCD.locate(0,9); - LCD.set_font((unsigned char*) Small_7); - j = LCD.get_contrast(); // read the actual contrast - LCD.printf("contrast : %d",j); - lcd_mutex.unlock(); - Thread::wait(500); // wait 0.5s - } -}
--- a/mbed-rtos.lib Sun Sep 22 17:57:46 2013 +0000 +++ b/mbed-rtos.lib Tue Oct 20 17:24:08 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#ee87e782d34f +http://mbed.org/users/mbed_official/code/mbed-rtos/#83e169389a69
--- a/mbed.bld Sun Sep 22 17:57:46 2013 +0000 +++ b/mbed.bld Tue Oct 20 17:24:08 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.cpp Tue Oct 20 17:24:08 2015 +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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mpr121.h Tue Oct 20 17:24:08 2015 +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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Tue Oct 20 17:24:08 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/sravet/code/wave_player/#acc3e18e77ad