From e313f5170f3aa1551a4d6cac545d5e61006d1fbd Mon Sep 17 00:00:00 2001 From: Tom Irgang Date: Sun, 16 Apr 2023 15:53:17 +0200 Subject: [PATCH] move functions to shared lib --- ESblinkt.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ESblinkt.hpp | 30 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 ESblinkt.cpp create mode 100644 ESblinkt.hpp diff --git a/ESblinkt.cpp b/ESblinkt.cpp new file mode 100644 index 0000000..56717dc --- /dev/null +++ b/ESblinkt.cpp @@ -0,0 +1,87 @@ +#include "ESblinkt.hpp" + +struct RgbColor color_wheel(uint8_t pos) +{ + uint16_t p = (uint8_t)pos; + + if (p < 85) + { + return RgbColor((uint8_t)(255 - p * 3), (uint8_t)(p * 3), (uint8_t)0); + } + else if (p < 170) + { + p -= 85; + return RgbColor((uint8_t)0, (uint8_t)(255 - p * 3), (uint8_t)(p * 3)); + } + else + { + p -= 170; + return RgbColor((uint8_t)(p * 3), (uint8_t)0, (uint8_t)(255 - p * 3)); + } +} + +void fill_rainbow(uint8_t offset, struct strip leds) +{ + uint16_t color_index = 0; + for (uint16_t i = 0; i < leds.pixelCount; i++) + { + color_index = (i + (uint16_t)offset) % 256; + leds.setPixelColor(i, color_wheel(color_index)); + } +} + +void fill_solid(struct RgbColor color, struct strip leds) +{ + for (uint16_t i = 0; i < leds.pixelCount; i++) + { + leds.setPixelColor(i, color); + } +} + +void rainbow(uint32_t rainbow_speed, struct strip leds) +{ + for (int i = 0; i < 256; i++) + { + fill_rainbow(i, leds); + leds.commit(); + vTaskDelay(rainbow_speed / portTICK_PERIOD_MS); + } +} + +void solid(struct RgbColor color, struct strip leds) +{ + fill_solid(color, leds); + leds.commit(); + vTaskDelay(1000 / portTICK_PERIOD_MS); +} + +void three_sided(struct three_sided three_sided_config, struct strip leds) +{ + uint32_t delay = 1000; + uint8_t n = 1; + + if (three_sided_config.anim) + { + delay = three_sided_config.speed; + n = 3; + } + + for (int off = 0; off < n; off++) + { + + for (int j = 0; j < three_sided_config.endRight; j++) + { + leds.setPixelColor(j, three_sided_config.colors[(off) % 3]); + } + for (int j = three_sided_config.endRight; j < three_sided_config.endTop; j++) + { + leds.setPixelColor(j, three_sided_config.colors[(off + 1) % 3]); + } + for (int j = three_sided_config.endTop; j < leds.pixelCount; j++) + { + leds.setPixelColor(j, three_sided_config.colors[(off + 2) % 3]); + } + leds.commit(); + vTaskDelay(delay / portTICK_PERIOD_MS); + } +} diff --git a/ESblinkt.hpp b/ESblinkt.hpp new file mode 100644 index 0000000..934090a --- /dev/null +++ b/ESblinkt.hpp @@ -0,0 +1,30 @@ +#ifndef ESBLINKT_HPP +#define ESBLINKT_HPP + +#include +#include + +struct RgbColor color_wheel(uint8_t pos); +void fill_rainbow(uint8_t offset, struct strip leds); +void fill_solid(struct RgbColor color, struct strip leds); +void rainbow(struct strip leds); +void solid(struct RgbColor color, struct strip leds); +void three_sided(struct three_sided three_sided_config, struct strip leds); + +struct strip +{ + uint16_t pixelCount; + void (*setPixelColor)(uint16_t, Neo3ByteElements::ColorObject); + void (*commit)(void); +}; + +struct three_sided +{ + bool anim; + uint32_t speed; + uint16_t endRight; + uint16_t endTop; + struct RgbColor colors[3]; +}; + +#endif