-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2271 lines (1967 loc) · 74.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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <driverlib/MSP430FR5xx_6xx/wdt_a.h>
// System includes:
#include <stdint.h>
#include <grlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <driverlib/MSP430FR5xx_6xx/driverlib.h>
// Grace includes:
#include <ti/mcu/msp430/Grace.h>
// Project includes:
#include <qc12_oled.h>
#include <img.h>
#include <qc12.h>
#include <radio.h>
#include <leds.h>
#include <oled.h>
// Interrupt flags:
volatile uint8_t f_bl = 0;
volatile uint8_t f_br = 0;
volatile uint8_t f_bs = 0;
volatile uint8_t f_time_loop = 0;
volatile uint8_t f_new_second = 0;
volatile uint8_t f_rfm_rx_done = 0;
volatile uint8_t f_rfm_tx_done = 0;
volatile uint8_t f_tlc_anim_done = 0;
volatile uint8_t s_new_minute = 0;
volatile uint8_t f_radio_fault = 0;
// Non-interrupt signal flags (no need to avoid optimization):
uint8_t s_default_conf_loaded = 0;
uint8_t s_need_rf_beacon = 0;
uint8_t s_flag_wave = 0;
uint8_t s_flag_send = 0;
uint8_t s_new_uber_seen = 0;
uint8_t s_new_badge_seen = 0;
uint8_t s_new_uber_friend = 0;
uint8_t s_new_friend = 0;
uint8_t s_oled_needs_redrawn_idle = 0;
uint8_t s_overhead_done;
uint8_t s_befriend_failed = 0;
uint8_t s_befriend_success = 0;
uint8_t s_new_checkin = 0;
uint8_t s_send_play = 0;
uint8_t s_send_puppy = 0;
uint8_t puppy_target = 0;
uint8_t disable_beacon_service = 0;
uint8_t idle_mode_softkey_sel = 0;
uint8_t idle_mode_softkey_dis = 0;
uint8_t s_need_idle_action = 0;
uint8_t idle_action_seconds = 10;
uint8_t minute_secs = 60;
uint8_t befriend_mode = 0;
uint8_t befriend_mode_loops_to_tick = 0;
uint8_t befriend_mode_ticks = 0;
uint8_t befriend_mode_secs = 0;
uint8_t befriend_candidate = 0;
char befriend_candidate_handle[NAME_MAX_LEN+1] = {0};
uint8_t befriend_candidate_age = 0;
#define PLAY_MODE_CAUSE 1
#define PLAY_MODE_OBSERVE 2
#define PLAY_MODE_RECV 3
#define PLAY_MODE_EFFECT 4
#define PLAY_MODE_CAUSE_ALONE 5
uint8_t play_mode = 0;
uint8_t play_id = 0;
#define START_BEFRIEND_TLC_ANIM tlc_start_anim(&flag_blue, 0, 30*GLOBAL_TLC_SPEED_SCALE, 1, 0)
#define LED_PULSE(colorflag) tlc_start_anim(&colorflag, 0, 3*GLOBAL_TLC_SPEED_SCALE, 1, 2);
#define BF_S_BEACON 1
#define BF_S_OPEN 3
#define BF_S_WAIT 5
#define BF_C_OPENING 2
#define BF_C_CLOSING 4
#define BF_C_WAIT 6
uint8_t flag_id = 0;
uint8_t am_puppy = 0;
void poll_buttons();
#pragma DATA_SECTION (my_conf, ".infoA");
#pragma DATA_SECTION (default_conf, ".infoB");
// This is PERSISTENT:
// That means that it will be CLOBBERED by the DEBUG TOOLCHAIN ONLY.
// But it WON'T be on power cycle.
qc12conf my_conf = {0};
const qc12conf default_conf = {
BADGE_ID, // id
50, // mood
};
#pragma PERSISTENT(badge_seen_ticks)
uint16_t badge_seen_ticks[BADGES_IN_SYSTEM] = {0};
#pragma PERSISTENT(badges_seen)
uint8_t badges_seen[BADGES_IN_SYSTEM] = {0};
#pragma PERSISTENT(fav_badges_ids)
uint8_t fav_badges_ids[FAVORITE_COUNT] = {0};
#pragma PERSISTENT(fav_badges_handles)
char fav_badges_handles[FAVORITE_COUNT][NAME_MAX_LEN+1] = {0};
// Gaydar:
uint8_t window_position = 0; // only used for skipping windows.
uint8_t skip_window = 1;
uint8_t neighbor_count = 0;
uint8_t window_seconds = RECEIVE_WINDOW_LENGTH_SECONDS;
uint8_t neighbor_badges[BADGES_IN_SYSTEM] = {0};
uint8_t at_base = 0;
uint8_t at_suite_base = 0;
// Mood:
uint8_t mood_tick_minutes = MOOD_TICK_MINUTES;
#define ACH_BABY 0
#define ACH_NEWBIE 1
#define ACH_NICE 2
#define ACH_SOCIAL 3
#define ACH_HIP 4
#define ACH_SEXY 5
#define ACH_FRIENDLY 6
#define ACH_COOL 7
#define ACH_STAR 8
#define ACH_BADGER 9
#define ACH_FISH 10
#define ACH_TWIN 11
#define ACH_TWINSY 12
#define ACH_CHILL 13
#define ACH_TEASE 14
#define ACH_PUNKD 15
#define ACH_TIRED 16
#define ACH_SLEEPY 17
#define ACH_SQUIRE 18
#define ACH_ELITE 19
#define ACH_MOODY 20
#define ACH_MIXER 21
#define ACH_KICKOFF 22
#define ACH_CHIEF 23
#define ACH_PUPPY 24
#define ACH_HANDLER 25
#define ACH_TOWEL 26
#define ACH_CHEATER 27
const char titles[NUM_ACHIEVEMENTS][8] = {
"baby",
"newbie",
"nice",
"social",
"hip",
"sexy",
"friend",
"cool",
"star",
"badger",
"fish",
"twin",
"twinsy!",
"chill",
"tease",
"punk'd",
"tired",
"sleepy",
"squire",
"elite",
"moody",
"queer",
"early",
"Chief",
"Puppy",
"Handler",
"Towel",
"Cheat",
};
const char title_descs[NUM_ACHIEVEMENTS][36] = {
"Turn it on.",
"You're all growed up",
"Meet 20 qcbadges",
"Meet 40 qcbadges",
"Meet 60 qcbadges",
"Sleep with your fave",
"Make 5 friends",
"Make 15 friends",
"Make 50 friends",
"qcbadge talk: 12p Fri, qcsuite",
"Go to QC pool party: 9p Fri Bally's",
"Find your twin",
"Befriend your twin",
"Hang around the suite a while",
"Play a lot",
"Be played with lot",
"Be awake 24 hours",
"Sleep over 8 hours",
"Meet 10 qc ubers",
"Befriend 10 qc ubers",
"Be sad for 8 hours",
"Go to a QC mixer: 4p daily, qcsuite",
"Attend QC12 Thurs kickoff",
"\"Popular!\"",
"\"Where is he?\"",
"\"You found him!\"",
"\"No, you're a towel.\"",
"\"?????\"",
};
// In the "modal" sense:
uint8_t op_mode = OP_MODE_IDLE;
uint8_t suppress_softkey = 0;
uint16_t softkey_en = 0;
const char sk_labels[SK_SEL_MAX+1][10] = {
"Play",
"ASL?",
"Befriend",
"Use flag",
"Set flag",
"Grow up!",
"Set name",
"Sleep"
};
void my_conf_write_crc() {
if (!my_conf.adult) { // Base child softkeys:
softkey_en = SK_BIT_ASL | SK_BIT_NAME | SK_BIT_PLAY;
if (my_conf.time_to_hatch) {
softkey_en |= SK_BIT_HATCH;
}
} else { // Base adult softkeys:
softkey_en = SK_BIT_ASL | SK_BIT_NAME | SK_BIT_PLAY | SK_BIT_SLEEP | SK_BIT_FRIEND;
if (my_conf.flag_unlocks) {
softkey_en |= SK_BIT_SETFLAG | SK_BIT_FLAG;
}
}
CRC_setSeed(CRC_BASE, 0x0C12);
for (uint8_t i = 0; i < sizeof(qc12conf) - 2; i++) {
CRC_set8BitData(CRC_BASE, ((uint8_t *) &default_conf)[i]);
}
my_conf.crc16 = CRC_getResult(CRC_BASE);
}
void mood_adjust_and_write_crc(int8_t change) {
if (change < 0) {
if (my_conf.mood < (-change)) {
my_conf.mood = 0;
} else {
my_conf.mood += change;
}
} else if (change > 0) {
if (my_conf.mood > (100 - change)) {
my_conf.mood = 100;
} else {
my_conf.mood += change;
}
}
if (my_conf.mood >= MOOD_THRESH_SAD) {
my_conf.sadtime = 0;
}
my_conf_write_crc();
tlc_set_ambient(my_conf.mood);
}
uint8_t achievement_have(uint8_t achievement_id) {
if (achievement_id >= NUM_ACHIEVEMENTS) {
return 0;
}
uint8_t frame = achievement_id / 8;
uint8_t bit = 0x01 << (achievement_id % 8);
if (my_conf.achievements[frame] & bit) {
return 1;
} else {
return 0;
}
}
void achievement_get(uint8_t achievement_id, uint8_t animate) {
if (achievement_id >= NUM_ACHIEVEMENTS) {
return;
}
uint8_t frame = achievement_id / 8;
uint8_t bit = 0x01 << (achievement_id % 8);
if (!(my_conf.achievements[frame] & bit) || achievement_id == ACH_SEXY) {
// New achievement. woot.
my_conf.achievements[frame] |= bit;
if (achievement_id == ACH_FISH || achievement_id == ACH_KICKOFF || achievement_id == ACH_MIXER || (achievement_id == ACH_NEWBIE && my_conf.title_index != ACH_BABY)) {
} else {
my_conf.title_index = achievement_id;
}
if (animate && TLC_IS_A_GO) { // If we've nothing better to do with the lights,
tlc_start_anim(&flag_rainbow, 0, 2*GLOBAL_TLC_SPEED_SCALE, 0, 1);
}
s_oled_needs_redrawn_idle = 1;
mood_adjust_and_write_crc(MOOD_NEW_TITLE);
} else {
// Already got it. Boring.
}
}
inline void set_badge_seen(uint8_t id) {
if (id >= BADGES_IN_SYSTEM) {
__no_operation();
return;
}
if (!(BADGE_SEEN_BIT & badges_seen[id])) {
badges_seen[id] |= BADGE_SEEN_BIT;
if (my_conf.seen_count < BADGES_IN_SYSTEM)
my_conf.seen_count++;
if (my_conf.seen_count >= 120) {
achievement_get(ACH_CHIEF, 1);
} else if (my_conf.seen_count >= 60) {
achievement_get(ACH_HIP, 1);
} else if (my_conf.seen_count >= 40) {
achievement_get(ACH_SOCIAL, 1);
} else if (my_conf.seen_count >=20) {
achievement_get(ACH_NICE, 1);
}
if (id < UBERS_IN_SYSTEM) {
if (my_conf.seen_count < UBERS_IN_SYSTEM)
my_conf.uber_seen_count++;
// flag an animation
if (id != my_conf.badge_id) {
s_new_uber_seen = SIGNAL_BIT_OLED | SIGNAL_BIT_TLC;
mood_adjust_and_write_crc(MOOD_NEW_UBER_SEEN);
// SQUIRE: seen 10 ubers, but not uber:
if (my_conf.uber_seen_count >= 10 && my_conf.badge_id < UBERS_IN_SYSTEM) {
achievement_get(ACH_SQUIRE, 1);
}
}
} else {
// flag a lamer animation
if (id != my_conf.badge_id) {
s_new_badge_seen = SIGNAL_BIT_OLED | SIGNAL_BIT_TLC;
mood_adjust_and_write_crc(MOOD_NEW_SEEN);
if (id >= 15 && my_conf.badge_id >= 15) {
if ((id-15)%80 == (my_conf.badge_id-15)%80) {
achievement_get(ACH_TWIN, 1);
}
}
}
}
my_conf_write_crc();
// No need to write a CRC here because adjust_mood takes care of that for us.
}
if (oled_overhead_type == OLED_OVERHEAD_OFF) {
s_overhead_done = 1;
}
}
void set_base_seen(uint8_t base) {
if (base >= BASES_IN_SYSTEM) {
return;
}
uint16_t base_mask = BIT0 << base;
if (!(base_mask & my_conf.bases_seen)) {
s_new_checkin = 1;
if (base == BASE_KICKOFF) {
achievement_get(ACH_KICKOFF, 1);
} else if (base == BASE_POOL) {
achievement_get(ACH_FISH, 1);
} else if (base == BASE_TALK) {
achievement_get(ACH_BADGER, 1);
} else if (base == BASE_MIXER) {
achievement_get(ACH_MIXER, 1);
}
my_conf.bases_seen |= base_mask;
mood_adjust_and_write_crc(MOOD_EVENT_ARRIVE);
}
}
void set_badge_friend(uint8_t id) {
if (!(id < BADGES_IN_SYSTEM)) {
return;
}
if (!(BADGE_FRIEND_BIT & badges_seen[id])) {
badges_seen[id] |= BADGE_FRIEND_BIT;
if (my_conf.friend_count < BADGES_IN_SYSTEM)
my_conf.friend_count++;
if (my_conf.friend_count >= 50) {
achievement_get(ACH_STAR, 1);
} else if (my_conf.friend_count >= 15) {
achievement_get(ACH_COOL, 1);
} else if (my_conf.friend_count >=5) {
achievement_get(ACH_FRIENDLY, 1);
}
if (id < UBERS_IN_SYSTEM) {
if (my_conf.uber_friend_count < UBERS_IN_SYSTEM)
my_conf.uber_friend_count++;
// flag an animation
if (id != my_conf.badge_id) {
s_new_uber_friend = SIGNAL_BIT_OLED | SIGNAL_BIT_TLC;
mood_adjust_and_write_crc(MOOD_NEW_UBER_FRIEND);
}
// ELITE: seen 10 ubers:
if (my_conf.uber_friend_count >= 10) {
achievement_get(ACH_ELITE, 1);
}
} else {
// flag a lamer animation
if (id != my_conf.badge_id) {
s_new_friend = SIGNAL_BIT_OLED | SIGNAL_BIT_TLC;
mood_adjust_and_write_crc(MOOD_NEW_FRIEND);
if (id >= 15 && my_conf.badge_id >= 15) {
if ((id-15)%80 == (my_conf.badge_id-15)%80) {
achievement_get(ACH_TWINSY, 1);
}
}
}
}
my_conf_write_crc();
} else {
mood_adjust_and_write_crc(MOOD_OLD_FRIEND);
}
if (oled_overhead_type == OLED_OVERHEAD_OFF) {
s_overhead_done = 1;
}
if (id != my_conf.badge_id) {
idle_mode_softkey_dis = 0;
oled_draw_pane_and_flush(idle_mode_softkey_sel);
}
}
void check_conf() {
CRC_setSeed(CRC_BASE, 0x0C12);
for (uint8_t i = 0; i < sizeof(qc12conf) - 2; i++) {
CRC_set8BitData(CRC_BASE, ((uint8_t *) &default_conf)[i]);
}
// Load default config:
if (my_conf.crc16 != CRC_getResult(CRC_BASE)) {
memcpy(&my_conf, &default_conf, sizeof(qc12conf));
memset(badges_seen, 0, sizeof(uint16_t) * BADGES_IN_SYSTEM);
memset(fav_badges_ids, 0xff, sizeof(uint8_t) * FAVORITE_COUNT);
memset(fav_badges_handles, 0, sizeof(char) * FAVORITE_COUNT * (NAME_MAX_LEN+1));
s_default_conf_loaded = 1;
out_payload.handle[0] = 0;
set_badge_seen(my_conf.badge_id);
set_badge_friend(my_conf.badge_id);
my_conf_write_crc();
// Suppress any flags set from these so we don't do the animation:
s_new_uber_seen = s_new_badge_seen = s_new_uber_friend = s_new_friend = 0;
}
am_puppy = 0;
// Set our mood lights.
mood_adjust_and_write_crc(0);
}
void tick_badge_seen(uint8_t id, char* handle) {
if (!(id < BADGES_IN_SYSTEM)) {
return;
}
if (badge_seen_ticks[id] < UINT16_MAX) {
badge_seen_ticks[id]++;
// do top badges:
for (uint8_t top_index=0; top_index<FAVORITE_COUNT; top_index++) {
if (fav_badges_ids[top_index] == id) {
// If we run into the ID we're currently handling before
// we run into one that needs to be demoted in its favor,
// then its rank is OK (because we start at index 0, which
// is the #1 spot). Let's go ahead and make sure we've still
// got the most up-to-date handle for them though.
strcpy(fav_badges_handles[top_index], handle); // handle has already been sanitized.
break;
}
if (fav_badges_ids[top_index] == 0xff || (badge_seen_ticks[id] > badge_seen_ticks[fav_badges_ids[top_index]])) {
// This is where it goes, and all the rest need to be demoted.
// So we can start at the lowest ranked favorite, which is fav_badges_ids[FAVORITE_COUNT-1]
// and clobber it with its superior until one IS CLOBBERED BY top_index.
// Then we replace top_id's original spot with id.
for (uint8_t index_to_clobber = FAVORITE_COUNT-1; index_to_clobber>top_index; index_to_clobber--) {
// index_to_clobber starts at the max value for fav_badges_ids
// and goes through top_index+1. (index_to_clobber will never be 0).
if (fav_badges_ids[index_to_clobber-1] == id) {
// If we would be clobbering something by DEMOTING the old, stale entry for the
// badge we're logging into this list now, just skip it.
} else {
fav_badges_ids[index_to_clobber] = fav_badges_ids[index_to_clobber-1];
strcpy(fav_badges_handles[index_to_clobber], fav_badges_handles[index_to_clobber-1]);
}
}
// now clobber top_index with out new one.
fav_badges_ids[top_index] = id;
strcpy(fav_badges_handles[top_index], handle); // handle has already been sanitized.
break; // We only care about the highest ranking that we qualify for.
}
}
}
}
void init_rtc() {
RTC_B_definePrescaleEvent(RTC_B_BASE, RTC_B_PRESCALE_1, RTC_B_PSEVENTDIVIDER_2); // 4 => 32 Hz; 2 => 64 Hz
RTC_B_clearInterrupt(RTC_B_BASE, RTC_B_CLOCK_READ_READY_INTERRUPT + RTC_B_TIME_EVENT_INTERRUPT + RTC_B_CLOCK_ALARM_INTERRUPT + RTC_B_PRESCALE_TIMER1_INTERRUPT);
RTC_B_enableInterrupt(RTC_B_BASE, RTC_B_CLOCK_READ_READY_INTERRUPT + RTC_B_TIME_EVENT_INTERRUPT + RTC_B_CLOCK_ALARM_INTERRUPT + RTC_B_PRESCALE_TIMER1_INTERRUPT);
}
void init_payload() {
out_payload.base_id = NOT_A_BASE;
out_payload.beacon = 0;
out_payload.flag_id = 0;
out_payload.play_id = 0;
out_payload.friendship = 0;
out_payload.from_addr = my_conf.badge_id;
out_payload.to_addr = RFM_BROADCAST;
strcpy(out_payload.handle, my_conf.handle);
}
void init() {
Grace_init(); // Activate Grace-generated configuration
check_conf();
init_payload();
init_tlc();
init_radio();
init_oled();
init_rtc();
srand(my_conf.badge_id);
}
// Power-on self test
void post() {
// If the radio is super-broken, we won't even get here.
// Check the crystal.
uint8_t crystal_error = CSCTL5 & LFXTOFFG;
// Check the shift register of the TLC.
uint8_t led_error = tlc_test_loopback(0b10101010);
led_error = led_error || tlc_test_loopback(0b01010101);
tlc_set_fun();
// If we detected no errors, we're done here.
if (!(led_error || crystal_error))
return;
// Otherwise, show those errors and then delay for a bit so we can
// see them.
GrClearDisplay(&g_sContext);
GrContextFontSet(&g_sContext, &SYS_FONT);
uint8_t print_loc = 32;
GrStringDraw(&g_sContext, "- POST -", -1, 0, 5, 0);
if (crystal_error) {
GrStringDraw(&g_sContext, "Err: XTAL!", -1, 0, print_loc, 0);
print_loc += 12;
}
if (led_error) {
GrStringDraw(&g_sContext, "Err: LED!", -1, 0, print_loc, 0);
print_loc += 12;
}
GrFlush(&g_sContext);
delay(5000);
}
// Play a cute animation when we first turn the badge on.
void intro() {
GrImageDraw(&g_sContext, &fingerprint_1BPP_UNCOMP, 0, 0);
GrStringDrawCentered(&g_sContext, "Queercon", -1, 31, 94 + SYS_FONT_HEIGHT/3, 0);
GrStringDrawCentered(&g_sContext, "twelve", -1, 31, 94 + SYS_FONT_HEIGHT/3 + SYS_FONT_HEIGHT, 0);
GrStringDrawCentered(&g_sContext, "- 2015 -", -1, 31, 94 + SYS_FONT_HEIGHT/3 + SYS_FONT_HEIGHT*2, 0);
GrFlush(&g_sContext);
}
void radio_send_beacon(uint8_t address, uint8_t beacons) {
out_payload.beacon = beacons;
out_payload.flag_id = 0;
out_payload.friendship = 0;
out_payload.from_addr = my_conf.badge_id;
out_payload.to_addr = address;
out_payload.play_id = 0;
radio_send_sync();
}
void radio_send_befriend(uint8_t mode) {
out_payload.beacon = 0;
out_payload.flag_id = 0;
out_payload.friendship = mode;
out_payload.from_addr = my_conf.badge_id;
out_payload.play_id = 0;
if (befriend_mode > 1) {
out_payload.to_addr = befriend_candidate;
} else {
out_payload.to_addr = RFM_BROADCAST;
}
radio_send_sync();
}
void radio_send_flag(uint8_t flag) {
out_payload.beacon = 0;
out_payload.flag_id = flag;
out_payload.friendship = 0;
out_payload.from_addr = my_conf.badge_id;
out_payload.to_addr = RFM_BROADCAST;
out_payload.play_id = 0;
radio_send_sync();
}
void radio_send_play(uint8_t index) {
out_payload.beacon = 0;
out_payload.flag_id = 0;
out_payload.friendship = 0;
out_payload.play_id = BIT7 | index;
out_payload.from_addr = my_conf.badge_id;
out_payload.to_addr = RFM_BROADCAST;
radio_send_sync();
}
void radio_send_puppy(uint8_t neighbor_index) {
static uint8_t neighbor_addr;
for (neighbor_addr=0; neighbor_index && (neighbor_addr<BADGES_IN_SYSTEM); neighbor_addr++) {
if (neighbor_badges[neighbor_addr])
neighbor_index--;
}
neighbor_addr--;
out_payload.beacon = 0;
out_payload.flag_id = 0;
out_payload.friendship = 0;
out_payload.base_id = 1; // puppy base.
out_payload.play_id = 0;
out_payload.from_addr = my_conf.badge_id;
out_payload.to_addr = neighbor_addr;
radio_send_sync();
out_payload.base_id = NOT_A_BASE;
}
void set_befriend_failed() {
oled_set_overhead_text("Okbai :(", 20);
s_befriend_failed = 1;
befriend_mode = 0;
idle_mode_softkey_dis = 0;
oled_draw_pane_and_flush(idle_mode_softkey_sel);
}
// Received_flag is ignored if from_radio is 0.
void befriend_proto_step(uint8_t from_radio, uint8_t received_flag, uint8_t from_id) {
if (!befriend_mode || rfm_state != RFM_IDLE) {
return;
}
if (from_radio) {
// Flag 5 is the highest that ever gets sent. If we see
// something higher, someone's ruined something.
// Therefore we fail.
if (received_flag > 5 || received_flag == 0) {
set_befriend_failed();
return;
}
// Here, we've received a radio message.
// We expect it to be either:
// befriend_mode+1 (time to move to next state)
// or
// befriend_mode-1 (need to re-send this state)
// First we'll want to validate the sender's ID.
if (befriend_mode == 1) {
// If we are a beaconing server, we use the incoming
// sender to set our befriend_candidate.
befriend_candidate = from_id;
if (received_flag == 1 && from_id > my_conf.badge_id) {
befriend_mode = 2;
}
} else if (from_id != befriend_candidate) {
// Otherwise, we need to ignore anything that's not
// from our befriend_candidate.
return;
}
// Now let's handle the protocol machine:
if (received_flag == befriend_mode-1) {
// Older message received: we need to re-transmit our current mode.
radio_send_befriend(befriend_mode);
} else if (received_flag == befriend_mode+1) {
// Next message received: we're ready to move along in the protocol.
befriend_mode_ticks = BEFRIEND_RESEND_TRIES;
befriend_mode += 2;
// Two valid protocol states are TIME ONLY:
// BF_S_WAIT (where we think we've transmitted the last packet
// in the conversation but want to make sure that
// the client has received it)
// and
// BF_C_WAIT (where we've transmitted the client's last packet in
// the conversation, received the server's last
// packet, and we're waiting a bit in order to try
// to synchronize our next actions with the server.)
// So if we're in one of those states, don't send anything.
if (befriend_mode != BF_S_WAIT && befriend_mode != BF_C_WAIT) {
oled_set_overhead_text("Hello!", 75);
radio_send_befriend(befriend_mode);
}
} else {
return;
}
// End of radio handling section
} else {
// (we've already ruled out 0)
if (befriend_mode < 5) { // the "normal" modes:
// BEACON, OPEN
// OPENING, and CLOSING.
// In these we retransmit if we have ticks left,
// otherwise we time out.
if (befriend_mode == BF_S_BEACON || befriend_mode == BF_C_OPENING) {
// Modes 1 and 2's timeout is controlled outside of this
// function, so make sure it never times out here.
// This is kind of a kludge.
befriend_mode_ticks = BEFRIEND_RESEND_TRIES;
}
if (befriend_mode_ticks) {
// still some retries left.
befriend_mode_ticks--;
// Do re-send our message.
oled_set_overhead_text("Hello?", 75);
radio_send_befriend(befriend_mode);
} else {
set_befriend_failed();
}
} else {
// CLOSE_WAIT and JUST_WAIT. (The waiting modes)
// Here we just tick down and call things a success if we reach
// zero.
if (befriend_mode_ticks) {
befriend_mode_ticks--;
} else {
befriend_mode = 0;
s_befriend_success = SIGNAL_BIT_OLED | SIGNAL_BIT_TLC;
oled_set_overhead_text(befriend_candidate_handle, 180);
set_badge_friend(befriend_candidate);
}
}
} // end of time-step handling section.
}
void handle_infrastructure_services() {
static uint8_t flag_in_cooldown = 0;
// Handle inbound and outbound background radio functionality, and buttons.
if (f_rfm_tx_done) {
f_rfm_tx_done = 0;
// And return to our normal receive automode:
// RX->SB->RX on receive.
mode_sync(RFM_MODE_RX);
write_single_register(0x3b, RFM_AUTOMODE_RX);
}
// Radio RX tasks:
if (f_rfm_rx_done) {
radio_recv(); // If the CRC is bad, this will unset the flag.
}
// Got a probably valid message:
if (f_rfm_rx_done && in_payload.from_addr != my_conf.badge_id) {
f_rfm_rx_done = 0;
in_payload.handle[NAME_MAX_LEN] = 0; // Make sure it's definitely null-terminated.
// BASE SERVICES:
// Base services are a sort of overlay - The incoming payload is going to tell us
// either that it's NOT A BASE (in which case we skip), OR it's going to give us
// a valid base ID and be either a badge (from_addr < BADGES_IN_SYSTEM) or be an
// actual base station (from_addr == DEDICATED_BASE_ID).
if (in_payload.base_id != NOT_A_BASE && (in_payload.from_addr < BADGES_IN_SYSTEM || in_payload.from_addr == DEDICATED_BASE_ID)) {
if (my_conf.adult && in_payload.base_id == 1) // puppy
{
oled_set_overhead_image(&puppy, 254);
achievement_get(ACH_HANDLER, 1);
}
else if (in_payload.base_id == BASE_SUITE) { // suite
at_base = RECEIVE_WINDOW;
at_suite_base = 1;
} else if (in_payload.base_id < BASES_IN_SYSTEM) { // other bases
at_base = RECEIVE_WINDOW;
set_base_seen(in_payload.base_id - 2);
}
}
// BEACON SERVICES:
// "Beacon services" refers to BADGE TO BADGE COMMUNICATION.
if (!disable_beacon_service && in_payload.from_addr < BADGES_IN_SYSTEM) {
// It's a standard beacon:
// Increment the badge count if needed:
if (in_payload.beacon) {
// It's a beacon (one per cycle).
// Update the TTL of the neighbor_badges record for the
// source badge.
neighbor_badges[in_payload.from_addr] = RECEIVE_WINDOW * in_payload.beacon;
// Mark it seen:
set_badge_seen(in_payload.from_addr);
tick_badge_seen(in_payload.from_addr, in_payload.handle);
}
// Play schedule. We only care about this if we're in IDLE mode,
// and if we're not busy. (We use the enabledness of the softkey as
// a surrogate for that ill-defined "business").
if (in_payload.play_id && op_mode == OP_MODE_IDLE && !idle_mode_softkey_dis && (in_payload.play_id & 0b01111111) <= play_anim_count && !play_mode) {
if (my_conf.mood > MOOD_THRESH_SAD) {
play_id = in_payload.play_id & 0b01111111;
// Just stand there for the length of time the causing badge
// is doing its cause animation.
if (my_conf.adult && play_id < play_anim_count) {
play_mode = PLAY_MODE_RECV;
oled_play_animation(&standing, play_cause[play_id]->len);
} else if (!my_conf.adult && play_id == play_anim_count) {
play_mode = PLAY_MODE_RECV;
oled_play_animation(&infant_standing, infant_play.len*3);
}
if (play_mode) {
idle_mode_softkey_dis = 1;
oled_draw_pane_and_flush(idle_mode_softkey_sel);
my_conf.play_margin--;
my_conf_write_crc();
if (my_conf.play_margin < -20) {
achievement_get(ACH_PUNKD, 1);
my_conf.play_margin = 0;
}
if (TLC_IS_A_GO) {
tlc_start_anim(&flag_pink, 0, 5*GLOBAL_TLC_SPEED_SCALE, 1, 0); // POW PINK!
}
}
}
}
// It's a flag schedule:
if (in_payload.flag_id && (in_payload.flag_id & 0b01111111) < FLAG_COUNT) {
// Wave a flag.
if (my_conf.adult && !my_conf.flag_unlocks) {
my_conf.flag_unlocks = 1;
softkey_en |= SK_BIT_FLAG | SK_BIT_SETFLAG;
mood_adjust_and_write_crc(MOOD_GOT_FLAG);
}
if (!flag_in_cooldown) {
mood_adjust_and_write_crc(MOOD_FLAG);
flag_id = in_payload.flag_id & 0b01111111;
tlc_start_anim(flags[in_payload.flag_id & 0b01111111], 0, 3*GLOBAL_TLC_SPEED_SCALE, 0, 0);
s_flag_wave = 1;
s_flag_send = 2;
} // Otherwise, ignore it.
}
// Resolve inbound or completed friendship requests:
if (in_payload.friendship && my_conf.adult) {
if (befriend_mode) {
// We're currently somewhere in the befriend process,
// so the protocol function will handle managing
// befriend_candidate for us.
if (in_payload.from_addr == befriend_candidate) {
strcpy(befriend_candidate_handle, in_payload.handle);
}
befriend_proto_step(1, in_payload.friendship, in_payload.from_addr);
} else if (in_payload.friendship == BF_S_BEACON) {
// Only track beacons in this way.
befriend_candidate = in_payload.from_addr;
befriend_candidate_age = BEFRIEND_BCN_AGE_LOOPS;
}
}
}
} else if (f_rfm_rx_done) {
// Ignore messages from our own ID, because what even is that anyway?
f_rfm_rx_done = 0;
}
// Poll buttons and check whether we need to resend a befriend message:
if (f_time_loop) {
// poll_buttons();
WDT_A_resetTimer(WDT_A_BASE); // pat pat pat
if (my_conf.adult && befriend_mode && !befriend_mode_loops_to_tick &&
rfm_state == RFM_IDLE) {
befriend_proto_step(0, 0, BADGES_IN_SYSTEM);
befriend_mode_loops_to_tick = BEFRIEND_LOOPS_TO_RESEND;
} else if (my_conf.adult && befriend_mode && befriend_mode_loops_to_tick) {
befriend_mode_loops_to_tick--;
}
if (befriend_candidate_age) {
befriend_candidate_age--;
}
}
if (f_new_second) {
if (f_radio_fault) {
f_radio_fault = 0;
LED_PULSE(flag_red);
init_radio();
}
if (befriend_mode == 1 || befriend_mode == 2) {
if (befriend_mode_secs) {
befriend_mode_secs--;
} else {
set_befriend_failed();
}
}
if (flag_in_cooldown) {
flag_in_cooldown--;
}
minute_secs--;
if (!minute_secs) {
minute_secs = 60;
s_new_minute = 1;
}
window_seconds--;
if (!window_seconds) {
window_seconds = RECEIVE_WINDOW_LENGTH_SECONDS;
if (at_base) {
at_base--;
if (!at_base) {
at_suite_base = 0;
}
}
if (skip_window != window_position) {
s_need_rf_beacon = 1;
}
neighbor_count = 0;
for (uint8_t i=0; i<BADGES_IN_SYSTEM; i++) {
if (neighbor_badges[i]) {
neighbor_count++;
neighbor_badges[i]--;
}
}
window_position = (window_position + 1) % RECEIVE_WINDOW;
if (!window_position) {
skip_window = rand() % RECEIVE_WINDOW;
// If there's no one around, reset the radio just in case.
if (!neighbor_count && rfm_state == RFM_IDLE) {
mode_sync(RFM_MODE_SL); // Going to sleep... mode...
write_single_register(0x3b, RFM_AUTOMODE_OFF);
init_radio();
}
}
}
}
if (s_new_minute) {
s_new_minute = 0;
if (my_conf.flag_cooldown) {