-
Notifications
You must be signed in to change notification settings - Fork 233
/
magic_home.c
275 lines (230 loc) · 8.67 KB
/
magic_home.c
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
/*
* This is an example of an rgb led strip using Magic Home wifi controller
*
* Debugging printf statements and UART are disabled below because it interfere with mutipwm
* you can uncomment them for debug purposes
*
* more info about the controller and flashing can be found here:
* https://github.com/arendst/Sonoff-Tasmota/wiki/MagicHome-LED-strip-controller
*
* Contributed April 2018 by https://github.com/PCSaito
*/
#include <stdio.h>
#include <espressif/esp_wifi.h>
#include <espressif/esp_sta.h>
#include <esp/uart.h>
#include <esp8266.h>
#include <FreeRTOS.h>
#include <task.h>
#include <math.h>
#include <homekit/homekit.h>
#include <homekit/characteristics.h>
#include <wifi_config.h>
#include "multipwm.h"
#define LPF_SHIFT 4 // divide by 16
#define LPF_INTERVAL 10 // in milliseconds
#define RED_PWM_PIN 5
#define GREEN_PWM_PIN 12
#define BLUE_PWM_PIN 13
#define LED_RGB_SCALE 255 // this is the scaling factor used for color conversion
typedef union {
struct {
uint16_t blue;
uint16_t green;
uint16_t red;
uint16_t white;
};
uint64_t color;
} rgb_color_t;
// Color smoothing variables
rgb_color_t current_color = { { 0, 0, 0, 0 } };
rgb_color_t target_color = { { 0, 0, 0, 0 } };
// Global variables
float led_hue = 0; // hue is scaled 0 to 360
float led_saturation = 59; // saturation is scaled 0 to 100
float led_brightness = 100; // brightness is scaled 0 to 100
bool led_on = false; // on is boolean on or off
//http://blog.saikoled.com/post/44677718712/how-to-convert-from-hsi-to-rgb-white
static void hsi2rgb(float h, float s, float i, rgb_color_t* rgb) {
int r, g, b;
while (h < 0) { h += 360.0F; }; // cycle h around to 0-360 degrees
while (h >= 360) { h -= 360.0F; };
h = 3.14159F*h / 180.0F; // convert to radians.
s /= 100.0F; // from percentage to ratio
i /= 100.0F; // from percentage to ratio
s = s > 0 ? (s < 1 ? s : 1) : 0; // clamp s and i to interval [0,1]
i = i > 0 ? (i < 1 ? i : 1) : 0; // clamp s and i to interval [0,1]
//i = i * sqrt(i); // shape intensity to have finer granularity near 0
if (h < 2.09439) {
r = LED_RGB_SCALE * i / 3 * (1 + s * cos(h) / cos(1.047196667 - h));
g = LED_RGB_SCALE * i / 3 * (1 + s * (1 - cos(h) / cos(1.047196667 - h)));
b = LED_RGB_SCALE * i / 3 * (1 - s);
}
else if (h < 4.188787) {
h = h - 2.09439;
g = LED_RGB_SCALE * i / 3 * (1 + s * cos(h) / cos(1.047196667 - h));
b = LED_RGB_SCALE * i / 3 * (1 + s * (1 - cos(h) / cos(1.047196667 - h)));
r = LED_RGB_SCALE * i / 3 * (1 - s);
}
else {
h = h - 4.188787;
b = LED_RGB_SCALE * i / 3 * (1 + s * cos(h) / cos(1.047196667 - h));
r = LED_RGB_SCALE * i / 3 * (1 + s * (1 - cos(h) / cos(1.047196667 - h)));
g = LED_RGB_SCALE * i / 3 * (1 - s);
}
rgb->red = (uint8_t) r;
rgb->green = (uint8_t) g;
rgb->blue = (uint8_t) b;
}
void led_identify_task(void *_args) {
printf("LED identify\n");
rgb_color_t color = target_color;
rgb_color_t black_color = { { 0, 0, 0, 0 } };
rgb_color_t white_color = { { 128, 128, 128, 128 } };
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
target_color = white_color;
vTaskDelay(100 / portTICK_PERIOD_MS);
target_color = black_color;
vTaskDelay(100 / portTICK_PERIOD_MS);
}
vTaskDelay(250 / portTICK_PERIOD_MS);
}
target_color = color;
vTaskDelete(NULL);
}
void led_identify(homekit_value_t _value) {
xTaskCreate(led_identify_task, "LED identify", 128, NULL, 2, NULL);
}
homekit_value_t led_on_get() {
return HOMEKIT_BOOL(led_on);
}
void led_on_set(homekit_value_t value) {
if (value.format != homekit_format_bool) {
// printf("Invalid on-value format: %d\n", value.format);
return;
}
led_on = value.bool_value;
}
homekit_value_t led_brightness_get() {
return HOMEKIT_INT(led_brightness);
}
void led_brightness_set(homekit_value_t value) {
if (value.format != homekit_format_int) {
// printf("Invalid brightness-value format: %d\n", value.format);
return;
}
led_brightness = value.int_value;
}
homekit_value_t led_hue_get() {
return HOMEKIT_FLOAT(led_hue);
}
void led_hue_set(homekit_value_t value) {
if (value.format != homekit_format_float) {
// printf("Invalid hue-value format: %d\n", value.format);
return;
}
led_hue = value.float_value;
}
homekit_value_t led_saturation_get() {
return HOMEKIT_FLOAT(led_saturation);
}
void led_saturation_set(homekit_value_t value) {
if (value.format != homekit_format_float) {
// printf("Invalid sat-value format: %d\n", value.format);
return;
}
led_saturation = value.float_value;
}
homekit_characteristic_t name = HOMEKIT_CHARACTERISTIC_(NAME, "LED Strip");
homekit_accessory_t *accessories[] = {
HOMEKIT_ACCESSORY(.id = 1, .category = homekit_accessory_category_lightbulb, .services = (homekit_service_t*[]) {
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics = (homekit_characteristic_t*[]) {
&name,
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "PCSaito"),
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "737A2BAFF19E"),
HOMEKIT_CHARACTERISTIC(MODEL, "Magic Home Led Strip"),
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.2"),
HOMEKIT_CHARACTERISTIC(IDENTIFY, led_identify),
NULL
}),
HOMEKIT_SERVICE(LIGHTBULB, .primary = true, .characteristics = (homekit_characteristic_t*[]) {
HOMEKIT_CHARACTERISTIC(NAME, "LED Strip"),
HOMEKIT_CHARACTERISTIC(
ON, true,
.getter = led_on_get,
.setter = led_on_set
),
HOMEKIT_CHARACTERISTIC(
BRIGHTNESS, 100,
.getter = led_brightness_get,
.setter = led_brightness_set
),
HOMEKIT_CHARACTERISTIC(
HUE, 0,
.getter = led_hue_get,
.setter = led_hue_set
),
HOMEKIT_CHARACTERISTIC(
SATURATION, 0,
.getter = led_saturation_get,
.setter = led_saturation_set
),
NULL
}),
NULL
}),
NULL
};
homekit_server_config_t config = {
.accessories = accessories,
.password = "190-11-978" //changed tobe valid
};
IRAM void multipwm_task(void *pvParameters) {
const TickType_t xPeriod = pdMS_TO_TICKS(LPF_INTERVAL);
TickType_t xLastWakeTime = xTaskGetTickCount();
uint8_t pins[] = {RED_PWM_PIN, GREEN_PWM_PIN, BLUE_PWM_PIN};
pwm_info_t pwm_info;
pwm_info.channels = 3;
multipwm_init(&pwm_info);
multipwm_set_freq(&pwm_info, 65535);
for (uint8_t i=0; i<pwm_info.channels; i++) {
multipwm_set_pin(&pwm_info, i, pins[i]);
}
while(1) {
if (led_on) {
// convert HSI to RGBW
hsi2rgb(led_hue, led_saturation, led_brightness, &target_color);
} else {
target_color.red = 0;
target_color.green = 0;
target_color.blue = 0;
}
current_color.red += ((target_color.red * 256) - current_color.red) >> LPF_SHIFT ;
current_color.green += ((target_color.green * 256) - current_color.green) >> LPF_SHIFT ;
current_color.blue += ((target_color.blue * 256) - current_color.blue) >> LPF_SHIFT ;
multipwm_stop(&pwm_info);
multipwm_set_duty(&pwm_info, 0, current_color.red);
multipwm_set_duty(&pwm_info, 1, current_color.green);
multipwm_set_duty(&pwm_info, 2, current_color.blue);
multipwm_start(&pwm_info);
vTaskDelayUntil(&xLastWakeTime, xPeriod);
}
}
void on_wifi_ready() {
homekit_server_init(&config);
}
void user_init(void) {
//uart_set_baud(0, 115200);
// This example shows how to use same firmware for multiple similar accessories
// without name conflicts. It uses the last 3 bytes of accessory's MAC address as
// accessory name suffix.
uint8_t macaddr[6];
sdk_wifi_get_macaddr(STATION_IF, macaddr);
int name_len = snprintf(NULL, 0, "LED Strip-%02X%02X%02X", macaddr[1], macaddr[2], macaddr[3]);
char *name_value = malloc(name_len + 1);
snprintf(name_value, name_len + 1, "LED Strip-%02X%02X%02X", macaddr[1], macaddr[2], macaddr[3]);
name.value = HOMEKIT_STRING(name_value);
wifi_config_init("MagicHome Led Strip", NULL, on_wifi_ready);
xTaskCreate(multipwm_task, "multipwm", 256, NULL, 2, NULL);
}