//#define FASTLED_ALLOW_INTERRUPTS 0 //#define FASTLED_INTERRUPT_RETRY_COUNT 1 #include #include "led.h" #include "hardware.h" using namespace std; Led::Led(void) { index = 0; duration = 0; wakeupIndex = 0; } void Led::loop_rainbow(void) { fill_rainbow(leds, NUM_LEDS, index, 14); } void Led::loop_confetti(void) { // random colored speckles that blink in and fade smoothly fadeToBlackBy( leds, NUM_LEDS, 10); int pos = random16(NUM_LEDS); leds[pos] += CHSV( index + random8(64), 200, 255); } void Led::loop_sweep(void) { // a colored dot sweeping back and forth, with fading trails fadeToBlackBy( leds, NUM_LEDS, 20); int pos = beatsin16( 13, 0, NUM_LEDS-1 ); leds[pos] += CHSV( index, 255, 192); } void Led::loop_steady(void) { fill_solid(leds, NUM_LEDS, color); } void Led::loop_off(void) { if (wakeupIndex == 0) { fill_solid(leds, NUM_LEDS, CRGB::Black); } else { // Scale the heat value from 0-255 down to 0-240 // for best results with color palettes. //byte colorindex = scale8( heat[j], 240); CRGB color = ColorFromPalette(HeatColors_p, wakeupIndex); if (wakeupIndex > 240) color = CRGB::White; fill_solid(leds, NUM_LEDS, color); // fadeToBlackBy( leds, NUM_LEDS, 255-wakeupIndex); setBrightness(wakeupIndex); if (wakeupIndex < 100) { leds[3] = CRGB::Black; leds[NUM_LEDS-4] = CRGB::Black; } if (wakeupIndex < 150) { leds[2] = CRGB::Black; leds[NUM_LEDS-3] = CRGB::Black; } if (wakeupIndex < 200) { leds[1] = CRGB::Black; leds[NUM_LEDS-2] = CRGB::Black; } if (wakeupIndex < 250) { leds[0] = CRGB::Black; leds[NUM_LEDS-1] = CRGB::Black; } } } void Led::wakeUpLight(uint8_t i) { Serial.printf("wakeuplight(%d)\n", i); wakeupIndex = i; } void Led::changeAnimation(uint8_t num, uint16_t duration) { if (duration == 0) this->duration = UINT16_MAX; else this->duration = duration; if (num == 0) refresh = &Led::loop_steady; else if (num == 1) refresh = &Led::loop_sweep; else if (num == 2) refresh = &Led::loop_confetti; else if (num == 3) refresh = &Led::loop_rainbow; else if (num == 255) refresh = &Led::loop_off; } bool Led::isOn() { if (refresh && refresh != &Led::loop_off && FastLED.getBrightness() != 0) return true; else return false; } void Led::changeColor(uint8_t r, uint8_t g, uint8_t b) { color = CRGB(r, g, b); } void Led::setBrightness(uint8_t brightness) { if (brightness > 254) { // don't set first byte to 255 on tm1829 FastLED.setBrightness(254); } else { // uint8_t b = pow(((double)brightness/255.0), 4.2) * 255.0; // FastLED.setBrightness(b); FastLED.setBrightness(brightness); } } void Led::stop(void) { runTask = false; delay(50); // FastLED.clear(); fill_solid(leds, NUM_LEDS, CRGB::Black); FastLED.show(); } void Led::setup(void) { pinMode(LED_PIN, OUTPUT); FastLED.addLeds(leds, NUM_LEDS); FastLED.setBrightness(96); //FastLED.setMaxPowerInVoltsAndMilliamps(5, 500); FastLED.setCorrection(TypicalLEDStrip); fill_solid(leds, NUM_LEDS, CRGB::Black); color = CRGB::Orange; refresh = &Led::loop_off; delay(100); xTaskCreate( &cTaskWrapper, /* Task function. */ "ledAnimationTask", /* String with name of task. */ 1024, /* Stack size in words. */ this, /* Parameter passed as input of the task */ tskIDLE_PRIORITY+3, /* Priority of the task. */ &ledTaskHandle); /* Task handle. */ } void Led::animationTask(void* parameters) { runTask = true; while(runTask) { if (refresh != nullptr) { (*this.*refresh)(); FastLED.show(); // EVERY_N_MILLISECONDS ( 20) { index++; } index++; if (duration == 0) { refresh = &Led::loop_off; } else if (duration == UINT16_MAX) { } else { duration--; } // EVERY_N_SECONDS(30) { // FastLED.clear(); // delay(200); // changeAnimation(2, 2000); // } } vTaskDelay(50 / portTICK_PERIOD_MS); } Serial.println("LED animation end!"); vTaskDelete(NULL); } void Led::cTaskWrapper(void* parameters) { static_cast(parameters)->animationTask(NULL); }