diff options
Diffstat (limited to 'main')
-rw-r--r-- | main/http_server.c | 121 | ||||
-rw-r--r-- | main/main.c | 18 | ||||
-rw-r--r-- | main/sockbuf.c | 6 | ||||
-rw-r--r-- | main/station.c | 168 | ||||
-rw-r--r-- | main/tcp_server.c | 45 | ||||
-rw-r--r-- | main/ws2812b_writer.c | 20 |
6 files changed, 195 insertions, 183 deletions
diff --git a/main/http_server.c b/main/http_server.c index e4c426c..0345324 100644 --- a/main/http_server.c +++ b/main/http_server.c @@ -19,9 +19,11 @@ static void handle_set_int(httpd_req_t* req, int* val) char param[32]; if (httpd_query_key_value(buf, "add", param, sizeof(param)) == ESP_OK) { *val += atoi(param); - } else if (httpd_query_key_value(buf, "set", param, sizeof(param)) == ESP_OK) { + } else if ( + httpd_query_key_value(buf, "set", param, sizeof(param)) == ESP_OK) { *val = atoi(param); - } else if (httpd_query_key_value(buf, "sub", param, sizeof(param)) == ESP_OK) { + } else if ( + httpd_query_key_value(buf, "sub", param, sizeof(param)) == ESP_OK) { *val -= atoi(param); } else { ESP_LOGI(TAG, "No valid parameters."); @@ -90,29 +92,34 @@ static void handle_set_uint32_t(httpd_req_t* req, uint32_t* val) *val = tmp; } -#define STATE_PARAM(typ, attr, ...) \ - static esp_err_t handle_set_ ## attr (httpd_req_t *req) { \ - ESP_LOGI(TAG, "Handling [%s] (%s)", #attr, req->uri); \ - ws_params_t* params = (ws_params_t*) req->user_ctx; \ +#define STATE_PARAM(typ, attr, ...) \ + static esp_err_t handle_set_##attr(httpd_req_t* req) \ + { \ + ESP_LOGI(TAG, "Handling [%s] (%s)", #attr, req->uri); \ + ws_params_t* params = (ws_params_t*)req->user_ctx; \ httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); \ - handle_set_ ## typ (req, ¶ms->state. attr); \ - httpd_resp_send(req, NULL, 0); \ - return ESP_OK; \ - } \ - static httpd_uri_t http_ ## attr ## _handler = { \ - .uri = "/" #attr, \ - .method = HTTP_POST, \ - .handler = handle_set_ ## attr, \ - .user_ctx = NULL \ - }; + handle_set_##typ(req, ¶ms->state.attr); \ + httpd_resp_send(req, NULL, 0); \ + return ESP_OK; \ + } \ + static httpd_uri_t http_##attr##_handler = { \ + .uri = "/" #attr, \ + .method = HTTP_POST, \ + .handler = handle_set_##attr, \ + .user_ctx = NULL}; #include "state_params.i" #undef STATE_PARAM static size_t write_uint32_t_value( char** out, size_t len, const char* attr, const char* dn, uint32_t value) { - size_t newlen = snprintf(*out, len, "\"%s\":{\"type\":\"uint32_t\",\"value\":\"%u\",\"disp\":\"%s\"}", - attr, (unsigned int) value, dn); + size_t newlen = snprintf( + *out, + len, + "\"%s\":{\"type\":\"uint32_t\",\"value\":\"%u\",\"disp\":\"%s\"}", + attr, + (unsigned int)value, + dn); *out += newlen; return len - newlen; } @@ -120,8 +127,13 @@ static size_t write_uint32_t_value( static size_t write_uint8_t_value( char** out, size_t len, const char* attr, const char* dn, uint8_t value) { - size_t newlen = snprintf(*out, len, "\"%s\":{\"type\":\"uint8_t\",\"value\":\"%u\",\"disp\":\"%s\"}", - attr, (unsigned int)value, dn); + size_t newlen = snprintf( + *out, + len, + "\"%s\":{\"type\":\"uint8_t\",\"value\":\"%u\",\"disp\":\"%s\"}", + attr, + (unsigned int)value, + dn); *out += newlen; return len - newlen; } @@ -129,8 +141,13 @@ static size_t write_uint8_t_value( static size_t write_int_value( char** out, size_t len, const char* attr, const char* dn, int value) { - size_t newlen = snprintf(*out, len, "\"%s\":{\"type\":\"int\",\"value\":\"%d\",\"disp\":\"%s\"}", - attr, value, dn); + size_t newlen = snprintf( + *out, + len, + "\"%s\":{\"type\":\"int\",\"value\":\"%d\",\"disp\":\"%s\"}", + attr, + value, + dn); *out += newlen; return len - newlen; } @@ -138,47 +155,55 @@ static size_t write_int_value( static size_t write_bool_value( char** out, size_t len, const char* attr, const char* dn, bool value) { - size_t newlen = snprintf(*out, len, "\"%s\":{\"type\":\"bool\",\"value\":\"%s\",\"disp\":\"%s\"}", - attr, value ? "on" : "off", dn); + size_t newlen = snprintf( + *out, + len, + "\"%s\":{\"type\":\"bool\",\"value\":\"%s\",\"disp\":\"%s\"}", + attr, + value ? "on" : "off", + dn); *out += newlen; return len - newlen; } static esp_err_t handle_get_params(httpd_req_t* req) { - httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); \ + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); size_t len = NUMBER_PARAMS * 128; char out_buffer[len + 1]; out_buffer[len] = 0; char* out_ptr = out_buffer; - ws_params_t* ws_params = (ws_params_t*) req->user_ctx; + ws_params_t* ws_params = (ws_params_t*)req->user_ctx; bool write_comma = false; httpd_resp_set_type(req, "application/json"); if (len > 1) { *out_ptr = '{'; - out_ptr ++; - len --; + out_ptr++; + len--; } -#define STATE_PARAM(typ, attr, display, ...) \ - if (write_comma && len > 1) { \ - *(out_ptr++) = ','; \ - len --; \ - } \ - len = write_ ## typ ## _value( \ - &out_ptr, len, #attr, display, ws_params->state. attr); \ +#define STATE_PARAM(typ, attr, display, ...) \ + if (write_comma && len > 1) { \ + *(out_ptr++) = ','; \ + len--; \ + } \ + len = write_##typ##_value( \ + &out_ptr, len, #attr, display, ws_params->state.attr); \ write_comma = true; #include "state_params.i" #undef STATE_PARAM if (len > 3) { - *(out_ptr++) = '}'; len --; - *(out_ptr++) = '\r'; len --; - *(out_ptr++) = '\n'; len --; + *(out_ptr++) = '}'; + len--; + *(out_ptr++) = '\r'; + len--; + *(out_ptr++) = '\n'; + len--; } if (len > 1) { *(out_ptr++) = 0; - len --; + len--; } httpd_resp_set_status(req, HTTPD_200); @@ -188,11 +213,10 @@ static esp_err_t handle_get_params(httpd_req_t* req) } static httpd_uri_t http_params_get_handler = { - .uri = "/params", - .method = HTTP_GET, - .handler = handle_get_params, - .user_ctx = NULL -}; + .uri = "/params", + .method = HTTP_GET, + .handler = handle_get_params, + .user_ctx = NULL}; httpd_handle_t start_webserver(ws_params_t* params) { @@ -203,14 +227,13 @@ httpd_handle_t start_webserver(ws_params_t* params) ESP_LOGI(TAG, "Starting httpd server on port: '%d'", config.server_port); if (httpd_start(&server, &config) == ESP_OK) { - http_params_get_handler.user_ctx = params; httpd_register_uri_handler(server, &http_params_get_handler); -#define STATE_PARAM(typ, attr, ...) \ - ESP_LOGI(TAG, "Registering URI handler [%s]", #attr); \ - http_ ## attr ## _handler.user_ctx = params; \ - httpd_register_uri_handler(server, & http_ ## attr ## _handler); +#define STATE_PARAM(typ, attr, ...) \ + ESP_LOGI(TAG, "Registering URI handler [%s]", #attr); \ + http_##attr##_handler.user_ctx = params; \ + httpd_register_uri_handler(server, &http_##attr##_handler); #include "state_params.i" #undef STATE_PARAM diff --git a/main/main.c b/main/main.c index 82fff1e..3b8c284 100644 --- a/main/main.c +++ b/main/main.c @@ -4,12 +4,11 @@ #include "esp_system.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "http_server.h" #include "sdkconfig.h" - #include "station.h" -#include "http_server.h" -#include "ws2812b_writer.h" #include "tcp_server.h" +#include "ws2812b_writer.h" #ifdef CONFIG_IDF_TARGET_ESP32 #define LCD_HOST HSPI_HOST @@ -52,7 +51,7 @@ static ws_params_t ws_params; * to the led strip. */ portTASK_FUNCTION(time_keeper, params_) { - ws_params_t* params = (ws_params_t*) params_; + ws_params_t* params = (ws_params_t*)params_; TickType_t last_wake; const TickType_t freq = 1; @@ -60,11 +59,10 @@ portTASK_FUNCTION(time_keeper, params_) // 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; + for (;;) { + // Wait for the next cycle. + vTaskDelayUntil(&last_wake, freq); + params->state.time += params->state.timetick; } } @@ -93,7 +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", (int) portTICK_PERIOD_MS); + printf("Tick period ms: %d\n", (int)portTICK_PERIOD_MS); error = spi_bus_initialize(HSPI_HOST, &cfg, SPI_DMA_CH_AUTO); printf("Bus Init\n"); diff --git a/main/sockbuf.c b/main/sockbuf.c index 0fede63..5b88393 100644 --- a/main/sockbuf.c +++ b/main/sockbuf.c @@ -1,7 +1,7 @@ #include "sockbuf.h" -#include <unistd.h> #include <string.h> +#include <unistd.h> void sockbuf_write(sockbuf_t* sockbuf, const char* value) { @@ -21,7 +21,7 @@ int peek_char(sockbuf_t* sockbuf) if (sockbuf->next == sockbuf->last) { sockbuf->next = 0; int size = read(sockbuf->socket, sockbuf->buf, sizeof(sockbuf->buf)); - sockbuf->last = (uint8_t) size; + sockbuf->last = (uint8_t)size; if (size <= 0) { return size - 1; @@ -36,7 +36,7 @@ int get_char(sockbuf_t* sockbuf) int ret = peek_char(sockbuf); if (ret >= 0) { - sockbuf->next ++; + sockbuf->next++; } return ret; diff --git a/main/station.c b/main/station.c index 8f69f49..d4c2d00 100644 --- a/main/station.c +++ b/main/station.c @@ -1,51 +1,53 @@ #include <string.h> -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" + #include "esp_event.h" #include "esp_log.h" -#include "nvs_flash.h" - +#include "esp_system.h" +#include "esp_wifi.h" +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "freertos/task.h" #include "lwip/err.h" #include "lwip/sys.h" +#include "nvs_flash.h" static EventGroupHandle_t s_wifi_event_group; static int s_retry_num = 0; -static const char * TAG = "wifi"; +static const char* TAG = "wifi"; #define WIFI_CONNECTED_BIT BIT0 -#define WIFI_FAIL_BIT BIT1 +#define WIFI_FAIL_BIT BIT1 -static void event_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) +static void event_handler( + void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { - if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { - esp_wifi_connect(); - } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { - if (s_retry_num < 5) { - esp_wifi_connect(); - s_retry_num++; - ESP_LOGI(TAG, "retry to connect to the AP"); - } else { - xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); - } - ESP_LOGI(TAG,"connect to the AP fail"); - } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { - ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); - s_retry_num = 0; - xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if ( + event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + if (s_retry_num < 5) { + esp_wifi_connect(); + s_retry_num++; + ESP_LOGI(TAG, "retry to connect to the AP"); + } else { + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } + ESP_LOGI(TAG, "connect to the AP fail"); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data; + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); + s_retry_num = 0; + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } +} void wifi_init_station(const char* ssid, const char* psk) { /** Initialize the flash. Not exactly sure why this is required, but it is, * otherwise there will be a fault. */ esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || + ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } @@ -66,60 +68,60 @@ void wifi_init_station(const char* ssid, const char* psk) esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_got_ip; - ESP_ERROR_CHECK( - esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, - &event_handler, NULL, &instance_any_id)); - - ESP_ERROR_CHECK( - esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, - &event_handler, NULL, &instance_got_ip)); - - wifi_config_t wifi_config = { - .sta = { - // .ssid = ssid, - // .password = psk, - /* Setting a password implies station will connect to all security modes including WEP/WPA. - * However these modes are deprecated and not advisable to be used. Incase your Access point - * doesn't support WPA2, these mode can be enabled by commenting below line */ - .threshold.authmode = WIFI_AUTH_WPA2_PSK, - - .pmf_cfg = { - .capable = true, - .required = false - }, - }, - }; - - memcpy(wifi_config.sta.ssid, ssid, strlen(ssid)); - memcpy(wifi_config.sta.password, psk, strlen(psk)); - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) ); - ESP_ERROR_CHECK(esp_wifi_start() ); - - ESP_LOGI(TAG, "wifi_init_sta finished."); - /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum - * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ - EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, - WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, - pdFALSE, - pdFALSE, - portMAX_DELAY); - - /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually - * happened. */ - if (bits & WIFI_CONNECTED_BIT) { - ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", - ssid, psk); - } else if (bits & WIFI_FAIL_BIT) { - ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", - ssid, psk); - } else { - ESP_LOGE(TAG, "UNEXPECTED EVENT"); - } + ESP_ERROR_CHECK(esp_event_handler_instance_register( + WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id)); + + ESP_ERROR_CHECK(esp_event_handler_instance_register( + IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip)); + + wifi_config_t wifi_config = { + .sta = + { + // .ssid = ssid, + // .password = psk, + /* Setting a password implies station will connect to all security + * modes including WEP/WPA. However these modes are deprecated and + * not advisable to be used. Incase your Access point doesn't + * support WPA2, these mode can be enabled by commenting below + * line */ + .threshold.authmode = WIFI_AUTH_WPA2_PSK, + + .pmf_cfg = {.capable = true, .required = false}, + }, + }; + + memcpy(wifi_config.sta.ssid, ssid, strlen(ssid)); + memcpy(wifi_config.sta.password, psk, strlen(psk)); + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + + ESP_LOGI(TAG, "wifi_init_sta finished."); + /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or + * connection failed for the maximum number of re-tries (WIFI_FAIL_BIT). The + * bits are set by event_handler() (see above) */ + EventBits_t bits = xEventGroupWaitBits( + s_wifi_event_group, + WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, + pdFALSE, + pdFALSE, + portMAX_DELAY); + + /* xEventGroupWaitBits() returns the bits before the call returned, hence we + * can test which event actually happened. */ + if (bits & WIFI_CONNECTED_BIT) { + ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", ssid, psk); + } else if (bits & WIFI_FAIL_BIT) { + ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", ssid, psk); + } else { + ESP_LOGE(TAG, "UNEXPECTED EVENT"); + } - /* The event will not be processed after unregister */ - ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip)); - ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id)); - vEventGroupDelete(s_wifi_event_group); + /* The event will not be processed after unregister */ + ESP_ERROR_CHECK(esp_event_handler_instance_unregister( + IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip)); + ESP_ERROR_CHECK(esp_event_handler_instance_unregister( + WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id)); + vEventGroupDelete(s_wifi_event_group); } diff --git a/main/tcp_server.c b/main/tcp_server.c index 2ee7e51..8e6b7de 100644 --- a/main/tcp_server.c +++ b/main/tcp_server.c @@ -1,9 +1,9 @@ -#include <ctype.h> - #include "tcp_server.h" -#include "sockbuf.h" + +#include <ctype.h> #include "lwip/sockets.h" +#include "sockbuf.h" #include "station.h" static int strcmp_(const char* s1, const char* s2) @@ -31,8 +31,8 @@ static char* read_token(sockbuf_t* sockbuf, char* into, size_t sz) while (ch >= 0 && !isspace(ch)) { if (sz > 1) { - into[cur++] = (char) ch; - sz --; + into[cur++] = (char)ch; + sz--; } ch = get_char(sockbuf); } @@ -47,10 +47,7 @@ static char* read_token(sockbuf_t* sockbuf, char* into, size_t sz) return into; } -static void handle_int_option( - const char* attr, - sockbuf_t* sockbuf, - int* ptr) +static void handle_int_option(const char* attr, sockbuf_t* sockbuf, int* ptr) { char op_[8]; char buf_[32]; @@ -79,9 +76,7 @@ static void handle_int_option( } static void handle_uint8_t_option( - const char* attr, - sockbuf_t* sockbuf, - uint8_t* ptr) + const char* attr, sockbuf_t* sockbuf, uint8_t* ptr) { int tmp = *ptr; handle_int_option(attr, sockbuf, &tmp); @@ -89,19 +84,14 @@ static void handle_uint8_t_option( } static void handle_uint32_t_option( - const char* attr, - sockbuf_t* sockbuf, - uint32_t* ptr) + const char* attr, sockbuf_t* sockbuf, uint32_t* ptr) { int tmp = *ptr; handle_int_option(attr, sockbuf, &tmp); *ptr = tmp; } -static void handle_bool_option( - const char* attr, - sockbuf_t* sockbuf, - bool* ptr) +static void handle_bool_option(const char* attr, sockbuf_t* sockbuf, bool* ptr) { char buf_[32]; char* op = read_token(sockbuf, buf_, sizeof(buf_)); @@ -128,11 +118,12 @@ static void handle_bool_option( static void handle_set_cmd( sockbuf_t* sockbuf, ws_params_t* ws_params, const char* key) { - if (false) {} - -#define STATE_PARAM(ty, attr, ...) \ - else if (!strcmp_(key, #attr)) { \ - handle_ ## ty ## _option(#attr, sockbuf, &ws_params->state. attr); \ + if (false) { + } +#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 @@ -154,7 +145,7 @@ static void print_uint32_t(sockbuf_t* sockbuf, const char* attr, uint32_t val) 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); + snprintf(buf, sizeof(buf), "%s: %d :: uint8_t\n", attr, (int)val); sockbuf_write(sockbuf, buf); } @@ -175,7 +166,7 @@ static void print_int(sockbuf_t* sockbuf, const char* attr, int val) 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 ); + print_##ty(sockbuf, #attr, ws_params->state.attr); #include "state_params.i" #undef STATE_PARAM } @@ -213,7 +204,7 @@ static void run_sockbuf(sockbuf_t* sockbuf, ws_params_t* ws_params) portTASK_FUNCTION(tcp_server, params) { - ws_params_t* ws_params = (ws_params_t*) params; + ws_params_t* ws_params = (ws_params_t*)params; int s = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; diff --git a/main/ws2812b_writer.c b/main/ws2812b_writer.c index 36fa666..3b90cd3 100644 --- a/main/ws2812b_writer.c +++ b/main/ws2812b_writer.c @@ -3,18 +3,19 @@ #include <stdlib.h> #include <string.h> -const int NUMBER_PARAMS = +const int NUMBER_PARAMS = #define STATE_PARAM(...) +1 #include "state_params.i" #undef STATE_PARAM -; + ; #define SIZE 300 /* Returns round(sin(2πn / 256) * 127.5 + 127.5). */ uint8_t byte_sin(uint8_t n); -static inline uint8_t byte_scale(uint8_t n, uint8_t sc) { +static inline uint8_t byte_scale(uint8_t n, uint8_t sc) +{ return n * sc / 255; } @@ -28,15 +29,12 @@ void reset_parameters(ws_params_t* params) { memset(¶ms->state, 0, sizeof(params->state)); -#define STATE_PARAM(type, attr, displ, def, ...) \ - params->state. attr = def; +#define STATE_PARAM(type, attr, displ, def, ...) params->state.attr = def; #include "state_params.i" #undef STATE_PARAM } -void calculate_colors( - ws_params_t* ws_params, - ws2812b_buffer_t* buffer) +void calculate_colors(ws_params_t* ws_params, ws2812b_buffer_t* buffer) { uint32_t red = 0, green = 0, blue = 0; int time; @@ -50,7 +48,8 @@ void calculate_colors( continue; } - red = byte_scale(byte_sin(time / 1000 + x * 4), 255 - ws_params->state.n_red) + + red = byte_scale( + byte_sin(time / 1000 + x * 4), 255 - ws_params->state.n_red) + ws_params->state.n_red; green = 255 - red; @@ -104,13 +103,12 @@ void calculate_colors( portTASK_FUNCTION(ws2812b_write_task, params) { - ws_params_t* ws_params = (ws_params_t*) params; + ws_params_t* ws_params = (ws_params_t*)params; reset_parameters(ws_params); ws2812b_buffer_t* buffer = ws2812b_new_buffer(SIZE); for (;;) { - // Copy the parameters to avoid a change occuring during the calculation. ws_params_t copy_params; memcpy(©_params, ws_params, sizeof(ws_params_t)); |