The world's greatest etch a sketch

Dependencies:   mbed C12832 MMA7660

Files at this revision

API Documentation at this revision

Comitter:
jlaqua
Date:
Wed Feb 26 03:39:10 2014 +0000
Parent:
3:c385027fcc0b
Commit message:
pong party faileth..no acks yo

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r c385027fcc0b -r be4ba8a111ce main.cpp
--- a/main.cpp	Tue Feb 25 20:52:24 2014 +0000
+++ b/main.cpp	Wed Feb 26 03:39:10 2014 +0000
@@ -6,43 +6,30 @@
 AnalogIn potY(p19);
 MMA7660 acc(p28, p27);
 C12832 lcd(p5, p7, p6, p8, p11);
+Serial term(USBTX, USBRX);
 
-DigitalIn play(p14);
-DigitalIn up(p15);
-DigitalIn down(p12);
+char addr = 0x42;
+
+void pongMasta(void)
+{
+    DigitalIn play(p14);
 
-static const int DOWN = 0;
-static const int UP = 1;
-static const int LEFTUP = 0;
-static const int LEFTDOWN = 1;
-static const int RIGHTUP = 2;
-static const int RIGHTDOWN = 3;
-static const int START = 4;
-int badGuy = 12;
-int badGuyState = DOWN;
-int goodGuy;
-int ballX;
-int ballY;
-int ballState = START;
+    static const int LEFTUP = 0;
+    static const int LEFTDOWN = 1;
+    static const int RIGHTUP = 2;
+    static const int RIGHTDOWN = 3;
+    static const int START = 4;
+    int badGuy = 12;
+    int goodGuy = 12;
+    int ballX = 64;
+    int ballY = 0;
+    int ballState = START;
 
-
-void pong(void)
-{
+    I2C masta(p9,p10);
+    
     while(1) {
         DigitalIn up(p15);
         DigitalIn down(p12);
-        switch(badGuyState) {
-            case DOWN:
-                badGuy++;
-                if (badGuy+5 >= lcd.height()-1)
-                    badGuyState = UP;
-                break;
-            case UP:
-                badGuy--;
-                if (badGuy <= 0)
-                    badGuyState = DOWN;
-                break;
-        }
 
         if (up)
             goodGuy--;
@@ -53,6 +40,20 @@
             goodGuy = 0;
         if (goodGuy + 5 > lcd.height()-1)
             goodGuy = lcd.height()-6;
+        
+        if (ballState != START) {
+            // Send good guy yo
+            masta.start();
+            masta.write(addr);
+            masta.write((char)goodGuy);
+            masta.stop();
+        
+            // Read bad guy bro
+            masta.start();
+            masta.write(addr|0x01);
+            badGuy = (int)masta.read(addr);
+            masta.stop();
+        }
 
         switch(ballState) {
             case LEFTUP:
@@ -107,8 +108,15 @@
                 ballX = lcd.width()/2;
                 ballY = 0;
                 goodGuy = 12;
-                if (play)
+                badGuy = 12;
+                //while(!play);
+                if (play) {
+                    masta.start();
+                    term.printf("ack = %d\n\r", masta.write(addr));
+                    term.printf("ack = %d\n\r", masta.write(0xFF));
+                    masta.stop();
                     ballState = RIGHTDOWN;
+                }
                 break;
         }
 
@@ -122,6 +130,122 @@
     }
 }
 
+void pongSlave(void)
+{
+    static const int LEFTUP = 0;
+    static const int LEFTDOWN = 1;
+    static const int RIGHTUP = 2;
+    static const int RIGHTDOWN = 3;
+    static const int START = 4;
+    int badGuy = 12;
+    int goodGuy = 12;
+    int ballX = 0;
+    int ballY = 64;
+    int ballState = START;
+    
+    I2CSlave slave(p9,p10);
+    slave.address(addr);
+    
+    while(1) {
+        if (ballState != START) {
+            DigitalIn up(p15);
+            DigitalIn down(p12);
+            if (up)
+                goodGuy--;
+            if (down)
+                goodGuy++;
+            
+            if (goodGuy < 0)
+                goodGuy = 0;
+            if (goodGuy + 5 > lcd.height()-1)
+                goodGuy = lcd.height()-6;
+        
+            char foo = slave.receive();
+            char futureBadGuy = slave.read();
+            switch (foo) {
+                case I2CSlave::WriteAddressed:
+                    badGuy = (int)futureBadGuy;
+                    break;
+                case I2CSlave::ReadAddressed:
+                    // Send good guy yo
+                    slave.write((char)goodGuy);
+                    break;
+            }
+        }
+
+        switch(ballState) {
+            case LEFTUP:
+                ballX--;
+                ballY--;
+                if (ballX <= 0) {
+                    if ((ballY >= goodGuy) && (ballY <= goodGuy+5))
+                        ballState = RIGHTUP;
+                    else
+                        ballState = START;
+                }
+                if (ballY <= 0)
+                    ballState = LEFTDOWN;
+                break;
+            case LEFTDOWN:
+                ballX--;
+                ballY++;
+                if (ballX <= 0) {
+                    if ((ballY >= goodGuy) && (ballY <= goodGuy+5))
+                        ballState = RIGHTDOWN;
+                    else
+                        ballState = START;
+                }
+                if (ballY >= lcd.height()-1)
+                    ballState = LEFTUP;
+                break;
+            case RIGHTUP:
+                ballX++;
+                ballY--;
+                if (ballX >= lcd.width()-1) {
+                    if ((ballY >= badGuy) && (ballY <= badGuy+5))
+                        ballState = LEFTUP;
+                    else
+                        ballState = START;
+                }
+                if (ballY <= 0)
+                    ballState = RIGHTDOWN;
+                break;
+            case RIGHTDOWN:
+                ballX++;
+                ballY++;
+                if (ballX >= lcd.width()-1) {
+                    if ((ballY >= badGuy) && (ballY <= badGuy+5))
+                        ballState = LEFTDOWN;
+                    else
+                        ballState = START;
+                }
+                if (ballY >= lcd.height()-1)
+                    ballState = RIGHTUP;
+                break;
+            case START:
+                ballX = lcd.width()/2;
+                ballY = 0;
+                goodGuy = 12;
+                badGuy = 12;
+                int bar = slave.receive();
+                unsigned char play = slave.read();
+                term.printf("bar = %d\tplay = 0x%02X\n\r", bar, play);
+                if (bar == I2CSlave::WriteAddressed && play == 0xFF) {
+                    ballState = RIGHTDOWN;
+                }
+                break;
+        }
+
+        // Draw stuff
+        lcd.cls(); // clear screen
+        lcd.circle(ballX, ballY, 2, 1);
+        lcd.line(0, goodGuy, 0, goodGuy+5, 1);
+        lcd.line(lcd.width()-1, badGuy, lcd.width()-1, badGuy+5, 1);
+        lcd.copy_to_lcd();
+        wait(0.03);
+    }
+}
+
 void etchASketch()
 {
     int counter = 0;
@@ -142,27 +266,44 @@
 
 int main()
 {
+    DigitalIn up(p15);
+    DigitalIn down(p12);
+    DigitalIn play(p14);
+    
     int choice = 0;
-    lcd.printf("\n    Etch-A-Sketch\n");
-    lcd.printf("    Pong");
+    lcd.printf("    Etch-A-Sketch\n");
+    lcd.printf("    PongMaster\n");
+    lcd.printf("    PongSlave\n");
     while(1) {
-        if (up)
-            choice = 0;
-        if (down)
-            choice = 1;
+        if (up) {
+            while(up);
+            choice--;
+        }
+        if (down) {
+            while(down); 
+            choice++;
+        }
+        choice = (choice + 3) % 3;
         if (play) {
+            while(play);
             lcd.cls();
             switch (choice) {
                 case 0:
                     etchASketch();
                     break;
                 case 1:
-                    pong();
+                    pongMasta();
+                    break;
+                case 2:
+                    pongSlave();
                     break;
             }
         }
-        lcd.fillcircle(12,13+!choice*8,2,0);
-        lcd.fillcircle(12,13+choice*8,2,1);
+        for (int i = 0; i < 3; i++) {
+            if (choice != i)
+                lcd.fillcircle(12,5+i*8,2,0);
+        }
+        lcd.fillcircle(12,5+choice*8,2,1);
         lcd.copy_to_lcd();
     }
 }