A simple game

Dependencies:   4DGL-uLCD-SE mbed-rtos mbed

Fork of rtos_signals by mbed official

Files at this revision

API Documentation at this revision

Comitter:
gmiles3
Date:
Fri Nov 04 13:45:06 2016 +0000
Parent:
3:c133402c77cb
Commit message:
Initial commit for angry balls game

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
Speaker.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
mpr121.cpp Show annotated file Show diff for this revision Revisions of this file
mpr121.h Show annotated file Show diff for this revision Revisions of this file
diff -r c133402c77cb -r 2bb5deb83b81 4DGL-uLCD-SE.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/4DGL-uLCD-SE.lib	Fri Nov 04 13:45:06 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#2cb1845d7681
diff -r c133402c77cb -r 2bb5deb83b81 Speaker.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Fri Nov 04 13:45:06 2016 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+// a 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 c133402c77cb -r 2bb5deb83b81 main.cpp
--- a/main.cpp	Tue Jun 04 15:54:12 2013 +0100
+++ b/main.cpp	Fri Nov 04 13:45:06 2016 +0000
@@ -1,21 +1,422 @@
 #include "mbed.h"
 #include "rtos.h"
+#include <mpr121.h>
+#include "uLCD_4DGL.h"
+#include "Speaker.h"
 
-DigitalOut led(LED1);
+uLCD_4DGL uLCD(p9,p10,p11);
+int curr_x_u, curr_y_u, old_x_u, old_y_u; //user's current and old x,y coordinates
+int curr_x_e, curr_y_e, old_x_e, old_y_e; //enemy's current and old x,y coordinates
+float dx_e, dy_e, fx_e, fy_e; //x,y velocity of enemy and floating point representation of x,y coordinates
+int curr_x_b, curr_y_b, old_x_b, old_y_b; //payload's current and old x,y coordinates
+int curr_x_p, curr_y_p, old_x_p, old_y_p; //portal's current and old x,y coordinates
+Mutex lcd_mutex;
+
+Thread box; //handles user-payload collision logic
+Thread portal; //handles portal logic
+
+Speaker amp(p21);
+
+Serial pc(USBTX, USBRX); //debugging
+
+InterruptIn interrupt(p26);
+I2C i2c(p28, p27);
+Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
+
+int score = 0;
+int oldscore = 0;
+
+volatile int key = -1; //key pressed from keypad; set via interrupts -> volatile so compiler doesn't optimize out value reads
+
+enum State { START, LIVE, PAUSE, DEAD }; //game status
+volatile State s = START;
+
+//interrupt method that runs when keypad has a new value
+void fallInterrupt() {
+    int key_code=0;
+    int i=0;
+    int value=mpr121.read(0x00);
+    value +=mpr121.read(0x01)<<8;
+    i=0;
+    for (i=0; i<12; i++) {
+        if (((value>>i)&0x01)==1) key_code=i+1;
+    }
+    key = key_code;
+}
+
+//handles portal logic
+void portal_thread() {
+    while(s == LIVE || s == PAUSE) {
+        while(s == PAUSE);
+        //if payload and portal collide, score a point and randomize portal location
+        if (((curr_x_p-3 <= curr_x_b+3 && curr_x_p >= curr_x_b) || (curr_x_p+3 >= curr_x_b-3 && curr_x_p <= curr_x_b)) && ((curr_y_p+3 >= curr_y_b-3 && curr_y_p <= curr_y_b) || (curr_y_p-3 <= curr_y_b+3 && curr_y_p >= curr_y_b))) {
+            score++;
+            curr_x_p = (rand() % 106) + 10;
+            curr_y_p = (rand() % 106) + 10;
+        } else if (((curr_y_p-3 <= curr_y_b+3 && curr_y_p >= curr_y_b) || (curr_y_p+3 >= curr_y_b-3 && curr_y_p <= curr_y_b)) && ((curr_x_p+3 >= curr_x_b-3 && curr_x_p <= curr_x_b) || (curr_x_p-3 <= curr_x_b+3 && curr_x_p >= curr_x_b))) {
+            score++;
+            curr_x_p = (rand() % 106) + 10;
+            curr_y_p = (rand() % 106) + 10;
+        }
+        lcd_mutex.lock();
+        //redraw portal and play note if score has changed
+        uLCD.rectangle(curr_x_p-5, curr_y_p-5, curr_x_p+5, curr_y_p+5, BLUE);
+        if (score != oldscore) {
+            amp.PlayNote(1000.0,0.025,0.5);
+            oldscore = score;
+            uLCD.rectangle(old_x_p-5, old_y_p-5, old_x_p+5, old_y_p+5, BLACK);
+            old_x_p = curr_x_p;
+            old_y_p = curr_y_p;
+        }
+        //print score
+        uLCD.locate(1,15);
+        uLCD.color(0xFF00FF);
+        uLCD.printf("%d", score);
+        lcd_mutex.unlock();
+    }
+}
 
-void led_thread(void const *argument) {
-    while (true) {
-        // Signal flags that are reported as event are automatically cleared.
-        Thread::signal_wait(0x1);
-        led = !led;
+//handles user-payload collisions
+void box_thread() {
+    while(s == LIVE) {
+        old_x_u = curr_x_u;
+        old_y_u = curr_y_u;
+        old_x_b = curr_x_b;
+        old_y_b = curr_y_b;
+        //if 3 pressed, then go into pause state
+        if (key==3+1) {
+            s = PAUSE;
+            lcd_mutex.lock();
+            uLCD.locate(1, 2);
+            uLCD.color(0xFFFF00);
+            uLCD.printf("PAUSE");
+            //wait until 3 is unpressed
+            while(key==3+1);
+            //wait until 3 is pressed
+            while(key!=3+1);
+            //wait until 3 is unpressed
+            while(key==3+1);
+            uLCD.locate(1, 2);
+            uLCD.color(BLACK);
+            uLCD.printf("PAUSE");
+            uLCD.color(WHITE);
+            //countdown from 3
+            for (int i=3; i>=1; i--) {
+                uLCD.locate(1, 2);
+                uLCD.printf("%2D", i);
+                amp.PlayNote(800.0,0.025,0.5);
+                wait(1);
+            }
+            uLCD.locate(1,2);
+            uLCD.color(BLACK);
+            uLCD.printf("  ");
+            lcd_mutex.unlock();
+            //play on
+            s = LIVE;
+        }
+        //really complicated collision logic
+        //basically checks each way the boxes could be colliding based on which key is pressed (which direction user is moving)
+        if (key==0+1) {
+
+            if ((curr_x_b+6 > curr_x_u-6 && curr_x_u > curr_x_b) && ((curr_y_u+6 > curr_y_b-6 && curr_y_u <= curr_y_b) || (curr_y_u-6 < curr_y_b+6 && curr_y_u >= curr_y_b))) {
+                curr_x_b-=1;
+                curr_x_u = curr_x_b+12;
+                if (curr_x_b < 7) {
+                    curr_x_b = 25;
+                    curr_y_b = 63;
+                }
+            } else {
+                curr_x_u-=1;
+            }
+            if (curr_x_u < 7) curr_x_u = 7;
+        } else if (key==8+1) {
+
+            if ((curr_x_b-6 < curr_x_u+6 && curr_x_u < curr_x_b) && ((curr_y_u+6 > curr_y_b-6 && curr_y_u <= curr_y_b) || (curr_y_u-6 < curr_y_b+6 && curr_y_u >= curr_y_b))) {
+                curr_x_b+=1;
+                curr_x_u = curr_x_b-12;
+                if (curr_x_b > 120) {
+                    curr_x_b = 25;
+                    curr_y_b = 63;
+                }
+            } else {
+                curr_x_u+=1;
+            }
+            if (curr_x_u > 120) curr_x_u = 120;
+        } else if (key==4+1) {
+
+            if ((curr_y_b-6 < curr_y_u+6 && curr_y_u < curr_y_b) && ((curr_x_u+6 > curr_x_b-6 && curr_x_u <= curr_x_b) || (curr_x_u-6 < curr_x_b+6 && curr_x_u >= curr_x_b))) {
+                curr_y_b+=1;
+                curr_y_u = curr_y_b-12;
+                if (curr_y_b > 120) {
+                    curr_x_b = 25;
+                    curr_y_b = 63;
+                }
+            } else {
+                curr_y_u+=1;
+            }
+            if (curr_y_u > 120) curr_y_u = 120;
+        } else if (key==5+1) {
+
+            if ((curr_y_b+6 > curr_y_u-6 && curr_y_u > curr_y_b) && ((curr_x_u+6 > curr_x_b-6 && curr_x_u <= curr_x_b) || (curr_x_u-6 < curr_x_b+6 && curr_x_u >= curr_x_b))) {
+                curr_y_b-=1;
+                curr_y_u = curr_y_b+12;
+                if (curr_y_b < 7) {
+                    curr_x_b = 25;
+                    curr_y_b = 63;
+                }
+            } else {
+                curr_y_u-=1;
+            }
+            if (curr_y_u < 7) curr_y_u = 7;
+        }
+        //redraw user and payload
+        lcd_mutex.lock();
+        uLCD.rectangle(20, 58, 30, 68, WHITE);
+        uLCD.filled_rectangle(old_x_u-5, old_y_u-5, old_x_u+5, old_y_u+5, BLACK);
+        uLCD.filled_rectangle(curr_x_u-5, curr_y_u-5, curr_x_u+5, curr_y_u+5, GREEN);
+        uLCD.filled_rectangle(old_x_b-5, old_y_b-5, old_x_b+5, old_y_b+5, BLACK);
+        uLCD.filled_rectangle(curr_x_b-5, curr_y_b-5, curr_x_b+5, curr_y_b+5, DGREY);
+        lcd_mutex.unlock();
     }
 }
 
-int main (void) {
-    Thread thread(led_thread);
-    
-    while (true) {
-        Thread::wait(1000);
-        thread.signal_set(0x1);
+//main thread handles setup of game, angry ball collision logic, gameover, and game resets
+int main() {
+    //setup keypad interrupts
+    interrupt.fall(&fallInterrupt);
+    interrupt.mode(PullUp);
+    uLCD.baudrate(3000000);
+    //load in Angry Balls image from SD
+    uLCD.media_init();
+    uLCD.set_sector_address(0x003B, 0xD400);
+    uLCD.display_image(0,0);
+    //wait 3 seconds to allow keypad time to set up while player can look at the pretty image
+    wait(3);
+    uLCD.textbackground_color(WHITE);
+    uLCD.text_width(1);
+    uLCD.text_height(1);
+    uLCD.color(RED);
+    uLCD.locate(0, 0);
+    uLCD.printf("Angry Balls!");
+    uLCD.locate(0, 1);
+    uLCD.color(BLACK);
+    uLCD.printf("Press 3 to play");
+    //start tone
+    amp.PlayNote(700.0,0.2,0.5);
+    amp.PlayNote(1000.0,0.2,0.0);
+    amp.PlayNote(600.0,0.1,0.5);
+    amp.PlayNote(1000.0,0.1,0.0);
+    amp.PlayNote(700.0,0.5,0.5);
+    //wait until 3 is pressed
+    while(key != 3+1);
+    //wait until 3 is unpressed
+    while(key == 3+1);
+    //clear screen
+    uLCD.cls();
+    uLCD.textbackground_color(BLACK);
+    uLCD.background_color(BLACK);
+    //setup initial state for every object
+    curr_x_u = 9;
+    curr_y_u = 63;
+    curr_x_b = 25;
+    curr_y_b = 63;
+    curr_x_e = 75;
+    curr_y_e = 75;
+    dx_e = -0.75;
+    dy_e = 0.75;
+    curr_x_p = 110;
+    curr_y_p = 15;
+    old_x_p = curr_x_p;
+    old_y_p = curr_y_p;
+    //draw everything
+    uLCD.filled_rectangle(curr_x_u-5, curr_y_u-5, curr_x_u+5, curr_y_u+5, GREEN);
+    uLCD.filled_rectangle(curr_x_b-5, curr_y_b-5, curr_x_b+5, curr_y_b+5, DGREY);
+    uLCD.filled_circle(curr_x_e, curr_y_e, 6, RED);
+    uLCD.rectangle(curr_x_p-5, curr_y_p-5, curr_x_p+5, curr_y_p+5, BLUE);
+    uLCD.color(WHITE);
+    uLCD.locate(1,2);
+    uLCD.printf("Place ball!");
+    //logic for placing ball before game starts based on key presses; prevents ball from going through walls & middle boundary
+    while(key != 3+1) {
+        old_x_e = curr_x_e;
+        old_y_e = curr_y_e;
+        if (key==0+1) {
+            curr_x_e-=2;
+            if (curr_x_e < 71) curr_x_e = 71;
+        }
+        if (key==8+1) {
+            curr_x_e+=2;
+            if (curr_x_e > 120) curr_x_e = 120;
+        }
+        if (key==4+1) {
+            curr_y_e+=2;
+            if (curr_y_e > 120) curr_y_e = 120;
+        }
+        if (key==5+1) {
+            curr_y_e-=2;
+            if (curr_y_e < 7) curr_y_e = 7;
+        }
+        //draw ball
+        uLCD.locate(1,2);
+        uLCD.printf("Place ball!");
+        uLCD.rectangle(curr_x_p-5, curr_y_p-5, curr_x_p+5, curr_y_p+5, BLUE);
+        uLCD.filled_circle(old_x_e, old_y_e, 6, BLACK);
+        uLCD.filled_circle(curr_x_e, curr_y_e, 6, RED);
+    }
+    //wait until 3 is unpressed
+    while(key==3+1);
+    uLCD.locate(1,2);
+    uLCD.color(BLACK);
+    uLCD.printf("Place ball!");
+    fx_e = curr_x_e;
+    fy_e = curr_y_e;
+    uLCD.color(WHITE);
+    //countdown til start
+    for (int i=3; i>=1; i--) {
+        uLCD.locate(1, 2);
+        uLCD.printf("%2D", i);
+        amp.PlayNote(800.0,0.025,0.5);
+        wait(1);
     }
-}
+    uLCD.locate(1,2);
+    uLCD.color(BLACK);
+    uLCD.printf("  ");
+    //game is live, start other two threads
+    s = LIVE;
+    box.start(box_thread);
+    portal.start(portal_thread);
+    //collision logic for angry ball
+    //check if ball has collided with a wall, the user, or the payload (gameover)
+    while(s == LIVE || s == PAUSE) {
+        while (s==PAUSE);
+        old_x_e = curr_x_e;
+        old_y_e = curr_y_e;
+        if (curr_x_e < 7) {
+            curr_x_e = 7;
+            dx_e = -dx_e;
+        } else if (curr_x_e > 120) {
+            curr_x_e = 120;
+            dx_e = -dx_e;
+        } else if (curr_y_e > 120) {
+            curr_y_e = 120;
+            dy_e = -dy_e;
+        } else if (curr_y_e < 7) {
+            curr_y_e = 7;
+            dy_e = -dy_e;
+        } else if (((dx_e < 0 && curr_x_e-6 < curr_x_u+6 && curr_x_e > curr_x_u) || (dx_e > 0 && curr_x_e+6 > curr_x_u-6 && curr_x_e < curr_x_u)) && ((curr_y_e+6 > curr_y_u-6 && curr_y_e < curr_y_u) || (curr_y_e-6 < curr_y_u+6 && curr_y_e > curr_y_u))) {
+            dx_e = -dx_e;
+        } else if (((dy_e < 0 && curr_y_e-6 < curr_y_u+6 && curr_y_e > curr_y_u) || (dy_e > 0 && curr_y_e+6 > curr_y_u-6 && curr_y_e < curr_y_u)) && ((curr_x_e+6 > curr_x_u-6 && curr_x_e < curr_x_u) || (curr_x_e-6 < curr_x_u+6 && curr_x_e > curr_x_u))) {
+            dy_e = -dy_e;
+        }
+        if (((dx_e < 0 && curr_x_e-6 < curr_x_b+6 && curr_x_e > curr_x_b) || (dx_e > 0 && curr_x_e+6 > curr_x_b-6 && curr_x_e < curr_x_b)) && ((curr_y_e+6 > curr_y_b-6 && curr_y_e < curr_y_b) || (curr_y_e-6 < curr_y_b+6 && curr_y_e > curr_y_b))) {
+            s = DEAD;
+        } else if (((dy_e < 0 && curr_y_e-6 < curr_y_b+6 && curr_y_e > curr_y_b) || (dy_e > 0 && curr_y_e+6 > curr_y_b-6 && curr_y_e < curr_y_b)) && ((curr_x_e+6 > curr_x_b-6 && curr_x_e < curr_x_b) || (curr_x_e-6 < curr_x_b+6 && curr_x_e > curr_x_b))) {
+            s = DEAD;
+        }
+        //update floating point coordinates with velocity values
+        fx_e += dx_e;
+        fy_e += dy_e;
+        //cast floating coordinates to integer coordinates
+        curr_x_e = (int) fx_e;
+        curr_y_e = (int) fy_e;
+        //redraw ball
+        lcd_mutex.lock();
+        uLCD.filled_circle(old_x_e, old_y_e, 6, BLACK);
+        uLCD.filled_circle(curr_x_e, curr_y_e, 6, RED);
+        lcd_mutex.unlock();
+        //if ball-payload collision has occurred, game is now dead
+        if (s == DEAD) {
+            //print gameover, score, instructions to start over, and play gameover sound
+            lcd_mutex.lock();
+            uLCD.cls();
+            uLCD.locate(1,2);
+            uLCD.color(RED);
+            uLCD.printf("GAMEOVER");
+            uLCD.locate(1,4);
+            uLCD.color(0x4B0082);
+            uLCD.printf("Score:%d", score);
+            uLCD.locate(1,8);
+            uLCD.color(WHITE);
+            uLCD.printf("Press 3 to retry");
+            amp.PlayNote(300.0,0.5,0.5);
+            amp.PlayNote(300.0,0.05,0.0);
+            amp.PlayNote(280.0,0.5,0.5);
+            amp.PlayNote(300.0,0.05,0.0);
+            amp.PlayNote(260.0,1.0,0.5);
+            //wait until 3 is pressed
+            while(key != 3+1);
+            //wait until 3 is unpressed
+            while(key == 3+1);
+            //reinitialize game state for new game; same code as above
+            uLCD.cls();
+            score = 0;
+            oldscore = 0;
+            curr_x_u = 9;
+            curr_y_u = 63;
+            curr_x_b = 25;
+            curr_y_b = 63;
+            curr_x_e = 75;
+            curr_y_e = 75;
+            dx_e = -0.75;
+            dy_e = 0.75;
+            curr_x_p = 110;
+            curr_y_p = 15;
+            old_x_p = curr_x_p;
+            old_y_p = curr_y_p;
+            uLCD.filled_rectangle(curr_x_u-5, curr_y_u-5, curr_x_u+5, curr_y_u+5, GREEN);
+            uLCD.filled_rectangle(curr_x_b-5, curr_y_b-5, curr_x_b+5, curr_y_b+5, DGREY);
+            uLCD.filled_circle(curr_x_e, curr_y_e, 6, RED);
+            uLCD.rectangle(curr_x_p-5, curr_y_p-5, curr_x_p+5, curr_y_p+5, BLUE);
+            uLCD.color(WHITE);
+            uLCD.locate(1,2);
+            uLCD.printf("Place ball!");
+            while(key != 3+1) {
+                old_x_e = curr_x_e;
+                old_y_e = curr_y_e;
+                if (key==0+1) {
+                    curr_x_e-=2;
+                    if (curr_x_e < 71) curr_x_e = 71;
+                }
+                if (key==8+1) {
+                    curr_x_e+=2;
+                    if (curr_x_e > 120) curr_x_e = 120;
+                }
+                if (key==4+1) {
+                    curr_y_e+=2;
+                    if (curr_y_e > 120) curr_y_e = 120;
+                }
+                if (key==5+1) {
+                    curr_y_e-=2;
+                    if (curr_y_e < 7) curr_y_e = 7;
+                }
+                uLCD.locate(1,2);
+                uLCD.printf("Place ball!");
+                uLCD.rectangle(curr_x_p-5, curr_y_p-5, curr_x_p+5, curr_y_p+5, BLUE);
+                uLCD.filled_circle(old_x_e, old_y_e, 6, BLACK);
+                uLCD.filled_circle(curr_x_e, curr_y_e, 6, RED);
+            }
+            while(key==3+1);
+            uLCD.locate(1,2);
+            uLCD.color(BLACK);
+            uLCD.printf("Place ball!");
+            fx_e = curr_x_e;
+            fy_e = curr_y_e;
+            uLCD.color(WHITE);
+            for (int i=3; i>=1; i--) {
+                uLCD.locate(1, 2);
+                uLCD.printf("%2D", i);
+                amp.PlayNote(800.0,0.025,0.5);
+                wait(1);
+            }
+            uLCD.locate(1,2);
+            uLCD.color(BLACK);
+            uLCD.printf("  ");
+            lcd_mutex.unlock();
+            //game is live again
+            //restart the other threads
+            s = LIVE;
+            box.start(box_thread);
+            portal.start(portal_thread);
+        }
+    }
+}
\ No newline at end of file
diff -r c133402c77cb -r 2bb5deb83b81 mbed-rtos.lib
--- a/mbed-rtos.lib	Tue Jun 04 15:54:12 2013 +0100
+++ b/mbed-rtos.lib	Fri Nov 04 13:45:06 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed-rtos/#3da5f554d8bf
diff -r c133402c77cb -r 2bb5deb83b81 mbed.bld
--- a/mbed.bld	Tue Jun 04 15:54:12 2013 +0100
+++ b/mbed.bld	Fri Nov 04 13:45:06 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/9bcdf88f62b0
\ No newline at end of file
diff -r c133402c77cb -r 2bb5deb83b81 mpr121.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mpr121.cpp	Fri Nov 04 13:45:06 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 c133402c77cb -r 2bb5deb83b81 mpr121.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mpr121.h	Fri Nov 04 13:45:06 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