Browse Source

add led functionality

main
Hendrik Langer 6 years ago
parent
commit
e48a3b6066
  1. 2
      software/src/hardware.h
  2. 69
      software/src/led.cpp
  3. 9
      software/src/led.h
  4. 6
      software/src/main.cpp
  5. 7
      software/src/shelf.cpp

2
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

69
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<LED_TYPE, LED_PIN, COLOR_ORDER>(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!");

9
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 */

6
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);
}

7
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);
}

Loading…
Cancel
Save