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.

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