-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
726 lines (583 loc) · 17.5 KB
/
main.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/**
*
* @file main.c
*
* @date May 24, 2017
* @author hamster
*
* @brief This is the entry point for the DC801 DC26 party badge
*
*/
#include <stdint.h>
#include "common.h"
#include "user.h"
volatile bool partyMode = false;
volatile bool sheepMode = false;
APP_TIMER_DEF(standby_animation_timer_id);
static void standby_animation_timeout_handler(void *p_context) {
UNUSED_PARAMETER(p_context);
util_gfx_draw_raw_file_stop();
}
/**
* Initialize the LEDs
*
* LED_POWER_UP_2 is not used because there is some bug in the softdevice where it realllllly
* wants to flash that LED on LCD SPI access and it's just annoying. So, we'll just disabled.
*/
static void led_init(void){
// Setup and turn off the LEDs
nrf_gpio_cfg_output(LED_LEVEL_UP_0);
nrf_gpio_cfg_output(LED_LEVEL_UP_1);
nrf_gpio_cfg_output(LED_LEVEL_UP_2);
nrf_gpio_cfg_output(LED_LEVEL_UP_3);
nrf_gpio_cfg_output(LED_POWER_UP_0);
nrf_gpio_cfg_output(LED_POWER_UP_1);
//nrf_gpio_cfg_output(LED_POWER_UP_2);
nrf_gpio_cfg_output(LED_POWER_UP_3);
nrf_gpio_pin_set(LED_LEVEL_UP_0);
nrf_gpio_pin_set(LED_LEVEL_UP_1);
nrf_gpio_pin_set(LED_LEVEL_UP_2);
nrf_gpio_pin_set(LED_LEVEL_UP_3);
nrf_gpio_pin_set(LED_POWER_UP_0);
nrf_gpio_pin_set(LED_POWER_UP_1);
nrf_gpio_pin_set(LED_POWER_UP_2);
nrf_gpio_pin_set(LED_POWER_UP_3);
}
/**
* Initialize the buttons
*/
static void button_init(void){
// Setup the buttons
nrf_gpio_cfg_input(USER_BUTTON_UP, NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(USER_BUTTON_DOWN, NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(USER_BUTTON_LEFT, NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(USER_BUTTON_RIGHT, NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(USER_BUTTON_A, NRF_GPIO_PIN_NOPULL);
nrf_gpio_cfg_input(USER_BUTTON_B, NRF_GPIO_PIN_NOPULL);
}
/**
* Initialize the speaker
*/
static void speaker_init(void){
// Setup the beeper
nrf_gpio_cfg_output(SPEAKER);
}
/**
* Initialize the logging backend for logging over JTAG
*/
static void log_init(void){
ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
/**
* Handler to show the LEDs during bootup animation
* @param frame
* @param p_data
*/
static void bootCallback(uint8_t frame, void *p_data){
if(frame > 50){
nrf_gpio_pin_clear(LED_LEVEL_UP_0);
}
if(frame > 100){
nrf_gpio_pin_clear(LED_LEVEL_UP_1);
}
if(frame > 150){
nrf_gpio_pin_clear(LED_LEVEL_UP_2);
}
if(frame > 200){
nrf_gpio_pin_clear(LED_LEVEL_UP_3);
}
if(frame > 205){
nrf_gpio_pin_clear(LED_POWER_UP_0);
}
if(frame > 210){
nrf_gpio_pin_clear(LED_POWER_UP_1);
}
if(frame > 215){
nrf_gpio_pin_clear(LED_POWER_UP_2);
}
if(frame > 222){
nrf_gpio_pin_clear(LED_POWER_UP_3);
}
}
/**
* Handler to show the LED blink during party mode
* @param frame
* @param p_data
*/
static void partyCallback(uint8_t frame, void *p_data){
if(frame % 8 == 0){
setLevelLEDs(LEVEL4);
}
else{
setLevelLEDs(LEVEL0);
}
if(frame % 2 == 0){
setPowerUpLEDs(POWERUP_0);
nrf_gpio_pin_clear(LED_POWER_UP_0);
}
if(frame % 3 == 0){
setPowerUpLEDs(POWERUP_0);
nrf_gpio_pin_clear(LED_POWER_UP_1);
}
if(frame % 4 == 0){
setPowerUpLEDs(POWERUP_0);
nrf_gpio_pin_clear(LED_POWER_UP_3);
}
if(frame == 0){
setPowerUpLEDs(POWERUP_0);
}
}
typedef enum {
item_games,
item_nearby,
item_extras,
item_credits,
item_godmode,
NUM_MENU_MAIN_ITEMS
} MENU_MAIN;
MENU mainMenu[NUM_MENU_MAIN_ITEMS] = {
{ item_games, "Games" },
{ item_nearby, "Nearby" },
{ item_extras, "Extras"},
{ item_credits, "Credits" },
{ item_godmode, "God Mode"}
};
/**
* @brief Main app
* @return Not used
*/
int main(void){
// Setup the system
led_init();
button_init();
speaker_init();
log_init();
// Pop pop!
beep(75, 600);
nrf_delay_ms(50);
beep(150, 800);
nrf_delay_ms(50);
// Timers
app_timer_init();
// BLE
//gap_params_init();
ble_stack_init();
scan_start();
// Init the display
st7735_init();
st7735_start();
util_gfx_init();
if(!util_sd_init()){
util_sd_error();
}
// Init the random number generator
nrf_drv_rng_init(NULL);
// Setup the battery monitor
adc_configure();
adc_start();
// Setup the UART
uart_init();
// Boot! Boot! Boot!
printf("Booted!\n");
// printf goes to the RTT_Terminal.log after you've fired up debug.sh
util_gfx_draw_raw_file("BOOT.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, bootCallback, false, NULL);
setPowerUpLEDs(POWERUP_0);
setLevelLEDs(LEVEL0);
// Load the user
loadUser();
if(!user.configured){
// Configure the system
printf("First time setup\n");
firstSetup();
}
// Init the WarGames game
wg_Init();
// Configure the systick
sysTickStart();
// Setup a timer for shutting down animations in standby
app_timer_create(&standby_animation_timer_id, APP_TIMER_MODE_SINGLE_SHOT, standby_animation_timeout_handler);
// Setup the BLE
advertising_setUser(user.name);
advertising_setClan(user.clan);
advertising_setScore(user.score);
ble_adv_start();
setTempScoreModifier(0);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
while(true) {
util_gfx_fill_screen(COLOR_BLACK);
drawScreenTemplate();
// Draw the main menu
#ifdef GODMODE
int subMenu = getMenuSelection(mainMenu, 50, ARRAY_SIZE(mainMenu), 4, 15000, true);
#else
int subMenu = getMenuSelection(mainMenu, 50, ARRAY_SIZE(mainMenu) - 1, 4, 15000, true);
#endif
switch(subMenu){
case item_games:
games();
break;
case item_nearby:
nearby();
break;
case item_extras:
extras();
break;
case item_credits:
credits();
break;
case item_godmode:
godMode();
break;
default:
// Show the standby screen
showStandby();
break;
}
}
#pragma clang diagnostic pop
}
/**
* Show the standby screen
* @param p_context
*/
void showStandby(void){
uint16_t nearbyCounter = 0;
uint16_t animationCounter = 0;
uint8_t delayCount = 0;
drawStandby();
while(true){
if(getButton(false)){
// User pressed a button
return;
}
nrf_delay_ms(25);
if(partyMode){
// PARTYYYY!
util_gfx_draw_raw_file("/EXTRAS/SHEEP6.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, partyCallback, true, NULL);
}
if(sheepMode){
// SHEEEEP
do{
uint8_t i;
util_gfx_draw_raw_file("/EXTRAS/SHEEP1.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, NULL, false, NULL);
for(i = 0; i < 100; i++){
if(getButton(false)){
break;
}
nrf_delay_ms(25);
}
util_gfx_draw_raw_file("/EXTRAS/SHEEP2.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, NULL, false, NULL);
for(i = 0; i < 100; i++){
if(getButton(false)){
break;
}
nrf_delay_ms(25);
}
util_gfx_draw_raw_file("/EXTRAS/SHEEP3.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, NULL, false, NULL);
for(i = 0; i < 100; i++){
if(getButton(false)){
break;
}
nrf_delay_ms(25);
}
util_gfx_draw_raw_file("/EXTRAS/SHEEP4.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, NULL, false, NULL);
for(i = 0; i < 100; i++){
if(getButton(false)){
break;
}
nrf_delay_ms(25);
}
util_gfx_draw_raw_file("/EXTRAS/SHEEP5.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, NULL, false, NULL);
for(i = 0; i < 100; i++){
if(getButton(false)){
break;
}
nrf_delay_ms(25);
}
util_gfx_draw_raw_file("/EXTRAS/SHEEP6.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, NULL, false, NULL);
for(i = 0; i < 100; i++){
if(getButton(false)){
break;
}
nrf_delay_ms(25);
}
}while (!getButton(false));
}
if(animationCounter ++ > (15000 / 25)){
// Show an animation
uint8_t rand;
nrf_drv_rng_rand(&rand, 1);
char files[64][9];
uint8_t numFiles = 0;
memset(files, 0, sizeof(files));
numFiles = getFiles(files, "/EXTRAS", 64);
rand = rand % numFiles;
app_timer_start(standby_animation_timer_id, APP_TIMER_TICKS(10000), NULL);
char filename[21];
snprintf(filename, 21, "/EXTRAS/%s.RAW", files[rand]);
util_gfx_draw_raw_file(filename, 0, 0, GFX_WIDTH, GFX_HEIGHT, NULL, true, NULL);
animationCounter = 0;
drawStandby();
}
if(nearbyCounter++ > (5000 / 25)){
// Draw a shout
uint8_t rand;
nrf_drv_rng_rand(&rand, 1);
bool haveBadge = false;
BADGE_ADV badge;
rand = rand % getBadgeNum();
if(getBadge(rand, &badge)){
if(getBadgeIconFile(badge.group) != NULL) {
util_gfx_draw_raw_file(getBadgeIconFile(badge.group), 0, 0, GFX_WIDTH, GFX_HEIGHT, NULL, false, NULL);
}
else{
util_gfx_draw_raw_file("/GROUPS/MISSING.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, NULL, false, NULL);
util_gfx_fill_rect(0, 0, GFX_WIDTH, 15, COLOR_BLACK);
uint16_t w, h;
char name[16];
util_gfx_set_font(FONT_VERAMONO_5PT);
util_gfx_get_text_bounds(getBadgeGroupName(badge.group), 0, 0, &w, &h);
util_gfx_set_cursor(64 - (w / 2), 2);
snprintf(name, 16, "%s", getBadgeGroupName(badge.group));
util_gfx_print(name);
}
for(int i = 0; i < 10; i++){
if(i % 2 == 0) {
setPowerUpLEDs(POWERUP_4);
setLevelLEDs(LEVEL0);
}
else{
setPowerUpLEDs(POWERUP_0);
setLevelLEDs(LEVEL4);
}
delayCount = 0;
while(delayCount++ < 10) {
if(getButton(false)){
setPowerUpLEDs(POWERUP_0);
setLevelLEDs(LEVEL0);
return;
}
nrf_delay_ms(25);
}
}
setPowerUpLEDs(POWERUP_0);
setLevelLEDs(LEVEL0);
delayCount = 0;
while(delayCount++ < 40) {
if(getButton(false)){
return;
}
nrf_delay_ms(25);
}
drawStandby();
}
nearbyCounter = 0;
}
}
}
/**
* Draw the user stats on the standby screen
*/
void drawStandby(void){
// Setup the main screen
util_gfx_fill_screen(COLOR_BLACK);
// Show the username
util_gfx_set_font(FONT_MONO55_8PT);
util_gfx_set_color(COLOR_WHITE);
uint16_t w, h;
util_gfx_get_text_bounds(user.name, 0, 0, &w, &h);
util_gfx_set_cursor(64 - (w / 2), 2);
util_gfx_print(user.name);
util_gfx_set_font(FONT_GAMEPLAY_5PT);
util_gfx_set_color(COLOR_BLUE);
util_gfx_set_cursor(2, 20);
char score[11];
snprintf(score, 11, "%u", user.score);
util_gfx_print(score);
char modifier[6];
if(getTotalScoreModifier() > 0){
util_gfx_set_color(COLOR_GREEN);
}
if(getTotalScoreModifier() == 0){
util_gfx_set_color(COLOR_WHITE);
}
if(getTotalScoreModifier() < 0){
util_gfx_set_color(COLOR_RED);
}
snprintf(modifier, 6, "%+d", getTotalScoreModifier());
util_gfx_get_text_bounds(modifier, 0, 0, &w, &h);
util_gfx_set_cursor(GFX_WIDTH - w - 1, 20);
util_gfx_print(modifier);
util_gfx_draw_raw_file(getClanFile(user.clan), 0, 29, GFX_WIDTH, 100, NULL, false, NULL);
}
/**
* Configure a new user
*/
void firstSetup(void){
// Clear out the user data
memset(&user, 0, sizeof(USER));
util_gfx_fill_screen(COLOR_BLACK);
util_gfx_set_font(FONT_COMPUTER_12PT);
util_gfx_set_color(COLOR_BLUE);
util_gfx_set_cursor(20, 0);
util_gfx_print("Welcome");
util_gfx_set_font(FONT_MONO55_8PT);
util_gfx_set_color(COLOR_WHITE);
util_gfx_set_cursor(5, 20);
util_gfx_print("Can you get the\n highest score?");
util_gfx_set_cursor(28, 65);
util_gfx_print("Let's get\n started");
util_gfx_set_cursor(35, 110);
util_gfx_print("Press A");
while(getButton(false) != USER_BUTTON_A){
// Wait until pressed
}
while(getButton(false) == USER_BUTTON_A){
// Wait until released
}
util_gfx_fill_rect(0, 18, 128, 88, COLOR_BLACK);
util_gfx_set_cursor(8, 20);
util_gfx_print("You can change\n later and your\n score will be\n kept");
while(getButton(false) != USER_BUTTON_A){
// Wait until pressed
}
while(getButton(false) == USER_BUTTON_A){
// Wait until released
}
util_gfx_fill_rect(0, 18, 128, 88, COLOR_BLACK);
util_gfx_set_cursor(18, 20);
util_gfx_print("You play for\n yourself and\n your clan");
while(getButton(false) != USER_BUTTON_A){
// Wait until pressed
}
while(getButton(false) == USER_BUTTON_A){
// Wait until released
}
util_gfx_fill_rect(0, 18, 128, 88, COLOR_BLACK);
util_gfx_set_cursor(5, 20);
util_gfx_print("Scoreboard will\n be at the\n party");
while(getButton(false) != USER_BUTTON_A){
// Wait until pressed
}
while(getButton(false) == USER_BUTTON_A){
// Wait until released
}
userConfigure();
}
typedef enum {
game_invaders,
game_snake,
game_pips_the_et,
game_tic_tac_toe,
NUM_MENU_GAMES_ITEMS
} MENU_GAMES;
MENU gameMenu[NUM_MENU_GAMES_ITEMS] = {
{ game_invaders, "Invaders" },
{ game_snake, "Snake" },
{ game_pips_the_et, "Pips the ET" },
{ game_tic_tac_toe, "TicTacToe" }
};
/**
* Show the games menu
*/
void games(void){
drawScreenTemplate();
int retScore = 0;
int getGame;
if(user.wargameUnlock) {
getGame = getMenuSelection(gameMenu, 50, ARRAY_SIZE(gameMenu), 4, 15000, true);
}
else{
getGame = getMenuSelection(gameMenu, 50, ARRAY_SIZE(gameMenu) , 4, 15000, true);
}
switch(getGame){
case game_invaders:
// Space invaders
retScore = SpaceInvaders();
break;
case game_snake:
// Snake run
retScore = Snake();
break;
case game_pips_the_et:
retScore = PipsTheET();
break;
case game_tic_tac_toe:
retScore = TicTacToe();
break;
default:
break;
}
// Figure out the score modifier
retScore = retScore + (retScore * ((getTotalScoreModifier() / 100)));
// Calculate the user's score
if(user.score + retScore > 0){
user.score += retScore;
}
else{
user.score = 0;
}
storeUser();
advertising_setScore(user.score);
}
typedef enum {
extra_reconfig,
extra_boot,
extra_groups,
extra_fun,
extra_version,
NUM_MENU_EXTRA_ITEMS
} MENU_EXTRA;
MENU extraMenu[NUM_MENU_EXTRA_ITEMS] = {
{ extra_reconfig, "Reconfig" },
{ extra_boot, "Boot Logo" },
{ extra_groups, "DC Groups" },
{ extra_fun, "Fun Stuff" },
{ extra_version, "Version" }
};
/**
* Show the extras menu
*/
void extras(void){
drawScreenTemplate();
int getExtra = getMenuSelection(extraMenu, 50, ARRAY_SIZE(extraMenu), 4, 15000, true);
switch(getExtra){
case extra_reconfig:
util_gfx_set_font(FONT_MONO55_8PT);
util_gfx_set_color(COLOR_WHITE);
userConfigure();
break;
case extra_boot:
util_gfx_draw_raw_file("BOOT.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, bootCallback, false, NULL);
setPowerUpLEDs(POWERUP_0);
setLevelLEDs(LEVEL0);
break;
case extra_groups:
extraShowGroups();
break;
case extra_fun:
extraFunBrowser();
break;
case extra_version:
extraVersion();
break;
default:
break;
}
}
/**
* Show the credits
*/
void credits(void){
util_gfx_draw_raw_file("CREDITS.RAW", 0, 0, GFX_WIDTH, GFX_HEIGHT, NULL, false, NULL);
while(true){
if(getButton(false)){
// User pressed a button
return;
}
nrf_delay_ms(10);
}
}