The world's greatest etch a sketch
Dependencies: mbed C12832 MMA7660
main.cpp
- Committer:
- jlaqua
- Date:
- 2014-02-25
- Revision:
- 3:c385027fcc0b
- Parent:
- 2:aed0788e8f96
- Child:
- 4:be4ba8a111ce
File content as of revision 3:c385027fcc0b:
#include "mbed.h" #include "C12832.h" #include "MMA7660.h" AnalogIn potX(p20); AnalogIn potY(p19); MMA7660 acc(p28, p27); C12832 lcd(p5, p7, p6, p8, p11); DigitalIn play(p14); DigitalIn up(p15); DigitalIn down(p12); 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; void pong(void) { 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--; if (down) goodGuy++; if (goodGuy < 0) goodGuy = 0; if (goodGuy + 5 > lcd.height()-1) goodGuy = lcd.height()-6; switch(ballState) { case LEFTUP: ballX--; ballY--; if (ballX <= 0) { if ((ballY >= badGuy) && (ballY <= badGuy+5)) ballState = RIGHTUP; else ballState = START; } if (ballY <= 0) ballState = LEFTDOWN; break; case LEFTDOWN: ballX--; ballY++; if (ballX <= 0) { if ((ballY >= badGuy) && (ballY <= badGuy+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 >= goodGuy) && (ballY <= goodGuy+5)) ballState = LEFTUP; else ballState = START; } if (ballY <= 0) ballState = RIGHTDOWN; break; case RIGHTDOWN: ballX++; ballY++; if (ballX >= lcd.width()-1) { if ((ballY >= goodGuy) && (ballY <= goodGuy+5)) ballState = LEFTDOWN; else ballState = START; } if (ballY >= lcd.height()-1) ballState = RIGHTUP; break; case START: ballX = lcd.width()/2; ballY = 0; goodGuy = 12; if (play) ballState = RIGHTDOWN; break; } // Draw stuff lcd.cls(); // clear screen lcd.circle(ballX, ballY, 2, 1); lcd.line(0, badGuy, 0, badGuy+5, 1); lcd.line(lcd.width()-1, goodGuy, lcd.width()-1, goodGuy+5, 1); lcd.copy_to_lcd(); wait(0.03); } } void etchASketch() { int counter = 0; acc.setSampleRate(120); while(1) { int x = (int)((1-potX) * lcd.width()); int y = (int)((1-potY) * lcd.height()); lcd.pixel(x, y, 1); lcd.copy_to_lcd(); if (counter > 100) { if (acc.x() > 1.3) lcd.cls(); counter = 0; } counter++; } } int main() { int choice = 0; lcd.printf("\n Etch-A-Sketch\n"); lcd.printf(" Pong"); while(1) { if (up) choice = 0; if (down) choice = 1; if (play) { lcd.cls(); switch (choice) { case 0: etchASketch(); break; case 1: pong(); break; } } lcd.fillcircle(12,13+!choice*8,2,0); lcd.fillcircle(12,13+choice*8,2,1); lcd.copy_to_lcd(); } }