Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1315 lines
39 KiB

6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. LIGHT MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  6. #include <Ticker.h>
  7. #include <ArduinoJson.h>
  8. #include <vector>
  9. extern "C" {
  10. #include "libs/fs_math.h"
  11. }
  12. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  13. #define PWM_CHANNEL_NUM_MAX LIGHT_CHANNELS
  14. extern "C" {
  15. #include "libs/pwm.h"
  16. }
  17. #endif
  18. // -----------------------------------------------------------------------------
  19. Ticker _light_comms_ticker;
  20. Ticker _light_save_ticker;
  21. Ticker _light_transition_ticker;
  22. struct channel_t {
  23. unsigned char pin; // real GPIO pin
  24. bool reverse; // wether we should invert the value before using it
  25. bool state; // is the channel ON
  26. unsigned char inputValue; // raw value, without the brightness
  27. unsigned char value; // normalized value, including brightness
  28. unsigned char target; // target value
  29. double current; // transition value
  30. };
  31. std::vector<channel_t> _light_channel;
  32. bool _light_state = false;
  33. bool _light_use_transitions = false;
  34. unsigned int _light_transition_time = LIGHT_TRANSITION_TIME;
  35. bool _light_has_color = false;
  36. bool _light_use_white = false;
  37. bool _light_use_cct = false;
  38. bool _light_use_gamma = false;
  39. unsigned long _light_steps_left = 1;
  40. unsigned char _light_brightness = LIGHT_MAX_BRIGHTNESS;
  41. unsigned int _light_mireds = round((LIGHT_COLDWHITE_MIRED+LIGHT_WARMWHITE_MIRED)/2);
  42. using light_brightness_func_t = void();
  43. light_brightness_func_t* _light_brightness_func = nullptr;
  44. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  45. #include <my92xx.h>
  46. my92xx * _my92xx;
  47. ARRAYINIT(unsigned char, _light_channel_map, MY92XX_MAPPING);
  48. #endif
  49. // UI hint about channel distribution
  50. const char _light_channel_desc[5][5] PROGMEM = {
  51. {'W', 0, 0, 0, 0},
  52. {'W', 'C', 0, 0, 0},
  53. {'R', 'G', 'B', 0, 0},
  54. {'R', 'G', 'B', 'W', 0},
  55. {'R', 'G', 'B', 'W', 'C'}
  56. };
  57. // Gamma Correction lookup table (8 bit)
  58. const unsigned char _light_gamma_table[] PROGMEM = {
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
  61. 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6,
  62. 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11,
  63. 12, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19,
  64. 19, 20, 20, 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28,
  65. 29, 30, 30, 31, 32, 33, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40,
  66. 41, 42, 43, 43, 44, 45, 46, 47, 48, 49, 50, 50, 51, 52, 53, 54,
  67. 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71,
  68. 72, 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89,
  69. 91, 92, 93, 94, 96, 97, 98, 100, 101, 102, 104, 105, 106, 108, 109, 110,
  70. 112, 113, 115, 116, 118, 119, 121, 122, 123, 125, 126, 128, 130, 131, 133, 134,
  71. 136, 137, 139, 140, 142, 144, 145, 147, 149, 150, 152, 154, 155, 157, 159, 160,
  72. 162, 164, 166, 167, 169, 171, 173, 175, 176, 178, 180, 182, 184, 186, 187, 189,
  73. 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221,
  74. 223, 225, 227, 229, 231, 233, 235, 238, 240, 242, 244, 246, 248, 251, 253, 255
  75. };
  76. // -----------------------------------------------------------------------------
  77. // UTILS
  78. // -----------------------------------------------------------------------------
  79. void _setRGBInputValue(unsigned char red, unsigned char green, unsigned char blue) {
  80. _light_channel[0].inputValue = constrain(red, 0, LIGHT_MAX_VALUE);
  81. _light_channel[1].inputValue = constrain(green, 0, LIGHT_MAX_VALUE);;
  82. _light_channel[2].inputValue = constrain(blue, 0, LIGHT_MAX_VALUE);;
  83. }
  84. void _setCCTInputValue(unsigned char warm, unsigned char cold) {
  85. _light_channel[0].inputValue = constrain(warm, 0, LIGHT_MAX_VALUE);
  86. _light_channel[1].inputValue = constrain(cold, 0, LIGHT_MAX_VALUE);
  87. }
  88. void _lightApplyBrightness(unsigned char channels = lightChannels()) {
  89. double brightness = (double) _light_brightness / LIGHT_MAX_BRIGHTNESS;
  90. for (unsigned char i=0; i < channels; i++) {
  91. _light_channel[i].value = _light_channel[i].inputValue * brightness;
  92. }
  93. }
  94. void _lightApplyBrightnessColor() {
  95. double brightness = (double) _light_brightness / LIGHT_MAX_BRIGHTNESS;
  96. // Substract the common part from RGB channels and add it to white channel. So [250,150,50] -> [200,100,0,50]
  97. unsigned char white = std::min(_light_channel[0].inputValue, std::min(_light_channel[1].inputValue, _light_channel[2].inputValue));
  98. for (unsigned int i=0; i < 3; i++) {
  99. _light_channel[i].value = _light_channel[i].inputValue - white;
  100. }
  101. // Split the White Value across 2 White LED Strips.
  102. if (_light_use_cct) {
  103. // This change the range from 153-500 to 0-347 so we get a value between 0 and 1 in the end.
  104. double miredFactor = ((double) _light_mireds - (double) LIGHT_COLDWHITE_MIRED)/((double) LIGHT_WARMWHITE_MIRED - (double) LIGHT_COLDWHITE_MIRED);
  105. // set cold white
  106. _light_channel[3].inputValue = 0;
  107. _light_channel[3].value = round(((double) 1.0 - miredFactor) * white);
  108. // set warm white
  109. _light_channel[4].inputValue = 0;
  110. _light_channel[4].value = round(miredFactor * white);
  111. } else {
  112. _light_channel[3].inputValue = 0;
  113. _light_channel[3].value = white;
  114. }
  115. // Scale up to equal input values. So [250,150,50] -> [200,100,0,50] -> [250, 125, 0, 63]
  116. unsigned char max_in = std::max(_light_channel[0].inputValue, std::max(_light_channel[1].inputValue, _light_channel[2].inputValue));
  117. unsigned char max_out = std::max(std::max(_light_channel[0].value, _light_channel[1].value), std::max(_light_channel[2].value, _light_channel[3].value));
  118. unsigned char channelSize = _light_use_cct ? 5 : 4;
  119. if (_light_use_cct) {
  120. max_out = std::max(max_out, _light_channel[4].value);
  121. }
  122. double factor = (max_out > 0) ? (double) (max_in / max_out) : 0;
  123. for (unsigned char i=0; i < channelSize; i++) {
  124. _light_channel[i].value = round((double) _light_channel[i].value * factor * brightness);
  125. }
  126. // Scale white channel to match brightness
  127. for (unsigned char i=3; i < channelSize; i++) {
  128. _light_channel[i].value = constrain(_light_channel[i].value * LIGHT_WHITE_FACTOR, 0, LIGHT_MAX_BRIGHTNESS);
  129. }
  130. // For the rest of channels, don't apply brightness, it is already in the inputValue
  131. // i should be 4 when RGBW and 5 when RGBWW
  132. for (unsigned char i=channelSize; i < _light_channel.size(); i++) {
  133. _light_channel[i].value = _light_channel[i].inputValue;
  134. }
  135. }
  136. String lightDesc(unsigned char id) {
  137. if (id >= _light_channel.size()) return F("UNKNOWN");
  138. const char tag = pgm_read_byte(&_light_channel_desc[_light_channel.size() - 1][id]);
  139. switch (tag) {
  140. case 'W': return F("WARM WHITE");
  141. case 'C': return F("COLD WHITE");
  142. case 'R': return F("RED");
  143. case 'G': return F("GREEN");
  144. case 'B': return F("BLUE");
  145. default: break;
  146. }
  147. return F("UNKNOWN");
  148. }
  149. // -----------------------------------------------------------------------------
  150. // Input Values
  151. // -----------------------------------------------------------------------------
  152. void _fromLong(unsigned long value, bool brightness) {
  153. if (brightness) {
  154. _setRGBInputValue((value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF);
  155. _light_brightness = (value & 0xFF) * LIGHT_MAX_BRIGHTNESS / 255;
  156. } else {
  157. _setRGBInputValue((value >> 16) & 0xFF, (value >> 8) & 0xFF, (value) & 0xFF);
  158. }
  159. }
  160. void _fromRGB(const char * rgb) {
  161. char * p = (char *) rgb;
  162. if (strlen(p) == 0) return;
  163. switch (p[0]) {
  164. case '#': // HEX Value
  165. if (_light_has_color) {
  166. ++p;
  167. unsigned long value = strtoul(p, NULL, 16);
  168. // RGBA values are interpreted like RGB + brightness
  169. _fromLong(value, strlen(p) > 7);
  170. }
  171. break;
  172. case 'M': // Mired Value
  173. _fromMireds(atol(p + 1));
  174. break;
  175. case 'K': // Kelvin Value
  176. _fromKelvin(atol(p + 1));
  177. break;
  178. default: // assume decimal values separated by commas
  179. char * tok;
  180. unsigned char count = 0;
  181. unsigned char channels = _light_channel.size();
  182. tok = strtok(p, ",");
  183. while (tok != NULL) {
  184. _light_channel[count].inputValue = atoi(tok);
  185. if (++count == channels) break;
  186. tok = strtok(NULL, ",");
  187. }
  188. // RGB but less than 3 values received, assume it is 0
  189. if (_light_has_color && (count < 3)) {
  190. // check channel 1 and 2:
  191. for (int i = 1; i <= 2; i++) {
  192. if (count < (i+1)) {
  193. _light_channel[i].inputValue = 0;
  194. }
  195. }
  196. }
  197. break;
  198. }
  199. }
  200. // HSV string is expected to be "H,S,V", where:
  201. // 0 <= H <= 360
  202. // 0 <= S <= 100
  203. // 0 <= V <= 100
  204. void _fromHSV(const char * hsv) {
  205. char * ptr = (char *) hsv;
  206. if (strlen(ptr) == 0) return;
  207. if (!_light_has_color) return;
  208. char * tok;
  209. unsigned char count = 0;
  210. unsigned int value[3] = {0};
  211. tok = strtok(ptr, ",");
  212. while (tok != NULL) {
  213. value[count] = atoi(tok);
  214. if (++count == 3) break;
  215. tok = strtok(NULL, ",");
  216. }
  217. if (count != 3) return;
  218. // HSV to RGB transformation -----------------------------------------------
  219. //INPUT: [0,100,57]
  220. //IS: [145,0,0]
  221. //SHOULD: [255,0,0]
  222. double h = (value[0] == 360) ? 0 : (double) value[0] / 60.0;
  223. double f = (h - floor(h));
  224. double s = (double) value[1] / 100.0;
  225. _light_brightness = round((double) value[2] * 2.55); // (255/100)
  226. unsigned char p = round(255 * (1.0 - s));
  227. unsigned char q = round(255 * (1.0 - s * f));
  228. unsigned char t = round(255 * (1.0 - s * (1.0 - f)));
  229. switch (int(h)) {
  230. case 0:
  231. _setRGBInputValue(255, t, p);
  232. break;
  233. case 1:
  234. _setRGBInputValue(q, 255, p);
  235. break;
  236. case 2:
  237. _setRGBInputValue(p, 255, t);
  238. break;
  239. case 3:
  240. _setRGBInputValue(p, q, 255);
  241. break;
  242. case 4:
  243. _setRGBInputValue(t, p, 255);
  244. break;
  245. case 5:
  246. _setRGBInputValue(255, p, q);
  247. break;
  248. default:
  249. _setRGBInputValue(0, 0, 0);
  250. break;
  251. }
  252. }
  253. // Thanks to Sacha Telgenhof for sharing this code in his AiLight library
  254. // https://github.com/stelgenhof/AiLight
  255. void _fromKelvin(unsigned long kelvin) {
  256. if (!_light_has_color) {
  257. if(!_light_use_cct) return;
  258. _light_mireds = constrain(round(1000000UL / kelvin), LIGHT_MIN_MIREDS, LIGHT_MAX_MIREDS);
  259. // This change the range from 153-500 to 0-347 so we get a value between 0 and 1 in the end.
  260. double factor = ((double) _light_mireds - (double) LIGHT_COLDWHITE_MIRED)/((double) LIGHT_WARMWHITE_MIRED - (double) LIGHT_COLDWHITE_MIRED);
  261. unsigned char warm = round(factor * LIGHT_MAX_VALUE);
  262. unsigned char cold = round(((double) 1.0 - factor) * LIGHT_MAX_VALUE);
  263. _setCCTInputValue(warm, cold);
  264. return;
  265. }
  266. _light_mireds = constrain(round(1000000UL / kelvin), LIGHT_MIN_MIREDS, LIGHT_MAX_MIREDS);
  267. if (_light_use_cct) {
  268. _setRGBInputValue(LIGHT_MAX_VALUE, LIGHT_MAX_VALUE, LIGHT_MAX_VALUE);
  269. return;
  270. }
  271. // Calculate colors
  272. kelvin /= 100;
  273. unsigned int red = (kelvin <= 66)
  274. ? LIGHT_MAX_VALUE
  275. : 329.698727446 * fs_pow((double) (kelvin - 60), -0.1332047592);
  276. unsigned int green = (kelvin <= 66)
  277. ? 99.4708025861 * fs_log(kelvin) - 161.1195681661
  278. : 288.1221695283 * fs_pow((double) kelvin, -0.0755148492);
  279. unsigned int blue = (kelvin >= 66)
  280. ? LIGHT_MAX_VALUE
  281. : ((kelvin <= 19)
  282. ? 0
  283. : 138.5177312231 * fs_log(kelvin - 10) - 305.0447927307);
  284. _setRGBInputValue(red, green, blue);
  285. }
  286. // Color temperature is measured in mireds (kelvin = 1e6/mired)
  287. void _fromMireds(unsigned long mireds) {
  288. unsigned long kelvin = constrain(1000000UL / mireds, 1000, 40000);
  289. _fromKelvin(kelvin);
  290. }
  291. // -----------------------------------------------------------------------------
  292. // Output Values
  293. // -----------------------------------------------------------------------------
  294. void _toRGB(char * rgb, size_t len, bool target) {
  295. unsigned long value = 0;
  296. value += target ? _light_channel[0].target : _light_channel[0].inputValue;
  297. value <<= 8;
  298. value += target ? _light_channel[1].target : _light_channel[1].inputValue;
  299. value <<= 8;
  300. value += target ? _light_channel[2].target : _light_channel[2].inputValue;
  301. snprintf_P(rgb, len, PSTR("#%06X"), value);
  302. }
  303. void _toRGB(char * rgb, size_t len) {
  304. _toRGB(rgb, len, false);
  305. }
  306. void _toHSV(char * hsv, size_t len, bool target) {
  307. double h, s, v;
  308. double brightness = (double) _light_brightness / LIGHT_MAX_BRIGHTNESS;
  309. double r = (double) ((target ? _light_channel[0].target : _light_channel[0].inputValue) * brightness) / 255.0;
  310. double g = (double) ((target ? _light_channel[1].target : _light_channel[1].inputValue) * brightness) / 255.0;
  311. double b = (double) ((target ? _light_channel[2].target : _light_channel[2].inputValue) * brightness) / 255.0;
  312. double min = std::min(r, std::min(g, b));
  313. double max = std::max(r, std::max(g, b));
  314. v = 100.0 * max;
  315. if (v == 0) {
  316. h = s = 0;
  317. } else {
  318. s = 100.0 * (max - min) / max;
  319. if (s == 0) {
  320. h = 0;
  321. } else {
  322. if (max == r) {
  323. if (g >= b) {
  324. h = 0.0 + 60.0 * (g - b) / (max - min);
  325. } else {
  326. h = 360.0 + 60.0 * (g - b) / (max - min);
  327. }
  328. } else if (max == g) {
  329. h = 120.0 + 60.0 * (b - r) / (max - min);
  330. } else {
  331. h = 240.0 + 60.0 * (r - g) / (max - min);
  332. }
  333. }
  334. }
  335. // String
  336. snprintf_P(hsv, len, PSTR("%d,%d,%d"), round(h), round(s), round(v));
  337. }
  338. void _toHSV(char * hsv, size_t len) {
  339. _toHSV(hsv, len, false);
  340. }
  341. void _toLong(char * color, size_t len, bool target) {
  342. if (!_light_has_color) return;
  343. snprintf_P(color, len, PSTR("%d,%d,%d"),
  344. (int) (target ? _light_channel[0].target : _light_channel[0].inputValue),
  345. (int) (target ? _light_channel[1].target : _light_channel[1].inputValue),
  346. (int) (target ? _light_channel[2].target : _light_channel[2].inputValue)
  347. );
  348. }
  349. void _toLong(char * color, size_t len) {
  350. _toLong(color, len, false);
  351. }
  352. void _toCSV(char * buffer, size_t len, bool applyBrightness, bool target) {
  353. char num[10];
  354. float b = applyBrightness ? (float) _light_brightness / LIGHT_MAX_BRIGHTNESS : 1;
  355. for (unsigned char i=0; i<_light_channel.size(); i++) {
  356. itoa((target ? _light_channel[i].target : _light_channel[i].inputValue) * b, num, 10);
  357. if (i>0) strncat(buffer, ",", len--);
  358. strncat(buffer, num, len);
  359. len = len - strlen(num);
  360. }
  361. }
  362. void _toCSV(char * buffer, size_t len, bool applyBrightness) {
  363. _toCSV(buffer, len, applyBrightness, false);
  364. }
  365. // -----------------------------------------------------------------------------
  366. // PROVIDER
  367. // -----------------------------------------------------------------------------
  368. unsigned int _toPWM(unsigned long value, bool gamma, bool reverse) {
  369. value = constrain(value, 0, LIGHT_MAX_VALUE);
  370. if (gamma) value = pgm_read_byte(_light_gamma_table + value);
  371. if (LIGHT_MAX_VALUE != LIGHT_LIMIT_PWM) value = map(value, 0, LIGHT_MAX_VALUE, 0, LIGHT_LIMIT_PWM);
  372. if (reverse) value = LIGHT_LIMIT_PWM - value;
  373. return value;
  374. }
  375. // Returns a PWM value for the given channel ID
  376. unsigned int _toPWM(unsigned char id) {
  377. bool useGamma = _light_use_gamma && _light_has_color && (id < 3);
  378. return _toPWM(_light_channel[id].current, useGamma, _light_channel[id].reverse);
  379. }
  380. void _transition() {
  381. // Update transition ticker
  382. _light_steps_left--;
  383. if (_light_steps_left == 0) _light_transition_ticker.detach();
  384. // Transitions
  385. for (unsigned int i=0; i < _light_channel.size(); i++) {
  386. if (_light_steps_left == 0) {
  387. _light_channel[i].current = _light_channel[i].target;
  388. } else {
  389. double difference = (double) (_light_channel[i].target - _light_channel[i].current) / (_light_steps_left + 1);
  390. _light_channel[i].current = _light_channel[i].current + difference;
  391. }
  392. }
  393. }
  394. void _lightProviderUpdate() {
  395. _transition();
  396. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  397. for (unsigned char i=0; i<_light_channel.size(); i++) {
  398. _my92xx->setChannel(_light_channel_map[i], _toPWM(i));
  399. }
  400. _my92xx->setState(true);
  401. _my92xx->update();
  402. #endif
  403. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  404. for (unsigned int i=0; i < _light_channel.size(); i++) {
  405. pwm_set_duty(_toPWM(i), i);
  406. }
  407. pwm_start();
  408. #endif
  409. }
  410. // -----------------------------------------------------------------------------
  411. // PERSISTANCE
  412. // -----------------------------------------------------------------------------
  413. union light_rtcmem_t {
  414. struct {
  415. uint8_t channels[5];
  416. uint8_t brightness;
  417. uint16_t mired;
  418. } packed;
  419. uint64_t value;
  420. };
  421. #define LIGHT_RTCMEM_CHANNELS_MAX sizeof(light_rtcmem_t().packed.channels)
  422. void _lightSaveRtcmem() {
  423. if (lightChannels() > LIGHT_RTCMEM_CHANNELS_MAX) return;
  424. light_rtcmem_t light;
  425. for (unsigned int i=0; i < lightChannels(); i++) {
  426. light.packed.channels[i] = _light_channel[i].inputValue;
  427. }
  428. light.packed.brightness = _light_brightness;
  429. light.packed.mired = _light_mireds;
  430. Rtcmem->light = light.value;
  431. }
  432. void _lightRestoreRtcmem() {
  433. if (lightChannels() > LIGHT_RTCMEM_CHANNELS_MAX) return;
  434. light_rtcmem_t light;
  435. light.value = Rtcmem->light;
  436. for (unsigned int i=0; i < lightChannels(); i++) {
  437. _light_channel[i].inputValue = light.packed.channels[i];
  438. }
  439. _light_brightness = light.packed.brightness;
  440. _light_mireds = light.packed.mired;
  441. }
  442. void _lightSaveSettings() {
  443. for (unsigned int i=0; i < _light_channel.size(); i++) {
  444. setSetting("ch", i, _light_channel[i].inputValue);
  445. }
  446. setSetting("brightness", _light_brightness);
  447. setSetting("mireds", _light_mireds);
  448. saveSettings();
  449. }
  450. void _lightRestoreSettings() {
  451. for (unsigned int i=0; i < _light_channel.size(); i++) {
  452. _light_channel[i].inputValue = getSetting("ch", i, i==0 ? 255 : 0).toInt();
  453. }
  454. _light_brightness = getSetting("brightness", LIGHT_MAX_BRIGHTNESS).toInt();
  455. _light_mireds = getSetting("mireds", _light_mireds).toInt();
  456. lightUpdate(false, false);
  457. }
  458. // -----------------------------------------------------------------------------
  459. // MQTT
  460. // -----------------------------------------------------------------------------
  461. #if MQTT_SUPPORT
  462. void _lightMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  463. String mqtt_group_color = getSetting("mqttGroupColor");
  464. if (type == MQTT_CONNECT_EVENT) {
  465. mqttSubscribe(MQTT_TOPIC_BRIGHTNESS);
  466. if (_light_has_color) {
  467. mqttSubscribe(MQTT_TOPIC_COLOR_RGB);
  468. mqttSubscribe(MQTT_TOPIC_COLOR_HSV);
  469. mqttSubscribe(MQTT_TOPIC_TRANSITION);
  470. }
  471. if (_light_has_color || _light_use_cct) {
  472. mqttSubscribe(MQTT_TOPIC_MIRED);
  473. mqttSubscribe(MQTT_TOPIC_KELVIN);
  474. }
  475. // Group color
  476. if (mqtt_group_color.length() > 0) mqttSubscribeRaw(mqtt_group_color.c_str());
  477. // Channels
  478. char buffer[strlen(MQTT_TOPIC_CHANNEL) + 3];
  479. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_CHANNEL);
  480. mqttSubscribe(buffer);
  481. }
  482. if (type == MQTT_MESSAGE_EVENT) {
  483. // Group color
  484. if ((mqtt_group_color.length() > 0) & (mqtt_group_color.equals(topic))) {
  485. lightColor(payload, true);
  486. lightUpdate(true, mqttForward(), false);
  487. return;
  488. }
  489. // Match topic
  490. String t = mqttMagnitude((char *) topic);
  491. // Color temperature in mireds
  492. if (t.equals(MQTT_TOPIC_MIRED)) {
  493. _fromMireds(atol(payload));
  494. lightUpdate(true, mqttForward());
  495. return;
  496. }
  497. // Color temperature in kelvins
  498. if (t.equals(MQTT_TOPIC_KELVIN)) {
  499. _fromKelvin(atol(payload));
  500. lightUpdate(true, mqttForward());
  501. return;
  502. }
  503. // Color
  504. if (t.equals(MQTT_TOPIC_COLOR_RGB)) {
  505. lightColor(payload, true);
  506. lightUpdate(true, mqttForward());
  507. return;
  508. }
  509. if (t.equals(MQTT_TOPIC_COLOR_HSV)) {
  510. lightColor(payload, false);
  511. lightUpdate(true, mqttForward());
  512. return;
  513. }
  514. // Brightness
  515. if (t.equals(MQTT_TOPIC_BRIGHTNESS)) {
  516. _light_brightness = constrain(atoi(payload), 0, LIGHT_MAX_BRIGHTNESS);
  517. lightUpdate(true, mqttForward());
  518. return;
  519. }
  520. // Transitions
  521. if (t.equals(MQTT_TOPIC_TRANSITION)) {
  522. lightTransitionTime(atol(payload));
  523. return;
  524. }
  525. // Channel
  526. if (t.startsWith(MQTT_TOPIC_CHANNEL)) {
  527. unsigned int channelID = t.substring(strlen(MQTT_TOPIC_CHANNEL)+1).toInt();
  528. if (channelID >= _light_channel.size()) {
  529. DEBUG_MSG_P(PSTR("[LIGHT] Wrong channelID (%d)\n"), channelID);
  530. return;
  531. }
  532. lightChannel(channelID, atoi(payload));
  533. lightUpdate(true, mqttForward());
  534. return;
  535. }
  536. }
  537. }
  538. void lightMQTT() {
  539. char buffer[20];
  540. if (_light_has_color) {
  541. // Color
  542. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  543. _toRGB(buffer, sizeof(buffer), true);
  544. } else {
  545. _toLong(buffer, sizeof(buffer), true);
  546. }
  547. mqttSend(MQTT_TOPIC_COLOR_RGB, buffer);
  548. _toHSV(buffer, sizeof(buffer), true);
  549. mqttSend(MQTT_TOPIC_COLOR_HSV, buffer);
  550. }
  551. if (_light_has_color || _light_use_cct) {
  552. // Mireds
  553. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _light_mireds);
  554. mqttSend(MQTT_TOPIC_MIRED, buffer);
  555. }
  556. // Channels
  557. for (unsigned int i=0; i < _light_channel.size(); i++) {
  558. itoa(_light_channel[i].target, buffer, 10);
  559. mqttSend(MQTT_TOPIC_CHANNEL, i, buffer);
  560. }
  561. // Brightness
  562. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _light_brightness);
  563. mqttSend(MQTT_TOPIC_BRIGHTNESS, buffer);
  564. }
  565. void lightMQTTGroup() {
  566. String mqtt_group_color = getSetting("mqttGroupColor");
  567. if (mqtt_group_color.length()>0) {
  568. char buffer[20];
  569. _toCSV(buffer, sizeof(buffer), true);
  570. mqttSendRaw(mqtt_group_color.c_str(), buffer);
  571. }
  572. }
  573. #endif
  574. // -----------------------------------------------------------------------------
  575. // Broker
  576. // -----------------------------------------------------------------------------
  577. #if BROKER_SUPPORT
  578. void lightBroker() {
  579. char buffer[10];
  580. for (unsigned int i=0; i < _light_channel.size(); i++) {
  581. itoa(_light_channel[i].inputValue, buffer, 10);
  582. brokerPublish(BROKER_MSG_TYPE_STATUS, MQTT_TOPIC_CHANNEL, i, buffer);
  583. }
  584. }
  585. #endif
  586. // -----------------------------------------------------------------------------
  587. // API
  588. // -----------------------------------------------------------------------------
  589. unsigned char lightChannels() {
  590. return _light_channel.size();
  591. }
  592. bool lightHasColor() {
  593. return _light_has_color;
  594. }
  595. bool lightUseCCT() {
  596. return _light_use_cct;
  597. }
  598. void _lightComms(unsigned char mask) {
  599. // Report color & brightness to MQTT broker
  600. #if MQTT_SUPPORT
  601. if (mask & 0x01) lightMQTT();
  602. if (mask & 0x02) lightMQTTGroup();
  603. #endif
  604. // Report color to WS clients (using current brightness setting)
  605. #if WEB_SUPPORT
  606. wsSend(_lightWebSocketStatus);
  607. #endif
  608. // Report channels to local broker
  609. #if BROKER_SUPPORT
  610. lightBroker();
  611. #endif
  612. }
  613. void lightUpdate(bool save, bool forward, bool group_forward) {
  614. _light_brightness_func();
  615. // Update channels
  616. for (unsigned int i=0; i < _light_channel.size(); i++) {
  617. _light_channel[i].target = _light_state && _light_channel[i].state ? _light_channel[i].value : 0;
  618. //DEBUG_MSG_P("[LIGHT] Channel #%u target value: %u\n", i, _light_channel[i].target);
  619. }
  620. // Configure color transition
  621. _light_steps_left = _light_use_transitions ? _light_transition_time / LIGHT_TRANSITION_STEP : 1;
  622. _light_transition_ticker.attach_ms(LIGHT_TRANSITION_STEP, _lightProviderUpdate);
  623. // Delay every communication 100ms to avoid jamming
  624. unsigned char mask = 0;
  625. if (forward) mask += 1;
  626. if (group_forward) mask += 2;
  627. _light_comms_ticker.once_ms(LIGHT_COMMS_DELAY, _lightComms, mask);
  628. _lightSaveRtcmem();
  629. #if LIGHT_SAVE_ENABLED
  630. // Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
  631. if (save) _light_save_ticker.once(LIGHT_SAVE_DELAY, _lightSaveSettings);
  632. #endif
  633. };
  634. void lightUpdate(bool save, bool forward) {
  635. lightUpdate(save, forward, true);
  636. }
  637. #if LIGHT_SAVE_ENABLED == 0
  638. void lightSave() {
  639. _lightSaveSettings();
  640. }
  641. #endif
  642. void lightState(unsigned char i, bool state) {
  643. _light_channel[i].state = state;
  644. }
  645. bool lightState(unsigned char i) {
  646. return _light_channel[i].state;
  647. }
  648. void lightState(bool state) {
  649. _light_state = state;
  650. }
  651. bool lightState() {
  652. return _light_state;
  653. }
  654. void lightColor(const char * color, bool rgb) {
  655. DEBUG_MSG_P(PSTR("[LIGHT] %s: %s\n"), rgb ? "RGB" : "HSV", color);
  656. if (rgb) {
  657. _fromRGB(color);
  658. } else {
  659. _fromHSV(color);
  660. }
  661. }
  662. void lightColor(const char * color) {
  663. lightColor(color, true);
  664. }
  665. void lightColor(unsigned long color) {
  666. _fromLong(color, false);
  667. }
  668. String lightColor(bool rgb) {
  669. char str[12];
  670. if (rgb) {
  671. _toRGB(str, sizeof(str));
  672. } else {
  673. _toHSV(str, sizeof(str));
  674. }
  675. return String(str);
  676. }
  677. String lightColor() {
  678. return lightColor(true);
  679. }
  680. unsigned int lightChannel(unsigned char id) {
  681. if (id <= _light_channel.size()) {
  682. return _light_channel[id].inputValue;
  683. }
  684. return 0;
  685. }
  686. void lightChannel(unsigned char id, int value) {
  687. if (id <= _light_channel.size()) {
  688. _light_channel[id].inputValue = constrain(value, 0, LIGHT_MAX_VALUE);
  689. }
  690. }
  691. void lightChannelStep(unsigned char id, int steps) {
  692. lightChannel(id, lightChannel(id) + steps * LIGHT_STEP);
  693. }
  694. unsigned int lightBrightness() {
  695. return _light_brightness;
  696. }
  697. void lightBrightness(int b) {
  698. _light_brightness = constrain(b, 0, LIGHT_MAX_BRIGHTNESS);
  699. }
  700. void lightBrightnessStep(int steps) {
  701. lightBrightness(_light_brightness + steps * LIGHT_STEP);
  702. }
  703. unsigned long lightTransitionTime() {
  704. if (_light_use_transitions) {
  705. return _light_transition_time;
  706. } else {
  707. return 0;
  708. }
  709. }
  710. void lightTransitionTime(unsigned long m) {
  711. if (0 == m) {
  712. _light_use_transitions = false;
  713. } else {
  714. _light_use_transitions = true;
  715. _light_transition_time = m;
  716. }
  717. setSetting("useTransitions", _light_use_transitions);
  718. setSetting("lightTime", _light_transition_time);
  719. saveSettings();
  720. }
  721. // -----------------------------------------------------------------------------
  722. // SETUP
  723. // -----------------------------------------------------------------------------
  724. #if WEB_SUPPORT
  725. bool _lightWebSocketOnReceive(const char * key, JsonVariant& value) {
  726. if (strncmp(key, "light", 5) == 0) return true;
  727. if (strncmp(key, "use", 3) == 0) return true;
  728. return false;
  729. }
  730. void _lightWebSocketStatus(JsonObject& root) {
  731. if (_light_has_color) {
  732. if (getSetting("useRGB", LIGHT_USE_RGB).toInt() == 1) {
  733. root["rgb"] = lightColor(true);
  734. } else {
  735. root["hsv"] = lightColor(false);
  736. }
  737. }
  738. if (_light_use_cct) {
  739. root["useCCT"] = _light_use_cct;
  740. root["mireds"] = _light_mireds;
  741. }
  742. JsonArray& channels = root.createNestedArray("channels");
  743. for (unsigned char id=0; id < _light_channel.size(); id++) {
  744. channels.add(lightChannel(id));
  745. }
  746. root["brightness"] = lightBrightness();
  747. }
  748. void _lightWebSocketOnSend(JsonObject& root) {
  749. root["colorVisible"] = 1;
  750. root["mqttGroupColor"] = getSetting("mqttGroupColor");
  751. root["useColor"] = _light_has_color;
  752. root["useWhite"] = _light_use_white;
  753. root["useGamma"] = _light_use_gamma;
  754. root["useTransitions"] = _light_use_transitions;
  755. root["useCSS"] = getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1;
  756. root["useRGB"] = getSetting("useRGB", LIGHT_USE_RGB).toInt() == 1;
  757. root["lightTime"] = _light_transition_time;
  758. _lightWebSocketStatus(root);
  759. }
  760. void _lightWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  761. if (_light_has_color) {
  762. if (strcmp(action, "color") == 0) {
  763. if (data.containsKey("rgb")) {
  764. lightColor(data["rgb"], true);
  765. lightUpdate(true, true);
  766. }
  767. if (data.containsKey("hsv")) {
  768. lightColor(data["hsv"], false);
  769. lightUpdate(true, true);
  770. }
  771. }
  772. }
  773. if (_light_use_cct) {
  774. if (strcmp(action, "mireds") == 0) {
  775. _fromMireds(data["mireds"]);
  776. lightUpdate(true, true);
  777. }
  778. }
  779. if (strcmp(action, "channel") == 0) {
  780. if (data.containsKey("id") && data.containsKey("value")) {
  781. lightChannel(data["id"], data["value"]);
  782. lightUpdate(true, true);
  783. }
  784. }
  785. if (strcmp(action, "brightness") == 0) {
  786. if (data.containsKey("value")) {
  787. lightBrightness(data["value"]);
  788. lightUpdate(true, true);
  789. }
  790. }
  791. }
  792. #endif
  793. #if API_SUPPORT
  794. void _lightAPISetup() {
  795. if (_light_has_color) {
  796. apiRegister(MQTT_TOPIC_COLOR_RGB,
  797. [](char * buffer, size_t len) {
  798. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  799. _toRGB(buffer, len, true);
  800. } else {
  801. _toLong(buffer, len, true);
  802. }
  803. },
  804. [](const char * payload) {
  805. lightColor(payload, true);
  806. lightUpdate(true, true);
  807. }
  808. );
  809. apiRegister(MQTT_TOPIC_COLOR_HSV,
  810. [](char * buffer, size_t len) {
  811. _toHSV(buffer, len, true);
  812. },
  813. [](const char * payload) {
  814. lightColor(payload, false);
  815. lightUpdate(true, true);
  816. }
  817. );
  818. apiRegister(MQTT_TOPIC_KELVIN,
  819. [](char * buffer, size_t len) {},
  820. [](const char * payload) {
  821. _fromKelvin(atol(payload));
  822. lightUpdate(true, true);
  823. }
  824. );
  825. apiRegister(MQTT_TOPIC_MIRED,
  826. [](char * buffer, size_t len) {},
  827. [](const char * payload) {
  828. _fromMireds(atol(payload));
  829. lightUpdate(true, true);
  830. }
  831. );
  832. }
  833. for (unsigned int id=0; id<_light_channel.size(); id++) {
  834. char key[15];
  835. snprintf_P(key, sizeof(key), PSTR("%s/%d"), MQTT_TOPIC_CHANNEL, id);
  836. apiRegister(key,
  837. [id](char * buffer, size_t len) {
  838. snprintf_P(buffer, len, PSTR("%d"), _light_channel[id].target);
  839. },
  840. [id](const char * payload) {
  841. lightChannel(id, atoi(payload));
  842. lightUpdate(true, true);
  843. }
  844. );
  845. }
  846. apiRegister(MQTT_TOPIC_TRANSITION,
  847. [](char * buffer, size_t len) {
  848. snprintf_P(buffer, len, PSTR("%d"), lightTransitionTime());
  849. },
  850. [](const char * payload) {
  851. lightTransitionTime(atol(payload));
  852. }
  853. );
  854. apiRegister(MQTT_TOPIC_BRIGHTNESS,
  855. [](char * buffer, size_t len) {
  856. snprintf_P(buffer, len, PSTR("%d"), _light_brightness);
  857. },
  858. [](const char * payload) {
  859. lightBrightness(atoi(payload));
  860. lightUpdate(true, true);
  861. }
  862. );
  863. }
  864. #endif // API_SUPPORT
  865. #if TERMINAL_SUPPORT
  866. void _lightInitCommands() {
  867. terminalRegisterCommand(F("BRIGHTNESS"), [](Embedis* e) {
  868. if (e->argc > 1) {
  869. const String value(e->argv[1]);
  870. if( value.length() > 0 ) {
  871. if( value[0] == '+' || value[0] == '-' ) {
  872. lightBrightness(lightBrightness()+String(e->argv[1]).toInt());
  873. } else {
  874. lightBrightness(String(e->argv[1]).toInt());
  875. }
  876. lightUpdate(true, true);
  877. }
  878. }
  879. DEBUG_MSG_P(PSTR("Brightness: %d\n"), lightBrightness());
  880. terminalOK();
  881. });
  882. terminalRegisterCommand(F("CHANNEL"), [](Embedis* e) {
  883. if (e->argc < 2) {
  884. terminalError(F("Wrong arguments"));
  885. }
  886. int id = String(e->argv[1]).toInt();
  887. if (e->argc > 2) {
  888. int value = String(e->argv[2]).toInt();
  889. lightChannel(id, value);
  890. lightUpdate(true, true);
  891. }
  892. DEBUG_MSG_P(PSTR("Channel #%d (%s): %d\n"), id, lightDesc(id).c_str(), lightChannel(id));
  893. terminalOK();
  894. });
  895. terminalRegisterCommand(F("COLOR"), [](Embedis* e) {
  896. if (e->argc > 1) {
  897. String color = String(e->argv[1]);
  898. lightColor(color.c_str());
  899. lightUpdate(true, true);
  900. }
  901. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  902. terminalOK();
  903. });
  904. terminalRegisterCommand(F("KELVIN"), [](Embedis* e) {
  905. if (e->argc > 1) {
  906. String color = String("K") + String(e->argv[1]);
  907. lightColor(color.c_str());
  908. lightUpdate(true, true);
  909. }
  910. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  911. terminalOK();
  912. });
  913. terminalRegisterCommand(F("MIRED"), [](Embedis* e) {
  914. if (e->argc > 1) {
  915. const String value(e->argv[1]);
  916. String color = String("M");
  917. if( value.length() > 0 ) {
  918. if( value[0] == '+' || value[0] == '-' ) {
  919. color += String(_light_mireds + String(e->argv[1]).toInt());
  920. } else {
  921. color += String(e->argv[1]);
  922. }
  923. lightColor(color.c_str());
  924. lightUpdate(true, true);
  925. }
  926. }
  927. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  928. terminalOK();
  929. });
  930. }
  931. #endif // TERMINAL_SUPPORT
  932. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  933. unsigned long getIOMux(unsigned long gpio) {
  934. unsigned long muxes[16] = {
  935. PERIPHS_IO_MUX_GPIO0_U, PERIPHS_IO_MUX_U0TXD_U, PERIPHS_IO_MUX_GPIO2_U, PERIPHS_IO_MUX_U0RXD_U,
  936. PERIPHS_IO_MUX_GPIO4_U, PERIPHS_IO_MUX_GPIO5_U, PERIPHS_IO_MUX_SD_CLK_U, PERIPHS_IO_MUX_SD_DATA0_U,
  937. PERIPHS_IO_MUX_SD_DATA1_U, PERIPHS_IO_MUX_SD_DATA2_U, PERIPHS_IO_MUX_SD_DATA3_U, PERIPHS_IO_MUX_SD_CMD_U,
  938. PERIPHS_IO_MUX_MTDI_U, PERIPHS_IO_MUX_MTCK_U, PERIPHS_IO_MUX_MTMS_U, PERIPHS_IO_MUX_MTDO_U
  939. };
  940. return muxes[gpio];
  941. }
  942. unsigned long getIOFunc(unsigned long gpio) {
  943. unsigned long funcs[16] = {
  944. FUNC_GPIO0, FUNC_GPIO1, FUNC_GPIO2, FUNC_GPIO3,
  945. FUNC_GPIO4, FUNC_GPIO5, FUNC_GPIO6, FUNC_GPIO7,
  946. FUNC_GPIO8, FUNC_GPIO9, FUNC_GPIO10, FUNC_GPIO11,
  947. FUNC_GPIO12, FUNC_GPIO13, FUNC_GPIO14, FUNC_GPIO15
  948. };
  949. return funcs[gpio];
  950. }
  951. #endif
  952. void _lightConfigure() {
  953. _light_has_color = getSetting("useColor", LIGHT_USE_COLOR).toInt() == 1;
  954. if (_light_has_color && (_light_channel.size() < 3)) {
  955. _light_has_color = false;
  956. setSetting("useColor", _light_has_color);
  957. }
  958. _light_use_white = getSetting("useWhite", LIGHT_USE_WHITE).toInt() == 1;
  959. if (_light_use_white && (_light_channel.size() < 4) && (_light_channel.size() != 2)) {
  960. _light_use_white = false;
  961. setSetting("useWhite", _light_use_white);
  962. }
  963. if (_light_has_color) {
  964. if (_light_use_white) {
  965. _light_brightness_func = _lightApplyBrightnessColor;
  966. } else {
  967. _light_brightness_func = []() { _lightApplyBrightness(3); };
  968. }
  969. } else {
  970. _light_brightness_func = []() { _lightApplyBrightness(); };
  971. }
  972. _light_use_cct = getSetting("useCCT", LIGHT_USE_CCT).toInt() == 1;
  973. if (_light_use_cct && (((_light_channel.size() < 5) && (_light_channel.size() != 2)) || !_light_use_white)) {
  974. _light_use_cct = false;
  975. setSetting("useCCT", _light_use_cct);
  976. }
  977. _light_use_gamma = getSetting("useGamma", LIGHT_USE_GAMMA).toInt() == 1;
  978. _light_use_transitions = getSetting("useTransitions", LIGHT_USE_TRANSITIONS).toInt() == 1;
  979. _light_transition_time = getSetting("lightTime", LIGHT_TRANSITION_TIME).toInt();
  980. }
  981. void lightSetup() {
  982. #ifdef LIGHT_ENABLE_PIN
  983. pinMode(LIGHT_ENABLE_PIN, OUTPUT);
  984. digitalWrite(LIGHT_ENABLE_PIN, HIGH);
  985. #endif
  986. _light_channel.reserve(LIGHT_CHANNELS);
  987. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  988. _my92xx = new my92xx(MY92XX_MODEL, MY92XX_CHIPS, MY92XX_DI_PIN, MY92XX_DCKI_PIN, MY92XX_COMMAND);
  989. for (unsigned char i=0; i<LIGHT_CHANNELS; i++) {
  990. _light_channel.push_back((channel_t) {0, false, true, 0, 0, 0});
  991. }
  992. #endif
  993. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  994. #ifdef LIGHT_CH1_PIN
  995. _light_channel.push_back((channel_t) {LIGHT_CH1_PIN, LIGHT_CH1_INVERSE, true, 0, 0, 0});
  996. #endif
  997. #ifdef LIGHT_CH2_PIN
  998. _light_channel.push_back((channel_t) {LIGHT_CH2_PIN, LIGHT_CH2_INVERSE, true, 0, 0, 0});
  999. #endif
  1000. #ifdef LIGHT_CH3_PIN
  1001. _light_channel.push_back((channel_t) {LIGHT_CH3_PIN, LIGHT_CH3_INVERSE, true, 0, 0, 0});
  1002. #endif
  1003. #ifdef LIGHT_CH4_PIN
  1004. _light_channel.push_back((channel_t) {LIGHT_CH4_PIN, LIGHT_CH4_INVERSE, true, 0, 0, 0});
  1005. #endif
  1006. #ifdef LIGHT_CH5_PIN
  1007. _light_channel.push_back((channel_t) {LIGHT_CH5_PIN, LIGHT_CH5_INVERSE, true, 0, 0, 0});
  1008. #endif
  1009. uint32 pwm_duty_init[PWM_CHANNEL_NUM_MAX];
  1010. uint32 io_info[PWM_CHANNEL_NUM_MAX][3];
  1011. for (unsigned int i=0; i < _light_channel.size(); i++) {
  1012. pwm_duty_init[i] = 0;
  1013. io_info[i][0] = getIOMux(_light_channel[i].pin);
  1014. io_info[i][1] = getIOFunc(_light_channel[i].pin);
  1015. io_info[i][2] = _light_channel[i].pin;
  1016. pinMode(_light_channel[i].pin, OUTPUT);
  1017. }
  1018. pwm_init(LIGHT_MAX_PWM, pwm_duty_init, PWM_CHANNEL_NUM_MAX, io_info);
  1019. pwm_start();
  1020. #endif
  1021. DEBUG_MSG_P(PSTR("[LIGHT] LIGHT_PROVIDER = %d\n"), LIGHT_PROVIDER);
  1022. DEBUG_MSG_P(PSTR("[LIGHT] Number of channels: %d\n"), _light_channel.size());
  1023. _lightConfigure();
  1024. if (rtcmemStatus()) {
  1025. _lightRestoreRtcmem();
  1026. } else {
  1027. _lightRestoreSettings();
  1028. }
  1029. #if WEB_SUPPORT
  1030. wsOnSendRegister(_lightWebSocketOnSend);
  1031. wsOnActionRegister(_lightWebSocketOnAction);
  1032. wsOnReceiveRegister(_lightWebSocketOnReceive);
  1033. #endif
  1034. #if API_SUPPORT
  1035. _lightAPISetup();
  1036. #endif
  1037. #if MQTT_SUPPORT
  1038. mqttRegister(_lightMQTTCallback);
  1039. #endif
  1040. #if TERMINAL_SUPPORT
  1041. _lightInitCommands();
  1042. #endif
  1043. // Main callbacks
  1044. espurnaRegisterReload([]() {
  1045. #if LIGHT_SAVE_ENABLED == 0
  1046. lightSave();
  1047. #endif
  1048. _lightConfigure();
  1049. });
  1050. }
  1051. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE