-
Notifications
You must be signed in to change notification settings - Fork 5
/
rtos1.c
103 lines (88 loc) · 2.45 KB
/
rtos1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "mbed.h"
#include "rtos.h"
#include "LCD_DISCO_F429ZI.h"
LCD_DISCO_F429ZI lcd;
InterruptIn button(PA_0);
DigitalOut led1(LED1);
bool bounce_up;
int y_movement;
int x_movement;
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;
Thread::wait(250);
}
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);
}
}
}
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;
// 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);
x_movement += 5;
Thread::wait(250);
}
}