ST7735 Pong by Jonne Valola, derived work from William Johnston\'s mbed Pong for NokiaLCD / PS2 keyboard This pong uses a rotary encoder hooked to pins 21 and 22, ST7735_TFT library by me, RotaryEncoder library by Shinichiro Nakamura. All copyrights of respective owners. Use on your own risk and discretion.

Dependencies:   mbed ST7735_TFT RotaryEncoder TFT_fonts_old

Committer:
smultron1977
Date:
Mon Dec 12 21:35:42 2011 +0000
Revision:
0:2353da390056
Original 2 hour hack

Who changed what in which revision?

UserRevisionLine numberNew contents of line
smultron1977 0:2353da390056 1 /** ST7735 Pong by Jonne Valola, derived work from William Johnston's mbed Pong for NokiaLCD / PS2 keyboard
smultron1977 0:2353da390056 2 * This pong uses a rotary encoder hooked to pins 21 and 22,
smultron1977 0:2353da390056 3 * ST7735_TFT library by me, RotaryEncoder library by Shinichiro Nakamura
smultron1977 0:2353da390056 4 * All copyrights of respective owners. Use on your own risk and discretion.
smultron1977 0:2353da390056 5 */
smultron1977 0:2353da390056 6
smultron1977 0:2353da390056 7 #include "mbed.h"
smultron1977 0:2353da390056 8 #include "ball.h"
smultron1977 0:2353da390056 9 #include "paddle.h"
smultron1977 0:2353da390056 10 #include "ST7735_TFT.h"
smultron1977 0:2353da390056 11 #include "string"
smultron1977 0:2353da390056 12 #include "Arial12x12.h"
smultron1977 0:2353da390056 13 #include "Arial24x23.h"
smultron1977 0:2353da390056 14 #include "RotaryEncoder.h"
smultron1977 0:2353da390056 15
smultron1977 0:2353da390056 16 RotaryEncoder re(p21, p22, 0, 600, 300);
smultron1977 0:2353da390056 17
smultron1977 0:2353da390056 18 // State enumerator
smultron1977 0:2353da390056 19 typedef enum {
smultron1977 0:2353da390056 20 RESET, RUN, PAUSE
smultron1977 0:2353da390056 21 } STATES;
smultron1977 0:2353da390056 22
smultron1977 0:2353da390056 23 ST7735_TFT lcd(p5, p6, p7, p8, p11, p15,"TFT"); // mosi, miso, sclk, cs, rs, reset
smultron1977 0:2353da390056 24
smultron1977 0:2353da390056 25 /*
smultron1977 0:2353da390056 26 * Subroutine drawScreen:
smultron1977 0:2353da390056 27 * Description: Draws both paddles
smultron1977 0:2353da390056 28 * and the ball.
smultron1977 0:2353da390056 29 */
smultron1977 0:2353da390056 30 void drawScreen(Paddle paddle1, Paddle paddle2, Ball theBall, bool isBlack) {
smultron1977 0:2353da390056 31 paddle1.draw(lcd, isBlack);
smultron1977 0:2353da390056 32 paddle2.draw(lcd ,isBlack);
smultron1977 0:2353da390056 33 theBall.draw(lcd ,isBlack);
smultron1977 0:2353da390056 34 }
smultron1977 0:2353da390056 35
smultron1977 0:2353da390056 36 /*
smultron1977 0:2353da390056 37 * Subroutine drawScores:
smultron1977 0:2353da390056 38 * Description: Draws the scoreboard
smultron1977 0:2353da390056 39 */
smultron1977 0:2353da390056 40 void drawScores(Paddle paddle1, Paddle paddle2) {
smultron1977 0:2353da390056 41 lcd.locate(60,1);
smultron1977 0:2353da390056 42 lcd.printf("%d", paddle1.getScore());
smultron1977 0:2353da390056 43 lcd.locate(90,1);
smultron1977 0:2353da390056 44 lcd.printf("%d", paddle2.getScore());
smultron1977 0:2353da390056 45 lcd.fillrect(79,0,81,128,0xFFFFFF);
smultron1977 0:2353da390056 46 }
smultron1977 0:2353da390056 47
smultron1977 0:2353da390056 48 int main() {
smultron1977 0:2353da390056 49 lcd.background(0x000000);
smultron1977 0:2353da390056 50 lcd.cls();
smultron1977 0:2353da390056 51 lcd.set_orientation(1);
smultron1977 0:2353da390056 52 lcd.set_font((unsigned char*) Arial12x12); // select the font
smultron1977 0:2353da390056 53 Paddle paddle1, paddle2;
smultron1977 0:2353da390056 54 Ball theBall;
smultron1977 0:2353da390056 55 int temp, count=0, oldY;
smultron1977 0:2353da390056 56 drawScreen(paddle1, paddle2, theBall, false);
smultron1977 0:2353da390056 57 drawScores(paddle1, paddle2);
smultron1977 0:2353da390056 58 STATES state = RESET; // Initial state is RESET
smultron1977 0:2353da390056 59 while(1) {
smultron1977 0:2353da390056 60 switch(state) {
smultron1977 0:2353da390056 61 case RESET: // Reset objects, draw the screen, state = PAUSE
smultron1977 0:2353da390056 62 lcd.cls();
smultron1977 0:2353da390056 63 paddle1 = Paddle(0,40,5,25,0xFFFFFF,paddle1.getLives(),paddle1.getScore());
smultron1977 0:2353da390056 64 paddle2 = Paddle(154,40,5,25,0xFFFFFF,paddle2.getLives(),paddle2.getScore());
smultron1977 0:2353da390056 65 theBall = Ball(80,64,5,5,0xFFFF00,1,1);
smultron1977 0:2353da390056 66 drawScreen(paddle1, paddle2, theBall, false);
smultron1977 0:2353da390056 67 drawScores(paddle1, paddle2);
smultron1977 0:2353da390056 68 oldY = re.getVal();
smultron1977 0:2353da390056 69 state = RUN;
smultron1977 0:2353da390056 70 break;
smultron1977 0:2353da390056 71 case RUN:
smultron1977 0:2353da390056 72 if (oldY > re.getVal()) { // Executes if encoder turned
smultron1977 0:2353da390056 73 if(paddle1.getY()>2)
smultron1977 0:2353da390056 74 paddle1.move(lcd, -2);
smultron1977 0:2353da390056 75 }
smultron1977 0:2353da390056 76 if (oldY < re.getVal()) { // Executes if encoder turned
smultron1977 0:2353da390056 77 if(paddle1.getY()+paddle1.getHeight()<128)
smultron1977 0:2353da390056 78 paddle1.move(lcd, 2);
smultron1977 0:2353da390056 79 }
smultron1977 0:2353da390056 80 oldY = re.getVal();
smultron1977 0:2353da390056 81
smultron1977 0:2353da390056 82 if(count%2) // Only let CPU move once every 2 times through the loop
smultron1977 0:2353da390056 83 paddle2.moveCPU(lcd, theBall.getY());
smultron1977 0:2353da390056 84 if(++count==5) { // Only move the ball once every 5 times through the loop
smultron1977 0:2353da390056 85 count = 0;
smultron1977 0:2353da390056 86 if(theBall.hitP1((paddle1.getX()+paddle1.getWidth()), paddle1.getY(), paddle1.getHeight()))
smultron1977 0:2353da390056 87 theBall.reverseX();
smultron1977 0:2353da390056 88 if(theBall.hitP2(paddle2.getX(), paddle2.getY(), paddle2.getHeight()))
smultron1977 0:2353da390056 89 theBall.reverseX();
smultron1977 0:2353da390056 90 if(theBall.hitX()) { // If the ball hits one of the sides of the screen
smultron1977 0:2353da390056 91 if(theBall.getX()<7) { // If the ball hit paddle1's side
smultron1977 0:2353da390056 92 paddle2.addPoint();
smultron1977 0:2353da390056 93 }
smultron1977 0:2353da390056 94 else if(theBall.getX()>153) { // If the ball hit paddle2's side
smultron1977 0:2353da390056 95 paddle1.addPoint();
smultron1977 0:2353da390056 96 }
smultron1977 0:2353da390056 97 theBall.reverseX();
smultron1977 0:2353da390056 98 state = RESET; // Reset the objects
smultron1977 0:2353da390056 99 }
smultron1977 0:2353da390056 100 if(theBall.hitY())
smultron1977 0:2353da390056 101 theBall.reverseY();
smultron1977 0:2353da390056 102 theBall.move(lcd);
smultron1977 0:2353da390056 103 }
smultron1977 0:2353da390056 104 break;
smultron1977 0:2353da390056 105 }
smultron1977 0:2353da390056 106 drawScreen(paddle1, paddle2, theBall, false);
smultron1977 0:2353da390056 107 drawScores(paddle1, paddle2);
smultron1977 0:2353da390056 108 }
smultron1977 0:2353da390056 109 }