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.

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