/// © MiroZ 2024 #include "Settings.h" #include "app_config.h" #include "Led.h" #if 0 static const uint8_t led_sin_256[] = { 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 6, 6, 8, 9, 10, 11, 12, 14, 15, 17, 18, 20, 22, 23, 25, 27, 29, 31, 33, 35, 38, 40, 42, 45, 47, 49, 52, 54, 57, 60, 62, 65, 68, 71, 73, 76, 79, 82, 85, 88, 91, 94, 97, 100, 103, 106, 109, 113, 116, 119, 122, 125, 128, 131, 135, 138, 141, 144, 147, 150, 153, 156, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 191, 194, 197, 199, 202, 204, 207, 209, 212, 214, 216, 218, 221, 223, 225, 227, 229, 231, 232, 234, 236, 238, 239, 241, 242, 243, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253, 253, 252, 252, 251, 250, 249, 248, 247, 246, 245, 243, 242, 241, 239, 238, 236, 234, 232, 231, 229, 227, 225, 223, 221, 218, 216, 214, 212, 209, 207, 204, 202, 199, 197, 194, 191, 189, 186, 183, 180, 177, 174, 171, 168, 165, 162, 159, 156, 153, 150, 147, 144, 141, 138, 135, 131, 128, 125, 122, 119, 116, 113, 109, 106, 103, 100, 97, 94, 91, 88, 85, 82, 79, 76, 73, 71, 68, 65, 62, 60, 57, 54, 52, 49, 47, 45, 42, 40, 38, 35, 33, 31, 29, 27, 25, 23, 22, 20, 18, 17, 15, 14, 12, 11, 10, 9, 8, 6, 6, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0 }; static const uint8_t led_sin_127[] = { 0, 0, 1, 1, 2, 4, 6, 8, 10, 12, 15, 18, 22, 25, 29, 34, 38, 42, 47, 52, 57, 63, 68, 74, 80, 86, 92, 98, 104, 110, 116, 123, 129, 135, 142, 148, 154, 160, 166, 172, 178, 184, 189, 195, 200, 205, 210, 215, 219, 224, 228, 231, 235, 238, 241, 244, 246, 248, 250, 252, 253, 254, 255, 255, 255, 255, 254, 253, 252, 250, 248, 246, 244, 241, 238, 235, 231, 228, 224, 219, 215, 210, 205, 200, 195, 189, 184, 178, 172, 166, 160, 154, 148, 142, 135, 129, 123, 116, 110, 104, 98, 92, 86, 80, 74, 68, 63, 57, 52, 47, 42, 38, 34, 29, 25, 22, 18, 15, 12, 10, 8, 6, 4, 2, 1, 1, 0, 0 }; #endif static const uint8_t led_sin_64[]{ 0, 1, 2, 5, 10, 15, 21, 29, 37, 47, 57, 67, 79, 90, 103, 115, 127, 140, 152, 165, 176, 188, 198, 208, 218, 226, 234, 240, 245, 250, 253, 254, 255, 254, 253, 250, 245, 240, 234, 226, 218, 208, 198, 188, 176, 165, 152, 140, 128, 115, 103, 90, 79, 67, 57, 47, 37, 29, 21, 15, 10, 5, 2, 1, 0 }; void Led::steady(uint8_t r, uint8_t g, uint8_t b, uint8_t d) { int r_s, g_s, b_s; uint32_t val = m_strip->getPixelColor(0); b_s = val & 0xff; g_s = (val>>8) & 0xff; r_s = (val>>16) & 0xff; for(int n=0; n<17; n++) { int r_ = r_s + ((int)r-r_s)*n/16; int g_ = g_s + ((int)g-g_s)*n/16; int b_ = b_s + ((int)b-b_s)*n/16; m_strip->setPixelColor(0, r_, g_, b_); m_strip->show(); delay(d); } } void Led::setLedColor(uint8_t led_index, uint8_t r, uint8_t g, uint8_t b) { if (led_index < m_strip->numPixels()) { m_strip->setPixelColor(led_index, scale(r), scale(g), scale(b)); m_strip->show(); } } void Led::suspendTask() { if (m_task != nullptr) { vTaskSuspend(m_task); ESP_LOGI("LED", "LED task suspended for OTA"); } } void Led::resumeTask() { if (m_task != nullptr) { vTaskResume(m_task); ESP_LOGI("LED", "LED task resumed"); } } void Led::setDirectColor(uint8_t led_index, uint8_t r, uint8_t g, uint8_t b) { // Direct LED control without going through the task queue if (led_index < m_strip->numPixels()) { m_strip->setPixelColor(led_index, scale(r), scale(g), scale(b)); m_strip->show(); } } void Led::run() { struct LED_QUEUE msg; while(true) { m_queue.receive(&msg, portMAX_DELAY); if(msg.program == PROGRAM::STEADY) { steady(msg.r, msg.g, msg.b, 30); } else if(msg.program == PROGRAM::PULSATING) { // turn off steady(0, 0, 0, 5); while(true) { for(int n=0; nsetPixelColor(0, msg.r*m/255, msg.g*m/255, msg.b*m/255); m_strip->show(); delay(30); } if(m_queue.receive(&msg, 0)) { if(msg.program == PROGRAM::STEADY) { steady(msg.r, msg.g, msg.b, 30); break; } } delay(60); } } } } Led::Led(uint32_t pin) : m_queue(5) { ESP_LOGW("led", "Starting led..."); m_strip = new Adafruit_NeoPixel(4, pin, NEO_GRB + NEO_KHZ800); m_strip->begin(); for(int i = 0; i < 4; i++) { m_strip->setPixelColor(i, 0, 0, 0); } m_strip->show(); m_brightness = SETTINGS.led.brightness; // create the led task m_task = TaskMgr::getInstance().createTask(std::bind(&Led::run, this), LED_TASK_NAME, LED_TASK_STACK_SIZE, LED_TASK_PRIORITY, LED_TASK_CORE); } Led::~Led() { vTaskSuspend(m_task); vTaskDelete(m_task); delete m_strip; } /// @brief Sets led strip color /// @param r red value 0-255 /// @param g green value 0-255 /// @param b blue value 0-255 void Led::setColor(uint8_t r, uint8_t g, uint8_t b) { struct LED_QUEUE msg; msg.program = PROGRAM::STEADY; msg.r = scale(r); msg.g = scale(g); msg.b = scale(b); m_queue.send(&msg, 1000); } void Led::setPulse(uint8_t r, uint8_t g, uint8_t b) { struct LED_QUEUE msg; msg.program = PROGRAM::PULSATING; msg.r = scale(r); msg.g = scale(g); msg.b = scale(b); m_queue.send(&msg, 1000); } void Led::allOff() { for(int i = 0; i < 4; i++) { m_strip->setPixelColor(i, 0, 0, 0); } m_strip->show(); } void Led::setBrightness(uint8_t brightness) { m_brightness = brightness; }