diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2021-12-02 17:19:00 -0700 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2021-12-02 17:19:00 -0700 |
commit | 8d379bf60e50960f6c855cd6b9c83956a082f73d (patch) | |
tree | abc799411d565f7e0330ceb6d2db5818ea987d6a /main | |
parent | 62715093e446d952bbc3fa82fdb65deacd576be2 (diff) | |
download | esp32-ws2812b-8d379bf60e50960f6c855cd6b9c83956a082f73d.tar.gz esp32-ws2812b-8d379bf60e50960f6c855cd6b9c83956a082f73d.tar.bz2 esp32-ws2812b-8d379bf60e50960f6c855cd6b9c83956a082f73d.zip |
Break out the state parmaters into their own include file.
This helps by creating one location to add state parameters.
Diffstat (limited to 'main')
-rw-r--r-- | main/main.c | 22 | ||||
-rw-r--r-- | main/tcp_server.c | 116 | ||||
-rw-r--r-- | main/ws2812b_writer.c | 14 |
3 files changed, 69 insertions, 83 deletions
diff --git a/main/main.c b/main/main.c index 691043d..9cd4f0f 100644 --- a/main/main.c +++ b/main/main.c @@ -46,6 +46,26 @@ static ws_params_t ws_params; +/* Ticks the clock at a constant rate independent of the time it takes to write + * to the led strip. */ +portTASK_FUNCTION(time_keeper, params_) +{ + ws_params_t* params = (ws_params_t*) params_; + + TickType_t last_wake; + const TickType_t freq = 1; + + // Initialise the xLastWakeTime variable with the current time. + last_wake = xTaskGetTickCount(); + + for( ;; ) + { + // Wait for the next cycle. + vTaskDelayUntil( &last_wake, freq ); + params->state.time += params->state.timetick; + } +} + void app_main(void) { esp_err_t error; @@ -71,6 +91,7 @@ void app_main(void) printf("miso: %d\n", PIN_NUM_MISO); printf("mosi: %d\n", PIN_NUM_MOSI); printf("sclk: %d\n", PIN_NUM_CLK); + printf("Tick period ms: %d\n", portTICK_PERIOD_MS); error = spi_bus_initialize(HSPI_HOST, &cfg, SPI_DMA_CH_AUTO); printf("Bus Init\n"); @@ -84,6 +105,7 @@ void app_main(void) printf("Configuration complete!!\n"); + xTaskCreate(time_keeper, "time_keeper", 1024, &ws_params, 1, NULL); xTaskCreate(ws2812b_write_task, "ws2812b_writer", 4096, &ws_params, 1, NULL); xTaskCreate(tcp_server, "tcp_server", 4096, &ws_params, 2, NULL); } diff --git a/main/tcp_server.c b/main/tcp_server.c index 7bfaa61..434f014 100644 --- a/main/tcp_server.c +++ b/main/tcp_server.c @@ -78,7 +78,7 @@ static void handle_int_option( sockbuf_write(sockbuf, buf_); } -static void handle_uint8_option( +static void handle_uint8_t_option( const char* attr, sockbuf_t* sockbuf, uint8_t* ptr) @@ -88,7 +88,7 @@ static void handle_uint8_option( *ptr = tmp; } -static void handle_uint32_option( +static void handle_uint32_t_option( const char* attr, sockbuf_t* sockbuf, uint32_t* ptr) @@ -125,41 +125,17 @@ static void handle_bool_option( sockbuf_write(sockbuf, buf_); } -#define HANDLE_UINT8_OPTION(attr) \ - else if (!strcmp_(key, #attr)) { \ - handle_uint8_option(#attr, sockbuf, &ws_params->state. attr); \ - } - -#define HANDLE_INT_OPTION(attr) \ - else if (!strcmp_(key, #attr)) { \ - handle_int_option(#attr, sockbuf, &ws_params->state. attr); \ - } - -#define HANDLE_UINT32_OPTION(attr) \ - else if (!strcmp_(key, #attr)) { \ - handle_uint32_option(#attr, sockbuf, &ws_params->state. attr); \ - } - -#define HANDLE_BOOL_OPTION(attr) \ - else if (!strcmp_(key, #attr)) { \ - handle_bool_option(#attr, sockbuf, &ws_params->state. attr); \ - } - static void handle_set_cmd( sockbuf_t* sockbuf, ws_params_t* ws_params, const char* key) { if (false) {} - HANDLE_UINT8_OPTION(brightness) - HANDLE_UINT8_OPTION(n_snow) - HANDLE_UINT8_OPTION(n_red) - - HANDLE_INT_OPTION(timetick) - HANDLE_UINT32_OPTION(time) - - HANDLE_BOOL_OPTION(sleep) - HANDLE_BOOL_OPTION(power) - HANDLE_BOOL_OPTION(cool) +#define STATE_PARAM(ty, attr) \ + else if (!strcmp_(key, #attr)) { \ + handle_ ## ty ## _option(#attr, sockbuf, &ws_params->state. attr); \ + } +#include "state_params.i" +#undef STATE_PARAM else { sockbuf_write(sockbuf, "Unknown attribute: '"); @@ -168,27 +144,40 @@ static void handle_set_cmd( } } -#define PRINT_INT(attr) \ - snprintf(buf, sizeof(buf), #attr ": %d\n", (int) ws_params->state. attr); \ - sockbuf_write(sockbuf, buf) -#define PRINT_BOOL(attr) \ - snprintf(buf, sizeof(buf), #attr ": %s\n", \ - ws_params->state. attr ? "on" : "off"); \ - sockbuf_write(sockbuf, buf) -static void handle_print_cmd(sockbuf_t* sockbuf, ws_params_t* ws_params) +static void print_uint32_t(sockbuf_t* sockbuf, const char* attr, uint32_t val) { char buf[128]; + snprintf(buf, sizeof(buf), "%s: %d :: uint32_t\n", attr, val); + sockbuf_write(sockbuf, buf); +} + +static void print_uint8_t(sockbuf_t* sockbuf, const char* attr, uint8_t val) +{ + char buf[128]; + snprintf(buf, sizeof(buf), "%s: %d :: uint8_t\n", attr, (int) val); + sockbuf_write(sockbuf, buf); +} + +static void print_bool(sockbuf_t* sockbuf, const char* attr, bool val) +{ + char buf[128]; + snprintf(buf, sizeof(buf), "%s: %s :: bool\n", attr, val ? "on" : "off"); + sockbuf_write(sockbuf, buf); +} - PRINT_INT(time); - PRINT_INT(timetick); - PRINT_INT(brightness); - PRINT_INT(off); - PRINT_INT(n_snow); - PRINT_INT(n_red); +static void print_int(sockbuf_t* sockbuf, const char* attr, int val) +{ + char buf[128]; + snprintf(buf, sizeof(buf), "%s: %d :: int\n", attr, val); + sockbuf_write(sockbuf, buf); +} - PRINT_BOOL(sleep); - PRINT_BOOL(power); - PRINT_BOOL(cool); +static void handle_print_cmd(sockbuf_t* sockbuf, ws_params_t* ws_params) +{ +#define STATE_PARAM(ty, attr) \ + print_ ## ty ( sockbuf, #attr, ws_params->state. attr ); +#include "state_params.i" +#undef STATE_PARAM } static void run_sockbuf(sockbuf_t* sockbuf, ws_params_t* ws_params) @@ -196,6 +185,7 @@ static void run_sockbuf(sockbuf_t* sockbuf, ws_params_t* ws_params) char buf[128]; while (true) { + sockbuf_write(sockbuf, "\x1b[1;32mws2812b>\x1b[0m "); char* cmd = read_token(sockbuf, buf, sizeof(buf)); if (!cmd) { @@ -253,33 +243,5 @@ portTASK_FUNCTION(tcp_server, params) sockbuf_t sockbuf; init_sockbuf(&sockbuf, sock); run_sockbuf(&sockbuf, ws_params); - - ssize_t len; - - char buf[128]; - while ((len = read(sock, buf, sizeof(buf) - 1)) > - 0) { - buf[len] = 0; - printf("Read %s\n", buf); - - if (!strcmp_(buf, "off\n")) { - ws_params->state.power = 0; - } - - if (!strcmp_(buf, "on\n")) { - ws_params->state.power = 1; - } - - for (int i = 0; i < len; ++ i) { - switch (buf[i]) { - case '+': - ws_params->state.brightness += 10; - break; - case '-': - ws_params->state.brightness -= 10; - break; - } - } - } } } diff --git a/main/ws2812b_writer.c b/main/ws2812b_writer.c index 7d0e30f..f7176f1 100644 --- a/main/ws2812b_writer.c +++ b/main/ws2812b_writer.c @@ -85,17 +85,19 @@ void calculate_colors( portTASK_FUNCTION(ws2812b_write_task, params) { ws_params_t* ws_params = (ws_params_t*) params; - reset_parameters(ws_params); ws2812b_buffer_t* buffer = ws2812b_new_buffer(SIZE); + for (;;) { - calculate_colors(ws_params, buffer); - ws2812b_write(ws_params->drv, buffer); - vTaskDelay(1); - // TODO make this happen as a separate, timer task. - ws_params->state.time += ws_params->state.timetick; + // Copy the parameters to avoid a change occuring during the calculation. + ws_params_t copy_params; + memcpy(©_params, ws_params, sizeof(ws_params_t)); + + calculate_colors(©_params, buffer); + ws2812b_write(copy_params.drv, buffer); + vTaskDelay(1); } } |