1
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
37
38
39
40
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
73
74
75
76
77
78
79
80
81
85
86
87
88
89
90
91
92
93
94
95
101
107
113
119
125
131
133
134
135
136
137
138
139
140
141
142
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
178
179
180
181
182
183
184
191
192
193
196
197
198
199
200
201
202
209
210
211
214
230
231
232
233
234
235
236
237
238
239
240
243
244
249
250
251
252
253
261
262
267
268
272
273
278
279
285
286
287
295
296
297
298
299
300
301
302
303
306
307
308
/* ... */
#include <stdio.h>
#include <inttypes.h>
#include "driver/gpio.h"
#include "esp_log.h"
#include "iot_button.h"
#include "lightbulb.h"
#include "genie_event.h"7 includes
#if CONFIG_IDF_TARGET_ESP32C3
#define BUTTON_ON_OFF 9
#else
#define BUTTON_ON_OFF 0
#endif
#define BUTTON_ACTIVE_LEVEL 0
static const char *TAG = "board";
static uint32_t dev_on_btn_num = BUTTON_ON_OFF;
extern void user_genie_event_handle(genie_event_t event, void *p_arg);
void button_tap_cb(void* arg)
{
user_genie_event_handle(GENIE_EVT_BUTTON_TAP, NULL);
}{ ... }
static void board_led_init(void)
{
/* ... */
lightbulb_config_t config = {
.type = DRIVER_ESP_PWM,
.driver_conf.pwm.freq_hz = 4000,
.capability.enable_fades = true,
.capability.fades_ms = CONFIG_LIGHT_FADE_PERIOD_MS,
.capability.enable_lowpower = false,
.capability.enable_mix_cct = false,
.capability.enable_status_storage = false,
.capability.mode_mask = COLOR_MODE,
.capability.storage_cb = NULL,
.capability.sync_change_brightness_value = true,
.io_conf.pwm_io.red = CONFIG_LIGHT_GPIO_RED,
.io_conf.pwm_io.green = CONFIG_LIGHT_GPIO_GREEN,
.io_conf.pwm_io.blue = CONFIG_LIGHT_GPIO_BLUE,
.io_conf.pwm_io.cold_cct = CONFIG_LIGHT_GPIO_COLD,
.io_conf.pwm_io.warm_brightness = CONFIG_LIGHT_GPIO_WARM,
.external_limit = NULL,
.gamma_conf = NULL,
.init_status.mode = WORK_COLOR,
.init_status.on = true,
.init_status.hue = 0,
.init_status.saturation = 100,
.init_status.value = 100,
}{...};
/* ... */
ESP_ERROR_CHECK(lightbulb_init(&config));
vTaskDelay(pdMS_TO_TICKS(1000) * 1);
lightbulb_set_switch(true);
button_handle_t dev_on_off_btn = iot_button_create(BUTTON_ON_OFF, BUTTON_ACTIVE_LEVEL);
iot_button_set_evt_cb(dev_on_off_btn, BUTTON_CB_TAP, button_tap_cb, &dev_on_btn_num);
}{ ... }
void board_init(void)
{
board_led_init();
}{ ... }
static esp_err_t board_led_hsl2rgb(uint16_t hue, uint8_t saturation, uint8_t lightness,
uint8_t *red, uint8_t *green, uint8_t *blue)
{
uint16_t hi = (hue / 60) % 6;
uint16_t C = (100 - abs(2 * lightness - 100)) * saturation / 100;
uint16_t M = 100 * (lightness - 0.5 * C) / 100;
uint16_t X = C * (100 - abs((hue * 100 / 60 ) % 200 - 100)) / 100;
switch (hi) {
case 0:
*red = C + M;
*green = X + M;
*blue = M;
break;
...
case 1:
*red = X + M;
*green = C + M;
*blue = M;
break;
...
case 2:
*red = M;
*green = C + M;
*blue = X + M;
break;
...
case 3:
*red = M;
*green = X + M;
*blue = C + M;
break;
...
case 4:
*red = X + M;
*green = M;
*blue = C + M;
break;
...
case 5:
*red = C + M;
*green = M;
*blue = X + M;
break;
...
default:
return ESP_FAIL;...
}{...}
*red = *red * 255 / 100;
*green = *green * 255 / 100;
*blue = *blue * 255 / 100;
return ESP_OK;
}{ ... }
/* ... */
void board_led_hsl(uint8_t elem_index, uint16_t hue, uint16_t saturation, uint16_t lightness)
{
static uint16_t last_hue = 0xFFFF;
static uint16_t last_saturation = 0xFFFF;
static uint16_t last_lightness = 0xFFFF;
ESP_LOGD(TAG, "hue last state %d, state %d", last_hue, hue);
ESP_LOGD(TAG, "saturation last state %d, state %d", last_saturation, saturation);
ESP_LOGD(TAG, "lightness last state %d, state %d", last_lightness, lightness);
if(last_hue != hue || last_saturation != saturation || last_lightness != lightness ) {
last_hue = hue;
last_saturation = saturation;
last_lightness = lightness;
uint16_t actual_hue = (float)last_hue / (UINT16_MAX / 360.0);
uint8_t actual_saturation = (float)last_saturation / (UINT16_MAX / 100.0);
uint8_t actual_lightness = (float)last_lightness / (UINT16_MAX / 100.0);
uint8_t r, g, b;
uint16_t h;
uint8_t s, v;
ESP_LOGD(TAG, "hsl: %d, %d, %d operation", actual_hue, actual_saturation, actual_lightness);
board_led_hsl2rgb(actual_hue, actual_saturation, actual_lightness, &r, &g, &b);
lightbulb_rgb2hsv(r, g, b, &h, &s, &v);
lightbulb_set_hsv(h, s, v);
}{...}
}{ ... }
/* ... */
void board_led_temperature(uint8_t elem_index, uint16_t temperature)
{
static uint16_t last_temperature = 0xFFFF;
ESP_LOGD(TAG, "temperature last state %d, state %d", last_temperature, temperature);
if(last_temperature != temperature) {
last_temperature = temperature;
uint16_t actual_temperature = (float)last_temperature / (UINT16_MAX / 100.0);
ESP_LOGD(TAG, "temperature %d %%%d operation", last_temperature, actual_temperature);
lightbulb_set_cct(actual_temperature);
}{...}
}{ ... }
/* ... */
void board_led_lightness(uint8_t elem_index, uint16_t actual)
{
static uint16_t last_acual = 0xFFFF;
ESP_LOGD(TAG, "actual last state %d, state %d", last_acual, actual);
if(last_acual != actual) {
last_acual = actual;
uint16_t actual_lightness = (float)last_acual / (UINT16_MAX / 100.0);
ESP_LOGD(TAG, "lightness %d %%%d operation", last_acual, actual_lightness);
lightbulb_set_brightness(actual_lightness);
}{...}
}{ ... }
/* ... */
void board_led_switch(uint8_t elem_index, uint8_t onoff)
{
static uint8_t last_onoff = 0xFF;
ESP_LOGD(TAG, "onoff last state %d, state %d", last_onoff, onoff);
if(last_onoff != onoff) {
last_onoff = onoff;
if (last_onoff) {
ESP_LOGD(TAG, "onoff %d operation", last_onoff);
lightbulb_set_switch(true);
}{...} else {
ESP_LOGD(TAG, "onoff %d operation", last_onoff);
lightbulb_set_switch(false);
}{...}
}{...}
}{ ... }
#define MINDIFF (2.25e-308)
static float bt_mesh_sqrt(float square)
{
float root, last, diff;
root = square / 3.0;
diff = 1;
if (square <= 0) {
return 0;
}{...}
do {
last = root;
root = (root + square / root) / 2.0;
diff = root - last;
}{...} while (diff > MINDIFF || diff < -MINDIFF);
return root;
}{ ... }
static int32_t bt_mesh_ceiling(float num)
{
int32_t inum = (int32_t)num;
if (num == (float)inum) {
return inum;
}{...}
return inum + 1;
}{ ... }
uint16_t convert_lightness_actual_to_linear(uint16_t actual)
{
float tmp = ((float) actual / UINT16_MAX);
return bt_mesh_ceiling(UINT16_MAX * tmp * tmp);
}{ ... }
uint16_t convert_lightness_linear_to_actual(uint16_t linear)
{
return (uint16_t)(UINT16_MAX * bt_mesh_sqrt(((float) linear / UINT16_MAX)));
}{ ... }
int16_t convert_temperature_to_level(uint16_t temp, uint16_t min, uint16_t max)
{
float tmp = (temp - min) * UINT16_MAX / (max - min);
return (int16_t) (tmp + INT16_MIN);
}{ ... }
uint16_t covert_level_to_temperature(int16_t level, uint16_t min, uint16_t max)
{
float diff = (float) (max - min) / UINT16_MAX;
uint16_t tmp = (uint16_t) ((level - INT16_MIN) * diff);
return (uint16_t) (min + tmp);
}{ ... }
void swap_buf(uint8_t *dst, const uint8_t *src, int len)
{
int i;
for (i = 0; i < len; i++) {
dst[len - 1 - i] = src[i];
}{...}
}{ ... }
uint8_t *mac_str2hex(const char *mac_str, uint8_t *mac_hex)
{
uint32_t mac_data[6] = {0};
sscanf(mac_str, "%02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32,
mac_data, mac_data + 1, mac_data + 2, mac_data + 3, mac_data + 4, mac_data + 5);
for (int i = 0; i < 6; i++) {
mac_hex[i] = mac_data[i];
}{...}
return mac_hex;
}{ ... }