diff options
-rw-r--r-- | include/drv/ws2812b.h | 10 | ||||
-rw-r--r-- | main/main.c | 39 |
2 files changed, 33 insertions, 16 deletions
diff --git a/include/drv/ws2812b.h b/include/drv/ws2812b.h index b14b31a..39f2794 100644 --- a/include/drv/ws2812b.h +++ b/include/drv/ws2812b.h @@ -40,4 +40,14 @@ static inline void ws2812b_buffer_set_rgb( buf->rgb[idx].b = b; } +static inline void ws2812b_buffer_set_color( + ws2812b_buffer_t* buf, size_t idx, ws2812b_rgb_t* color) +{ + if (idx >= buf->n_rgb) { + return; + } + + buf->rgb[idx] = *color; +} + #endif /* INCLUDE_DRV_WS2812B_H_ */ diff --git a/main/main.c b/main/main.c index 5723c4c..ff78f76 100644 --- a/main/main.c +++ b/main/main.c @@ -103,6 +103,9 @@ void app_main(void) printf("Listening ...\n"); listen(s, 1); + ws2812b_t* drv = ws2812b_init(spi); + ws2812b_buffer_t* buffer = ws2812b_new_buffer(5); + while (true) { printf("Waiting for connection... \n"); char buf[128]; @@ -117,22 +120,26 @@ void app_main(void) while ((len = read(sock, buf, sizeof(buf) - 1)) > 0) { buf[len] = 0; printf("Got %s\n", buf); - } - } - ws2812b_t* drv = ws2812b_init(spi); - ws2812b_buffer_t* buffer = ws2812b_new_buffer(5); - - ws2812b_buffer_set_rgb(buffer, 0, 255, 0, 0); - ws2812b_buffer_set_rgb(buffer, 1, 0, 255, 0); - ws2812b_buffer_set_rgb(buffer, 2, 0, 0, 255); - ws2812b_buffer_set_rgb(buffer, 3, 255, 255, 0); - ws2812b_buffer_set_rgb(buffer, 4, 255, 0, 255); - - while (1) { - ws2812b_write_sync(drv, buffer); - // TODO(rahm) push this into the sync write, or otherwise make this better - // to deal with. - vTaskDelay(10 / portTICK_PERIOD_MS); + ws2812b_rgb_t color = {0}; + if (!strcmp(buf, "green\n")) { + color.g = 255; + } else if (!strcmp(buf, "red\n")) { + color.r = 255; + } else if (!strcmp(buf, "blue\n")) { + color.b = 255; + } else { + color.r = 255; + color.g = 255; + color.b = 255; + } + + for (int i = 0; i < 5; ++ i) { + ws2812b_buffer_set_color(buffer, i, &color); + } + + ws2812b_write_sync(drv, buffer); + vTaskDelay(10 / portTICK_PERIOD_MS); + } } } |