-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads.c
639 lines (582 loc) · 17.7 KB
/
threads.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
* threads.c
* - Define functions of background threads, periodic threads, and aperiodic threads
* Created on: Nov 29, 2024
* Author: Nina Temridiwong
*
*/
/************************************Includes***************************************/
#include "./threads.h"
#include "./MultimodDrivers/multimod.h"
#include "./MultimodDrivers/multimod_dac.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "driverlib/timer.h"
#include <driverlib/adc.h>
#include <driverlib/gpio.h>
#include "Pixel.h"
#include "Tama_Struct.h"
#define MAX_NUM_SAMPLES (200)
#define SIGNAL_STEPS (2)
#define SPRITE_OFFSET_X 14
#define SPRITE_OFFSET_Y 22
/*********************************Global Variables**********************************/
// Display
uint8_t menu_index = 0;
uint32_t tama_time = 0;
uint8_t current_menu = 0;
bool burger = true;
bool food_menu_on = false;
bool health_menu_on = false;
// Tamagotchi
tama_t *Tama;
bool game_started = false;
/*********************************Global Variables**********************************/
/********************************Public Functions***********************************/
void Initialize_Tama()
{
// Set default tama values
tama_time = 0;
current_menu = 0;
Tama = malloc(sizeof(tama_t));
Tama->age = 0;
Tama->weight = 0;
Tama->sprite_L = Kuchipatchi_L;
Tama->sprite_R = Kuchipatchi_R;
Tama->hunger = 10;
Tama->discipline = 10;
Tama->poop = 10;
Tama->happiness = 10;
Tama->health = 10;
Tama->sleep_deficit = 0;
Tama->sleeping = false;
Tama->dead = false;
game_started = true;
// Clear screen
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_Fill(ST7789_BLACK);
G8RTOS_SignalSemaphore(&sem_SPIA);
sleep(80);
// Set default thread ID
ID_walking_tama = G8RTOS_AddThread(&Walking_Tama_Thread, 2, "walking");
ID_virus_thread = -1;
ID_sleepy_thread = -1;
}
/*
* Draw_Sprite()
* Draw a 32x32 sprite with a specific color and pixel size.
*/
void Draw_Sprite(const uint32_t *sprite, int8_t pos_x, int8_t pos_y, uint16_t color, uint8_t size)
{
uint16_t x_sprite;
uint16_t y_sprite;
for (int i = 0; i < 1024; i++)
{
if (sprite[i] != 0)
{
x_sprite = (i % 32) + pos_x;
y_sprite = (i / 32) + pos_y;
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle(x_sprite * size, y_sprite * size, size, size, color);
G8RTOS_SignalSemaphore(&sem_SPIA);
}
}
}
void Draw_Menu()
{
menu_index++;
switch (menu_index)
{
case FEED:
Draw_Sprite(Food_Icon, 89, 10, ST7789_BLUE, 2);
break;
case LIGHT:
Draw_Sprite(Food_Icon, 89, 10, ST7789_BLACK, 2);
Draw_Sprite(Light_Icon, 66, 10, ST7789_BLUE, 2);
break;
case PLAY:
Draw_Sprite(Light_Icon, 66, 10, ST7789_BLACK, 2);
Draw_Sprite(Game_Icon, 43, 10, ST7789_BLUE, 2);
break;
case MEDICINE:
Draw_Sprite(Game_Icon, 43, 10, ST7789_BLACK, 2);
Draw_Sprite(Medicine_Icon, 20, 10, ST7789_BLUE, 2);
break;
case DUCK:
Draw_Sprite(Medicine_Icon, 20, 10, ST7789_BLACK, 2);
Draw_Sprite(Duck_Icon, 89, 120, ST7789_BLUE, 2);
break;
case HEALTH_METER:
Draw_Sprite(Duck_Icon, 89, 120, ST7789_BLACK, 2);
Draw_Sprite(Health_Icon, 66, 120, ST7789_BLUE, 2);
break;
case DISCIPLINE:
Draw_Sprite(Health_Icon, 66, 120, ST7789_BLACK, 2);
Draw_Sprite(Food_Icon, 43, 120, ST7789_BLUE, 2);
break;
case ATTENTION:
Draw_Sprite(Food_Icon, 43, 120, ST7789_BLACK, 2);
Draw_Sprite(Food_Icon, 20, 120, ST7789_BLUE, 2);
break;
case OTHER:
Draw_Sprite(Food_Icon, 20, 120, ST7789_BLACK, 2);
menu_index = NONE;
break;
}
}
void Flush_Poop()
{
// Reset poop timer
if (Tama->poop <= 0)
Tama->poop = 10;
// Clear screen
G8RTOS_KillThread(ID_walking_tama);
// Draw flush poop
for (int i = -6; i < 36; i++)
{
Draw_Sprite(Sprite_Flush, i - 2, 20, ST7789_BLACK, 4);
Draw_Sprite(Sprite_Flush, i, 20, ST7789_BLUE, 4);
sleep(40);
}
// Back to previous activity
if (Tama->sleep_deficit > 0)
ID_sleepy_thread = G8RTOS_AddThread(Sleepy_Tama_Thread, 2, "Sleep");
else
ID_walking_tama = G8RTOS_AddThread(Walking_Tama_Thread, 2, "walk");
// Clear screen
current_menu = OTHER;
}
void Draw_Digits(uint16_t number, uint16_t x, uint16_t y)
{
for (int i = 0; i < 3; i++)
{
int digit = number % 10;
Draw_Sprite(Digit_Sprites[digit], x, y, ST7789_BLUE, 4);
number = number / 10;
x += 6;
}
}
/*************************************Threads***************************************/
void Idle_Thread(void)
{
while (1)
;
}
/*
* Blinking_LED()
* Background thread to notify when a tamagotchi needs attention.
*/
void Blinking_LED(void)
{
// PCA9956b_SetAllOff();
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 5; i++)
{
PCA9556b_SetLED(LEDOUT0 + 2 + i, 0xFF, 0xFF);
PCA9556b_SetLED(LEDOUT0 + 3 + i, 0xFF, 0xFF);
PCA9556b_SetLED(LEDOUT0 + 4 + i, 0xFF, 0xFF);
PCA9556b_SetLED(LEDOUT0 + 5 + i, 0xFF, 0xFF);
sleep(40);
PCA9556b_SetLED(LEDOUT0 + 2 + i, 0x00, 0xFF);
PCA9556b_SetLED(LEDOUT0 + 3 + i, 0x00, 0xFF);
PCA9556b_SetLED(LEDOUT0 + 4 + i, 0x00, 0xFF);
PCA9556b_SetLED(LEDOUT0 + 5 + i, 0x00, 0xFF);
}
sleep(80);
}
G8RTOS_KillSelf();
}
/*
* Button A - Move through options
* Button B - Select an option
* Button C - Cancel or exit a menu
*/
void Read_Buttons(void)
{
// Initialize / declare any variables here
uint8_t buttons_data;
while (1)
{
// wait for button semaphore
G8RTOS_WaitSemaphore(&sem_PCA9555_Debounce);
// Get buttons
G8RTOS_WaitSemaphore(&sem_I2CA);
buttons_data = MultimodButtons_Get();
G8RTOS_SignalSemaphore(&sem_I2CA);
// debounce buttons
sleep(15);
// Button A
if (((buttons_data >> 4) & 1) == 0)
{
if (game_started)
{
if (food_menu_on)
burger = !burger;
else
Draw_Menu();
}
}
// Button B - Select
else if (((buttons_data >> 3) & 1) == 0)
{
// Update current menu selection
if (game_started)
{
if (food_menu_on)
{
if (Tama->hunger < 10)
{
Tama->hunger++;
if (!burger)
Tama->weight++;
}
}
else
{
current_menu = menu_index;
}
}
// Hatch new tama
else
{
G8RTOS_KillThread(ID_start_thread);
Initialize_Tama();
}
}
// Button C - Exit
else if (((buttons_data >> 2) & 1) == 0)
{
if (game_started)
{
// Kill current menu page
if (food_menu_on)
{
food_menu_on = false;
G8RTOS_KillThread(ID_food_thread);
}
else if (health_menu_on)
{
health_menu_on = false;
G8RTOS_KillThread(ID_health_thread);
}
// Clear board
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle(10, 76, 220, 132, ST7789_BLACK);
G8RTOS_SignalSemaphore(&sem_SPIA);
// Back to main
if (Tama->sleep_deficit > 0)
Draw_Sprite(Sleepy_Tama, 21, 27, ST7789_BLUE, 4);
else
ID_walking_tama = G8RTOS_AddThread(Walking_Tama_Thread, 2, "Walk");
}
}
// clear button interrupt
GPIOIntClear(BUTTONS_INT_GPIO_BASE, BUTTONS_INT_PIN);
GPIOIntEnable(BUTTONS_INT_GPIO_BASE, BUTTONS_INT_PIN);
}
}
void Menu_Thread(void)
{
while (1)
{
if (game_started)
{
switch (current_menu)
{
case NONE:
break;
case FEED:
G8RTOS_KillThread(ID_walking_tama);
current_menu = NONE;
food_menu_on = true;
ID_food_thread = G8RTOS_AddThread(Food_Thread, 2, "food");
break;
case LIGHT:
if (Tama->sleeping)
{
G8RTOS_KillThread(ID_light_thread);
Tama->sleeping = false;
if (Tama->sleep_deficit > 0)
ID_sleepy_thread = G8RTOS_AddThread(Sleepy_Tama_Thread, 2, "Sleepy");
else
ID_walking_tama = G8RTOS_AddThread(Walking_Tama_Thread, 2, "walk");
}
else
{
G8RTOS_KillThread(ID_sleepy_thread);
G8RTOS_KillThread(ID_walking_tama);
Tama->sleeping = true;
ID_light_thread = G8RTOS_AddThread(Light_Off_Thread, 2, "Light");
}
current_menu = NONE;
break;
case MEDICINE:
// Reset health score & kill virus
Tama->health = 10;
current_menu = NONE;
if (ID_virus_thread)
{
G8RTOS_KillThread(ID_virus_thread);
Draw_Sprite(Sprite_Virus, 7, 18, ST7789_BLACK, 4);
}
break;
case DUCK:
Flush_Poop();
break;
case HEALTH_METER:
G8RTOS_KillThread(ID_walking_tama);
current_menu = NONE;
health_menu_on = true;
ID_health_thread = G8RTOS_AddThread(Draw_Health_Thread, 2, "health");
break;
case OTHER:
current_menu = NONE;
break;
}
}
}
}
void Walking_Tama_Thread(void)
{
// Clear board
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle(10, 76, 220, 132, ST7789_BLACK);
G8RTOS_SignalSemaphore(&sem_SPIA);
int8_t poop_space = 0;
while (1)
{
// Check for poop space
if (Tama->poop <= 0)
{
poop_space = 10;
Draw_Sprite(Sprite_Poop, 0, SPRITE_OFFSET_Y, ST7789_BLUE, 4);
}
// Walk right
for (int i = 12; i > -12 + poop_space; i--)
{
Draw_Sprite(Tama->sprite_R, SPRITE_OFFSET_X + i, SPRITE_OFFSET_Y, ST7789_BLACK, 4);
if (current_menu != 0)
return;
Draw_Sprite(Tama->sprite_R, SPRITE_OFFSET_X + i - 1, SPRITE_OFFSET_Y, ST7789_BLUE, 4);
sleep(80);
}
Draw_Sprite(Tama->sprite_R, SPRITE_OFFSET_X + -12 + poop_space, SPRITE_OFFSET_Y, ST7789_BLACK, 4);
// Walk left
for (int i = -12 + poop_space; i < 12; i++)
{
Draw_Sprite(Tama->sprite_L, SPRITE_OFFSET_X + i - 1, SPRITE_OFFSET_Y, ST7789_BLACK, 4);
if (current_menu != 0)
return;
Draw_Sprite(Tama->sprite_L, SPRITE_OFFSET_X + i, SPRITE_OFFSET_Y, ST7789_BLUE, 4);
sleep(80);
}
Draw_Sprite(Tama->sprite_L, SPRITE_OFFSET_X + 11, SPRITE_OFFSET_Y, ST7789_BLACK, 4);
}
}
void Draw_Start_Thread()
{
for (int i = 0; i < 1760; i++)
{
if (Start_Page[i] != 0)
{
uint16_t x_sprite = i % 55;
uint16_t y_sprite = i / 55;
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle((x_sprite * 4) + 10, (y_sprite * 4) + 76, 4, 4, ST7789_BLUE);
G8RTOS_SignalSemaphore(&sem_SPIA);
}
}
while (1)
{
// Blink button B
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle(114, 140, 12, 12, ST7789_BLUE);
G8RTOS_SignalSemaphore(&sem_SPIA);
sleep(300);
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle(114, 140, 12, 12, ST7789_BLACK);
G8RTOS_SignalSemaphore(&sem_SPIA);
sleep(300);
}
}
void Virus_Thread()
{
while (1)
{
Draw_Sprite(Sprite_Virus, 7, 18, ST7789_BLUE, 4);
sleep(250);
Draw_Sprite(Sprite_Virus, 7, 18, ST7789_BLACK, 4);
sleep(250);
}
}
void Sleepy_Tama_Thread()
{
uint32_t start_time = tama_time;
// Clear screen
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle(10, 76, 220, 128, ST7789_BLACK);
G8RTOS_SignalSemaphore(&sem_SPIA);
// Re-draw poop
if (Tama->poop <= 0)
Draw_Sprite(Sprite_Poop, 0, SPRITE_OFFSET_Y, ST7789_BLUE, 4);
// Draw sleepy tama
Draw_Sprite(Sleepy_Tama, 21, 27, ST7789_BLUE, 4);
while (1)
{
// TODO
Tama->sleep_deficit = tama_time - start_time;
}
}
void Light_Off_Thread()
{
uint32_t start_time = tama_time;
// Clear screen
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle(10, 76, 220, 128, ST7789_BLUE);
G8RTOS_SignalSemaphore(&sem_SPIA);
while (1)
{
// TODO
if (tama_time - start_time > 10)
{
// Clear screen
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle(10, 76, 220, 128, ST7789_BLACK);
G8RTOS_SignalSemaphore(&sem_SPIA);
// Re-draw poop
if (Tama->poop <= 0)
Draw_Sprite(Sprite_Poop, 0, SPRITE_OFFSET_Y, ST7789_BLUE, 4);
// Kill thread to turn the light on
Tama->sleep_deficit = 0;
current_menu = NONE;
ID_walking_tama = G8RTOS_AddThread(Walking_Tama_Thread, 2, "Walk");
G8RTOS_KillSelf();
}
}
}
void Food_Thread()
{
// Clear screen
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle(10, 76, 220, 128, ST7789_BLACK);
G8RTOS_SignalSemaphore(&sem_SPIA);
// Draw food
Draw_Sprite(Sprite_Food, 18, 26, ST7789_BLUE, 4);
// Draw Arrow
Draw_Sprite(Sprite_Arrow, 40, 26, ST7789_BLUE, 4);
while (1)
{
if (burger)
{
Draw_Sprite(Sprite_Arrow, 40, 38, ST7789_BLACK, 4);
Draw_Sprite(Sprite_Arrow, 40, 26, ST7789_BLUE, 4);
}
else
{
Draw_Sprite(Sprite_Arrow, 40, 26, ST7789_BLACK, 4);
Draw_Sprite(Sprite_Arrow, 40, 38, ST7789_BLUE, 4);
}
}
}
void Draw_Health_Thread()
{
// Clear board
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle(10, 76, 220, 132, ST7789_BLACK);
G8RTOS_SignalSemaphore(&sem_SPIA);
Draw_Sprite(Age_Icon, 31, 9, ST7789_BLUE, 4);
Draw_Sprite(Scale_Icon, 32, 30, ST7789_BLUE, 4);
Draw_Sprite(Sprite_Year, 3, 12, ST7789_BLUE, 4);
Draw_Sprite(Sprite_Ounce, 3, 32, ST7789_BLUE, 4);
Draw_Digits(Tama->age, 6, 10);
while (1)
{
}
}
/********************************Periodic Threads***********************************/
// keep the clock running and keep track of status
void Clock_Thread()
{
if (game_started && !Tama->dead)
{
tama_time++;
if (tama_time % 30 == 0)
{
Tama->age++;
if (Tama->age % 2 == 0)
{
Tama->weight += 5;
}
}
// Decrement hunger
if (tama_time % 5 == 2)
{
Tama->hunger--;
}
// Decrement health
if (tama_time % 3 == 0)
{
Tama->health--;
if (Tama->health == 0)
{
ID_virus_thread = G8RTOS_AddThread(Virus_Thread, 2, "virus");
G8RTOS_AddThread(Blinking_LED, 2, "LED");
}
}
// Decrement poop
if (tama_time % 5 == 0)
{
Tama->poop--;
if (Tama->poop == 0)
{
Draw_Sprite(Sprite_Poop, 0, SPRITE_OFFSET_Y, ST7789_BLUE, 4);
G8RTOS_AddThread(Blinking_LED, 2, "LED");
}
}
if (!Tama->sleeping && (tama_time % 40 == 0))
{
G8RTOS_KillThread(ID_walking_tama);
ID_sleepy_thread = G8RTOS_AddThread(Sleepy_Tama_Thread, 2, "Sleepy");
G8RTOS_AddThread(Blinking_LED, 2, "LED");
}
// Check tama's status need to changed to dead
if (Tama->hunger < -2 || Tama->health < -2 || Tama->poop < -2 || Tama->sleep_deficit > 15)
{
Tama->dead = true;
tama_time = 0;
game_started = false;
G8RTOS_KillThread(ID_walking_tama);
G8RTOS_KillThread(ID_sleepy_thread);
G8RTOS_KillThread(ID_virus_thread);
G8RTOS_KillThread(ID_light_thread);
sleep(80);
// Clear board
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_Fill(ST7789_BLACK);
G8RTOS_SignalSemaphore(&sem_SPIA);
// Draw end page
for (int i = 0; i < 1760; i++)
{
if (End_Page[i] != 0)
{
uint16_t x_sprite = i % 55;
uint16_t y_sprite = i / 55;
G8RTOS_WaitSemaphore(&sem_SPIA);
ST7789_DrawRectangle((x_sprite * 4) + 10, (y_sprite * 4) + 76, 4, 4, ST7789_BLUE);
G8RTOS_SignalSemaphore(&sem_SPIA);
}
}
free(Tama);
}
}
}
/*******************************Aperiodic Threads***********************************/
void Button_Handler()
{
// disable interrupt and signal semaphore
GPIOIntDisable(BUTTONS_INT_GPIO_BASE, BUTTONS_INT_PIN);
G8RTOS_SignalSemaphore(&sem_PCA9555_Debounce);
}