1111
1212void setupEntry () {
1313
14- asm ( " nop " );
14+ // Blank
1515
1616};
1717
18- uint8_t step;
19- millis_t nextstep;
18+ #define COOKIE_BYTE 0b10110111 // Non repeating bit pattern to detect errors.
2019
21- millis_t nextsend;
20+ uint8_t cookie_byte_buffer = COOKIE_BYTE ; // Must be allocated in RAM so we can pass a pointer to it to senddata()
2221
23- uint8_t message[] = { ' J' , ' o' , ' s' , ' h' };
24-
25- uint8_t errorFlag[ IR_FACE_COUNT ];
22+ #define TX_BLIND_SEND_TIME_MS 250 // How often to do a blind send when no RX has happened recently to trigger ping pong
2623
27- void loopEntry ( loopstate_in_t const *loopstate_in , loopstate_out_t *loopstate_out) {
24+ #define MISSED_RX_SHOW_TIME_MS 500 // How long to show blue when a RX timeout happens
25+ #define RX_TIMEOUT_RX 200 // If we do not see a message in this long, then show a timeout
2826
29- millis_t now = loopstate_in-> millis ;
27+ // Note these all init to 0's, which is what we want.
3028
31- if ( loopstate_in->buttonstate .bitflags & BUTTON_BITFLAG_PRESSED ) {
29+ struct face_t {
30+
31+ millis_t next_tx_ms; // Next time to transmit
32+ millis_t last_rx_ms; // Last time a good packet seen
33+
34+ uint8_t errorFlag;
35+ };
3236
33- loopstate_out-> colors [step] = pixelColor_t ( 0 , 0 , 0 , 1 ) ;
37+ face_t faces[ IR_FACE_COUNT ] ;
3438
35- step++;
39+ void loopEntry ( loopstate_in_t const *loopstate_in , loopstate_out_t *loopstate_out) {
3640
37- if (step>= PIXEL_FACE_COUNT ) {
38- step= 0 ;
39- }
41+ millis_t now_ms = loopstate_in-> millis ;
42+
43+ // Clear out any errors on button press
4044
41- loopstate_out-> colors [step] = pixelColor_t ( 23 , 10 , 0 , 1 );
45+ if ( loopstate_in-> buttonstate . bitflags & BUTTON_BITFLAG_PRESSED ) {
4246
43- errorFlag[step]=0 ;
44-
45- nextstep = loopstate_in->millis + 200 ;
46- }
47-
48-
47+ for ( uint8_t f=0 ; f< IR_FACE_COUNT ; f++ ) {
48+
49+ if ( faces[f].errorFlag ) {
50+
51+ faces[f].errorFlag =0 ;
52+
53+ }
54+
55+ }
56+
57+ }
58+
59+
4960 for ( uint8_t f=0 ; f< IR_FACE_COUNT ; f++ ){
61+
62+ // Look for received packet....
5063
5164 if ( loopstate_in->ir_data_buffers [f].ready_flag ) {
5265
53- if ( (loopstate_in->ir_data_buffers [f].len == 4 )) {
54-
55- const uint8_t *d = loopstate_in->ir_data_buffers [f].data ;
56-
57- if ( d[0 ] ==' J' && d[1 ] ==' o' && d[2 ] == ' s' && d[3 ] == ' h' ) {
58-
59- // Good packet!
60- loopstate_out->colors [f] = pixelColor_t ( 0 , 20 , 0 , 1 );
61-
62- } else {
66+ if ( (loopstate_in->ir_data_buffers [f].len == 1 ) && loopstate_in->ir_data_buffers [f].data [0 ] == COOKIE_BYTE ) {
6367
64- errorFlag[f] = 2 ;
65-
66- }
67-
68- } else {
69- // Bad len
68+ // Got a good packet!
69+
70+ faces[f].last_rx_ms = now_ms;
71+
72+ faces[f].next_tx_ms = now_ms; // pong
73+
74+ } else {
75+
76+ // Either len or cookie was wrong, so data transmission error
77+
78+ faces[f].errorFlag = 1 ;
7079
71- errorFlag[f] = 1 ;
72-
73-
7480 }
75-
76- }
81+
82+ }
7783
78- if (errorFlag[f]==1 ) {
84+ // Update shown color
85+
86+ if (faces[f].errorFlag ) {
7987
88+ // Red on error
8089 loopstate_out->colors [f] = pixelColor_t ( 20 , 0 , 0 , 1 );
90+
91+ } else {
92+
93+ millis_t elapsed_ms = now_ms - faces[f].last_rx_ms ; // Elapsed time since last RX
94+
95+ if (elapsed_ms < 200 ) {
96+
97+ // Less than 200ms since last good RX, so show green
98+ loopstate_out->colors [f] = pixelColor_t ( 0 , 20 , 0 , 1 );
99+
100+ } else if ( elapsed_ms < 500 ) {
81101
102+ // More then 200ms since last good RX, so show red for 300ms
103+ loopstate_out->colors [f] = pixelColor_t ( 0 , 0 , 20 , 1 );
82104
83- } else if (errorFlag[f]==2 ) {
84-
85-
86- loopstate_out->colors [f] = pixelColor_t ( 0 , 0 , 20 , 1 );
105+
106+ } else {
107+
108+ // Been long enough that we assume no one is there
109+ loopstate_out->colors [f] = pixelColor_t ( 0 , 0 , 0 , 1 );
110+
111+ }
87112
88- }
89-
90- }
113+ }
114+
115+
116+ if ( faces[f].next_tx_ms < now_ms ) {
117+
118+ // Time to send on this face
119+
120+ ir_send_userdata ( f , &cookie_byte_buffer , 1 );
121+
122+ // Schedule a blind send in case we don't see a ping soon
123+ faces[f].next_tx_ms = now_ms + TX_BLIND_SEND_TIME_MS ;
124+
125+ } // for(f)
126+
127+ }
91128
92-
93-
94- if ( nextsend < now ) {
95-
96- ir_send_userdata ( 4 , message , 4 );
97-
98- nextsend = now + 200 ;
99- }
100-
101-
102129}
0 commit comments