A simple Pong game with STM32F407VET6 black board (Seeed Arch Max) and ILI9341 320x240 TFT display.
Dependencies: mbed ILI9341_STM32F4
A simple Pong game with STM32F407VET6 black board (compatible with Seed Arch Max) and ILI9341 320x240 TFT display.
Connect the SPI interface of an ILI9341 320x240 TFT display to the STM32F407VET6 board (Seeed Arch Max) as follows:
ILI9341 TFT | SPI interface | STM32F407VET6 |
---|---|---|
VCC | +5V | |
GND | GND | |
CS | PB_7 | |
RESET | PB_8 | |
D/C | PB_6 | |
SDI(MOSI) | PB_5 | |
SCK | PB_3 | |
LED | over a 56 ohm resistor | +5V |
SDO(MISO) | PB_4 |
Diff: main.cpp
- Revision:
- 1:971e721f6ef2
- Parent:
- 0:887dd664eca0
--- a/main.cpp Thu Apr 11 20:18:15 2019 +0000 +++ b/main.cpp Mon Apr 15 06:02:55 2019 +0000 @@ -1,5 +1,5 @@ -// A simple Pong game with the STM32F407VET6 black board (Seeed Arch Max) and ILI9341 320x240 TFT display. -// More info on STM32F407VET6 black board at https://os.mbed.com/users/hudakz/code/STM32F407VET6_Hello/ +// A simple Pong game with STM32F407VET6 black board (Seeed Arch Max) and ILI9341 320x240 TFT display. +// See more info on the STM32F407VET6 black board at https://os.mbed.com/users/hudakz/code/STM32F407VET6_Hello/ // #include "mbed.h" #include "tft.h" @@ -7,7 +7,8 @@ #include "Ball.h" // Connect the SPI interface of an ILI9341 320x240 TFT display to the STM32F407VET6 board (Seeed Arch Max) as follows: -// ILI9341 STM32F103C8T6 + +// ILI9341 STM32F407VET6 // VCC +5V // GND GND // CS PB_7 @@ -18,12 +19,41 @@ // LED over a 56 ohm resistor +5V // SDO(MISO) PB_4 // -Ticker ticker; -DigitalIn racketUp(PE_4, PullUp); // K0 button -DigitalIn racketDown(PE_3, PullUp); // K1 button -Ball ball; -Racket racket; -bool gameOver; +Ticker ticker; +DigitalIn racketUp(PE_4, PullUp); // K0 button +DigitalIn racketDown(PE_3, PullUp); // K1 button +Ball ball; +Racket racket; +volatile bool gameOn; +volatile bool missed; +Timeout timeout; + +/** + * @brief + * @note + * @param + * @retval + */ +void newGame() +{ + tft_clear(TFT_BLACK); + racket.home(); + ball.home(); + gameOn = true; +} + +/** + * @brief + * @note + * @param + * @retval + */ +void gameOver() +{ + gameOn = false; + tft_text(320 / 2 - 35, 240 / 2, "GAME OVER", TFT_WHITE, TFT_BLACK); + timeout.attach(callback(newGame), 2); +} /** * @brief @@ -33,14 +63,15 @@ */ void updateField() { - racket.paint(TFT_BLACK); // hide racket at old position - racket.move(&racketUp, &racketDown); // move racket - racket.paint(TFT_WHITE); // draw racket at new position - if (!gameOver) - ball.paint(TFT_BLACK); // hide ball at old position - gameOver = ball.move(&racket); // move ball and check for collisions + game over - if (!gameOver) - ball.paint(TFT_WHITE); // draw ball at new position + if (gameOn) { + racket.move(&racketUp, &racketDown); // move racket + racket.paint(); // paint racket + missed = ball.move(&racket); // move ball and check for collisions + if (missed) + gameOver(); + else + ball.paint(); // paint ball + } } /** @@ -52,7 +83,7 @@ int main() { tft_init(); - tft_clear(TFT_BLACK); - ticker.attach_us(updateField, 40 * 1000); // update period = 40 ms + newGame(); + ticker.attach_us(updateField, 20 * 1000); // update period = 20 ms while (true) { } }