Browse Source

add led functionality

main
Hendrik Langer 7 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 int PROGMEM SERVO_PINS[] = {23, 22, 1, 3, 21, 19};
static constexpr uint8_t PROGMEM LED_PIN = 4; 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 LED_TYPE TM1829
#define COLOR_ORDER BRG #define COLOR_ORDER BRG

69
software/src/led.cpp

@ -7,21 +7,58 @@
using namespace std; using namespace std;
Led::Led(void) { 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) { void Led::setup(void) {
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS); FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
leds[0] = CRGB::Red; FastLED.setBrightness(96);
leds[1] = CRGB::Green; FastLED.setCorrection(TypicalLEDStrip);
leds[2] = CRGB::Blue; fill_solid(leds, NUM_LEDS, CRGB::White);
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.show(); FastLED.show();
refresh = &Led::loop_steady;
xTaskCreate( xTaskCreate(
&cTaskWrapper, /* Task function. */ &cTaskWrapper, /* Task function. */
"ledAnimationTask", /* String with name of task. */ "ledAnimationTask", /* String with name of task. */
@ -33,7 +70,19 @@ void Led::setup(void) {
void Led::animationTask(void* parameters) { void Led::animationTask(void* parameters) {
while(true) { 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); vTaskDelay(50 / portTICK_PERIOD_MS);
} }
Serial.println("LED animation end!"); Serial.println("LED animation end!");

9
software/src/led.h

@ -6,15 +6,24 @@
#include "hardware.h" #include "hardware.h"
class Led { class Led {
public: public:
Led(void); Led(void);
void setup(void); void setup(void);
void changeAnimation(uint8_t num, uint16_t duration);
private: private:
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
void animationTask(void*); void animationTask(void*);
static void cTaskWrapper(void*); static void cTaskWrapper(void*);
TaskHandle_t ledTaskHandle; 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 */ #endif /* _LED_H */

6
software/src/main.cpp

@ -50,7 +50,7 @@ void setup()
} }
void loop() void loop()
{ {/*
// turn the LED on (HIGH is the voltage level) // turn the LED on (HIGH is the voltage level)
digitalWrite(LED_BUILTIN, HIGH); digitalWrite(LED_BUILTIN, HIGH);
// wait for a second // wait for a second
@ -59,7 +59,7 @@ void loop()
digitalWrite(LED_BUILTIN, LOW); digitalWrite(LED_BUILTIN, LOW);
// wait for a second // wait for a second
delay(1000); delay(1000);
*/
delay(100); delay(5000);
} }

7
software/src/shelf.cpp

@ -1,10 +1,12 @@
#include "shelf.h" #include "shelf.h"
#include "item.h" #include "item.h"
#include "pusher.h" #include "pusher.h"
#include "led.h"
using namespace std; using namespace std;
extern Pusher pusher; extern Pusher pusher;
extern Led led;
Shelf::Shelf(int size) Shelf::Shelf(int size)
: num_items(size) { : num_items(size) {
@ -25,5 +27,10 @@ void Shelf::reload(void) {
void Shelf::dispense(int num) { void Shelf::dispense(int num) {
item.at(num)->quantity--; item.at(num)->quantity--;
led.changeAnimation(1, 0);
pusher.push(num); pusher.push(num);
delay(500);
led.changeAnimation(2, 0);
delay(7000);
led.changeAnimation(0, 0);
} }

Loading…
Cancel
Save