-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLight.h
159 lines (147 loc) · 4.33 KB
/
Light.h
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef LIGHT_H
#define LIGHT_H
#define NUM_PARAMS 4
#include <Arduino.h>
#include <SPI.h>
#include <FastLED.h>
#include "config.h"
#include <iostream>
#include <ArduinoJson.h>
#include "StreamPrint.h"
class Light {
public:
struct State {
String name;
int* params = nullptr;
String color = "";
String program = "";
int onoff = -1;
int count = -1;
};
private:
CRGB** _leds;
State _state;
CRGB _color;
int _params [NUM_PARAMS];
int _num_leds;
int _offset;
int _last_brightness;
bool _onoff;
String _name;
unsigned int _count;
unsigned int _index;
int _prog_solid(int x);
int _prog_chase(int x);
int _prog_blink(int x);
int _prog_fade(int x);
int _prog_warm(int x);
int _prog_xmas(int x);
int _prog_lfo(int x);
int _prog_fadeout(int x);
int _prog_fadein(int x);
int _prog_longfade(int x);
int (Light::*_prog)(int x);
public:
Light() :
_color { CRGB::White },
_onoff { 0 },
_num_leds { 0 },
_name { "light" },
_prog { &Light::_prog_solid },
_count { 0 },
_params { 1, 0, 0, 0 }
{ };
Light(String name, CRGB* leds, int offset, int num_leds, int reverse=0) :
_name { name },
_num_leds { num_leds },
_color { CRGB::White },
_onoff { 0 },
_count { 0 },
_offset { offset },
_prog { &Light::_prog_solid },
_params { 1, 0, 0, 0 }
{
_leds = new CRGB*[num_leds];
for (int i=0; i<num_leds; i++) {
_leds[i] = reverse? &leds[offset+num_leds-i-1] : &leds[offset+i];
}
};
Light(String name, CRGB** leds) :
_name { name },
_color { CRGB::White },
_count { 0 },
_offset { 0 },
_onoff { 0 },
_num_leds { sizeof(leds) },
_prog { &Light::_prog_solid },
_params { 1, 0, 0, 0 }
{
_leds = new CRGB*[_num_leds];
for (int i=0; i<sizeof(leds); i++) {
_leds[i] = leds[i];
}
};
Light(CRGB* leds, JsonObject jsonLight) :
_name { jsonLight["name"].as<String>() },
_onoff { 0 },
_offset { 0 },
_count { 0 },
_color { CRGB::White },
_prog { &Light::_prog_solid },
_params { 1, 0, 0, 0 }
{
if (jsonLight["leds"]) {
JsonArray jsonLeds = jsonLight["leds"];
int led_count = jsonLeds.size();
_leds = new CRGB*[led_count];
for (int i=0; i<led_count; i++) {
_leds[i] = &leds[jsonLeds[i].as<int>()];
}
_num_leds = led_count;
} else if (jsonLight["led_count"]) {
int led_count = jsonLight["led_count"];
int led_offset = jsonLight["led_offset"];
int reverse = jsonLight["reverse"];
_leds = new CRGB*[led_count];
for (int i=0; i<led_count; i++) {
_leds[i] = reverse? &leds[led_offset+led_count-i-1] : &leds[led_offset+i];
}
_num_leds = led_count;
} else {
// bad
}
if (jsonLight["program"]) setProgram(jsonLight["program"]);
if (jsonLight["color"]) setColor(jsonLight["color"]);
if (jsonLight["params"]) {
JsonArray jsonParams = jsonLight["params"];
int numParams = jsonParams.size();
for (int i=0; i<numParams; i++) {
_params[i] = jsonParams[i].as<int>();
}
}
};
String getName();
void turnOn();
void turnOff();
void turn(int onoff);
void blink();
void toggle();
void setHue(int);
void setRgb(CRGB);
void setHsv(CHSV);
void setHsv(int hue, int sat, int val);
void setBrightness(int);
void setSaturation(int);
void setProgram(String programName);
void setParam(int p, int v);
void setParams(int* params);
void setColor(String colorName);
void setState(State* state);
int getParam(int p);
CRGB getRgb();
CHSV getHsv();
void update();
};
uint8_t nblendU8TowardU8(uint8_t cur, const uint8_t target, uint8_t x);
CRGB fadeTowardColor(CRGB cur, CRGB target, uint8_t x);
#endif // LIGHT_H