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.

1316 lines
39 KiB

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