|
|
|
//#define FASTLED_ALLOW_INTERRUPTS 0
|
|
|
|
//#define FASTLED_INTERRUPT_RETRY_COUNT 1
|
|
|
|
#include <FastLED.h>
|
|
|
|
|
|
|
|
#include "led.h"
|
|
|
|
#include "hardware.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
Led::Led(void) {
|
|
|
|
index = 0;
|
|
|
|
duration = 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::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
|
|
|
|
FastLED.setBrightness(96);
|
|
|
|
FastLED.setCorrection(0xFF5050);
|
|
|
|
fill_solid(leds, NUM_LEDS, CRGB::White);
|
|
|
|
color = CRGB::Orange;
|
|
|
|
|
|
|
|
refresh = &Led::loop_rainbow;
|
|
|
|
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_rainbow;
|
|
|
|
} 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<Led*>(parameters)->animationTask(NULL);
|
|
|
|
}
|