From e48a3b606640ba2c6aa1657ab37577ff8c052f92 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Sat, 6 Jan 2018 14:47:42 +0100 Subject: [PATCH] add led functionality --- software/src/hardware.h | 2 +- software/src/led.cpp | 69 +++++++++++++++++++++++++++++++++++------ software/src/led.h | 9 ++++++ software/src/main.cpp | 6 ++-- software/src/shelf.cpp | 7 +++++ 5 files changed, 79 insertions(+), 14 deletions(-) diff --git a/software/src/hardware.h b/software/src/hardware.h index dfe6e0a..4c95c7c 100644 --- a/software/src/hardware.h +++ b/software/src/hardware.h @@ -9,7 +9,7 @@ static constexpr int PROGMEM SERVO_PINS[] = {23, 22, 1, 3, 21, 19}; static constexpr uint8_t PROGMEM LED_PIN = 4; -static constexpr uint8_t PROGMEM NUM_LEDS = 22; +static constexpr uint8_t PROGMEM NUM_LEDS = 21; #define LED_TYPE TM1829 #define COLOR_ORDER BRG diff --git a/software/src/led.cpp b/software/src/led.cpp index 7d4f004..d44e572 100644 --- a/software/src/led.cpp +++ b/software/src/led.cpp @@ -7,21 +7,58 @@ using namespace std; Led::Led(void) { + index = 0; + duration = 0; +} + +void Led::loop_rainbow(void) { + fill_rainbow(leds, NUM_LEDS, index, 7); +} + +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); +} + +void Led::loop_steady(void) { + fill_solid(leds, NUM_LEDS, CRGB::Orange); +} + +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_dispense; + else if (num == 2) refresh = &Led::loop_confetti; + else if (num == 3) refresh = &Led::loop_rainbow; } void Led::setup(void) { FastLED.addLeds(leds, NUM_LEDS); - leds[0] = CRGB::Red; - leds[1] = CRGB::Green; - leds[2] = CRGB::Blue; - leds[NUM_LEDS-6] = CRGB::White; - leds[NUM_LEDS-5] = CRGB::White; - leds[NUM_LEDS-4] = CRGB::White; - leds[NUM_LEDS-3] = CRGB::White; - leds[NUM_LEDS-2] = CRGB::White; - leds[NUM_LEDS-1] = CRGB::White; + FastLED.setBrightness(96); + FastLED.setCorrection(TypicalLEDStrip); + fill_solid(leds, NUM_LEDS, CRGB::White); FastLED.show(); + refresh = &Led::loop_steady; xTaskCreate( &cTaskWrapper, /* Task function. */ "ledAnimationTask", /* String with name of task. */ @@ -33,7 +70,19 @@ void Led::setup(void) { void Led::animationTask(void* parameters) { while(true) { - + if (refresh != nullptr) { + (*this.*refresh)(); + FastLED.show(); +// EVERY_N_MILLISECONDS ( 20) { index++; } + index++; + if (duration == 0) { + refresh = &Led::loop_steady; + } else if (duration == UINT16_MAX) { + } else { + duration--; + } + EVERY_N_SECONDS(300) { changeAnimation(3, 200); } + } vTaskDelay(50 / portTICK_PERIOD_MS); } Serial.println("LED animation end!"); diff --git a/software/src/led.h b/software/src/led.h index 5efc322..23d4d4d 100644 --- a/software/src/led.h +++ b/software/src/led.h @@ -6,15 +6,24 @@ #include "hardware.h" + class Led { public: Led(void); void setup(void); + void changeAnimation(uint8_t num, uint16_t duration); private: CRGB leds[NUM_LEDS]; void animationTask(void*); static void cTaskWrapper(void*); TaskHandle_t ledTaskHandle; + void (Led::*refresh)(void) = NULL; + void loop_steady(void); + void loop_rainbow(void); + void loop_confetti(void); + void loop_dispense(void); + uint16_t duration; + uint8_t index; }; #endif /* _LED_H */ diff --git a/software/src/main.cpp b/software/src/main.cpp index 3ad83af..5b64777 100644 --- a/software/src/main.cpp +++ b/software/src/main.cpp @@ -50,7 +50,7 @@ void setup() } void loop() -{ +{/* // turn the LED on (HIGH is the voltage level) digitalWrite(LED_BUILTIN, HIGH); // wait for a second @@ -59,7 +59,7 @@ void loop() digitalWrite(LED_BUILTIN, LOW); // wait for a second delay(1000); - -delay(100); +*/ +delay(5000); } diff --git a/software/src/shelf.cpp b/software/src/shelf.cpp index d8400bf..5850f82 100644 --- a/software/src/shelf.cpp +++ b/software/src/shelf.cpp @@ -1,10 +1,12 @@ #include "shelf.h" #include "item.h" #include "pusher.h" +#include "led.h" using namespace std; extern Pusher pusher; +extern Led led; Shelf::Shelf(int size) : num_items(size) { @@ -25,5 +27,10 @@ void Shelf::reload(void) { void Shelf::dispense(int num) { item.at(num)->quantity--; + led.changeAnimation(1, 0); pusher.push(num); + delay(500); + led.changeAnimation(2, 0); + delay(7000); + led.changeAnimation(0, 0); }