Вы находитесь на странице: 1из 7

#include "mbed.

h"

#include "rtos.h"

#include "LCD_DISCO_F429ZI.h"

LCD_DISCO_F429ZI lcd;

InterruptIn button(PA_0);

DigitalOut led1(LED1);

Thread thread_blink;

Thread t2;

Thread thread_lcd;

bool bounce_up;

int y_movement;

int x_movement;

// memory pool as storage space for queue.

struct xy {

public:

int x;

int y;

};

MemoryPool<xy, 16> mpool;

// allocat memory to pool.

xy *xy2 = mpool.alloc();

// intialize queue as int of size 1.

Queue<xy, 16> queue;


void btn_int()

bounce_up = 1;

void ball_thread()

led1 = 1;

// set font

BSP_LCD_SetFont(&Font20);

// set LCD background to white

// lcd.Clear(LCD_COLOR_WHITE);

// lcd.SetBackColor(LCD_COLOR_WHITE);

// initial state of ball

y_movement = 0;

bool bounce_down = 0;

while(1)

if (x_movement >= 220 && y_movement <= 70) // collision!

// Prompte the end of the game.

lcd.Clear(LCD_COLOR_WHITE);

lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"GAME OVER", CENTER_MODE);


// reset positions.

x_movement = 0;

y_movement = 0;

bounce_up = 0;

bounce_down = 0;

Thread::wait(5000);

else if (bounce_up) // ball going up

// lcd.SetTextColor(LCD_COLOR_RED);

// lcd.FillCircle(20+y_movement, 40, 20);

y_movement += 3;

if (y_movement >= 120) // the highest ball can go.

bounce_up = 0;

bounce_down = 1;

// Thread::wait(250);

else if (bounce_down) // ball going down

// lcd.SetTextColor(LCD_COLOR_RED);

// lcd.FillCircle(20+y_movement, 40, 20);

y_movement -= 5;

if (y_movement <= 0) // touching the ground.

bounce_down = 0;

else // default position of the ball

{
//lcd.Clear(LCD_COLOR_WHITE);

//lcd.SetBackColor(LCD_COLOR_WHITE);

//lcd.SetTextColor(LCD_COLOR_RED);

//lcd.FillCircle(20, 40, 20);

//Thread::wait(250);

Thread::wait(250);

xy2->x = x_movement;

xy2->y = y_movement;

queue.put(xy2);

void bg_thread()

// color and shape of obstacle.

//lcd.SetTextColor(LCD_COLOR_BLACK);

//lcd.FillTriangle(0, 75, 0, 250, 275, 300);

// initial position

x_movement = 0;

while (1)

if (x_movement >= 250) // the obstacle is through.

x_movement = 0;
else

x_movement += 5;

// obstacle approaching.

//lcd.Clear(LCD_COLOR_WHITE);

// lcd.SetTextColor(LCD_COLOR_BLACK);

//lcd.FillTriangle(0,75,0,250-x_movement, 275-x_movement, 300-x_movement);

Thread::wait(250);

xy2->x = x_movement;

xy2->y = y_movement;

queue.put(xy2);

void lcd_resource(){

led1 = 1;

// set font

BSP_LCD_SetFont(&Font20);

// set LCD background to white

lcd.Clear(LCD_COLOR_WHITE);

lcd.SetBackColor(LCD_COLOR_WHITE);

// initial state of ball


xy * xy3;

int x_1, y_1;

while (1)

// receive queue event.

osEvent evt = queue.get();

if (evt.status == osEventMessage){

xy3 = (xy *)evt.value.p;

x_1 = xy3->x;

y_1 = xy3->y;

mpool.free(xy3);

lcd.Clear(LCD_COLOR_WHITE);

lcd.SetTextColor(LCD_COLOR_BLACK);

lcd.FillTriangle(0,75,0,250-x_movement, 275-x_movement, 300-x_movement);

lcd.SetTextColor(LCD_COLOR_RED);

lcd.FillCircle(20+y_movement, 40, 20);

int main() {

// btn_int as rising edge interrupt.

button.rise(&btn_int);

// initialize thread.
thread_blink.set_priority(osPriorityHigh);

thread_blink.start(ball_thread);

t2.start(bg_thread);

thread_lcd.start(lcd_resource);

Вам также может понравиться