static const char *TAG = "leds"; #include #include "leds.hpp" #include "utils.hpp" static inline uint8_t mix(int l, int r, Fixed x) { return ((l << shift) + (x * (r - l))) >> shift; } Color Gradient::at(Fixed x) const { int i_left = (x * n_colors) >> shift; int i_right = i_left < n_colors - 1 ? i_left + 1 : 0; Fixed ratio = (x * n_colors) - (i_left << shift); //ESP_LOGI(TAG, "at(%d) = %d between %d and %d", x, ratio, i_left, i_right); Color cl = colors[i_left]; Color cr = colors[i_right]; Color color = { mix(cl.r, cr.r, ratio), mix(cl.g, cr.g, ratio), mix(cl.b, cr.b, ratio) }; /*ESP_LOGI(TAG, "(%d, %d, %d) %f (%d, %d, %d) -> (%d, %d, %d)", cl.r, cl.g, cl.b, ratio / (float)factor, cr.r, cr.g, cr.b, color.r, color.g, color.b );*/ return color; } void GradientPattern::step(Color pixels[], int len, Pattern::State *_state) const { GradientPattern::State *state = (GradientPattern::State*)_state; int64_t now_us = time_us(); int64_t duration_us = state->last_us == 0 ? 0 : now_us - state->last_us; state->last_us = now_us; //ESP_LOGI(TAG, "duration %d", duration_us); /* int period_us = 1000000 / 60; int n_skipped = (duration_us - period_us / 2) / period_us; if (n_skipped) { ESP_LOGW(TAG, "Skipped %d frames", n_skipped); } */ // Don't make a major animation jump if it's been terribly long if (duration_us > 100000) duration_us = 100000; int offset_delta = (duration_us << shift) / (cycle_time_ms * 1000); if (reverse) state->offset += offset_delta; else state->offset -= offset_delta; Fixed off = march ? (((int)state->offset * cycle_length) & ~((1 << shift) - 1)) / cycle_length : state->offset; //ESP_LOGI(TAG, "cycle time %d, delta %d, offset %d, off %d", cycle_time_ms, offset_delta, offset, off); for (int i = 0; i < len; ++i) { pixels[i] = gradient->at(off + ((i << shift) / cycle_length)); } } LEDStrip::LEDStrip(int _length) : length(_length), pattern(NULL), pixels(new Color[_length]), state(NULL) {} void LEDStrip::update_state() { if (state) delete state; if (pattern) state = pattern->start(length); else state = NULL; } void LEDStrip::setLength(int _length) { length_changing(_length); delete[] pixels; length = _length; pixels = new Color[_length]; update_state(); } void LEDStrip::step() { if (pattern) pattern->step(pixels, length, state); } static std::string termcolor(Color c) { return "\x1b[48;2;" + std::to_string((int)c.r) + ";" + std::to_string((int)c.g) + ";" + std::to_string((int)c.b) + "m"; } void TerminalLEDs::show() { std::string line; for (int i = 0; i < length; i++) { line += termcolor(pixels[i]) + " "; } line += "\x1b[0m"; ESP_LOGI(TAG, "%s", line.c_str()); }