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.

1320 lines
40 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. 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. wsSend(_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 _lightWebSocketOnReceive(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 _lightWebSocketOnSend(JsonObject& root) {
  753. root["colorVisible"] = 1;
  754. root["mqttGroupColor"] = getSetting("mqttGroupColor");
  755. root["useColor"] = _light_has_color;
  756. root["useWhite"] = _light_use_white;
  757. root["useGamma"] = _light_use_gamma;
  758. root["useTransitions"] = _light_use_transitions;
  759. root["useCSS"] = getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1;
  760. root["useRGB"] = getSetting("useRGB", LIGHT_USE_RGB).toInt() == 1;
  761. root["lightTime"] = _light_transition_time;
  762. _lightWebSocketStatus(root);
  763. }
  764. void _lightWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  765. if (_light_has_color) {
  766. if (strcmp(action, "color") == 0) {
  767. if (data.containsKey("rgb")) {
  768. lightColor(data["rgb"], true);
  769. lightUpdate(true, true);
  770. }
  771. if (data.containsKey("hsv")) {
  772. lightColor(data["hsv"], false);
  773. lightUpdate(true, true);
  774. }
  775. }
  776. }
  777. if (_light_use_cct) {
  778. if (strcmp(action, "mireds") == 0) {
  779. _fromMireds(data["mireds"]);
  780. lightUpdate(true, true);
  781. }
  782. }
  783. if (strcmp(action, "channel") == 0) {
  784. if (data.containsKey("id") && data.containsKey("value")) {
  785. lightChannel(data["id"], data["value"]);
  786. lightUpdate(true, true);
  787. }
  788. }
  789. if (strcmp(action, "brightness") == 0) {
  790. if (data.containsKey("value")) {
  791. lightBrightness(data["value"]);
  792. lightUpdate(true, true);
  793. }
  794. }
  795. }
  796. #endif
  797. #if API_SUPPORT
  798. void _lightAPISetup() {
  799. if (_light_has_color) {
  800. apiRegister(MQTT_TOPIC_COLOR_RGB,
  801. [](char * buffer, size_t len) {
  802. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  803. _toRGB(buffer, len, true);
  804. } else {
  805. _toLong(buffer, len, true);
  806. }
  807. },
  808. [](const char * payload) {
  809. lightColor(payload, true);
  810. lightUpdate(true, true);
  811. }
  812. );
  813. apiRegister(MQTT_TOPIC_COLOR_HSV,
  814. [](char * buffer, size_t len) {
  815. _toHSV(buffer, len, true);
  816. },
  817. [](const char * payload) {
  818. lightColor(payload, false);
  819. lightUpdate(true, true);
  820. }
  821. );
  822. apiRegister(MQTT_TOPIC_KELVIN,
  823. [](char * buffer, size_t len) {},
  824. [](const char * payload) {
  825. _fromKelvin(atol(payload));
  826. lightUpdate(true, true);
  827. }
  828. );
  829. apiRegister(MQTT_TOPIC_MIRED,
  830. [](char * buffer, size_t len) {},
  831. [](const char * payload) {
  832. _fromMireds(atol(payload));
  833. lightUpdate(true, true);
  834. }
  835. );
  836. }
  837. for (unsigned int id=0; id<_light_channel.size(); id++) {
  838. char key[15];
  839. snprintf_P(key, sizeof(key), PSTR("%s/%d"), MQTT_TOPIC_CHANNEL, id);
  840. apiRegister(key,
  841. [id](char * buffer, size_t len) {
  842. snprintf_P(buffer, len, PSTR("%d"), _light_channel[id].target);
  843. },
  844. [id](const char * payload) {
  845. lightChannel(id, atoi(payload));
  846. lightUpdate(true, true);
  847. }
  848. );
  849. }
  850. apiRegister(MQTT_TOPIC_TRANSITION,
  851. [](char * buffer, size_t len) {
  852. snprintf_P(buffer, len, PSTR("%d"), lightTransitionTime());
  853. },
  854. [](const char * payload) {
  855. lightTransitionTime(atol(payload));
  856. }
  857. );
  858. apiRegister(MQTT_TOPIC_BRIGHTNESS,
  859. [](char * buffer, size_t len) {
  860. snprintf_P(buffer, len, PSTR("%d"), _light_brightness);
  861. },
  862. [](const char * payload) {
  863. lightBrightness(atoi(payload));
  864. lightUpdate(true, true);
  865. }
  866. );
  867. }
  868. #endif // API_SUPPORT
  869. #if TERMINAL_SUPPORT
  870. void _lightInitCommands() {
  871. terminalRegisterCommand(F("BRIGHTNESS"), [](Embedis* e) {
  872. if (e->argc > 1) {
  873. const String value(e->argv[1]);
  874. if( value.length() > 0 ) {
  875. if( value[0] == '+' || value[0] == '-' ) {
  876. lightBrightness(lightBrightness()+String(e->argv[1]).toInt());
  877. } else {
  878. lightBrightness(String(e->argv[1]).toInt());
  879. }
  880. lightUpdate(true, true);
  881. }
  882. }
  883. DEBUG_MSG_P(PSTR("Brightness: %d\n"), lightBrightness());
  884. terminalOK();
  885. });
  886. terminalRegisterCommand(F("CHANNEL"), [](Embedis* e) {
  887. if (e->argc < 2) {
  888. terminalError(F("Wrong arguments"));
  889. }
  890. int id = String(e->argv[1]).toInt();
  891. if (e->argc > 2) {
  892. int value = String(e->argv[2]).toInt();
  893. lightChannel(id, value);
  894. lightUpdate(true, true);
  895. }
  896. DEBUG_MSG_P(PSTR("Channel #%d (%s): %d\n"), id, lightDesc(id).c_str(), lightChannel(id));
  897. terminalOK();
  898. });
  899. terminalRegisterCommand(F("COLOR"), [](Embedis* e) {
  900. if (e->argc > 1) {
  901. String color = String(e->argv[1]);
  902. lightColor(color.c_str());
  903. lightUpdate(true, true);
  904. }
  905. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  906. terminalOK();
  907. });
  908. terminalRegisterCommand(F("KELVIN"), [](Embedis* e) {
  909. if (e->argc > 1) {
  910. String color = String("K") + String(e->argv[1]);
  911. lightColor(color.c_str());
  912. lightUpdate(true, true);
  913. }
  914. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  915. terminalOK();
  916. });
  917. terminalRegisterCommand(F("MIRED"), [](Embedis* e) {
  918. if (e->argc > 1) {
  919. const String value(e->argv[1]);
  920. String color = String("M");
  921. if( value.length() > 0 ) {
  922. if( value[0] == '+' || value[0] == '-' ) {
  923. color += String(_light_mireds + String(e->argv[1]).toInt());
  924. } else {
  925. color += String(e->argv[1]);
  926. }
  927. lightColor(color.c_str());
  928. lightUpdate(true, true);
  929. }
  930. }
  931. DEBUG_MSG_P(PSTR("Color: %s\n"), lightColor().c_str());
  932. terminalOK();
  933. });
  934. }
  935. #endif // TERMINAL_SUPPORT
  936. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  937. unsigned long getIOMux(unsigned long gpio) {
  938. unsigned long muxes[16] = {
  939. PERIPHS_IO_MUX_GPIO0_U, PERIPHS_IO_MUX_U0TXD_U, PERIPHS_IO_MUX_GPIO2_U, PERIPHS_IO_MUX_U0RXD_U,
  940. PERIPHS_IO_MUX_GPIO4_U, PERIPHS_IO_MUX_GPIO5_U, PERIPHS_IO_MUX_SD_CLK_U, PERIPHS_IO_MUX_SD_DATA0_U,
  941. PERIPHS_IO_MUX_SD_DATA1_U, PERIPHS_IO_MUX_SD_DATA2_U, PERIPHS_IO_MUX_SD_DATA3_U, PERIPHS_IO_MUX_SD_CMD_U,
  942. PERIPHS_IO_MUX_MTDI_U, PERIPHS_IO_MUX_MTCK_U, PERIPHS_IO_MUX_MTMS_U, PERIPHS_IO_MUX_MTDO_U
  943. };
  944. return muxes[gpio];
  945. }
  946. unsigned long getIOFunc(unsigned long gpio) {
  947. unsigned long funcs[16] = {
  948. FUNC_GPIO0, FUNC_GPIO1, FUNC_GPIO2, FUNC_GPIO3,
  949. FUNC_GPIO4, FUNC_GPIO5, FUNC_GPIO6, FUNC_GPIO7,
  950. FUNC_GPIO8, FUNC_GPIO9, FUNC_GPIO10, FUNC_GPIO11,
  951. FUNC_GPIO12, FUNC_GPIO13, FUNC_GPIO14, FUNC_GPIO15
  952. };
  953. return funcs[gpio];
  954. }
  955. #endif
  956. void _lightConfigure() {
  957. _light_has_color = getSetting("useColor", LIGHT_USE_COLOR).toInt() == 1;
  958. if (_light_has_color && (_light_channel.size() < 3)) {
  959. _light_has_color = false;
  960. setSetting("useColor", _light_has_color);
  961. }
  962. _light_use_white = getSetting("useWhite", LIGHT_USE_WHITE).toInt() == 1;
  963. if (_light_use_white && (_light_channel.size() < 4) && (_light_channel.size() != 2)) {
  964. _light_use_white = false;
  965. setSetting("useWhite", _light_use_white);
  966. }
  967. if (_light_has_color) {
  968. if (_light_use_white) {
  969. _light_brightness_func = _lightApplyBrightnessColor;
  970. } else {
  971. _light_brightness_func = []() { _lightApplyBrightness(3); };
  972. }
  973. } else {
  974. _light_brightness_func = []() { _lightApplyBrightness(); };
  975. }
  976. _light_use_cct = getSetting("useCCT", LIGHT_USE_CCT).toInt() == 1;
  977. if (_light_use_cct && (((_light_channel.size() < 5) && (_light_channel.size() != 2)) || !_light_use_white)) {
  978. _light_use_cct = false;
  979. setSetting("useCCT", _light_use_cct);
  980. }
  981. _light_use_gamma = getSetting("useGamma", LIGHT_USE_GAMMA).toInt() == 1;
  982. _light_use_transitions = getSetting("useTransitions", LIGHT_USE_TRANSITIONS).toInt() == 1;
  983. _light_transition_time = getSetting("lightTime", LIGHT_TRANSITION_TIME).toInt();
  984. }
  985. void lightSetup() {
  986. #ifdef LIGHT_ENABLE_PIN
  987. pinMode(LIGHT_ENABLE_PIN, OUTPUT);
  988. digitalWrite(LIGHT_ENABLE_PIN, HIGH);
  989. #endif
  990. _light_channel.reserve(LIGHT_CHANNELS);
  991. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  992. _my92xx = new my92xx(MY92XX_MODEL, MY92XX_CHIPS, MY92XX_DI_PIN, MY92XX_DCKI_PIN, MY92XX_COMMAND);
  993. for (unsigned char i=0; i<LIGHT_CHANNELS; i++) {
  994. _light_channel.push_back((channel_t) {0, false, true, 0, 0, 0});
  995. }
  996. #endif
  997. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  998. #ifdef LIGHT_CH1_PIN
  999. _light_channel.push_back((channel_t) {LIGHT_CH1_PIN, LIGHT_CH1_INVERSE, true, 0, 0, 0});
  1000. #endif
  1001. #ifdef LIGHT_CH2_PIN
  1002. _light_channel.push_back((channel_t) {LIGHT_CH2_PIN, LIGHT_CH2_INVERSE, true, 0, 0, 0});
  1003. #endif
  1004. #ifdef LIGHT_CH3_PIN
  1005. _light_channel.push_back((channel_t) {LIGHT_CH3_PIN, LIGHT_CH3_INVERSE, true, 0, 0, 0});
  1006. #endif
  1007. #ifdef LIGHT_CH4_PIN
  1008. _light_channel.push_back((channel_t) {LIGHT_CH4_PIN, LIGHT_CH4_INVERSE, true, 0, 0, 0});
  1009. #endif
  1010. #ifdef LIGHT_CH5_PIN
  1011. _light_channel.push_back((channel_t) {LIGHT_CH5_PIN, LIGHT_CH5_INVERSE, true, 0, 0, 0});
  1012. #endif
  1013. uint32 pwm_duty_init[PWM_CHANNEL_NUM_MAX];
  1014. uint32 io_info[PWM_CHANNEL_NUM_MAX][3];
  1015. for (unsigned int i=0; i < _light_channel.size(); i++) {
  1016. pwm_duty_init[i] = 0;
  1017. io_info[i][0] = getIOMux(_light_channel[i].pin);
  1018. io_info[i][1] = getIOFunc(_light_channel[i].pin);
  1019. io_info[i][2] = _light_channel[i].pin;
  1020. pinMode(_light_channel[i].pin, OUTPUT);
  1021. }
  1022. pwm_init(LIGHT_MAX_PWM, pwm_duty_init, PWM_CHANNEL_NUM_MAX, io_info);
  1023. pwm_start();
  1024. #endif
  1025. DEBUG_MSG_P(PSTR("[LIGHT] LIGHT_PROVIDER = %d\n"), LIGHT_PROVIDER);
  1026. DEBUG_MSG_P(PSTR("[LIGHT] Number of channels: %d\n"), _light_channel.size());
  1027. _lightConfigure();
  1028. if (rtcmemStatus()) {
  1029. _lightRestoreRtcmem();
  1030. } else {
  1031. _lightRestoreSettings();
  1032. }
  1033. #if WEB_SUPPORT
  1034. wsOnSendRegister(_lightWebSocketOnSend);
  1035. wsOnActionRegister(_lightWebSocketOnAction);
  1036. wsOnReceiveRegister(_lightWebSocketOnReceive);
  1037. #endif
  1038. #if API_SUPPORT
  1039. _lightAPISetup();
  1040. #endif
  1041. #if MQTT_SUPPORT
  1042. mqttRegister(_lightMQTTCallback);
  1043. #endif
  1044. #if TERMINAL_SUPPORT
  1045. _lightInitCommands();
  1046. #endif
  1047. // Main callbacks
  1048. espurnaRegisterReload([]() {
  1049. #if LIGHT_SAVE_ENABLED == 0
  1050. lightSave();
  1051. #endif
  1052. _lightConfigure();
  1053. });
  1054. }
  1055. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE