-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1173 lines (1054 loc) · 30.4 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
/*
* Queercon XI Badge main code
*
* main.c
*
* (c) 2014 George Louthan
* 3-clause BSD license; see license.md.
*/
#include "qcxi.h"
#include <string.h>
#include <stdlib.h>
#include "radio.h"
#include "fonts.h"
#include "clocks.h"
#include "leds.h"
#include "ir.h"
#include "ws2812.h"
#include "anim.h"
#include "main.h"
#pragma DATA_SECTION (my_conf, ".infoA");
#pragma DATA_SECTION (backup_conf, ".infoB");
const qcxiconf my_conf;
#if BADGE_TARGET
const qcxiconf backup_conf
//= {
// {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff},
// {0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff},
// {0xffff, 0xffff, 0xffff, 0xffff},
// 0xff, 0xff,
// 0x55,
// "George",
// "0xDECAFBAD!",
// 0xffff
//}
;
#else
const qcxiconf backup_conf = {
{0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff},
{0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff},
{0xffff, 0xffff, 0xffff, 0xffff},
0xff, 0xff,
0xff,
"",
"",
0xffff
};
#endif
// Interrupt flags to signal the main thread:
volatile uint8_t f_rfm_rx_done = 0;
volatile uint8_t f_rfm_tx_done = 0;
volatile uint8_t f_ir_tx_done = 0;
volatile uint8_t f_ir_rx_ready = 0;
volatile uint8_t f_new_second = 0;
volatile uint8_t f_alarm = 0;
uint8_t f_paired = 0;
uint8_t f_unpaired = 0;
uint8_t f_paired_new_person = 0;
uint8_t f_paired_new_trick = 0;
uint8_t f_animation_done = 0;
uint8_t f_ir_itp_step = 0;
uint8_t f_ir_pair_abort = 0;
uint8_t f_paired_trick = 0;
// Global state:
uint8_t clock_is_set = 0;
uint8_t my_clock_authority = 0xff;
uint16_t loops_to_rf_beacon = 10 * TIME_LOOP_HZ;
char pairing_message[20] = "";
char event_message[40] = "";
uint8_t my_trick = 0;
uint16_t known_tricks = 0;
uint8_t known_trick_count = 0;
uint8_t known_props = 0;
qcxipayload in_payload;
qcxipayload out_payload = {
RFM_BROADCAST, 0xff, 0xff, 0xff, 0xff, 0};
uint16_t rainbow_lights = 0;
uint8_t light_blink = 0;
// My general app-level status:
uint8_t badge_status = 0;
uint8_t am_idle = 1;
#define PAIR_INIT 0
#define PAIR_ONSCREEN 1
#define PAIR_WAVE 2
#define PAIR_GREETING 3
#define PAIR_MESSAGE 4
#define PAIR_IDLE 5
uint8_t pair_state = 0;
// Gaydar:
uint8_t window_position = 0; // Currently only used for restarting radio & skipping windows.
uint8_t neighbor_count = 0;
uint8_t window_seconds = RECEIVE_WINDOW_LENGTH_SECONDS;
uint8_t trick_seconds = TRICK_INTERVAL_SECONDS;
uint8_t target_gaydar_index = 0;
uint8_t gaydar_index = 0;
uint8_t neighbor_badges[BADGES_IN_SYSTEM] = {0};
#if !BADGE_TARGET
volatile uint8_t f_ser_rx = 0;
#endif
char time[6] = "00:00";
// Main thread signals:
uint16_t s_prop_cycles = 0,
s_prop_animation_length = 0;
uint8_t s_prop_id = 0,
s_prop_authority = 0,
s_propped = 0,
s_need_rf_beacon = 0,
s_rf_retransmit = 0,
s_new_pair = 0,
s_new_trick = 0,
s_new_score = 0,
s_new_prop = 0,
s_trick = 0,
s_prop = 0,
s_update_rainbow = 0;
uint8_t itps_pattern = 0;
// Scores:
uint8_t my_score = 0;
uint8_t shown_score = 0;
uint8_t s_count_score = 0;
#define COUNT_SCORE_CYCLES 4
uint8_t s_count_score_cycles = 0;
void set_known_tricks() {
known_tricks = 1 << my_trick;
known_trick_count = 1;
for (uint8_t trick_id = 0; trick_id < TRICK_COUNT; trick_id++) {
if (trick_id == my_trick) continue;
for (uint8_t badge_id = trick_id; badge_id < BADGES_IN_SYSTEM; badge_id+=TRICK_COUNT) {
if (paired_badge(badge_id) && !(known_tricks & 1<<(trick_id))) {
known_tricks |= 1 << trick_id;
known_trick_count++;
switch(known_trick_count) {
case 4:
set_score(37, 1); //4th trick
break;
case 10:
set_score(38, 2); //10th trick
break;
case TRICK_COUNT:
set_score(40,3); // all tricks
}
break;
}
}
}
}
void set_my_score_from_config() {
my_score = 0;
// Count the bits set in score:
uint16_t v = 0;
for (uint8_t i=0; i<4; i++) {
v = ~my_conf.scores[i];
for (;v;my_score++) {
v &= v - 1;
}
}
s_count_score_cycles = COUNT_SCORE_CYCLES;
shown_score = 0;
s_count_score = 1;
known_props = 0;
known_props += (my_score>=3);
known_props += (my_score>=6);
known_props += (my_score>=11);
known_props += (my_score>=17);
known_props += (my_score>=24);
known_props += (my_score>=31);
}
void set_score(uint16_t id, uint16_t value) {
uint16_t score_frame = id / 16;
uint16_t score_bits = 0;
while (value--) {
score_bits |= 1 << (id % 16) + value;
}
if (!(~(my_conf.scores[score_frame]) & score_bits)) {
// haven't seen it, so we need to set its 1 to a 0.
uint16_t new_config_word = my_conf.scores[score_frame] & ~(score_bits);
FLASH_unlockInfoA();
FLASH_write16(&new_config_word, &(my_conf.scores[score_frame]), 1);
FLASH_lockInfoA();
s_new_score = 1;
set_my_score_from_config();
}
}
void init_power() {
#if BADGE_TARGET
// Set Vcore to 1.8 V - NB: allows MCLK up to 8 MHz only
// PMM_setVCore(PMM_CORE_LEVEL_0);
// PMMCTL0 &= 0b11111100;
// PMMCTL0 |= 0b11;
#else
PMM_setVCore(PMM_CORE_LEVEL_3);
// PMMCTL0 |= 0b11;
#endif
}
void init_gpio() {
// Start out by turning off all the pins.
P1DIR = 0xFF;
P1OUT = LED_BLANK;
P2DIR = 0xFF;
P2OUT = 0x00;
P3DIR = 0xFF;
P3OUT = 0x00;
P4DIR = 0xFF;
P4OUT = 0x00;
P5DIR = 0xFF;
P5OUT = 0x00;
P6DIR = 0xFF;
P6OUT = 0x00;
}
void set_badge_seen(uint8_t id) {
uint8_t badge_frame = id / 16;
uint8_t badge_bit = 1 << (id % 16);
if (!(~(my_conf.met_ids[badge_frame]) & badge_bit)) {
// haven't seen it, so we need to set its 1 to a 0.
uint16_t new_config_word = my_conf.met_ids[badge_frame] & ~(badge_bit);
FLASH_unlockInfoA();
FLASH_write16(&new_config_word, &(my_conf.met_ids[badge_frame]), 1);
FLASH_lockInfoA();
} // otherwise, nothing to do.
if (id < 12 && currentTime.Hours == 5) {
set_score(34, 1); // sleep your way to the top
}
}
uint8_t event_attended(uint8_t id) {
return ~my_conf.events_attended & (1 << id);
}
void set_event_attended(uint8_t id) {
// No if event_attended() is needed here because this is only called once,
// and that call is wrapped in an if.
if (light_blink == 128 + id) {
light_blink = 0;
s_update_rainbow = 1;
}
uint8_t new_event_attended = my_conf.events_attended & ~(1 << id);
FLASH_unlockInfoA();
FLASH_write8(&new_event_attended, &my_conf.events_attended, 1);
FLASH_lockInfoA();
}
void set_event_occurred(uint8_t id) {
if (~my_conf.events_occurred & (1 << id))
return;
uint8_t new_event_occurred = my_conf.events_occurred & ~(1 << id);
FLASH_unlockInfoA();
FLASH_write8(&new_event_occurred, &my_conf.events_occurred, 1);
FLASH_lockInfoA();
}
uint8_t paired_badge(uint8_t id) {
uint16_t badge_frame = id / 16;
uint16_t badge_bit = 1 << (id % 16);
return (~(my_conf.paired_ids[badge_frame]) & badge_bit)? 1: 0;
}
void set_badge_paired(uint8_t id) {
uint16_t badge_frame = id / 16;
uint16_t badge_bit = 1 << (id % 16);
// See if this is a new pair.
if (id != 0xff && !(~(my_conf.paired_ids[badge_frame]) & badge_bit)) {
if (id < 12) {
set_score(id, 1); // seen each uber
// if there are any 1s left, we have NOT seen all ubers:
if (!(my_conf.scores[0] & 0b111111111111)) {
set_score(12, 3); // seen all ubers
}
}
set_score(16, 1); // first pair
f_paired_new_person = 1;
// haven't seen it, so we need to set its 1 to a 0.
uint16_t new_config_word = my_conf.paired_ids[badge_frame] & ~(badge_bit);
FLASH_unlockInfoA();
FLASH_write16(&new_config_word, &(my_conf.paired_ids[badge_frame]), 1);
FLASH_lockInfoA();
set_known_tricks();
} // otherwise, nothing to do.
if (id != 0xff) {
f_paired = 1;
}
ir_proto_seqnum = 0;
ir_pair_setstate(IR_PROTO_PAIRED);
}
void set_gaydar_target() {
if (neighbor_count > 3)
target_gaydar_index = 4;
else
target_gaydar_index = neighbor_count;
}
/*
* So here's the flow of this thing.
*
* * STARTUP (POST, message, etc)
* ------ block until finished ----
*
* Here are the things that can be flagged:
*
* Time based:
* * Event alert raised (interrupt flag)
* * DONE It's been long enough that we can do a trick (set flag from time loop)
* ** DONE (maybe the trick is a prop)
* * DONE Time to beacon the radio (set flag from time loop)
* * DONE Time to beacon the IR (set flag from time loop)
*
* From the radio:
* * DONE Receive a beacon (at some point, we need to decide if it means we:)
* ** adjust neighbor count
* ** DONE Are near a base station (arrive event)
* ** DONE should schedule a prop
* ** DONE set our clock
*
* From the IR
* * DONE Docking with base station - don't REALLY dock...
* * DONE Pairing
* ** DONE possibly new person
* *** DONE possibly new person with a new trick
*
* Looping - here are the priorities:
*
* * Set clock (pre-empts)
* * Arrived at event (animation, don't wait for previous to finish)
* * Event alert (animation, wait for idle)
* * Pair begins (animation and behavior pre-empts, don't wait to finish)
* * New pairing person (wait for idle)
* * New trick learned (wait for idle)
* * Score earned (wait for idle)
* * Prop earned (wait for idle)
* * Pair expires (wait for idle)
* * Get propped (from radio beacon)
* * Do a trick or prop (idle only)
*
* * Adjust neighbor count (from radio beacon)
* *
*
*/
uint8_t skip_window = 1;
int main( void )
{
init_watchdog();
init_power();
init_gpio();
init_clocks();
check_config();
led_init();
init_rtc();
init_ir();
init_alarms();
#if !BADGE_TARGET
ws2812_init();
ser_init();
#endif
__bis_SR_register(GIE);
init_radio(); // requires interrupts enabled.
srand(my_conf.badge_id);
// Power-on self test:
uint8_t post_result = post();
if (post_result != 0) {
#if BADGE_TARGET
// Display error code:
char hex[4] = {0, 0, 0, 0};
hex[0] = (post_result/16 < 10)? '0' + post_result/16 : 'A' - 10 + post_result/16;
hex[1] = (post_result%16 < 10)? '0' + post_result%16 : 'A' - 10 + post_result%16;
led_print_scroll(hex, 4);
#else
fillFrameBufferSingleColor(&leds[6], NUMBEROFLEDS, ws_frameBuffer, ENCODING);
ws_set_colors_async(NUMBEROFLEDS);
delay(1000);
#endif
} else if (my_conf.handle[0]) {
led_print_scroll(my_conf.handle, 1);
} else {
led_print_scroll("qcxi", 0);
}
#if BADGE_TARGET
led_set_rainbow(0);
led_clear();
led_enable(LED_PERIOD/4);
#else
uint8_t color = 0;
#endif
led_anim_init();
#if BADGE_TARGET
// Startup sequence:
uint8_t startup_seq_index = 0;
while (startup_seq_index<3) {
// Time to do something because of time?
if (f_time_loop) {
f_time_loop = 0;
led_timestep();
}
// Is an animation finished?
if (f_animation_done) {
f_animation_done = 0;
startup_seq_index++;
switch(startup_seq_index) {
// case 1:
// led_print_scroll("Hello NAMENAME. I am your badge.", 1, 1, 0);
// break;
// case 2:
// led_print_scroll("There are many like me, but I am yours.", 1, 1, 0);
// break;
// case 3:
// led_print_scroll("Please leave my batteries in!", 1, 1, 0);
// break;
case 1:
left_sprite_animate((spriteframe *) anim_sprite_walkin, 4);
break;
case 2:
left_sprite_animate((spriteframe *) anim_sprite_wave, 4);
break;
}
}
}
delay(750);
#endif
// Main sequence:
f_rfm_rx_done = 0;
while (1) {
#if !BADGE_TARGET
// New serial message?
if (f_ser_rx) {
ser_print((uint8_t *) ser_buffer_rx);
f_ser_rx = 0;
}
#endif
if (f_rfm_tx_done) {
f_rfm_tx_done = 0;
// Back to normal RX automode:
write_single_register(0x3b, RFM_AUTOMODE_RX);
}
/*
* Process link-layer IR messages if needed.
*/
if (f_ir_rx_ready) {
f_ir_rx_ready = 0;
ir_process_rx_ready();
}
if (f_ir_pair_abort) {
f_ir_pair_abort = 0;
#if BADGE_TARGET
if (itps_pattern) {
s_count_score_cycles = (my_score > 31) ? 1 : COUNT_SCORE_CYCLES;
shown_score = 0;
s_count_score = 1;
}
itps_pattern = 0;
#endif
}
/*
* Unlike with the IR pairing mechanism, there is very little state
* in the RF system. So we just need to load up the beacon into a
* buffer, and decide if we:
* * adjust neighbor count
* * are near a base station (arrive event)
* * should schedule a prop
* * set our clock
* * get the puppy
* * got confirmation we should give up the puppy (this will have
* some state)
*
*
* Here we can set:
* s_event_arrival = 0,
* s_on_bus = 0;
* s_new_score = 0,
* s_new_prop = 0,
* s_get_puppy;
* s_lose_puppy;
*
*
*/
if (f_rfm_rx_done) {
f_rfm_rx_done = 0;
if ((!clock_is_set && in_payload.clock_authority != 0xff) || (in_payload.clock_authority < my_clock_authority))
{
memcpy(¤tTime, &in_payload.time, sizeof (Calendar));
clock_is_set = 1;
init_rtc();
my_clock_authority = in_payload.clock_authority;
out_payload.clock_authority = 1;
init_alarms();
set_score(48, 1); // clock setting
}
#if BADGE_TARGET
if (in_payload.base_id == BUS_BASE_ID && in_payload.from_addr == 0xff) {
set_score(33, 1); // bus
}
else if (in_payload.base_id < 7 && in_payload.from_addr == 0xff) {
// Base station, may need to check in.
// base_id = event_id, should be 0..7
if (!event_attended(in_payload.base_id)) {
set_event_attended(in_payload.base_id);
switch(in_payload.base_id) {
case 0:
set_score(19, 3); // thursday party
break;
case 1:
set_score(22, 2); // friday mixer
break;
case 2:
set_score(26, 1); // saturday mixer
break;
case 3:
set_score(32, 1); // sunday mixer
break;
case 4:
set_score(27, 5); // sunday after party
break;
case 5:
set_score(24, 2); // pool party
break;
}
}
if (clock_is_set) {
// Check to see if we're at a base before "NOW"
if (next_event_flag & 0b0111 == in_payload.base_id && next_event_flag & ALARM_NOW_MSG) {
// early!
set_score(35, 2); // early to party
} else if (next_event_flag & 0b0111 != in_payload.base_id) {
// Basically, we're at a base after the light turns off:
set_score(54, 3); // late at party
}
}
}
if (in_payload.prop_from != 0xFF && in_payload.prop_from != my_conf.badge_id) {
// It's a prop notification.
// If we don't currently have a prop scheduled, or if this prop is
// more authoritative than our currently scheduled prop, it's time
// to do a prop.
// If we're paired, and this is from the person
// we're paired with, it's the most authoritative prop possible.
uint8_t prop_authority = in_payload.prop_from;
if (( (!s_propped && !s_prop)
|| prop_authority < s_prop_authority
)
&& in_payload.prop_time_loops_before_start)
{
s_propped = 1;
s_prop = 0;
s_prop_authority = in_payload.prop_from;
s_prop_cycles = in_payload.prop_time_loops_before_start;
s_prop_id = in_payload.prop_id;
out_payload.prop_from = s_prop_authority;
out_payload.prop_time_loops_before_start = s_prop_cycles;
out_payload.prop_id = s_prop_id;
} else if (s_propped) {
// If we're already propped, and our current prop has higher authority,
// retransmit it.
s_rf_retransmit = 1;
}
}
if (in_payload.beacon && in_payload.from_addr < BADGES_IN_SYSTEM) {
// It's a beacon (one per cycle).
// Increment our beacon count in the current position in our
// sliding window.
set_score(15, 1); // first radio
neighbor_badges[in_payload.from_addr] = RECEIVE_WINDOW;
set_badge_seen(in_payload.from_addr);
}
#endif
}
#if BADGE_TARGET
static uint8_t event_id = 0;
/*
* Calendar interrupts:
*
* * Event alert raised (interrupt flag)
*
*/
if (f_alarm) { // needs to be before f_new_second?
event_id = f_alarm & 0b0111;
if (clock_is_set && !event_attended(event_id)) {
if (f_alarm & ALARM_START_LIGHT) {
light_blink = 128 + event_id;
}
if (f_alarm & ALARM_STOP_LIGHT) {
light_blink = 0;
s_update_rainbow = 1;
}
if (f_alarm & ALARM_DISP_MSG) {
strcpy(event_message, alarm_msg);
if (!led_display_text) {
led_print_scroll(event_message, 1);
am_idle = 0;
}
}
}
if (!(f_alarm & ALARM_NO_REINIT)) {
init_alarms();
}
f_alarm = 0;
}
#endif
if (f_new_second) {
f_new_second = 0;
#if BADGE_TARGET
// BIT7 of events_occurred is !defcon_over:
if (BIT7 & my_conf.events_occurred) {
if (light_blink) {
rainbow_lights ^= 1 << (9-(light_blink & 127));
s_update_rainbow = 1;
}
if (!clock_is_set) {
rainbow_lights ^= BIT9;
s_update_rainbow = 1;
}
}
if (!s_count_score && !(rand() % 30)) {
s_count_score_cycles = (my_score > 31) ? 1 : COUNT_SCORE_CYCLES;
shown_score = 0;
s_count_score = 1;
if (BIT7 & ~my_conf.events_occurred && my_conf.handle[0] && !led_display_text) { // defcon is over:
am_idle = 0;
led_print_scroll(my_conf.handle, 0);
}
}
#endif
currentTime = RTC_A_getCalendarTime(RTC_A_BASE);
out_payload.time.Hours = currentTime.Hours;
out_payload.time.Minutes = currentTime.Minutes;
out_payload.time.DayOfMonth = currentTime.DayOfMonth;
out_payload.time.DayOfWeek = currentTime.DayOfWeek;
out_payload.time.Month = currentTime.Month;
out_payload.time.Year = currentTime.Year;
out_payload.time.Seconds = currentTime.Seconds;
if (currentTime.Hours == 7 && currentTime.DayOfMonth >= 8 &&
currentTime.DayOfMonth <= 10) {
// 7am Friday, Saturday, and Sunday.
set_score(49+currentTime.DayOfMonth-8, 1); // OBEY: on at 7
}
#if BADGE_TARGET
if (!trick_seconds && !itps_pattern) {
trick_seconds = TRICK_INTERVAL_SECONDS-1 + (rand()%3);
if (f_paired_new_trick) {
s_trick = f_paired_new_trick;
f_paired_new_trick = 0;
} else if (rand() % 3) {
// wave
s_trick = TRICK_COUNT+1;
} else if (badge_status == BSTAT_GAYDAR && !s_prop && !s_propped && neighbor_count && known_props && rand() % 2) {
// prop
s_prop = 1;
s_prop_authority = my_conf.badge_id;
s_prop_id = rand() % known_props;
// We need to convert from generated-prop-id to actual-prop-id. This is because
// some of them are full-width and some of them are sprites. Yay!
// We'll need to SEND the "effect" version of the id, and we'll keep the "use"
// version for ourselves.
// Happily, for effects, 0 and 1 are FULL; and the rest are SPRITES, in order.
// So we can keep it the same, and we'll just subtract 2 at the other end if
// it's a full one.
out_payload.prop_id = s_prop_id;
// For ourselves, #0 can stay 0 (it's full[0])
// but #4 will need to become 1 (it's full[1]).
// therefore, #1 will be 2 (so we can just do full[#-2])
// #2 will be #4, #4 will be #4, and #5 will be #5
// Then 1 will need to be 2 (prop 1 is sprite).
if (s_prop_id == 4) {
s_prop_id = 1;
} else if (s_prop_id != 5 && s_prop_id > 0) {
s_prop_id+=1;
}
s_prop_animation_length = 0;
if (s_prop_id < 2) {
while(!(prop_uses[s_prop_id][s_prop_animation_length++].lastframe & BIT7));
} else { // the rest are sprites:
while(!(prop_uses_sprites[s_prop_id-2][s_prop_animation_length++].lastframe & BIT7));
}
s_prop_animation_length *= PROP_FRAMESKIP;
s_prop_cycles = ANIM_DELAY + s_prop_animation_length;
out_payload.prop_from = my_conf.badge_id;
out_payload.prop_time_loops_before_start = s_prop_cycles;
s_rf_retransmit = 1;
// Then we're testing whether s_prop_cycles == s_prop_animation_length
} else {
// trick
static uint8_t known_trick_to_do;
known_trick_to_do = rand() % known_trick_count;
// start with the first known trick:
while (!(known_tricks & 1<<s_trick)) {
s_trick++;
}
while (known_trick_to_do) {
s_trick++;
if (known_tricks & 1<<s_trick) {
// if and only if we know the candidate trick, do
// we decrement known_trick_to_do.
known_trick_to_do--;
}
}
// doing s_trick...
s_trick++; // because the s_trick flag is trick_id+1
}
} else { // if (!sprite_animate && !led_text_scrolling && !s_propped) {
trick_seconds--;
}
#endif
window_seconds--;
if (!window_seconds) {
window_seconds = RECEIVE_WINDOW_LENGTH_SECONDS;
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]--;
}
}
if (neighbor_count > 5 && (next_event_flag & ALARM_START_LIGHT)) {
// if next alarm is turning a light on (low-rent stand-in
// for being between events), and you're around >5 people,
// that's 2 points.
set_score(17, 2); // Socialite.
}
window_position = (window_position + 1) % RECEIVE_WINDOW;
if (!window_position) {
skip_window = rand() % RECEIVE_WINDOW;
}
// If we're rolling over the window and have no neighbors,
// try a radio reboot, in case that can gin up some neighbors
// for some reason.
if (!window_position && neighbor_count == 0) {
init_radio();
}
set_gaydar_target();
}
}
if (f_time_loop) {
f_time_loop = 0;
#if BADGE_TARGET
led_timestep();
#else
fillFrameBufferSingleColor(&leds[color], NUMBEROFLEDS, ws_frameBuffer, ENCODING);
ws_set_colors_async(NUMBEROFLEDS);
color++;
if (color==21) f_animation_done = 1;
#endif
if (loops_to_ir_timestep) {
loops_to_ir_timestep--;
} else {
loops_to_ir_timestep = IR_LOOPS;
ir_process_timestep();
}
#if BADGE_TARGET
// number of ITPs to display data for: (ITPS_TO_PAIR-ITPS_TO_SHOW_PAIRING)
// (ITPS_TO_PAIR-ITPS_TO_SHOW_PAIRING) / 5: ITPS per light
if (ir_proto_state == IR_PROTO_ITP && ir_proto_seqnum > ITPS_TO_SHOW_PAIRING) {
itps_pattern = 0;
for (uint8_t i=0; i <= (ir_proto_seqnum - ITPS_TO_SHOW_PAIRING) / ((ITPS_TO_PAIR - ITPS_TO_SHOW_PAIRING) / 5); i++) {
itps_pattern |= (1 << i);
}
s_update_rainbow = 1;
} else if (itps_pattern) {
itps_pattern = 0;
s_update_rainbow = 1;
} else if (s_count_score && !s_count_score_cycles) {
s_count_score_cycles = (my_score > 31) ? 1 : COUNT_SCORE_CYCLES;;
if (shown_score == my_score || shown_score == 31)
s_count_score = 0;
else {
shown_score++;
s_update_rainbow = 1;
}
} else if (s_count_score) {
s_count_score_cycles--;
}
if (s_prop_cycles) {
s_prop_cycles--;
out_payload.prop_time_loops_before_start = s_prop_cycles;
}
#endif
}
// This is background:
if (s_need_rf_beacon && rfm_reg_state == RFM_REG_IDLE && !(read_single_register_sync(0x27) & (BIT1+BIT0))) {
#if BADGE_TARGET
out_payload.beacon = 1;
#else
out_payload.beacon = 0;
out_payload.base_id = 3;
#endif
if (!clock_is_set)
out_payload.clock_authority = 0xff;
radio_send_sync();
s_need_rf_beacon = 0;
} else if (s_rf_retransmit && rfm_reg_state == RFM_REG_IDLE && !(read_single_register_sync(0x27) & (BIT1+BIT0))) {
out_payload.beacon = 0;
if (!clock_is_set)
out_payload.clock_authority = 0xff;
radio_send_sync();
s_rf_retransmit = 0;
}
// Is an animation finished?
if (f_animation_done) {
f_animation_done = 0;
led_display_left |= BIT0;
am_idle = 1;
#if !BADGE_TARGET
color = 0;
#endif
}
#if BADGE_TARGET
// Pre-emptive:
if (am_idle) { // Can do another action now.
switch(badge_status) {
case BSTAT_GAYDAR:
if (f_paired) { // TODO: This might should be pre-emptive?
f_paired = 0;
s_prop = 0;
s_propped = 0;
s_prop_cycles = 0;
out_payload.prop_from = 0xff;
out_payload.prop_time_loops_before_start = 0;
pair_state = PAIR_INIT;
itps_pattern = 0;
s_update_rainbow = 1;
badge_status = BSTAT_PAIR;
am_idle = 0;
gaydar_index = 0;
right_sprite_animate(anim_sprite_walkin, 2, 1, 1, 1);
} else if (s_prop && s_prop_cycles <= s_prop_animation_length) {
// Do a prop use:
am_idle = 0;
s_prop = 0;
out_payload.prop_from = 0xff;
out_payload.prop_time_loops_before_start = 0;
// 0,1 uses are full; the rest are sprites:
if (s_prop_id <= 1) {
led_display_left &= ~BIT0;
full_animate(prop_uses[s_prop_id], PROP_FRAMESKIP);
} else {
left_sprite_animate(prop_uses_sprites[s_prop_id-2], PROP_FRAMESKIP);
}
s_trick = 0;
} else if (s_propped && !s_prop_cycles) {
// Do a prop effect:
am_idle = 0;
s_propped = 0;
out_payload.prop_from = 0xff;
out_payload.prop_time_loops_before_start = 0;
if (s_prop_id <= 1) { // 0, 1 are full:
if (s_prop_id != 1)
led_display_left &= ~BIT0;
full_animate(prop_effects[s_prop_id], PROP_FRAMESKIP);
} else { // the rest are full:
left_sprite_animate(prop_effects_sprites[s_prop_id-2], PROP_FRAMESKIP);
}
s_trick = 0;
} else if (target_gaydar_index > gaydar_index) {
am_idle = 0;
right_sprite_animate(gaydar[gaydar_index], 4, 0, 1, 1);
left_sprite_animate(anim_sprite_wave, 4);
gaydar_index++;
} else if (target_gaydar_index < gaydar_index) {
am_idle = 0;
gaydar_index--;
right_sprite_animate(gaydar[gaydar_index], 4, 0, -1, gaydar_index!=0);
left_sprite_animate(anim_sprite_wave, 4);
}
break;
case BSTAT_PAIR:
if (f_unpaired) {
f_unpaired = 0;
itps_pattern = 0;
s_update_rainbow = 1;
badge_status = BSTAT_GAYDAR;
am_idle = 0;
right_sprite_animate(anim_sprite_walkin, 2, 1, -1, 0);
left_sprite_animate(anim_sprite_wave, 2);
break;
}
switch(pair_state) {
case PAIR_INIT: // Pat just walked on
am_idle = 0;
pair_state = PAIR_WAVE;
right_sprite_animate(anim_sprite_wave, 5, 1, 1, 1);
left_sprite_animate(anim_sprite_wave, 5);
break;
case PAIR_WAVE:
am_idle = 0;
memset(pairing_message, 0, 20);
strcat(pairing_message, "Hi ");
strcat(pairing_message, ir_rx_handle);
led_print_scroll(pairing_message, 1);
pair_state = PAIR_GREETING;
break;
case PAIR_GREETING:
if (!ir_rx_message[0]) {
pair_state = PAIR_IDLE;
} else {
am_idle = 0;
led_print_scroll(ir_rx_message, 1);
pair_state = PAIR_MESSAGE;
break;
}
break;
case PAIR_MESSAGE:
pair_state = PAIR_IDLE;
break;
}
} // end switch(badge_status)
}
// Any status, if we have no more idle-status-change processing to do:
if (am_idle) {
if (s_new_score) {
am_idle = 0;
led_print_scroll("Score!", 0);
s_new_score = 0;
s_update_rainbow = 1;
}
if (s_trick) {
uint16_t trick_len = 0;