-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
386 lines (331 loc) · 9.84 KB
/
Copy pathmain.cpp
File metadata and controls
386 lines (331 loc) · 9.84 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <iostream>
#include "main.h"
#include <random>
#include "pico/stdlib.h"
#include "pico/aon_timer.h"
#include "pico/multicore.h"
#include "pico/mutex.h"
#include "external/ydf/ydf_model.h"
extern "C" {
#include "GUI_Paint.h"
#include "Infrared.h"
#include "LCD_1in3.h"
#include "external/Config/DEV_Config.h"
#include <stdio.h>
}
// --- Parameters ---
bool game_status = true;
// LCD refresh rate
constexpr uint16_t LCD_REFRESH_DELAY_MS = 50; // ~20 FPS
// pinecones
struct PineconeData {
uint16_t x = 0;
bool active = false; // true=表示、false=非表示
};
static mutex_t g_mutex;
std::vector<PineconeData> pinecones;
constexpr size_t MAX_PINECONES = 50; // 最大数
// pine
uint16_t pine_thickness = 32;
uint16_t pine_height = 10;
constexpr uint16_t pine_x = 104;
// kiwi
enum class KiwiStatus : uint8_t {
Idle,
Eating,
Wet
};
KiwiStatus kiwi_status = KiwiStatus::Idle;
uint16_t kiwi_x = 40; // body
constexpr uint16_t kiwi_y = LCD_1IN3_HEIGHT - 30; // body
constexpr uint8_t kiwi_size = 20; // body
constexpr uint16_t kiwi_head_size = 7;
bool move_positive = true; // true = right, false = left
uint8_t kiwi_speed = 3;
// user
uint8_t watered_times = 0;
uint8_t burned_times = 0;
uint8_t logged_times = 0;
// --- Functions ---
// Pinecone functions --->
void init_pinecones() { // use this function is only for test
pinecones.reserve(MAX_PINECONES);
// 最初に5個を配置
pinecones.push_back({.x = 10, .active = true});
pinecones.push_back({.x = 30, .active = true});
pinecones.push_back({.x = 50, .active = true});
pinecones.push_back({.x = 70, .active = true});
pinecones.push_back({.x = 90, .active = true});
}
void update_pinecones(const uint16_t x) {
mutex_enter_blocking(&g_mutex);
for (auto& pc : pinecones) {
if (!pc.active) {
pc.x = x;
pc.active = true;
break;
}
}
mutex_exit(&g_mutex);
}
void remove_pinecones(uint16_t target_x, const uint8_t pineconeCollisionDistance = 5) {
mutex_enter_blocking(&g_mutex);
for (auto& pc : pinecones) {
if (pc.active && abs(static_cast<int>(pc.x) - static_cast<int>(target_x)) < pineconeCollisionDistance) {
pc.active = false;
break;
}
}
mutex_exit(&g_mutex);
}
// Draw functions --->
void draw_pinecones() {
for (const auto& pc : pinecones) {
if (pc.active) {
constexpr int height = LCD_1IN3_HEIGHT;
constexpr uint8_t size = 2;
Paint_DrawRectangle(
pc.x,
height - size,
pc.x + size,
height,
BROWN,
DOT_PIXEL_4X4, DRAW_FILL_EMPTY);
}
}
}
void draw_pine(const uint16_t height) {
Paint_DrawRectangle( // TODO: make it to AA(# or & or % or $)
pine_x,
LCD_1IN3_HEIGHT - height,
pine_x + pine_thickness,
LCD_1IN3_HEIGHT,
BLACK,
DOT_PIXEL_4X4, DRAW_FILL_FULL);
}
void draw_kiwi(const uint16_t x) {
uint16_t kiwi_head_x; // increase from (kiwi_x + kiwi_size)
uint16_t kiwi_head_y;
switch (kiwi_status) {
case KiwiStatus::Eating:
if (move_positive) {
kiwi_head_x = x + kiwi_size + kiwi_head_size;
kiwi_head_y = kiwi_y + kiwi_size;
} else {
kiwi_head_x = x - kiwi_size - kiwi_head_size;
kiwi_head_y = kiwi_y + kiwi_size;
}
Paint_DrawCircle( // head
kiwi_head_x,
kiwi_head_y,
kiwi_head_size,
BROWN,
DOT_PIXEL_4X4,
DRAW_FILL_EMPTY
);
Paint_DrawCircle(
kiwi_x,
kiwi_y,
kiwi_size,
WHITE,
DOT_PIXEL_4X4,
DRAW_FILL_FULL
);
Paint_DrawCircle( // body
x,
kiwi_y,
kiwi_size,
GREEN,
DOT_PIXEL_6X6,
DRAW_FILL_EMPTY
);
break;
case KiwiStatus::Wet:
if (move_positive) {
kiwi_head_x = x + kiwi_size + kiwi_head_size;
kiwi_head_y = kiwi_y;
} else {
kiwi_head_x = x - kiwi_size - kiwi_head_size;
kiwi_head_y = kiwi_y;
}
Paint_DrawCircle( // head
kiwi_head_x,
kiwi_head_y,
kiwi_head_size,
BROWN,
DOT_PIXEL_4X4,
DRAW_FILL_EMPTY
);
Paint_DrawCircle(
kiwi_x,
kiwi_y,
kiwi_size,
WHITE,
DOT_PIXEL_4X4,
DRAW_FILL_FULL
);
Paint_DrawCircle( // body
x,
kiwi_y,
kiwi_size,
GREEN,
DOT_PIXEL_6X6,
DRAW_FILL_EMPTY
);
break;
case KiwiStatus::Idle:
default:
if (move_positive) {
kiwi_head_x = x + kiwi_size + kiwi_head_size;
kiwi_head_y = kiwi_y - kiwi_size;
} else {
kiwi_head_x = x - kiwi_size - kiwi_head_size;
kiwi_head_y = kiwi_y - kiwi_size;
}
Paint_DrawCircle( // head
kiwi_head_x,
kiwi_head_y,
kiwi_head_size,
BROWN,
DOT_PIXEL_4X4,
DRAW_FILL_EMPTY
);
Paint_DrawCircle(
kiwi_x,
kiwi_y,
kiwi_size,
WHITE,
DOT_PIXEL_4X4,
DRAW_FILL_FULL
);
Paint_DrawCircle( // body
x,
kiwi_y,
kiwi_size,
GREEN,
DOT_PIXEL_6X6,
DRAW_FILL_EMPTY
);
break;
}
}
int LCD() {
if (DEV_Module_Init() != 0) {
return -1;
}
DEV_SET_PWM(50);
printf("1.3inch LCD init...\r\n");
LCD_1IN3_Init(HORIZONTAL);
LCD_1IN3_Clear(BLACK);
UDOUBLE Imagesize = LCD_1IN3_HEIGHT * LCD_1IN3_WIDTH * 2;
UWORD *BlackImage;
if ((BlackImage = static_cast<uint16_t *>(malloc(Imagesize))) == nullptr) {
printf("Failed to apply for black memory...\r\n");
exit(0);
}
Paint_NewImage(reinterpret_cast<uint8_t *>(BlackImage), LCD_1IN3.WIDTH, LCD_1IN3.HEIGHT, 0, WHITE);
Paint_SetScale(65);
Paint_Clear(BLACK);
Paint_SetRotate(ROTATE_0);
// ボタンピンの初期化
SET_Infrared_PIN(keyA);
SET_Infrared_PIN(keyB);
SET_Infrared_PIN(keyX);
SET_Infrared_PIN(keyY);
SET_Infrared_PIN(keyUp);
SET_Infrared_PIN(keyDown);
SET_Infrared_PIN(keyLeft);
SET_Infrared_PIN(keyRight);
SET_Infrared_PIN(keyCtrl);
// 初期描画
LCD_1IN3_Display(BlackImage);
uint32_t core1_msg = 0;
game_status = true;
bool update_need = false;
uint16_t kiwi_space = kiwi_size*2 + kiwi_head_size * 2;
draw_kiwi(kiwi_x);
draw_pine(pine_height);
while (true) {
Paint_Clear(WHITE);
if (DEV_Digital_Read(keyUp) == 0) {
printf("keyUp Pressed!\r\n"); // for Debug
kiwi_status = KiwiStatus::Wet;
update_need = true;
sleep_ms(LCD_REFRESH_DELAY_MS);
}
if (DEV_Digital_Read(keyDown) == 0) {
printf("keyDown Pressed!\r\n"); // for Debug
kiwi_status = KiwiStatus::Eating;
remove_pinecones(kiwi_x);
update_need = true;
sleep_ms(LCD_REFRESH_DELAY_MS);
}
if (DEV_Digital_Read(keyLeft) == 0 && kiwi_x > kiwi_space) {
printf("keyLeft Pressed!\r\n"); // for Debug
kiwi_x -= kiwi_speed;
move_positive = false;
update_need = true;
sleep_ms(LCD_REFRESH_DELAY_MS);
} else {
draw_kiwi(kiwi_x);
}
if (DEV_Digital_Read(keyRight) == 0 && kiwi_x < (LCD_1IN3_WIDTH + kiwi_space) ) {
printf("KeyRight Pressed!\r\n"); // for Debug
kiwi_x += kiwi_speed;
move_positive = true;
update_need = true;
sleep_ms(LCD_REFRESH_DELAY_MS);
} else {
draw_kiwi(kiwi_x);
}
// User Action
if (DEV_Digital_Read(keyA) == 0 ) {
printf("KeyA Pressed!\r\n");
// show_statics() // TODO: make this function
pine_height += 10; // TODO : For Debug
update_need = true;
sleep_ms(LCD_REFRESH_DELAY_MS);
} else {
draw_pine(pine_height);
}
if (DEV_Digital_Read(keyB) == 0) {
printf("KeyB Pressed!\r\n");
sleep_ms(LCD_REFRESH_DELAY_MS);
break;
}
if (DEV_Digital_Read(keyX) == 0) {
printf("KeyX Pressed!\r\n");
kiwi_status = KiwiStatus::Eating;
update_need = true;
sleep_ms(LCD_REFRESH_DELAY_MS);
} else {
draw_kiwi(kiwi_x);
}
if (DEV_Digital_Read(keyY) == 0) {
printf("KeyY Pressed!\r\n");
// water_pine() // TODO: make this function
watered_times++;
} else {
draw_pinecones();
}
if (update_need) {
printf("Screen Updated!\r\n");
LCD_1IN3_Display(BlackImage);
kiwi_status = KiwiStatus::Idle;
update_need = false;
sleep_ms(LCD_REFRESH_DELAY_MS);
}
}
free(BlackImage);
BlackImage = NULL;
DEV_Module_Exit();
return 0;
}
int main() {
stdio_init_all();
printf("CORE0: start.\r\n");
mutex_init(&g_mutex);
init_pinecones();
LCD();
return 0;
}