-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay.c
204 lines (184 loc) · 5.27 KB
/
display.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include "game.h"
#include "led.h"
#define COMPONENT_MAX_NUM 16
#define DISPLAY_WIDTH 32
#define DISPLAY_HEIGHT 16
#define SLOT_SIZE 8
#define SLOT_NUM 4
#define MAX_LIFE 8
/*
* button:
* - 0 : A (Green)
* - 1 : B (Red)
* - 2 : X (Blue)
* - 3 : Y (Yellow)
*/
typedef struct {
int slot;
int height;
} component;
static int color_mapping[SLOT_NUM] = { 2, 1, 4, 3 }; //index : contoroller button number & SLOT number
static pthread_t displayThread;
static pthread_mutex_t currentComponentLock = PTHREAD_MUTEX_INITIALIZER;
//static pthread_mutex_t lifeLock = PTHREAD_MUTEX_INITIALIZER;
static bool running;
static long totalDelay;
static int goneLife;
static component currentComponent[COMPONENT_MAX_NUM];
static void* displayLoop (void *);
static void displayLife(void);
/*
* Function: Display_init()
* -------------------------
* init Display
*
* @ delaytime : the period which it takes for the component to go down one row in ms
*/
int Display_init (int delayTimeInMs, void (*f) (int))
{
for (int i = 0; i < COMPONENT_MAX_NUM; i++){
currentComponent[i].height = -1;
currentComponent[i].slot = -1;
}
running = true;
totalDelay = delayTimeInMs;
goneLife = 0;
int threadCreateResult = pthread_create (&displayThread, NULL, displayLoop, NULL);
return threadCreateResult;
}
// function for thread which will keep checking currentComponent and display them
static void* displayLoop (void* empty)
{
printf ( "start displaying loop \n");
struct timespec reqtime;
reqtime.tv_sec = totalDelay / 1000000000;
reqtime.tv_nsec = totalDelay % 1000000000;
int width = DISPLAY_WIDTH / SLOT_NUM;
int currentHeight;
int slot;
while (running) {
LED_clean_display();
for (int i = 0; i < COMPONENT_MAX_NUM && goneLife < MAX_LIFE; i++){
pthread_mutex_lock (¤tComponentLock);
{
currentHeight = currentComponent[i].height;
slot = currentComponent[i].slot;
}
pthread_mutex_unlock (¤tComponentLock);
if ( currentHeight >= DISPLAY_HEIGHT){
currentComponent[i].height = -1;
Game_checkTimeout(slot);
}
pthread_mutex_lock (¤tComponentLock);
{
currentHeight = currentComponent[i].height;
slot = currentComponent[i].slot;
if ( currentHeight >=0 ){
LED_display_rectangle(slot*SLOT_SIZE + goneLife/2, currentHeight, slot*SLOT_SIZE + width-1 - goneLife/2, (currentHeight), color_mapping[slot] );
currentComponent[i].height++;
if (currentComponent[i].height >= DISPLAY_HEIGHT){
currentComponent[i].height = -1;
Game_checkTimeout(slot);
}
}
}
pthread_mutex_unlock (¤tComponentLock);
}
displayLife();
nanosleep(&reqtime, NULL);
}
printf ( "stop displaying loop \n");
return NULL;
}
// Generate LED bar at the bottom of a LED matrix to indicate
// how many lives are left
static void displayLife (void)
{
for (int i = 0; i < 4; i++){
LED_display_rectangle(i*SLOT_SIZE + goneLife/2, DISPLAY_HEIGHT-1, (i+1)*SLOT_SIZE - (1 + goneLife/2), DISPLAY_HEIGHT-1, color_mapping[i]);
}
}
void Display_cleanup(void)
{
running = false;
pthread_join (displayThread, NULL);
LED_cleanup();
}
/*
* Function: Display_geneationComponenet()
* ---------------------------
* Function to check the free slots for displaying component
* - if there is, enqueue in it
* - if no slot available, skip
*
* @ button:
* - 0 : A (Green)
* - 1 : B (Red)
* - 2 : X (Blue)
* - 3 : Y (Yellow)
*/
void Display_generateComponent (int button)
{
if (button > 3 || button < 0)
return;
pthread_mutex_lock (¤tComponentLock);
{
for (int i = 0; i < COMPONENT_MAX_NUM; i++){
if (currentComponent[i].height == -1){
currentComponent[i].height = 0;
currentComponent[i].slot = button;
break;
}
}
}
pthread_mutex_unlock (¤tComponentLock);
return;
}
/*
* Function: Display_decreaseLife()
* ---------------------------
* Function to decrease life to @life
*
*/
void Display_decreaseLife(int life)
{
if (life >= MAX_LIFE){
goneLife = 0;
return;
}
if (goneLife <= MAX_LIFE -1)
{
goneLife = MAX_LIFE - life;
}
}
/*
* Function: Display_decreaseLife()
* ---------------------------
* Function to recharge life to @life
* if @life is more than MAX_LIFE, it recharge just as much as MAX_LIFE
*
*/
void Display_rechargeLife(int life)
{
if (life > MAX_LIFE){
goneLife = 0;
return;
}
if (life <= 0) {
goneLife = MAX_LIFE;
return;
}
goneLife = MAX_LIFE - life;
pthread_mutex_lock (¤tComponentLock);
{
for (int i = 0; i < COMPONENT_MAX_NUM; i++){
currentComponent[i].height = -1;
currentComponent[i].slot = -1;
}
}
pthread_mutex_unlock (¤tComponentLock);
}