Skip to content

Commit 703cfe7

Browse files
author
Josh Levine
committed
New IR tester code in bareblinkOS/main.cpp
1 parent 1d41fb9 commit 703cfe7

3 files changed

Lines changed: 98 additions & 70 deletions

File tree

AS7/blink/bareblinkOS/bareblinkOS/blinkos.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ typedef uint32_t millis_t;
2323
// loopstate is passed in and out of the user program on each pass
2424
// through the main event loop
2525

26-
27-
2826
// I know this is ugly, but keeping them in a single byte lets us pass them by value
2927
// and also lets us OR them together. Efficiency in the updater trumps since it
3028
// runs every millisecond.
@@ -57,11 +55,6 @@ struct ir_data_buffer_t {
5755

5856
};
5957

60-
// Sends immediately. Blocks until send is complete.
61-
// Higher level should provide some collision control.
62-
63-
void ir_send_userdata( uint8_t face, const uint8_t *data , uint8_t len );
64-
6558

6659
struct loopstate_in_t {
6760

@@ -87,10 +80,19 @@ struct loopstate_out_t {
8780

8881
};
8982

83+
// Sends immediately. Blocks until send is complete.
84+
// Higher level should provide some collision control.
85+
// No error checking except the intrinsic robustnesses of the framing and the header byte.
86+
// Higher level can decide if it is worth adding more
87+
88+
void ir_send_userdata( uint8_t face, const uint8_t *data , uint8_t len );
89+
9090
// The state record we are sending to userland
9191
extern loopstate_in_t loopstate_in;
9292

9393
// These are provided by blinklib
94+
// Will soon be jump vectors
95+
96+
void setupEntry();
9497

95-
//void setupEntry();
9698
void loopEntry( loopstate_in_t const *loopstate_in , loopstate_out_t *loopstate_out);

AS7/blink/bareblinkOS/bareblinkOS/main.cpp

Lines changed: 88 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,92 +11,119 @@
1111

1212
void 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
}

libraries/blinkos/src/blinkos.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ void run(void) {
363363

364364
if (sleepTimer.isExpired()) {
365365
sleep();
366-
367366
}
368367

369368

0 commit comments

Comments
 (0)