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.

976 lines
28 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. LIGHT MODULE
  3. Copyright (C) 2016-2017 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. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  10. extern "C" {
  11. #include "libs/pwm.h"
  12. }
  13. #endif
  14. // -----------------------------------------------------------------------------
  15. Ticker _light_save_ticker;
  16. Ticker _light_transition_ticker;
  17. typedef struct {
  18. unsigned char pin;
  19. bool reverse;
  20. unsigned char value; // target or nominal value
  21. unsigned char shadow; // represented value
  22. double current; // transition value
  23. } channel_t;
  24. std::vector<channel_t> _light_channel;
  25. bool _light_state = false;
  26. bool _light_use_transitions = false;
  27. bool _light_has_color = false;
  28. bool _light_use_white = false;
  29. bool _light_use_gamma = false;
  30. unsigned long _light_steps_left = 1;
  31. unsigned int _light_brightness = LIGHT_MAX_BRIGHTNESS;
  32. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  33. #include <my92xx.h>
  34. my92xx * _my92xx;
  35. ARRAYINIT(unsigned char, _light_channel_map, MY92XX_MAPPING);
  36. #endif
  37. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  38. #define PWM_CHANNEL_NUM_MAX LIGHT_CHANNELS
  39. extern "C" {
  40. #include "libs/pwm.h"
  41. }
  42. #endif
  43. #if LIGHT_PROVIDER == LIGHT_PROVIDER_FASTLED
  44. #include <FastLED.h>
  45. CRGB _fastleds[LIGHT_FASTLED_NUM];
  46. #endif
  47. // Gamma Correction lookup table (8 bit)
  48. // TODO: move to PROGMEM
  49. const unsigned char _light_gamma_table[] = {
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
  52. 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6,
  53. 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11,
  54. 12, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19,
  55. 19, 20, 20, 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28,
  56. 29, 30, 30, 31, 32, 33, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40,
  57. 41, 42, 43, 43, 44, 45, 46, 47, 48, 49, 50, 50, 51, 52, 53, 54,
  58. 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71,
  59. 72, 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89,
  60. 91, 92, 93, 94, 96, 97, 98, 100, 101, 102, 104, 105, 106, 108, 109, 110,
  61. 112, 113, 115, 116, 118, 119, 121, 122, 123, 125, 126, 128, 130, 131, 133, 134,
  62. 136, 137, 139, 140, 142, 144, 145, 147, 149, 150, 152, 154, 155, 157, 159, 160,
  63. 162, 164, 166, 167, 169, 171, 173, 175, 176, 178, 180, 182, 184, 186, 187, 189,
  64. 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221,
  65. 223, 225, 227, 229, 231, 233, 235, 238, 240, 242, 244, 246, 248, 251, 253, 255
  66. };
  67. // -----------------------------------------------------------------------------
  68. // UTILS
  69. // -----------------------------------------------------------------------------
  70. void _fromLong(unsigned long value, bool brightness) {
  71. if (brightness) {
  72. _light_channel[0].value = (value >> 24) & 0xFF;
  73. _light_channel[1].value = (value >> 16) & 0xFF;
  74. _light_channel[2].value = (value >> 8) & 0xFF;
  75. _light_brightness = (value & 0xFF) * LIGHT_MAX_BRIGHTNESS / 255;
  76. } else {
  77. _light_channel[0].value = (value >> 16) & 0xFF;
  78. _light_channel[1].value = (value >> 8) & 0xFF;
  79. _light_channel[2].value = (value) & 0xFF;
  80. }
  81. }
  82. void _fromRGB(const char * rgb) {
  83. char * p = (char *) rgb;
  84. if (strlen(p) == 0) return;
  85. // if color begins with a # then assume HEX RGB
  86. if (p[0] == '#') {
  87. if (_light_has_color) {
  88. ++p;
  89. unsigned long value = strtoul(p, NULL, 16);
  90. // RGBA values are interpreted like RGB + brightness
  91. _fromLong(value, strlen(p) > 7);
  92. }
  93. // it's a temperature in mireds
  94. } else if (p[0] == 'M') {
  95. if (_light_has_color) {
  96. unsigned long mireds = atol(p + 1);
  97. _fromMireds(mireds);
  98. }
  99. // it's a temperature in kelvin
  100. } else if (p[0] == 'K') {
  101. if (_light_has_color) {
  102. unsigned long kelvin = atol(p + 1);
  103. _fromKelvin(kelvin);
  104. }
  105. // otherwise assume decimal values separated by commas
  106. } else {
  107. char * tok;
  108. unsigned char count = 0;
  109. unsigned char channels = _light_channel.size();
  110. tok = strtok(p, ",");
  111. while (tok != NULL) {
  112. _light_channel[count].value = atoi(tok);
  113. if (++count == channels) break;
  114. tok = strtok(NULL, ",");
  115. }
  116. // RGB but less than 3 values received
  117. if (_light_has_color && (count < 3)) {
  118. _light_channel[1].value = _light_channel[0].value;
  119. _light_channel[2].value = _light_channel[0].value;
  120. }
  121. }
  122. }
  123. void _toRGB(char * rgb, size_t len, bool applyBrightness) {
  124. if (!_light_has_color) return;
  125. float b = applyBrightness ? (float) _light_brightness / LIGHT_MAX_BRIGHTNESS : 1;
  126. unsigned long value = 0;
  127. value += _light_channel[0].value * b;
  128. value <<= 8;
  129. value += _light_channel[1].value * b;
  130. value <<= 8;
  131. value += _light_channel[2].value * b;
  132. snprintf_P(rgb, len, PSTR("#%06X"), value);
  133. }
  134. void _toRGB(char * rgb, size_t len) {
  135. _toRGB(rgb, len, false);
  136. }
  137. // HSV string is expected to be "H,S,V", where:
  138. // 0 <= H <= 360
  139. // 0 <= S <= 100
  140. // 0 <= V <= 100
  141. void _fromHSV(const char * hsv) {
  142. char * ptr = (char *) hsv;
  143. if (strlen(ptr) == 0) return;
  144. if (!_light_has_color) return;
  145. char * tok;
  146. unsigned char count = 0;
  147. unsigned int value[3] = {0};
  148. tok = strtok(ptr, ",");
  149. while (tok != NULL) {
  150. value[count] = atoi(tok);
  151. if (++count == 3) break;
  152. tok = strtok(NULL, ",");
  153. }
  154. if (count != 3) return;
  155. // HSV to RGB transformation -----------------------------------------------
  156. double h = (value[0] == 360) ? 0 : (double) value[0] / 60.0;
  157. double f = (h - floor(h));
  158. double s = (double) value[1] / 100.0;
  159. unsigned char v = round((double) value[2] * 255.0 / 100.0);
  160. unsigned char p = round(v * (1.0 - s));
  161. unsigned char q = round(v * (1.0 - s * f));
  162. unsigned char t = round(v * (1.0 - s * (1.0 - f)));
  163. switch (int(h)) {
  164. case 0:
  165. _light_channel[0].value = v;
  166. _light_channel[1].value = t;
  167. _light_channel[2].value = p;
  168. break;
  169. case 1:
  170. _light_channel[0].value = q;
  171. _light_channel[1].value = v;
  172. _light_channel[2].value = p;
  173. break;
  174. case 2:
  175. _light_channel[0].value = p;
  176. _light_channel[1].value = v;
  177. _light_channel[2].value = t;
  178. break;
  179. case 3:
  180. _light_channel[0].value = p;
  181. _light_channel[1].value = q;
  182. _light_channel[2].value = v;
  183. break;
  184. case 4:
  185. _light_channel[0].value = t;
  186. _light_channel[1].value = p;
  187. _light_channel[2].value = v;
  188. break;
  189. case 5:
  190. _light_channel[0].value = v;
  191. _light_channel[1].value = p;
  192. _light_channel[2].value = q;
  193. break;
  194. default:
  195. _light_channel[0].value = 0;
  196. _light_channel[1].value = 0;
  197. _light_channel[2].value = 0;
  198. break;
  199. }
  200. _light_brightness = LIGHT_MAX_BRIGHTNESS;
  201. }
  202. void _toHSV(char * hsv, size_t len) {
  203. if (!_light_has_color) return;
  204. double min, max;
  205. double h, s, v;
  206. double r = (double) _light_channel[0].value / 255.0;
  207. double g = (double) _light_channel[1].value / 255.0;
  208. double b = (double) _light_channel[2].value / 255.0;
  209. min = (r < g) ? r : g;
  210. min = (min < b) ? min : b;
  211. max = (r > g) ? r : g;
  212. max = (max > b) ? max : b;
  213. v = 100.0 * max;
  214. if (v == 0) {
  215. h = s = 0;
  216. } else {
  217. s = 100.0 * (max - min) / max;
  218. if (s == 0) {
  219. h = 0;
  220. } else {
  221. if (max == r) {
  222. if (g >= b) {
  223. h = 0.0 + 60.0 * (g - b) / (max - min);
  224. } else {
  225. h = 360.0 + 60.0 * (g - b) / (max - min);
  226. }
  227. } else if (max == g) {
  228. h = 120.0 + 60.0 * (b - r) / (max - min);
  229. } else {
  230. h = 240.0 + 60.0 * (r - g) / (max - min);
  231. }
  232. }
  233. }
  234. // String
  235. snprintf_P(hsv, len, PSTR("%d,%d,%d"), round(h), round(s), round(v));
  236. }
  237. void _toLong(char * color, size_t len, bool applyBrightness) {
  238. if (!_light_has_color) return;
  239. float b = applyBrightness ? (float) _light_brightness / LIGHT_MAX_BRIGHTNESS : 1;
  240. snprintf_P(color, len, PSTR("%d,%d,%d"),
  241. (int) (_light_channel[0].value * b),
  242. (int) (_light_channel[1].value * b),
  243. (int) (_light_channel[2].value * b)
  244. );
  245. }
  246. void _toLong(char * color, size_t len) {
  247. _toLong(color, len, false);
  248. }
  249. // Thanks to Sacha Telgenhof for sharing this code in his AiLight library
  250. // https://github.com/stelgenhof/AiLight
  251. void _fromKelvin(unsigned long kelvin) {
  252. // Check we have RGB channels
  253. if (!_light_has_color) return;
  254. // Calculate colors
  255. unsigned int red = (kelvin <= 66)
  256. ? LIGHT_MAX_VALUE
  257. : 329.698727446 * pow((kelvin - 60), -0.1332047592);
  258. unsigned int green = (kelvin <= 66)
  259. ? 99.4708025861 * log(kelvin) - 161.1195681661
  260. : 288.1221695283 * pow(kelvin, -0.0755148492);
  261. unsigned int blue = (kelvin >= 66)
  262. ? LIGHT_MAX_VALUE
  263. : ((kelvin <= 19)
  264. ? 0
  265. : 138.5177312231 * log(kelvin - 10) - 305.0447927307);
  266. // Save values
  267. _light_channel[0].value = constrain(red, 0, LIGHT_MAX_VALUE);
  268. _light_channel[1].value = constrain(green, 0, LIGHT_MAX_VALUE);
  269. _light_channel[2].value = constrain(blue, 0, LIGHT_MAX_VALUE);
  270. }
  271. // Color temperature is measured in mireds (kelvin = 1e6/mired)
  272. void _fromMireds(unsigned long mireds) {
  273. if (mireds == 0) mireds = 1;
  274. unsigned long kelvin = constrain(1000000UL / mireds, 1000, 40000) / 100;
  275. _fromKelvin(kelvin);
  276. }
  277. // -----------------------------------------------------------------------------
  278. // PROVIDER
  279. // -----------------------------------------------------------------------------
  280. unsigned int _toPWM(unsigned long value, bool gamma, bool reverse) {
  281. value = constrain(value, 0, LIGHT_MAX_VALUE);
  282. if (gamma) value = _light_gamma_table[value];
  283. if (LIGHT_MAX_VALUE != LIGHT_LIMIT_PWM) value = map(value, 0, LIGHT_MAX_VALUE, 0, LIGHT_LIMIT_PWM);
  284. if (reverse) value = LIGHT_LIMIT_PWM - value;
  285. return value;
  286. }
  287. // Returns a PWM value for the given channel ID
  288. unsigned int _toPWM(unsigned char id) {
  289. bool useGamma = _light_use_gamma && _light_has_color && (id < 3);
  290. return _toPWM(_light_channel[id].shadow, useGamma, _light_channel[id].reverse);
  291. }
  292. void _shadow() {
  293. // Update transition ticker
  294. _light_steps_left--;
  295. if (_light_steps_left == 0) _light_transition_ticker.detach();
  296. // Transitions
  297. unsigned char target;
  298. for (unsigned int i=0; i < _light_channel.size(); i++) {
  299. if (_light_state) {
  300. target = _light_channel[i].value;
  301. if ((_light_brightness < LIGHT_MAX_BRIGHTNESS) && _light_has_color && (i < 3)) {
  302. target *= ((float) _light_brightness / LIGHT_MAX_BRIGHTNESS);
  303. }
  304. } else {
  305. target = 0;
  306. }
  307. if (_light_steps_left == 0) {
  308. _light_channel[i].current = target;
  309. } else {
  310. double difference = (double) (target - _light_channel[i].current) / (_light_steps_left + 1);
  311. _light_channel[i].current = _light_channel[i].current + difference;
  312. }
  313. _light_channel[i].shadow = _light_channel[i].current;
  314. }
  315. // Use white channel for same RGB
  316. if (_light_use_white && _light_has_color) {
  317. if (_light_channel[0].shadow == _light_channel[1].shadow && _light_channel[1].shadow == _light_channel[2].shadow ) {
  318. _light_channel[3].shadow = _light_channel[0].shadow * ((float) _light_brightness / LIGHT_MAX_BRIGHTNESS);
  319. _light_channel[2].shadow = 0;
  320. _light_channel[1].shadow = 0;
  321. _light_channel[0].shadow = 0;
  322. }
  323. }
  324. }
  325. void _lightProviderUpdate() {
  326. _shadow();
  327. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  328. for (unsigned char i=0; i<_light_channel.size(); i++) {
  329. _my92xx->setChannel(_light_channel_map[i], _toPWM(i));
  330. }
  331. _my92xx->setState(true);
  332. _my92xx->update();
  333. #endif
  334. #if LIGHT_PROVIDER == LIGHT_PROVIDER_FASTLED
  335. for (CRGB & pixel : _fastleds) {
  336. pixel = CRGB(_toPWM(0), _toPWM(1), _toPWM(2));
  337. }
  338. FastLED.show();
  339. #endif
  340. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  341. for (unsigned int i=0; i < _light_channel.size(); i++) {
  342. pwm_set_duty(_toPWM(i), i);
  343. }
  344. pwm_start();
  345. #endif
  346. }
  347. // -----------------------------------------------------------------------------
  348. // PERSISTANCE
  349. // -----------------------------------------------------------------------------
  350. void _lightColorSave() {
  351. for (unsigned int i=0; i < _light_channel.size(); i++) {
  352. setSetting("ch", i, _light_channel[i].value);
  353. }
  354. setSetting("brightness", _light_brightness);
  355. saveSettings();
  356. }
  357. void _lightColorRestore() {
  358. for (unsigned int i=0; i < _light_channel.size(); i++) {
  359. _light_channel[i].value = getSetting("ch", i, i==0 ? 255 : 0).toInt();
  360. }
  361. _light_brightness = getSetting("brightness", LIGHT_MAX_BRIGHTNESS).toInt();
  362. lightUpdate(false, false);
  363. }
  364. // -----------------------------------------------------------------------------
  365. // MQTT
  366. // -----------------------------------------------------------------------------
  367. #if MQTT_SUPPORT
  368. void _lightMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  369. if (type == MQTT_CONNECT_EVENT) {
  370. if (_light_has_color) {
  371. mqttSubscribe(MQTT_TOPIC_BRIGHTNESS);
  372. mqttSubscribe(MQTT_TOPIC_MIRED);
  373. mqttSubscribe(MQTT_TOPIC_KELVIN);
  374. mqttSubscribe(MQTT_TOPIC_COLOR); // DEPRECATE
  375. mqttSubscribe(MQTT_TOPIC_COLOR_RGB);
  376. mqttSubscribe(MQTT_TOPIC_COLOR_HSV);
  377. }
  378. char buffer[strlen(MQTT_TOPIC_CHANNEL) + 3];
  379. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_CHANNEL);
  380. mqttSubscribe(buffer);
  381. }
  382. if (type == MQTT_MESSAGE_EVENT) {
  383. // Match topic
  384. String t = mqttSubtopic((char *) topic);
  385. // Color temperature in mireds
  386. if (t.equals(MQTT_TOPIC_MIRED)) {
  387. _fromMireds(atol(payload));
  388. lightUpdate(true, mqttForward());
  389. }
  390. // Color temperature in kelvins
  391. if (t.equals(MQTT_TOPIC_KELVIN)) {
  392. _fromKelvin(atol(payload));
  393. lightUpdate(true, mqttForward());
  394. }
  395. // Color
  396. if (t.equals(MQTT_TOPIC_COLOR) || t.equals(MQTT_TOPIC_COLOR_RGB)) { // DEPRECATE MQTT_TOPIC_COLOR
  397. lightColor(payload, true);
  398. lightUpdate(true, mqttForward());
  399. }
  400. if (t.equals(MQTT_TOPIC_COLOR_HSV)) {
  401. lightColor(payload, false);
  402. lightUpdate(true, mqttForward());
  403. }
  404. // Brightness
  405. if (t.equals(MQTT_TOPIC_BRIGHTNESS)) {
  406. _light_brightness = constrain(atoi(payload), 0, LIGHT_MAX_BRIGHTNESS);
  407. lightUpdate(true, mqttForward());
  408. }
  409. // Channel
  410. if (t.startsWith(MQTT_TOPIC_CHANNEL)) {
  411. unsigned int channelID = t.substring(strlen(MQTT_TOPIC_CHANNEL)+1).toInt();
  412. if (channelID >= _light_channel.size()) {
  413. DEBUG_MSG_P(PSTR("[LIGHT] Wrong channelID (%d)\n"), channelID);
  414. return;
  415. }
  416. lightChannel(channelID, atoi(payload));
  417. lightUpdate(true, mqttForward());
  418. }
  419. }
  420. }
  421. void lightMQTT() {
  422. char buffer[12];
  423. if (_light_has_color) {
  424. // Color
  425. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  426. _toRGB(buffer, sizeof(buffer), false);
  427. } else {
  428. _toLong(buffer, sizeof(buffer), false);
  429. }
  430. mqttSend(MQTT_TOPIC_COLOR, buffer); // DEPRECATE
  431. mqttSend(MQTT_TOPIC_COLOR_RGB, buffer);
  432. _toHSV(buffer, sizeof(buffer));
  433. mqttSend(MQTT_TOPIC_COLOR_HSV, buffer);
  434. // Brightness
  435. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _light_brightness);
  436. mqttSend(MQTT_TOPIC_BRIGHTNESS, buffer);
  437. }
  438. // Channels
  439. for (unsigned int i=0; i < _light_channel.size(); i++) {
  440. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _light_channel[i].value);
  441. mqttSend(MQTT_TOPIC_CHANNEL, i, buffer);
  442. }
  443. }
  444. #endif
  445. // -----------------------------------------------------------------------------
  446. // API
  447. // -----------------------------------------------------------------------------
  448. unsigned char lightChannels() {
  449. return _light_channel.size();
  450. }
  451. bool lightHasColor() {
  452. return _light_has_color;
  453. }
  454. unsigned char lightWhiteChannels() {
  455. return _light_channel.size() % 3;
  456. }
  457. void lightUpdate(bool save, bool forward) {
  458. // Configure color transition
  459. _light_steps_left = _light_use_transitions ? LIGHT_TRANSITION_STEPS : 1;
  460. _light_transition_ticker.attach_ms(LIGHT_TRANSITION_STEP, _lightProviderUpdate);
  461. // Report color & brightness to MQTT broker
  462. #if MQTT_SUPPORT
  463. if (forward) lightMQTT();
  464. #endif
  465. // Report color to WS clients (using current brightness setting)
  466. #if WEB_SUPPORT
  467. wsSend(_lightWebSocketOnSend);
  468. #endif
  469. #if LIGHT_SAVE_ENABLED
  470. // Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
  471. if (save) _light_save_ticker.once(LIGHT_SAVE_DELAY, _lightColorSave);
  472. #endif
  473. };
  474. #if LIGHT_SAVE_ENABLED == 0
  475. void lightSave() {
  476. _lightColorSave();
  477. }
  478. #endif
  479. void lightState(bool state) {
  480. _light_state = state;
  481. }
  482. bool lightState() {
  483. return _light_state;
  484. }
  485. void lightColor(const char * color, bool rgb) {
  486. DEBUG_MSG_P(PSTR("[LIGHT] %s: %s\n"), rgb ? "RGB" : "HSV", color);
  487. if (rgb) {
  488. _fromRGB(color);
  489. } else {
  490. _fromHSV(color);
  491. }
  492. }
  493. void lightColor(const char * color) {
  494. lightColor(color, true);
  495. }
  496. void lightColor(unsigned long color) {
  497. _fromLong(color, false);
  498. }
  499. String lightColor(bool rgb) {
  500. char str[12];
  501. if (rgb) {
  502. _toRGB(str, sizeof(str), false);
  503. } else {
  504. _toHSV(str, sizeof(str));
  505. }
  506. return String(str);
  507. }
  508. String lightColor() {
  509. return lightColor(true);
  510. }
  511. unsigned int lightChannel(unsigned char id) {
  512. if (id <= _light_channel.size()) {
  513. return _light_channel[id].value;
  514. }
  515. return 0;
  516. }
  517. void lightChannel(unsigned char id, unsigned int value) {
  518. if (id <= _light_channel.size()) {
  519. _light_channel[id].value = constrain(value, 0, LIGHT_MAX_VALUE);
  520. }
  521. }
  522. unsigned int lightBrightness() {
  523. return _light_brightness;
  524. }
  525. void lightBrightness(int b) {
  526. _light_brightness = constrain(b, 0, LIGHT_MAX_BRIGHTNESS);
  527. }
  528. void lightBrightnessStep(int steps) {
  529. lightBrightness(_light_brightness + steps * LIGHT_STEP);
  530. }
  531. // -----------------------------------------------------------------------------
  532. // SETUP
  533. // -----------------------------------------------------------------------------
  534. #if WEB_SUPPORT
  535. void _lightWebSocketOnSend(JsonObject& root) {
  536. root["colorVisible"] = 1;
  537. root["useColor"] = _light_has_color;
  538. root["useWhite"] = _light_use_white;
  539. root["useGamma"] = _light_use_gamma;
  540. root["useTransitions"] = _light_use_transitions;
  541. root["useCSS"] = getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1;
  542. bool useRGB = getSetting("useRGB", LIGHT_USE_RGB).toInt() == 1;
  543. root["useRGB"] = useRGB;
  544. if (_light_has_color) {
  545. if (useRGB) {
  546. root["rgb"] = lightColor(true);
  547. root["brightness"] = lightBrightness();
  548. } else {
  549. root["hsv"] = lightColor(false);
  550. }
  551. }
  552. JsonArray& channels = root.createNestedArray("channels");
  553. for (unsigned char id=0; id < lightChannels(); id++) {
  554. channels.add(lightChannel(id));
  555. }
  556. }
  557. void _lightWebSocketOnAction(const char * action, JsonObject& data) {
  558. if (_light_has_color) {
  559. if (strcmp(action, "color") == 0) {
  560. if (data.containsKey("rgb")) {
  561. lightColor(data["rgb"], true);
  562. lightUpdate(true, true);
  563. }
  564. if (data.containsKey("hsv")) {
  565. lightColor(data["hsv"], false);
  566. lightUpdate(true, true);
  567. }
  568. if (data.containsKey("brightness")) {
  569. lightBrightness(data["brightness"]);
  570. lightUpdate(true, true);
  571. }
  572. }
  573. }
  574. if (strcmp(action, "channel") == 0) {
  575. if (data.containsKey("id") && data.containsKey("value")) {
  576. lightChannel(data["id"], data["value"]);
  577. lightUpdate(true, true);
  578. }
  579. }
  580. }
  581. void _lightAPISetup() {
  582. // API entry points (protected with apikey)
  583. if (_light_has_color) {
  584. // DEPRECATE
  585. apiRegister(MQTT_TOPIC_COLOR,
  586. [](char * buffer, size_t len) {
  587. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  588. _toRGB(buffer, len, false);
  589. } else {
  590. _toLong(buffer, len, false);
  591. }
  592. },
  593. [](const char * payload) {
  594. lightColor(payload, true);
  595. lightUpdate(true, true);
  596. }
  597. );
  598. apiRegister(MQTT_TOPIC_COLOR_RGB,
  599. [](char * buffer, size_t len) {
  600. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  601. _toRGB(buffer, len, false);
  602. } else {
  603. _toLong(buffer, len, false);
  604. }
  605. },
  606. [](const char * payload) {
  607. lightColor(payload, true);
  608. lightUpdate(true, true);
  609. }
  610. );
  611. apiRegister(MQTT_TOPIC_COLOR_HSV,
  612. [](char * buffer, size_t len) {
  613. _toHSV(buffer, len);
  614. },
  615. [](const char * payload) {
  616. lightColor(payload, false);
  617. lightUpdate(true, true);
  618. }
  619. );
  620. apiRegister(MQTT_TOPIC_BRIGHTNESS,
  621. [](char * buffer, size_t len) {
  622. snprintf_P(buffer, len, PSTR("%d"), _light_brightness);
  623. },
  624. [](const char * payload) {
  625. lightBrightness(atoi(payload));
  626. lightUpdate(true, true);
  627. }
  628. );
  629. apiRegister(MQTT_TOPIC_KELVIN,
  630. [](char * buffer, size_t len) {},
  631. [](const char * payload) {
  632. _fromKelvin(atol(payload));
  633. lightUpdate(true, true);
  634. }
  635. );
  636. apiRegister(MQTT_TOPIC_MIRED,
  637. [](char * buffer, size_t len) {},
  638. [](const char * payload) {
  639. _fromMireds(atol(payload));
  640. lightUpdate(true, true);
  641. }
  642. );
  643. }
  644. for (unsigned int id=0; id<lightChannels(); id++) {
  645. char key[15];
  646. snprintf_P(key, sizeof(key), PSTR("%s/%d"), MQTT_TOPIC_CHANNEL, id);
  647. apiRegister(key,
  648. [id](char * buffer, size_t len) {
  649. snprintf_P(buffer, len, PSTR("%d"), lightChannel(id));
  650. },
  651. [id](const char * payload) {
  652. lightChannel(id, atoi(payload));
  653. lightUpdate(true, true);
  654. }
  655. );
  656. }
  657. }
  658. #endif // WEB_SUPPORT
  659. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  660. unsigned long getIOMux(unsigned long gpio) {
  661. unsigned long muxes[16] = {
  662. PERIPHS_IO_MUX_GPIO0_U, PERIPHS_IO_MUX_U0TXD_U, PERIPHS_IO_MUX_GPIO2_U, PERIPHS_IO_MUX_U0RXD_U,
  663. PERIPHS_IO_MUX_GPIO4_U, PERIPHS_IO_MUX_GPIO5_U, PERIPHS_IO_MUX_SD_CLK_U, PERIPHS_IO_MUX_SD_DATA0_U,
  664. PERIPHS_IO_MUX_SD_DATA1_U, PERIPHS_IO_MUX_SD_DATA2_U, PERIPHS_IO_MUX_SD_DATA3_U, PERIPHS_IO_MUX_SD_CMD_U,
  665. PERIPHS_IO_MUX_MTDI_U, PERIPHS_IO_MUX_MTCK_U, PERIPHS_IO_MUX_MTMS_U, PERIPHS_IO_MUX_MTDO_U
  666. };
  667. return muxes[gpio];
  668. }
  669. unsigned long getIOFunc(unsigned long gpio) {
  670. unsigned long funcs[16] = {
  671. FUNC_GPIO0, FUNC_GPIO1, FUNC_GPIO2, FUNC_GPIO3,
  672. FUNC_GPIO4, FUNC_GPIO5, FUNC_GPIO6, FUNC_GPIO7,
  673. FUNC_GPIO8, FUNC_GPIO9, FUNC_GPIO10, FUNC_GPIO11,
  674. FUNC_GPIO12, FUNC_GPIO13, FUNC_GPIO14, FUNC_GPIO15
  675. };
  676. return funcs[gpio];
  677. }
  678. #endif
  679. void _lightConfigure() {
  680. _light_has_color = getSetting("useColor", LIGHT_USE_COLOR).toInt() == 1;
  681. if (_light_has_color && (_light_channel.size() < 3)) {
  682. _light_has_color = false;
  683. setSetting("useColor", _light_has_color);
  684. }
  685. _light_use_white = getSetting("useWhite", LIGHT_USE_WHITE).toInt() == 1;
  686. if (_light_use_white && (_light_channel.size() < 4)) {
  687. _light_use_white = false;
  688. setSetting("useWhite", _light_use_white);
  689. }
  690. _light_use_gamma = getSetting("useGamma", LIGHT_USE_GAMMA).toInt() == 1;
  691. _light_use_transitions = getSetting("useTransitions", LIGHT_USE_TRANSITIONS).toInt() == 1;
  692. }
  693. void lightSetup() {
  694. #ifdef LIGHT_ENABLE_PIN
  695. pinMode(LIGHT_ENABLE_PIN, OUTPUT);
  696. digitalWrite(LIGHT_ENABLE_PIN, HIGH);
  697. #endif
  698. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  699. _my92xx = new my92xx(MY92XX_MODEL, MY92XX_CHIPS, MY92XX_DI_PIN, MY92XX_DCKI_PIN, MY92XX_COMMAND);
  700. for (unsigned char i=0; i<LIGHT_CHANNELS; i++) {
  701. _light_channel.push_back((channel_t) {0, false, 0, 0, 0});
  702. }
  703. #endif
  704. #if LIGHT_PROVIDER == LIGHT_PROVIDER_FASTLED
  705. #if LIGHT_FASTLED_TYPE == NEOPIXEL
  706. FastLED.addLeds<LIGHT_FASTLED_TYPE, LIGHT_FASTLED_DATA_PIN>(_fastleds, LIGHT_FASTLED_NUM);
  707. #else
  708. #ifdef LIGHT_FASTLED_CLOCK_PIN
  709. FastLED.addLeds<LIGHT_FASTLED_TYPE, LIGHT_FASTLED_DATA_PIN, LIGHT_FASTLED_CLOCK_PIN, LIGHT_FASTLED_ORDER>(_fastleds, LIGHT_FASTLED_NUM);
  710. #else
  711. FastLED.addLeds<LIGHT_FASTLED_TYPE, LIGHT_FASTLED_DATA_PIN, LIGHT_FASTLED_ORDER>(_fastleds, LIGHT_FASTLED_NUM);
  712. #endif // LIGHT_FASTLED_CLOCK_PIN
  713. #endif // LIGHT_FASTLED_TYPE == NEOPIXEL
  714. for (unsigned char i=0; i<LIGHT_CHANNELS; i++) {
  715. _light_channel.push_back((channel_t) {0, false, 0, 0, 0});
  716. }
  717. #endif
  718. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  719. #ifdef LIGHT_CH1_PIN
  720. _light_channel.push_back((channel_t) {LIGHT_CH1_PIN, LIGHT_CH1_INVERSE, 0, 0, 0});
  721. #endif
  722. #ifdef LIGHT_CH2_PIN
  723. _light_channel.push_back((channel_t) {LIGHT_CH2_PIN, LIGHT_CH2_INVERSE, 0, 0, 0});
  724. #endif
  725. #ifdef LIGHT_CH3_PIN
  726. _light_channel.push_back((channel_t) {LIGHT_CH3_PIN, LIGHT_CH3_INVERSE, 0, 0, 0});
  727. #endif
  728. #ifdef LIGHT_CH4_PIN
  729. _light_channel.push_back((channel_t) {LIGHT_CH4_PIN, LIGHT_CH4_INVERSE, 0, 0, 0});
  730. #endif
  731. #ifdef LIGHT_CH5_PIN
  732. _light_channel.push_back((channel_t) {LIGHT_CH5_PIN, LIGHT_CH5_INVERSE, 0, 0, 0});
  733. #endif
  734. uint32 pwm_duty_init[PWM_CHANNEL_NUM_MAX];
  735. uint32 io_info[PWM_CHANNEL_NUM_MAX][3];
  736. for (unsigned int i=0; i < _light_channel.size(); i++) {
  737. pwm_duty_init[i] = 0;
  738. io_info[i][0] = getIOMux(_light_channel[i].pin);
  739. io_info[i][1] = getIOFunc(_light_channel[i].pin);
  740. io_info[i][2] = _light_channel[i].pin;
  741. pinMode(_light_channel[i].pin, OUTPUT);
  742. }
  743. pwm_init(LIGHT_MAX_PWM, pwm_duty_init, PWM_CHANNEL_NUM_MAX, io_info);
  744. pwm_start();
  745. #endif
  746. DEBUG_MSG_P(PSTR("[LIGHT] LIGHT_PROVIDER = %d\n"), LIGHT_PROVIDER);
  747. DEBUG_MSG_P(PSTR("[LIGHT] Number of channels: %d\n"), _light_channel.size());
  748. _lightColorRestore();
  749. _lightConfigure();
  750. #if WEB_SUPPORT
  751. _lightAPISetup();
  752. wsOnSendRegister(_lightWebSocketOnSend);
  753. wsOnActionRegister(_lightWebSocketOnAction);
  754. wsOnAfterParseRegister([]() {
  755. #if LIGHT_SAVE_ENABLED == 0
  756. lightSave();
  757. #endif
  758. _lightConfigure();
  759. });
  760. #endif
  761. #if MQTT_SUPPORT
  762. mqttRegister(_lightMQTTCallback);
  763. #endif
  764. }
  765. void lightLoop(){
  766. }
  767. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE