diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2021-12-01 00:34:18 -0700 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2021-12-01 00:34:18 -0700 |
commit | b1ca00800f34ab86c0a99c46796c73a8028157ff (patch) | |
tree | 60b90da71bf12c034ecec08eced9f07d825ad92a /main/ws2812b_writer.c | |
parent | d89195e2144df51ca02a860f4b46fd38b4dfeb29 (diff) | |
download | esp32-ws2812b-b1ca00800f34ab86c0a99c46796c73a8028157ff.tar.gz esp32-ws2812b-b1ca00800f34ab86c0a99c46796c73a8028157ff.tar.bz2 esp32-ws2812b-b1ca00800f34ab86c0a99c46796c73a8028157ff.zip |
Port the code from my STM project over to ESP32.
There is still definitely some jenk to it. For one I still
need to figure out how to protect critical sections to prevent stutter.
I do need to set up a timer to control the time incrementer rather than
depending on the speed of the spi bus.
Diffstat (limited to 'main/ws2812b_writer.c')
-rw-r--r-- | main/ws2812b_writer.c | 139 |
1 files changed, 111 insertions, 28 deletions
diff --git a/main/ws2812b_writer.c b/main/ws2812b_writer.c index fc3c777..7d0e30f 100644 --- a/main/ws2812b_writer.c +++ b/main/ws2812b_writer.c @@ -5,37 +5,80 @@ #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) { + return n * sc / 255; +} + +#define min(a, b) (a) < (b) ? (a) : (b) +#define max(a, b) (a) > (b) ? (a) : (b) + +/* returns (in / 256)^n * 256. */ +uint8_t amp(uint8_t in, uint8_t n); + +void reset_parameters(ws_params_t* params) +{ + memset(¶ms->state, 0, sizeof(params->state)); + params->state.brightness = 32; + params->state.off = 8; + params->state.timetick = 100; + params->state.power = 1; + params->state.n_snow = 2; +} + void calculate_colors( ws_params_t* ws_params, ws2812b_buffer_t* buffer) { - /* RACE Conditions for sure. Need to fix that. */ - ws2812b_rgb_t color = {0}; - if (!strcmp(ws_params->color, "black\n")) { - } else if (!strcmp(ws_params->color, "green\n")) { - color.g = 255; - } else if (!strcmp(ws_params->color, "red\n")) { - color.r = 255; - } else if (!strcmp(ws_params->color, "blue\n")) { - color.b = 255; - } else if (!strcmp(ws_params->color, "yellow\n")) { - color.r = 255; - color.g = 255; - } else if (!strcmp(ws_params->color, "magenta\n")) { - color.r = 255; - color.b = 255; - } else if (!strcmp(ws_params->color, "teal\n")) { - color.g = 255; - color.b = 255; - } else { - color.r = 128; - color.g = 128; - color.b = 128; - } + uint32_t red = 0, green = 0, blue = 0; + + for (int i = 0; i < SIZE; ++i) { + if (!ws_params->state.power) { + ws2812b_buffer_set_rgb(buffer, i, 0, 0, 0); + continue; + } + + red = byte_scale(byte_sin(ws_params->state.time / 1000 + i * 4), 255 - ws_params->state.n_red) + + ws_params->state.n_red; + green = 255 - red; + + if (ws_params->state.cool) { + uint32_t tmp = green; + green = blue; + blue = tmp; + } + + /* Add a little white flair that comes around every once in a while. */ - int i; - for (i = 0; i < SIZE; ++i) { - ws2812b_buffer_set_color(buffer, i, &color); + uint32_t whitesum = 0; + if (ws_params->state.n_snow) { + uint32_t white[] = { + /* Parallax "snow" */ + ws_params->state.time / 179 + i * 8, + ws_params->state.time / 193 + i * 12, + ws_params->state.time / 211 + i * 16, + (ws_params->state.time) / 233 + i * 8, + // (ws_params->state.time + 128) / 233 + i * 8, + }; + + for (int i = 0; i < sizeof(white) / sizeof(uint32_t); ++i) { + if ((white[i] / 256) % ws_params->state.n_snow == 0) { + white[i] = amp(byte_sin(white[i]), 20); + } else { + white[i] = 0; + } + whitesum += white[i]; + } + } + + ws2812b_buffer_set_rgb( + buffer, + i, + byte_scale(min(red + whitesum, 255), ws_params->state.brightness), + byte_scale(min(green + whitesum, 255), ws_params->state.brightness), + byte_scale(min(blue + whitesum, 255), ws_params->state.brightness)); } } @@ -43,12 +86,52 @@ portTASK_FUNCTION(ws2812b_write_task, params) { ws_params_t* ws_params = (ws_params_t*) params; - strcpy(ws_params->color, "red\n"); + 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(10 / portTICK_PERIOD_MS); + vTaskDelay(1); + + // TODO make this happen as a separate, timer task. + ws_params->state.time += ws_params->state.timetick; } } + +uint8_t sintable[256] = { + 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, + 173, 176, 179, 182, 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211, + 213, 215, 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 238, 240, + 241, 243, 244, 245, 246, 248, 249, 250, 250, 251, 252, 253, 253, 254, 254, + 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251, + 250, 250, 249, 248, 246, 245, 244, 243, 241, 240, 238, 237, 235, 234, 232, + 230, 228, 226, 224, 222, 220, 218, 215, 213, 211, 208, 206, 203, 201, 198, + 196, 193, 190, 188, 185, 182, 179, 176, 173, 170, 167, 165, 162, 158, 155, + 152, 149, 146, 143, 140, 137, 134, 131, 128, 124, 121, 118, 115, 112, 109, + 106, 103, 100, 97, 93, 90, 88, 85, 82, 79, 76, 73, 70, 67, 65, + 62, 59, 57, 54, 52, 49, 47, 44, 42, 40, 37, 35, 33, 31, 29, + 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, 10, 9, 7, 6, + 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, 10, 11, + 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, + 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76, + 79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121, + 124, +}; + +uint8_t byte_sin(uint8_t n) +{ + return sintable[n]; +} + +uint8_t amp(uint8_t in, uint8_t n) +{ + uint32_t out = in; + + for (int i = 0; i < n; ++i) { + out = (out * in) / 256; + } + + return min(out, 255); +} |