VizBlocks
LEDBehaviours.h
Go to the documentation of this file.
1 #ifndef LED_BEHAVIOUR_h
2 #define LED_BEHAVIOUR_h
3 #include "Arduino.h"
4 #include "Behaviours.h"
5 
6 #include <Adafruit_NeoPixel.h>
7 
8 class NumLEDs : public Behaviour {
9  Adafruit_NeoPixel* _strip;
10  uint32_t _color;
11 
12 public:
13  NumLEDs(Adafruit_NeoPixel* strip, String name = "NumLEDs", uint32_t color=0xFFFFFFFF) :
14  Behaviour(name), _strip(strip), _color(color){ }
15  char* args() {return "<int num_leds>"; };
16 
17  String start(String args) {
18  int val = args.toInt();
19  //Always clear the strip first
20  _strip->clear();
21  if( val > 0 ) {
22  _strip->fill(_color, 0, val);
23  }
24  _strip->show();
25  return "";
26  }
27 
28 };
29 
30 class BrightnessLEDs : public Behaviour {
31  Adafruit_NeoPixel* _strip;
32  uint32_t _hue;
33  uint32_t _sat;
34 
35 public:
36  BrightnessLEDs(Adafruit_NeoPixel* strip, String name = "BrightnessLEDs", uint32_t hue=0, uint32_t sat=0) :
37  Behaviour(name), _strip(strip), _hue(hue), _sat(sat){ }
38  char* args() {return "<int brightness>"; };
39 
40  String start(String args) {
41  int val = args.toInt();
42  _strip->clear();
43  _strip->fill(_strip->ColorHSV(_hue,_sat,val));
44  _strip->show();
45  return "";
46  }
47 
48 };
49 
50 class BreathingLEDs : public Behaviour {
51  Adafruit_NeoPixel* _strip;
52  uint _hue;
53  uint _sat;
54  int32_t _current = 0;
55  //Allows us to have slightly slower behaviours on the go...
56  int _factor = 4;
57  int _rate = 0;
58  int _direction = 1;
59 
60 public:
61  BreathingLEDs(Adafruit_NeoPixel* strip, String name = "BreathingLEDs", uint32_t hue=0, uint32_t sat=0) :
62  Behaviour(name), _strip(strip), _hue(hue * 255), _sat(sat) { }
63  char* args() {return "<int rate (1-255ish)>"; };
64 
65  String start(String args) {
66  _current = 0;
67  _direction = 1;
68  _running = true;
69  int val = args.toInt();
70  _rate = val;
71  return "";
72  }
73 
74  void update() {
75  if( _rate <= 0 ) {
76  _strip->fill(0);
77  _strip->show();
78  return;
79  }
80  _current = _current + (_rate * _direction);
81  if( _current < 0 ) {
82  _current = 0;
83  _direction = 1;
84  }
85  if( _current > 255 * _factor ) {
86  _current = 255 * _factor;
87  _direction = -1;
88  }
89  _strip->fill(_strip->ColorHSV(_hue,_sat,_current / _factor));
90  _strip->show();
91 
92  }
93 
94 };
95 
96 
97 #endif
Behaviour::name
virtual String name()
Definition: Behaviours.h:28
BrightnessLEDs::args
char * args()
Definition: LEDBehaviours.h:38
Behaviour
Definition: Behaviours.h:4
NumLEDs::NumLEDs
NumLEDs(Adafruit_NeoPixel *strip, String name="NumLEDs", uint32_t color=0xFFFFFFFF)
Definition: LEDBehaviours.h:13
BreathingLEDs
Definition: LEDBehaviours.h:50
BreathingLEDs::BreathingLEDs
BreathingLEDs(Adafruit_NeoPixel *strip, String name="BreathingLEDs", uint32_t hue=0, uint32_t sat=0)
Definition: LEDBehaviours.h:61
BrightnessLEDs::start
String start(String args)
Definition: LEDBehaviours.h:40
NumLEDs
Definition: LEDBehaviours.h:8
BreathingLEDs::update
void update()
Definition: LEDBehaviours.h:74
BreathingLEDs::start
String start(String args)
Definition: LEDBehaviours.h:65
NumLEDs::start
String start(String args)
Definition: LEDBehaviours.h:17
Behaviours.h
Behaviour::_running
boolean _running
Definition: Behaviours.h:9
BreathingLEDs::args
char * args()
Definition: LEDBehaviours.h:63
NumLEDs::args
char * args()
Definition: LEDBehaviours.h:15
BrightnessLEDs::BrightnessLEDs
BrightnessLEDs(Adafruit_NeoPixel *strip, String name="BrightnessLEDs", uint32_t hue=0, uint32_t sat=0)
Definition: LEDBehaviours.h:36
BrightnessLEDs
Definition: LEDBehaviours.h:30