You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

155 lines
4.1 KiB

//#define FASTLED_ALLOW_INTERRUPTS 0
//#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h>
#include "led.h"
#include "hardware.h"
#include "coin.h"
extern Coin coin;
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);
leds[0] = CRGB::White;
leds[1] = CRGB::White;
leds[NUM_LEDS-1] = CRGB::White;
leds[NUM_LEDS-2] = CRGB::White;
leds[NUM_LEDS-3] = CRGB::White;
}
void Led::loop_dispense(void) {
// top: led 9 and 10
// 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);
leds[0] = CRGB::White;
leds[NUM_LEDS-1] = CRGB::White;
leds[NUM_LEDS-2] = CRGB::White;
}
void Led::loop_denied(void) {
fill_solid(leds, NUM_LEDS, CRGB::Red);
}
void Led::loop_steady(void) {
fill_solid(leds, NUM_LEDS, color);
if (int moneyinserted = coin.isMoneyInserted()) {
fill_solid(leds, 8, CRGB::Black);
if (moneyinserted >= 5) {
leds[0] = CRGB::Yellow;
} if (moneyinserted >= 10) {
leds[1] = CRGB::Yellow;
} if (moneyinserted >= 20) {
leds[2] = CRGB::Yellow;
} if (moneyinserted >= 50) {
leds[3] = CRGB::Yellow;
} if (moneyinserted >= 75) {
leds[4] = CRGB::Yellow;
} if (moneyinserted >= 100) {
leds[5] = CRGB::Yellow;
} if (moneyinserted >= 150) {
leds[6] = CRGB::Yellow;
} if (moneyinserted >= 200) {
leds[7] = CRGB::Yellow;
}
}
}
void Led::changeAnimation(uint8_t num, uint16_t duration) {
change = true;
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_dispense;
else if (num == 2) refresh = &Led::loop_confetti;
else if (num == 3) refresh = &Led::loop_rainbow;
else if (num == 4) refresh = &Led::loop_denied;
}
void Led::changeColor(uint8_t r, uint8_t g, uint8_t b) {
color = CRGB(r, g, b);
change = true;
}
void Led::changeBrightness(uint8_t brightness) {
if (brightness > 254) { // don't set first byte to 255 on tm1829
FastLED.setBrightness(254);
} else {
FastLED.setBrightness(brightness);
}
change = true;
}
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;
change = true;
refresh = &Led::loop_steady;
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) {
while(true) {
if (refresh != nullptr) {
(*this.*refresh)();
try {
if (refresh != (&Led::loop_steady) || change == true) FastLED.show();
change = false;
} catch(...) {
Serial.println("FastLED error");
this->setup();
}
// EVERY_N_MILLISECONDS ( 20) { index++; }
index++;
if (duration == 0) {
refresh = &Led::loop_steady;
change = true;
} else if (duration == UINT16_MAX) {
} else {
duration--;
}
EVERY_N_SECONDS(300) {
// FastLED.clear();
// delay(200);
changeAnimation(3, 400);
}
}
vTaskDelay(50 / portTICK_PERIOD_MS);
}
Serial.println("LED animation end!");
vTaskDelete(NULL);
}
void Led::cTaskWrapper(void* parameters) {
static_cast<Led*>(parameters)->animationTask(NULL);
}