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.

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