-
Notifications
You must be signed in to change notification settings - Fork 2
/
Example05_WS2812_Neopixel.ino
342 lines (279 loc) · 7.71 KB
/
Example05_WS2812_Neopixel.ino
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Example05_WS2812_Neopixel.ino
*
* Created on: 2020-10-01
* Author: Juergen Fink
* Thanks to all the other helpful people commenting here.
*
* This example allows to change brightness and color of a connected neopixel strip/matrix
*
* You should:
* 1. read and use the Example01_TemperatureSensor with detailed comments
* to know the basic concept and usage of this library before other examples。
* 2. erase the full flash or call homekit_storage_reset() in setup()
* to remove the previous HomeKit pairing storage and
* enable the pairing with the new accessory of this new HomeKit example.
*/
#include <Arduino.h>
#include <arduino_homekit_server.h>
#include <Adafruit_NeoPixel.h>
#include "wifi_info.h"
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
#define NEOPIN 2
#define NUMPIXELS 30
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEOPIN, NEO_GRB + NEO_KHZ800);
bool received_sat = false;
bool received_hue = false;
bool is_on = false;
//彩虹开关
bool rainbow_on = false;
//闪烁开关
bool rainbow_flow_on = false;
float current_brightness = 100.0;
float current_sat = 0.0;
float current_hue = 0.0;
int ii = 0;
int rgb_colors[3];
void setup() {
Serial.begin(115200);
wifi_connect(); // in wifi_info.h
pixels.begin();
for(int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, 0, 0, 0);
}
pixels.show();
delay(1000);
rgb_colors[0] = 255;
rgb_colors[1] = 255;
rgb_colors[2] = 255;
my_homekit_setup();
}
void loop() {
my_homekit_loop();
//闪烁实现
if (rainbow_flow_on){
pixels.setPixelColor(ii, 0, 0, 0);
pixels.setPixelColor(NUMPIXELS-ii, 0, 0, 0);
pixels.show();
ii += 1;
if(ii>=NUMPIXELS){
ii=0;
}
delay(30);
updateColor();
delay(30);
}
delay(10);
}
//==============================
// HomeKit setup and loop
//==============================
// access your HomeKit characteristics defined in my_accessory.c
extern "C" homekit_server_config_t accessory_config;
extern "C" homekit_characteristic_t cha_on;
extern "C" homekit_characteristic_t cha_bright;
extern "C" homekit_characteristic_t cha_sat;
extern "C" homekit_characteristic_t cha_hue;
extern "C" homekit_characteristic_t cha_rainbow_on;
extern "C" homekit_characteristic_t cha_rainbow_flow;
static uint32_t next_heap_millis = 0;
void my_homekit_setup() {
cha_on.setter = set_on;
cha_bright.setter = set_bright;
cha_sat.setter = set_sat;
cha_hue.setter = set_hue;
cha_rainbow_on.setter = set_rainbow;
cha_rainbow_flow.setter = set_rainbow_flow;
arduino_homekit_setup(&accessory_config);
}
void my_homekit_loop() {
arduino_homekit_loop();
const uint32_t t = millis();
if (t > next_heap_millis) {
// show heap info every 5 seconds
next_heap_millis = t + 5 * 1000;
LOG_D("Free heap: %d, HomeKit clients: %d",
ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
}
}
void set_on(const homekit_value_t v) {
bool on = v.bool_value;
cha_on.value.bool_value = on; //sync the value
if(on) {
is_on = true;
Serial.println("On");
} else {
is_on = false;
Serial.println("Off");
}
updateColor();
}
void set_rainbow(const homekit_value_t v) {
bool on = v.bool_value;
cha_rainbow_on.value.bool_value = on; //sync the value
if(on) {
rainbow_on = true;
Serial.println("Rainbow On");
} else {
rainbow_on = false;
// is_on = false;
Serial.println("Rainbow Off");
}
updateColor();
}
void set_rainbow_flow(const homekit_value_t v) {
bool on = v.bool_value;
cha_rainbow_flow.value.bool_value = on; //sync the value
if(on) {
rainbow_flow_on = true;
Serial.println("RainbowFlow On");
} else {
rainbow_flow_on = false;
// is_on = false;
Serial.println("RainbowFlow Off");
}
updateColor();
}
void set_hue(const homekit_value_t v) {
Serial.println("set_hue");
float hue = v.float_value;
cha_hue.value.float_value = hue; //sync the value
current_hue = hue;
received_hue = true;
updateColor();
}
void set_sat(const homekit_value_t v) {
Serial.println("set_sat");
float sat = v.float_value;
cha_sat.value.float_value = sat; //sync the value
current_sat = sat;
received_sat = true;
updateColor();
}
void set_bright(const homekit_value_t v) {
Serial.println("set_bright");
int bright = v.int_value;
cha_bright.value.int_value = bright; //sync the value
current_brightness = bright;
updateColor();
}
void updateColor()
{
if(is_on)
{
//彩虹效果,避免与其他发生冲突
if (rainbow_on){
if(received_hue && received_sat)
{
HSV2RGB(current_hue, current_sat, current_brightness);
received_hue = false;
received_sat = false;
}
int b = map(current_brightness,0, 100,75, 255);
Serial.println(b);
pixels.setBrightness(b);
for(int i = 0; i < NUMPIXELS; i++)
{
if(i%7==0){
pixels.setPixelColor(i, pixels.Color(255,0,0));
}
else if(i%7==1){
pixels.setPixelColor(i, pixels.Color(255,165,0));
}
else if(i%7==2){
pixels.setPixelColor(i, pixels.Color(255,255,0));
}
else if(i%7==3){
pixels.setPixelColor(i, pixels.Color(0,255,0));
}
else if(i%7==4){
pixels.setPixelColor(i, pixels.Color(0,127,255));
}
else if(i%7==5){
pixels.setPixelColor(i, pixels.Color(0,0,255));
}
else{
pixels.setPixelColor(i, pixels.Color(139,0,255));
}
}
pixels.show();
}
else{
if(received_hue && received_sat)
{
HSV2RGB(current_hue, current_sat, current_brightness);
received_hue = false;
received_sat = false;
}
int b = map(current_brightness,0, 100,75, 255);
Serial.println(b);
pixels.setBrightness(b);
for(int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(rgb_colors[0],rgb_colors[1],rgb_colors[2]));
}
pixels.show();
}
}
else if(!is_on) //lamp - switch to off
{
Serial.println("is_on == false");
pixels.setBrightness(0);
for(int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(0,0,0));
}
pixels.show();
}
}
void HSV2RGB(float h,float s,float v) {
int i;
float m, n, f;
s/=100;
v/=100;
if(s==0){
rgb_colors[0]=rgb_colors[1]=rgb_colors[2]=round(v*255);
return;
}
h/=60;
i=floor(h);
f=h-i;
if(!(i&1)){
f=1-f;
}
m=v*(1-s);
n=v*(1-s*f);
switch (i) {
case 0: case 6:
rgb_colors[0]=round(v*255);
rgb_colors[1]=round(n*255);
rgb_colors[2]=round(m*255);
break;
case 1:
rgb_colors[0]=round(n*255);
rgb_colors[1]=round(v*255);
rgb_colors[2]=round(m*255);
break;
case 2:
rgb_colors[0]=round(m*255);
rgb_colors[1]=round(v*255);
rgb_colors[2]=round(n*255);
break;
case 3:
rgb_colors[0]=round(m*255);
rgb_colors[1]=round(n*255);
rgb_colors[2]=round(v*255);
break;
case 4:
rgb_colors[0]=round(n*255);
rgb_colors[1]=round(m*255);
rgb_colors[2]=round(v*255);
break;
case 5:
rgb_colors[0]=round(v*255);
rgb_colors[1]=round(m*255);
rgb_colors[2]=round(n*255);
break;
}
}