-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlicker.cpp-x
100 lines (81 loc) · 2.4 KB
/
Flicker.cpp-x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright Chris Marrin 2021
//
// Flicker Class
//
// This class flickers the LEDs according to input params
#include "Flicker.h"
#include "Interpreter.h"
const Flicker::Speed Flicker::_speedTable[ ] = {
{ 250, 255, 40 },
{ 220, 250, 30 },
{ 180, 240, 20 },
{ 150, 200, 20 },
{ 70, 120, 20 },
{ 20, 80, 20 },
{ 8, 40, 10 },
{ 2, 5, 10 },
};
Flicker::Flicker(Adafruit_NeoPixel* pixels)
: Effect(pixels)
{
_leds = new LED[_pixels->numPixels()];
}
Flicker::~Flicker()
{
delete [ ] _leds;
_leds = nullptr;
}
bool
Flicker::init(uint8_t cmd, const uint8_t* buf, uint32_t size)
{
Effect::init(cmd, buf, size);
if (size < 4 || !_buf) {
Serial.println("***** Flicker: Buffer not passed");
return false;
}
if (_buf[3] > 7) {
Serial.println("***** Flicker: speed out of range");
return false;
}
_color = Color(_buf[0], _buf[1], _buf[2]);
Serial.print("Flicker started: hue=");
Serial.print(_buf[0]);
Serial.print(", sat=");
Serial.print(_buf[1]);
Serial.print(", val=");
Serial.print(_buf[2]);
Serial.print(", speed=");
Serial.println(_buf[3]);
return true;
}
int32_t
Flicker::loop()
{
for (uint32_t i = 0; i < _pixels->numPixels(); i++) {
LED& led = _leds[i];
led.off += led.inc;
// Check for going past lim (increasing) or below 0 (decreasing)
if (led.inc > 0) {
if (led.off >= led.lim) {
led.off = led.lim;
led.inc = -led.inc;
}
} else if (led.off <= 0) {
// We are done with the throb, select a new inc and lim
led.off = 0;
// Increment inc value for each step, in 1/10s
led.inc = arly::Interpreter::randomFloat(IncMin, IncMax);
// Random number of steps to throb up and down
led.lim = led.inc + arly::Interpreter::randomFloat(_speedTable[_buf[3]].stepsMin, _speedTable[_buf[3]].stepsMax);
}
// What is the relative brightness. led.off always starts at 0, so that is
// a brightness multiplier of 1. at its limit it is equal to led.lim, so
// that is a multiplier of 1 + led.lim / 255.
float brightness = BrightnessMin + (led.off / 255);
float val = _color.val() * brightness;
val = max(val, ValMin);
_pixels->setPixelColor(i, Color(_color.hue(), _color.sat(), val).rgb());
}
_pixels->show();
return _speedTable[_buf[3]].delay;
}