simple game using 2 mbed devices

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

Files at this revision

API Documentation at this revision

Comitter:
bfoley13
Date:
Tue Oct 20 20:04:10 2015 +0000
Commit message:
Project

Changed in this revision

player1/4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
player1/main.cpp Show annotated file Show diff for this revision Revisions of this file
player1/mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
player1/mbed.bld Show annotated file Show diff for this revision Revisions of this file
player2/LSM9DS0.lib Show annotated file Show diff for this revision Revisions of this file
player2/SongPlayer.h Show annotated file Show diff for this revision Revisions of this file
player2/main.cpp Show annotated file Show diff for this revision Revisions of this file
player2/mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
player2/mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player1/4DGL-uLCD-SE.lib	Tue Oct 20 20:04:10 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player1/main.cpp	Tue Oct 20 20:04:10 2015 +0000
@@ -0,0 +1,334 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "uLCD_4DGL.h"
+#include <math.h>
+
+Serial pc(USBTX, USBRX);
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+//digital in for player 1 contorls
+DigitalIn up1(p16);
+DigitalIn center1(p17);
+DigitalIn left1(p15);
+DigitalIn down1(p19);
+DigitalIn right1(p20);
+////
+//////digital in for player 2 controls
+DigitalIn up2(p22);
+DigitalIn center2(p23);
+DigitalIn left2(p24);
+DigitalIn down2(p25);
+DigitalIn right2(p26);
+//pwmout for sound
+AnalogOut sound_out(p18);
+//
+////mutex lock for printing to the lcd (only one thread can have the lock at once)
+//Mutex p1_mutex;
+//Mutex p2_mutex;
+//
+//the lcd instance to print to
+uLCD_4DGL uLCD(p9, p10, p11);
+
+typedef struct {
+    int x;
+    int y;
+    int old_x;
+    int old_y;
+    int points;
+    int size;
+    int color;
+} player;
+
+player player1 = {5,5,0,0,0,5,0xFFFFFF}; 
+player player2 = {120,120,0,0,0,5,0xFF00FF}; 
+
+
+int getDX(int left, int right){
+    if(left == 0)
+        return -1;
+    else if (right == 0)
+        return 1;
+    else 
+        return 0;
+}
+
+int getDY(int up, int down){
+    if(up == 0)
+        return -1;
+    else if (down == 0)
+        return 1;
+    else 
+        return 0;
+}
+
+
+void getP1Info(void const *args){
+    while(1){
+//        int dx = getDX(left1, right1);
+//        int dy = getDY(up1, down1);
+//        p1_mutex.lock();
+//        player1.x += dx;
+//        player1.y += dy;
+//        p1_mutex.unlock();
+//        
+        led1 = !led1;
+        Thread::wait(100);
+    }
+    
+}
+
+void getP2Info(void const *args){
+    while(1){
+ //       int dx = getDX(left2, right2);
+//        int dy = getDY(up2, down2);
+//        p2_mutex.lock();
+//        player2.x += dx;
+//        player2.y += dy;
+//        p2_mutex.unlock();
+//        
+        led2 = !led2;
+        Thread::wait(100);
+        
+        
+    }
+}
+
+void playMusic(double amp){
+    sound_out = amp;
+}
+
+////the method that displays information to 
+//void display2LCD(char x, char y, const char* s){
+//    lcd.locate(x,y);
+//    lcd.printf("%s\n", s);
+//}
+
+char* strAppendInt(const char* s, int i){
+    char integer_string[32];
+    sprintf(integer_string, "%d", i);
+
+    char* new_s = (char*) malloc(strlen(s) + strlen(integer_string));
+    strcpy(new_s, s);
+    strcat(new_s, integer_string); // other_string now contains "Integer: 1234"
+    
+    return new_s;
+}
+
+void fixPlayersXY(player* player){
+    if(player->x < 2){
+        player->x = 3;
+    }else if(player->x > 126){
+        player->x = 125;
+    }
+    
+    if(player->y < 2){
+        player->y = 3;
+    }else if(player->y > 126){
+        player->y = 125;
+    }
+}
+
+void update(){
+    while(1){
+        //p1_mutex.lock();
+        
+        int dx = getDX(left1, right1);
+        int dy = getDY(up1, down1);
+        player1.x += dx;
+        player1.y += dy;
+        fixPlayersXY(&player1);
+        uLCD.circle(player1.old_x , player1.old_y , player1.size, 0);
+        uLCD.circle(player1.x , player1.y , player1.size, player1.color);
+        player1.old_x = player1.x;
+        player1.old_y = player1.y;
+        int x1 = player1.x;
+        int y1 = player1.y;
+        
+ //       char* s = "\n1:";
+//        pc.printf(strAppendInt(strAppendInt(s, x1), y1));
+        
+        //p1_mutex.unlock();
+        
+        //p2_mutex.lock();
+        int dx2 = getDX(left2, right2);
+        int dy2 = getDY(up2, down2);        
+        player2.x += dx2;
+        player2.y += dy2;
+        fixPlayersXY(&player2);
+                
+        uLCD.circle(player2.old_x , player2.old_y , player2.size, 0);
+        uLCD.circle(player2.x , player2.y , player2.size, player2.color);
+        player2.old_x = player2.x;
+        player2.old_y = player2.y;
+        int x2 = player2.x;
+        int y2 = player2.y;
+        
+        //char* s1 = " 2:";
+        //pc.printf(strAppendInt(strAppendInt(s1, x2), y2));
+        
+        //p2_mutex.unlock();
+        
+        double dist = sqrt((double) ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)));
+        //char* s2 = " d:";
+        //pc.printf(strAppendInt(s2, (int) (dist*100)));
+        
+        playMusic(dist/1810.0);
+        
+        //WINNING CONDITION        
+        if(dist < (double) player1.size || dist < (double) player2.size){
+            while(1){
+                playMusic(0.0);
+            }
+        }
+
+        //uLCD.filled_circle(60, 50, 30, 0xFF00FF);
+//        uLCD.triangle(120, 100, 40, 40, 10, 100, 0x0000FF);
+//        uLCD.line(0, 0, 80, 60, 0xFF0000);
+//        uLCD.filled_rectangle(50, 50, 100, 90, 0x00FF00);
+//        uLCD.pixel(60, 60, BLACK);
+//        uLCD.read_pixel(120, 70);
+//        uLCD.circle(120, 60, 10, BLACK);
+//        uLCD.set_font(FONT_7X8);
+//        uLCD.text_mode(TRANSPARENT);
+//        uLCD.text_bold(ON);
+//        uLCD.text_char('B', 9, 8, BLACK);
+//        uLCD.text_char('I',10, 8, BLACK);
+//        uLCD.text_char('G',11, 8, BLACK);
+//        uLCD.text_italic(ON);
+//        uLCD.text_string("This is a test of string", 1, 4, FONT_7X8, WHITE);
+//        wait(2);
+        
+        led3 = !led3;
+        wait(0.05);
+    }
+}
+
+
+//sets up the LCD display
+void setup(){
+    //set up the lcd
+    uLCD.baudrate(3000000);
+    uLCD.background_color(0);
+    uLCD.cls();
+    uLCD.printf("Initializing...");
+    uLCD.cls();
+    
+    pc.baud(9600);
+    
+    //load the wave file from sd
+    //loadWaveFileFromSD();
+}
+
+
+int main() {
+    //set up for the threads
+    setup();
+
+    //test();
+
+//    Thread t1(getP1Info);
+//    Thread t2(getP2Info);
+    //one of the threads is just going to be the main method
+    update();
+}
+
+
+
+//        int dur = mySpeaker.getDuration();
+//        int note = mySpeaker.getNoteCount();
+//        
+//        char* s = "Duration:";
+//        s = strAppendInt(s, dur);
+//        strcat(s, ", Current Note:");
+//        s = strAppendInt(s, note); 
+//        
+//        display2LCD(0, 0, s);
+//        
+//        Thread::wait(1000);
+
+////appends an integer to a string and returns the pointer to the new string
+
+
+
+//
+//void test(){
+//    uLCD.printf("\nHello uLCD World\n"); //Default Green on black text
+//    uLCD.printf("\n  Starting Demo...");
+//    uLCD.text_width(4); //4X size text
+//    uLCD.text_height(4);
+//    uLCD.color(RED);
+//    for (int i=10; i>=0; --i) {
+//        uLCD.locate(1,2);
+//        uLCD.printf("%2D",i);
+//        wait(.5);
+//    }
+//    uLCD.cls();
+//    uLCD.printf("Change baudrate......");
+//    uLCD.baudrate(3000000); //jack up baud rate to max for fast display
+//    //if demo hangs here - try lower baud rates
+//    //
+//    // printf text only full screen mode demo
+//    uLCD.background_color(BLUE);
+//    uLCD.cls();
+//    uLCD.locate(0,0);
+//    uLCD.color(WHITE);
+//    uLCD.textbackground_color(BLUE);
+//    uLCD.set_font(FONT_7X8);
+//    uLCD.text_mode(OPAQUE);
+//    int i=0;
+//    while(i<64) {
+//        if(i%16==0) uLCD.cls();
+//        uLCD.printf("TxtLine %2D Page %D\n",i%16,i/16 );
+//        i++; //16 lines with 18 charaters per line
+//    }
+//    wait(0.5);
+//    //demo graphics commands
+//    uLCD.background_color(BLACK);
+//    uLCD.cls();
+//    uLCD.background_color(DGREY);
+//    uLCD.filled_circle(60, 50, 30, 0xFF00FF);
+//    uLCD.triangle(120, 100, 40, 40, 10, 100, 0x0000FF);
+//    uLCD.line(0, 0, 80, 60, 0xFF0000);
+//    uLCD.filled_rectangle(50, 50, 100, 90, 0x00FF00);
+//    uLCD.pixel(60, 60, BLACK);
+//    uLCD.read_pixel(120, 70);
+//    uLCD.circle(120, 60, 10, BLACK);
+//    uLCD.set_font(FONT_7X8);
+//    uLCD.text_mode(TRANSPARENT);
+//    uLCD.text_bold(ON);
+//    uLCD.text_char('B', 9, 8, BLACK);
+//    uLCD.text_char('I',10, 8, BLACK);
+//    uLCD.text_char('G',11, 8, BLACK);
+//    uLCD.text_italic(ON);
+//    uLCD.text_string("This is a test of string", 1, 4, FONT_7X8, WHITE);
+//    wait(2);
+// 
+////Bouncing Ball Demo
+//    float fx=50.0,fy=21.0,vx=1.0,vy=0.4;
+//    int x=50,y=21,radius=4;
+//    uLCD.background_color(BLACK);
+//    uLCD.cls();
+//    //draw walls
+//    uLCD.line(0, 0, 127, 0, WHITE);
+//    uLCD.line(127, 0, 127, 127, WHITE);
+//    uLCD.line(127, 127, 0, 127, WHITE);
+//    uLCD.line(0, 127, 0, 0, WHITE);
+//    for (int i=0; i<1500; i++) {
+//        //draw ball
+//        uLCD.filled_circle(x, y, radius, RED);
+//        //bounce off edge walls and slow down a bit?
+//        if ((x<=radius+1) || (x>=126-radius)) vx = -.90*vx;
+//        if ((y<=radius+1) || (y>=126-radius)) vy = -.90*vy;
+//        //erase old ball location
+//        uLCD.filled_circle(x, y, radius, BLACK);
+//        //move ball
+//        fx=fx+vx;
+//        fy=fy+vy;
+//        x=(int)fx;
+//        y=(int)fy;
+//    }
+//}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player1/mbed-rtos.lib	Tue Oct 20 20:04:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#d7bd06319118
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player1/mbed.bld	Tue Oct 20 20:04:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player2/LSM9DS0.lib	Tue Oct 20 20:04:10 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/aswild/code/LSM9DS0/#5556e6fb99f5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player2/SongPlayer.h	Tue Oct 20 20:04:10 2015 +0000
@@ -0,0 +1,52 @@
+//#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class SongPlayer
+{
+public:
+    SongPlayer(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlaySong(float frequency[], float duration[], float volume=1.0) {
+        vol = volume;
+        notecount = 0;
+        _pin.period(1.0/frequency[notecount]);
+        _pin = volume/2.0;
+        noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
+        // setup timer to interrupt for next note to play
+        frequencyptr = frequency;
+        durationptr = duration;
+        //returns after first note starts to play
+    }
+    void nextnote();
+    int getDuration();
+    int getNoteCount();
+    
+private:
+    Timeout noteduration;
+    PwmOut _pin;
+    int notecount;
+    float vol;
+    float * frequencyptr;
+    float * durationptr;
+};
+//Interrupt Routine to play next note
+void SongPlayer::nextnote()
+{
+    _pin = 0.0;
+    notecount++; //setup next note in song
+    if (durationptr[notecount]!=0.0) {
+        _pin.period(1.0/frequencyptr[notecount]);
+        noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
+        _pin = vol/2.0;
+    } else
+        _pin = 0.0; //turn off on last note
+}
+
+int SongPlayer::getDuration(){
+    return sizeof(durationptr)/sizeof(durationptr[0]);   
+}
+
+int SongPlayer::getNoteCount(){
+    return notecount;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player2/main.cpp	Tue Oct 20 20:04:10 2015 +0000
@@ -0,0 +1,120 @@
+#include "rtos.h"
+#include "mbed.h"
+#include <math.h>
+#include "LSM9DS0.h"
+#include "SongPlayer.h"
+
+SongPlayer mySpeaker(p26);
+AnalogIn aIn(p20);
+AnalogOut aOut(p18);
+
+DigitalOut left(p21);
+DigitalOut right(p22);
+DigitalOut up(p23);
+DigitalOut down(p24);
+DigitalOut center(p25);
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+// SDO_XM and SDO_G are pulled up, so our addresses are:
+#define LSM9DS0_XM_ADDR  0x1D // Would be 0x1E if SDO_XM is LOW
+#define LSM9DS0_G_ADDR   0x6B // Would be 0x6A if SDO_G is LOW
+
+// refresh time. set to 500 for part 2 and 50 for part 4
+#define REFRESH_TIME_MS 500
+
+// Verify that the pin assignments below match your breadboard
+LSM9DS0 imu(p9, p10, LSM9DS0_G_ADDR, LSM9DS0_XM_ADDR);
+
+
+Serial pc(USBTX, USBRX);
+float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
+                 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
+                };
+float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
+                     0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
+                    };
+
+void setup()
+{
+    //pc.baud(115200);
+
+    // Use the begin() function to initialize the LSM9DS0 library.
+    // You can either call it with no parameters (the easy way):
+    uint16_t status = imu.begin();
+
+    //Make sure communication is working
+    pc.printf("LSM9DS0 WHO_AM_I's returned: 0x%X\n", status);
+    pc.printf("Should be 0x49D4\n\n");
+}
+
+void playInputSig(void const *args) {
+    float testVal = aIn;
+    float testNote = 1333.3;
+    while(1) {
+        //aOut.write(0.0);
+        testVal = aIn.read();
+        mySpeaker.PlaySong(&testNote, &testVal);
+        wait(testVal * 10);
+    }    
+}
+
+void readData() {
+    // To read from the device, you must first call the
+    // readMag(), readAccel(), and readGyro() functions.
+    // When this exits, it'll update the appropriate
+    // variables ([mx, my, mz], [ax, ay, az], [gx, gy, gz])
+    // with the most current data.
+ 
+    imu.readMag();
+    imu.readAccel();
+    imu.readGyro();
+}
+
+int main() {
+    aOut = 0.0;
+    Thread t1(playInputSig);
+
+    imu.begin();
+    float testVal = 0;
+    
+    while(1){
+        readData();
+        if(imu.gx < testVal) {
+            left = 0;
+            right = 1;
+            led1 = 1;
+            led2 = 0;
+            center = 1; 
+        } else if(imu.gx > testVal) {
+            left = 1;
+            right = 0;
+            led1 = 0;
+            led2 = 1;
+            center = 1;   
+        }
+        if(imu.gy < testVal) {
+            down = 0;
+            up = 1;
+            led3 = 1;
+            led4 = 0;
+        } else if(imu.gy > testVal) {
+            down = 1;
+            up = 0;
+            led3 = 0;
+            led4 = 1;     
+        }
+        if((led1 | led2 | led3 | led4) == 0){
+            led1 = 1;
+            led2 = 1;
+            led3 = 1;
+            led4 = 1;
+            center = 0; 
+        }
+        
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player2/mbed-rtos.lib	Tue Oct 20 20:04:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#d7bd06319118
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player2/mbed.bld	Tue Oct 20 20:04:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/ba1f97679dad
\ No newline at end of file