Resolve a race condition around the radio interrupt

This commit is contained in:
Savanni D'Gerinel 2022-08-09 22:43:45 -04:00
parent 2acff8d3f5
commit 966ef20eca
1 changed files with 53 additions and 54 deletions

View File

@ -30,7 +30,6 @@ You should have received a copy of the GNU General Public License along with thi
typedef struct {
bool frame_timeout;
bool message_arrived;
} status_flags_t;
status_flags_t status_flags;
@ -40,7 +39,6 @@ ISR(TIMER1_COMPA_vect) {
}
ISR(INT6_vect) {
status_flags.message_arrived = true;
}
void setup_fps_timer(void) {
@ -62,11 +60,6 @@ int main(void) {
EICRA |= 1 << ISC21 | 1 << ISC20;
EICRB |= 1 << ISC61 | 1 << ISC60;
/*
for (int i = 0; i < 4; i++) {
status_flags.button_press[i] = false;
}
*/
status_flags.frame_timeout = false;
// Disable unneeded modules
@ -88,32 +81,39 @@ int main(void) {
rfm_init(&radio, (uint8_t [4]){ 0xde, 0xca, 0xfb, 0xad }, 4, &error);
rfm_listen(&radio);
/*
display_t display = {
.reg = {
.output = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 7 },
.shift_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 6 },
.latch_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 5 },
}
};
display_init(&display);
display_write_message(&display, "ready");
_delay_ms(1000);
*/
/*
for (int i = 0; i < 4; i++) {
dio_set_direction(&buttons[i], LINE_IN);
}
*/
sk9822_t lights = {
.clock_pin = { .ddr = &DDRC, .port = &PORTC, .pin = &PINC, .addr = 7 },
.data_pin = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 6 }
};
rgb_t colors[LIGHT_COUNT];
rgb_t off[LIGHT_COUNT] = {
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
};
/*
rgb_t error_indicator[LIGHT_COUNT] = {
{ .brightness = 1, .r = 255, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
};
*/
sk9822_send(&lights, off, LIGHT_COUNT);
_delay_ms(500);
lantern_t lantern = lantern_new(lights);
lantern_set_mode(&lantern, normal);
@ -129,34 +129,33 @@ int main(void) {
status_flags.frame_timeout = false;
lantern_step(&lantern, colors);
sk9822_send(&lights, colors, LIGHT_COUNT);
} else if (status_flags.message_arrived) {
status_flags.message_arrived = false;
char msg[60];
uint8_t length;
interrupt_flags_t flags = rfm_interrupts(&radio);
while (flags.fifo_not_empty) {
rfm_receive(&radio, (uint8_t *)msg, &length);
if (!strncmp(msg, "normal", strlen("normal"))) {
lantern_set_mode(&lantern, normal);
} else if (!strncmp(msg, "spooky", strlen("spooky"))) {
lantern_set_mode(&lantern, spooky);
} else if (!strncmp(msg, "eerie", strlen("eerie"))) {
lantern_set_mode(&lantern, eerie);
}
flags = rfm_interrupts(&radio);
}
}
/*
if (status_flags.button_press[0]) {
status_flags.button_press[0] = false;
lantern_set_mode(&lantern, normal);
} else if (status_flags.button_press[1]) {
status_flags.button_press[1] = false;
lantern_set_mode(&lantern, spooky);
} else if (status_flags.button_press[2]) {
status_flags.button_press[2] = false;
lantern_set_mode(&lantern, eerie);
*/
interrupt_flags_t flags = rfm_interrupts(&radio);
while (flags.fifo_not_empty) {
/*
sk9822_send(&lights, off, LIGHT_COUNT);
_delay_ms(100);
*/
char msg[60] = "";
uint8_t length;
rfm_receive(&radio, (uint8_t *)msg, &length);
if (length == strlen("normal") + 1 && !strncmp(msg, "normal", length)) {
lantern_set_mode(&lantern, normal);
} else if (length == strlen("spooky") + 1 && !strncmp(msg, "spooky", length)) {
lantern_set_mode(&lantern, spooky);
} else if (length == strlen("eerie") + 1 && !strncmp(msg, "eerie", length)) {
lantern_set_mode(&lantern, eerie);
/*
} else {
sk9822_send(&lights, error_indicator, LIGHT_COUNT);
_delay_ms(500);
*/
}
flags = rfm_interrupts(&radio);
}
sei();
sleep_mode();