RSeries astromech firmware
NeoPixelPSI.h
Go to the documentation of this file.
1 #ifndef NeoPixelPSI_h
2 #define NeoPixelPSI_h
3 
4 #include "ReelTwo.h"
5 #include "core/SetupEvent.h"
6 #include "core/AnimatedEvent.h"
7 #include <Adafruit_NeoPixel.h>
8 
9 #define SWIPE_SPEED 75
10 #define SWIPE_DELAY 1000
11 #define STICKINESS 0
12 #define BRIGHTNESS 28
13 
14 #ifdef USE_DEBUG
15  #define PSI_DEBUG
16 #endif
17 
40 class NeoPixelPSI :
42 {
43  public:
44  Adafruit_NeoPixel leds;
45 
47  {
48  kNormalVal = 0
49  };
50 
51  enum Sequence
52  {
53  kNormal = 0,
54  kSolid = 1,
55  kHeart = 2,
56  kMalf = 3,
57  };
58 
59  enum PSIID
60  {
62  kFrontPSI = 1,
64  kRearPSI = 2,
65  };
66 
67  virtual void setup() override
68  {
69  }
70 
76  NeoPixelPSI(int psi_pin, int psi_size = 5)
77  {
78  leds.updateLength(num_leds[psi_size]);
79  leds.updateType(NEO_GRB + NEO_KHZ800);
80  leds.setPin(psi_pin);
81  switch (psi_size) {
82  case 8:
83  memcpy(LEDmap, LEDmap8, sizeof(LEDmap8));
84  break;
85  case 7:
86  for(int x = 0; x < psi_size ; x++ ){
87  memcpy(LEDmap[x], LEDmap7[x], sizeof(LEDmap7[x]));
88  }
89  break;
90  case 5:
91  for(int x = 0; x < psi_size ; x++ ){
92  memcpy(LEDmap[x], LEDmap5[x], sizeof(LEDmap5[x]));
93  }
94  break;
95  }
96  grid_size = psi_size;
97  leds.begin();
98  leds.setBrightness(brightness);
99  }
100 
101 
109  void selectEffect(long inputNum)
110  {
111  }
112 
117  virtual void handleCommand(const char* cmd) override
118  {
119  if (*cmd++ == 'P' && *cmd++ == 'S')
120  {
121  long int cmdvalue = 0;
122  const char* c = cmd;
123  while (*c >= '0' && *c <= '9')
124  {
125  cmdvalue = cmdvalue * 10 + (*c++ - '0');
126  }
127  selectEffect(cmdvalue);
128  }
129  }
130 
134  inline void setSequence(Sequence seq = kNormal, uint8_t speedScale = 0, uint8_t numSeconds = 0)
135  {
136  selectEffect((long int)seq * 10000L + (long int)speedScale * 100 + numSeconds);
137  }
138 
139  virtual void animate() {
140  unsigned long currentMillis = millis();
141  if (currentMillis >= fSwipeSpeed)
142  {
143  swipe_main(swipe_position);
144  if (swipe_direction == 0) {
145  if (swipe_position >= grid_size-1) {
146  swipe_direction = 1;
147  fSwipeSpeed = currentMillis + random(sdelay, sdelay*4);
148  } else {
149  fSwipeSpeed = currentMillis + sspeed;
150  swipe_position++;
151  }
152  } else {
153  if (swipe_position <= 0) {
154  swipe_direction = 0;
155  fSwipeSpeed = currentMillis + random(sdelay, sdelay*4);
156  } else {
157  fSwipeSpeed = currentMillis + sspeed;
158  swipe_position--;
159  }
160  }
161  if (swipe_position == int((grid_size/2)+0.5)) {
162  if (random(0,100) < stickiness) {
163  fSwipeSpeed = currentMillis + random(sdelay, sdelay*4);
164  }
165  }
166  }
167  }
168 
169 
177  void set_brightness(int bright) {
178  brightness = bright;
179  leds.setBrightness(brightness);
180  leds.show();
181  }
182 
191  virtual void set_color(int c, int r, int g, int b) {
192  if (c == 1)
193  color_one = leds.Color(r,g,b);
194  else
195  color_two = leds.Color(r,g,b);
196 
197  }
198 
205  void set_speed(int s) {
206  sspeed = s;
207  }
208 
214  void set_delay(int d) {
215  sdelay = d;
216  }
217 
227  void set_stickiness(int s) {
228  stickiness = s;
229  }
230 
231 
232  private:
233  int fPSI = 0;
234  int swipe_direction = 0;
235  int swipe_position = 0;
236  int sspeed = SWIPE_SPEED;
237  int sdelay = SWIPE_DELAY;
238  int brightness = BRIGHTNESS;
239  int stickiness = STICKINESS;
240  int grid_size = 5;
241  int num_leds[9] = {0, 0, 0, 0, 0, 21, 0, 37, 52};
242  uint32_t color_one = leds.Color(255,0,0);
243  uint32_t color_two = leds.Color(0,0, 255);
244  unsigned long fSwipeSpeed;
245  unsigned long fSwipeMillis;
246  int LEDmap[8][8];
247 
248  const int LEDmap5[5][5] PROGMEM = {
249  {99, 0, 1, 2, 99 },
250  {3, 4, 5, 6, 7},
251  {8, 9, 10, 11, 12},
252  {13, 14, 15, 16, 17},
253  {99, 18, 19, 20, 99},
254  };
255 
256  const int LEDmap7[7][7] PROGMEM = {
257  {99, 99, 2, 1, 0, 99, 99},
258  {99, 7, 6, 5, 4, 3, 99},
259  {14, 13, 12, 11, 10, 9, 8},
260  {21, 20, 19, 18, 17, 16, 15},
261  {28, 27, 26, 25, 24, 23, 22},
262  {99, 33, 32, 31, 30, 29, 99 },
263  {99, 99, 36, 35, 34, 99, 99}
264  };
265 
266  const int LEDmap8[8][8] PROGMEM = {
267  {99,99,3,2,1,0,99,99},
268  {99,9,8,7,6,5,4,99},
269  {17,16,15,14,13,12,11,10},
270  {25,24,23,22,21,20,19,18},
271  {33,32,31,30,29,28,27,26},
272  {41,40,39,38,37,36,35,34},
273  {99,47,46,45,44,43,42,99},
274  {99,99,51,50,49,48,99,99}
275  };
276 
277  void swipe_main(uint8_t pos)
278  {
279  uint32_t color;
280  for(int row = 0; row <= grid_size-1 ; row++) {
281  if(swipe_direction == 0)
282  color = color_one;
283  else
284  color = color_two;
285  int led = LEDmap[pos][row];
286  if(led != 99)
287  leds.setPixelColor(led, color);
288  }
289  leds.show();
290  }
291 };
292 
293 #endif
294 
#define SWIPE_DELAY
Definition: NeoPixelPSI.h:10
#define STICKINESS
Definition: NeoPixelPSI.h:11
#define SWIPE_SPEED
Definition: NeoPixelPSI.h:9
#define BRIGHTNESS
Definition: NeoPixelPSI.h:12
Base class for all animated devices.
Definition: AnimatedEvent.h:19
Base class for all command enabled devices.
Definition: CommandEvent.h:18
NeoPixelPSI by Darren Poulson daz@r2djp.co.uk
Definition: NeoPixelPSI.h:42
virtual void handleCommand(const char *cmd) override
Command Prefix: PS.
Definition: NeoPixelPSI.h:117
void setSequence(Sequence seq=kNormal, uint8_t speedScale=0, uint8_t numSeconds=0)
Select the specified effect sequence.
Definition: NeoPixelPSI.h:134
void set_brightness(int bright)
Set the brightness of the PSI.
Definition: NeoPixelPSI.h:177
virtual void animate()
Subclasses must implement this function to run through a single frame of animation/activity.
Definition: NeoPixelPSI.h:139
void set_stickiness(int s)
Set the 'stickiness'.
Definition: NeoPixelPSI.h:227
NeoPixelPSI(int psi_pin, int psi_size=5)
Constructor.
Definition: NeoPixelPSI.h:76
EffectValue
Definition: NeoPixelPSI.h:47
@ kNormalVal
Definition: NeoPixelPSI.h:48
PSIID
Definition: NeoPixelPSI.h:60
@ kRearPSI
Rear PSI ID.
Definition: NeoPixelPSI.h:64
@ kFrontPSI
Front PSI ID.
Definition: NeoPixelPSI.h:62
virtual void set_color(int c, int r, int g, int b)
set the colors of the PSI
Definition: NeoPixelPSI.h:191
Sequence
Definition: NeoPixelPSI.h:52
@ kHeart
Definition: NeoPixelPSI.h:55
@ kNormal
Definition: NeoPixelPSI.h:53
@ kSolid
Definition: NeoPixelPSI.h:54
@ kMalf
Definition: NeoPixelPSI.h:56
Adafruit_NeoPixel leds
Definition: NeoPixelPSI.h:44
void selectEffect(long inputNum)
Select the specified effect using a 32-bit integer.
Definition: NeoPixelPSI.h:109
virtual void setup() override
Subclasses must implement this function to perform any necessary setup that cannot happen in the cons...
Definition: NeoPixelPSI.h:67
void set_delay(int d)
Set the delay between swipes.
Definition: NeoPixelPSI.h:214
void set_speed(int s)
Set the speed the swipe effect moves at.
Definition: NeoPixelPSI.h:205
Utility class to control a VMusic2 module.
Definition: SetupEvent.h:16