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.

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