aboutsummaryrefslogtreecommitdiff
path: root/main/main.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2021-11-29 18:03:00 -0700
committerJosh Rahm <joshuarahm@gmail.com>2021-11-29 18:03:00 -0700
commit80d6050ab3fe42f8850bf166c33eb3c9150a252b (patch)
treebd4f501468626bbc14867544ed3a66bf725b60e8 /main/main.c
parentc6a0cf2ff3403e9726ea6429af50bedcfd4f9a01 (diff)
downloadesp32-ws2812b-80d6050ab3fe42f8850bf166c33eb3c9150a252b.tar.gz
esp32-ws2812b-80d6050ab3fe42f8850bf166c33eb3c9150a252b.tar.bz2
esp32-ws2812b-80d6050ab3fe42f8850bf166c33eb3c9150a252b.zip
Add limited ability to control LEDs remotely.
Diffstat (limited to 'main/main.c')
-rw-r--r--main/main.c39
1 files changed, 23 insertions, 16 deletions
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);
+ }
}
}