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.

937 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. 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. // 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. // -----------------------------------------------------------------------------
  268. // PROVIDER
  269. // -----------------------------------------------------------------------------
  270. unsigned int _toPWM(unsigned long value, bool gamma, bool reverse) {
  271. value = constrain(value, 0, LIGHT_MAX_VALUE);
  272. if (gamma) value = _light_gamma_table[value];
  273. if (LIGHT_MAX_VALUE != LIGHT_LIMIT_PWM) value = map(value, 0, LIGHT_MAX_VALUE, 0, LIGHT_LIMIT_PWM);
  274. if (reverse) value = LIGHT_LIMIT_PWM - value;
  275. return value;
  276. }
  277. // Returns a PWM value for the given channel ID
  278. unsigned int _toPWM(unsigned char id) {
  279. bool useGamma = _light_use_gamma && _light_has_color && (id < 3);
  280. return _toPWM(_light_channel[id].shadow, useGamma, _light_channel[id].reverse);
  281. }
  282. void _shadow() {
  283. // Update transition ticker
  284. _light_steps_left--;
  285. if (_light_steps_left == 0) _light_transition_ticker.detach();
  286. // Transitions
  287. unsigned char target;
  288. for (unsigned int i=0; i < _light_channel.size(); i++) {
  289. if (_light_state) {
  290. target = _light_channel[i].value;
  291. if ((_light_brightness < LIGHT_MAX_BRIGHTNESS) && _light_has_color && (i < 3)) {
  292. target *= ((float) _light_brightness / LIGHT_MAX_BRIGHTNESS);
  293. }
  294. } else {
  295. target = 0;
  296. }
  297. if (_light_steps_left == 0) {
  298. _light_channel[i].current = target;
  299. } else {
  300. double difference = (double) (target - _light_channel[i].current) / (_light_steps_left + 1);
  301. _light_channel[i].current = _light_channel[i].current + difference;
  302. }
  303. _light_channel[i].shadow = _light_channel[i].current;
  304. }
  305. // Use white channel for same RGB
  306. if (_light_use_white && _light_has_color) {
  307. if (_light_channel[0].shadow == _light_channel[1].shadow && _light_channel[1].shadow == _light_channel[2].shadow ) {
  308. _light_channel[3].shadow = _light_channel[0].shadow * ((float) _light_brightness / LIGHT_MAX_BRIGHTNESS);
  309. _light_channel[2].shadow = 0;
  310. _light_channel[1].shadow = 0;
  311. _light_channel[0].shadow = 0;
  312. }
  313. }
  314. }
  315. void _lightProviderUpdate() {
  316. _shadow();
  317. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  318. for (unsigned char i=0; i<_light_channel.size(); i++) {
  319. _my92xx->setChannel(_light_channel_map[i], _toPWM(i));
  320. }
  321. _my92xx->setState(true);
  322. _my92xx->update();
  323. #endif
  324. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  325. for (unsigned int i=0; i < _light_channel.size(); i++) {
  326. pwm_set_duty(_toPWM(i), i);
  327. }
  328. pwm_start();
  329. #endif
  330. }
  331. // -----------------------------------------------------------------------------
  332. // PERSISTANCE
  333. // -----------------------------------------------------------------------------
  334. void _lightColorSave() {
  335. for (unsigned int i=0; i < _light_channel.size(); i++) {
  336. setSetting("ch", i, _light_channel[i].value);
  337. }
  338. setSetting("brightness", _light_brightness);
  339. saveSettings();
  340. }
  341. void _lightColorRestore() {
  342. for (unsigned int i=0; i < _light_channel.size(); i++) {
  343. _light_channel[i].value = getSetting("ch", i, i==0 ? 255 : 0).toInt();
  344. }
  345. _light_brightness = getSetting("brightness", LIGHT_MAX_BRIGHTNESS).toInt();
  346. lightUpdate(false, false);
  347. }
  348. // -----------------------------------------------------------------------------
  349. // MQTT
  350. // -----------------------------------------------------------------------------
  351. #if MQTT_SUPPORT
  352. void _lightMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  353. if (type == MQTT_CONNECT_EVENT) {
  354. if (_light_has_color) {
  355. mqttSubscribe(MQTT_TOPIC_BRIGHTNESS);
  356. mqttSubscribe(MQTT_TOPIC_MIRED);
  357. mqttSubscribe(MQTT_TOPIC_KELVIN);
  358. mqttSubscribe(MQTT_TOPIC_COLOR); // DEPRECATE
  359. mqttSubscribe(MQTT_TOPIC_COLOR_RGB);
  360. mqttSubscribe(MQTT_TOPIC_COLOR_HSV);
  361. }
  362. char buffer[strlen(MQTT_TOPIC_CHANNEL) + 3];
  363. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_CHANNEL);
  364. mqttSubscribe(buffer);
  365. }
  366. if (type == MQTT_MESSAGE_EVENT) {
  367. // Match topic
  368. String t = mqttSubtopic((char *) topic);
  369. // Color temperature in mireds
  370. if (t.equals(MQTT_TOPIC_MIRED)) {
  371. _fromMireds(atol(payload));
  372. lightUpdate(true, mqttForward());
  373. }
  374. // Color temperature in kelvins
  375. if (t.equals(MQTT_TOPIC_KELVIN)) {
  376. _fromKelvin(atol(payload));
  377. lightUpdate(true, mqttForward());
  378. }
  379. // Color
  380. if (t.equals(MQTT_TOPIC_COLOR) || t.equals(MQTT_TOPIC_COLOR_RGB)) { // DEPRECATE MQTT_TOPIC_COLOR
  381. lightColor(payload, true);
  382. lightUpdate(true, mqttForward());
  383. }
  384. if (t.equals(MQTT_TOPIC_COLOR_HSV)) {
  385. lightColor(payload, false);
  386. lightUpdate(true, mqttForward());
  387. }
  388. // Brightness
  389. if (t.equals(MQTT_TOPIC_BRIGHTNESS)) {
  390. _light_brightness = constrain(atoi(payload), 0, LIGHT_MAX_BRIGHTNESS);
  391. lightUpdate(true, mqttForward());
  392. }
  393. // Channel
  394. if (t.startsWith(MQTT_TOPIC_CHANNEL)) {
  395. unsigned int channelID = t.substring(strlen(MQTT_TOPIC_CHANNEL)+1).toInt();
  396. if (channelID >= _light_channel.size()) {
  397. DEBUG_MSG_P(PSTR("[LIGHT] Wrong channelID (%d)\n"), channelID);
  398. return;
  399. }
  400. lightChannel(channelID, atoi(payload));
  401. lightUpdate(true, mqttForward());
  402. }
  403. }
  404. }
  405. void lightMQTT() {
  406. char buffer[12];
  407. if (_light_has_color) {
  408. // Color
  409. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  410. _toRGB(buffer, sizeof(buffer), false);
  411. } else {
  412. _toLong(buffer, sizeof(buffer), false);
  413. }
  414. mqttSend(MQTT_TOPIC_COLOR, buffer); // DEPRECATE
  415. mqttSend(MQTT_TOPIC_COLOR_RGB, buffer);
  416. _toHSV(buffer, sizeof(buffer));
  417. mqttSend(MQTT_TOPIC_COLOR_HSV, buffer);
  418. // Brightness
  419. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _light_brightness);
  420. mqttSend(MQTT_TOPIC_BRIGHTNESS, buffer);
  421. }
  422. // Channels
  423. for (unsigned int i=0; i < _light_channel.size(); i++) {
  424. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _light_channel[i].value);
  425. mqttSend(MQTT_TOPIC_CHANNEL, i, buffer);
  426. }
  427. }
  428. #endif
  429. // -----------------------------------------------------------------------------
  430. // API
  431. // -----------------------------------------------------------------------------
  432. unsigned char lightChannels() {
  433. return _light_channel.size();
  434. }
  435. bool lightHasColor() {
  436. return _light_has_color;
  437. }
  438. unsigned char lightWhiteChannels() {
  439. return _light_channel.size() % 3;
  440. }
  441. void lightUpdate(bool save, bool forward) {
  442. // Configure color transition
  443. _light_steps_left = _light_use_transitions ? LIGHT_TRANSITION_STEPS : 1;
  444. _light_transition_ticker.attach_ms(LIGHT_TRANSITION_STEP, _lightProviderUpdate);
  445. // Report color & brightness to MQTT broker
  446. #if MQTT_SUPPORT
  447. if (forward) lightMQTT();
  448. #endif
  449. // Report color to WS clients (using current brightness setting)
  450. #if WEB_SUPPORT
  451. wsSend(_lightWebSocketOnSend);
  452. #endif
  453. #if LIGHT_SAVE_ENABLED
  454. // Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
  455. if (save) _light_save_ticker.once(LIGHT_SAVE_DELAY, _lightColorSave);
  456. #endif
  457. };
  458. #if LIGHT_SAVE_ENABLED == 0
  459. void lightSave() {
  460. _lightColorSave();
  461. }
  462. #endif
  463. void lightState(bool state) {
  464. _light_state = state;
  465. }
  466. bool lightState() {
  467. return _light_state;
  468. }
  469. void lightColor(const char * color, bool rgb) {
  470. DEBUG_MSG_P(PSTR("[LIGHT] %s: %s\n"), rgb ? "RGB" : "HSV", color);
  471. if (rgb) {
  472. _fromRGB(color);
  473. } else {
  474. _fromHSV(color);
  475. }
  476. }
  477. void lightColor(const char * color) {
  478. lightColor(color, true);
  479. }
  480. void lightColor(unsigned long color) {
  481. _fromLong(color, false);
  482. }
  483. String lightColor(bool rgb) {
  484. char str[12];
  485. if (rgb) {
  486. _toRGB(str, sizeof(str), false);
  487. } else {
  488. _toHSV(str, sizeof(str));
  489. }
  490. return String(str);
  491. }
  492. String lightColor() {
  493. return lightColor(true);
  494. }
  495. unsigned int lightChannel(unsigned char id) {
  496. if (id <= _light_channel.size()) {
  497. return _light_channel[id].value;
  498. }
  499. return 0;
  500. }
  501. void lightChannel(unsigned char id, unsigned int value) {
  502. if (id <= _light_channel.size()) {
  503. _light_channel[id].value = constrain(value, 0, LIGHT_MAX_VALUE);
  504. }
  505. }
  506. unsigned int lightBrightness() {
  507. return _light_brightness;
  508. }
  509. void lightBrightness(int b) {
  510. _light_brightness = constrain(b, 0, LIGHT_MAX_BRIGHTNESS);
  511. }
  512. void lightBrightnessStep(int steps) {
  513. lightBrightness(_light_brightness + steps * LIGHT_STEP);
  514. }
  515. // -----------------------------------------------------------------------------
  516. // SETUP
  517. // -----------------------------------------------------------------------------
  518. #if WEB_SUPPORT
  519. void _lightWebSocketOnSend(JsonObject& root) {
  520. root["colorVisible"] = 1;
  521. root["useColor"] = _light_has_color;
  522. root["useWhite"] = _light_use_white;
  523. root["useGamma"] = _light_use_gamma;
  524. root["useTransitions"] = _light_use_transitions;
  525. root["useCSS"] = getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1;
  526. bool useRGB = getSetting("useRGB", LIGHT_USE_RGB).toInt() == 1;
  527. root["useRGB"] = useRGB;
  528. if (_light_has_color) {
  529. if (useRGB) {
  530. root["rgb"] = lightColor(true);
  531. root["brightness"] = lightBrightness();
  532. } else {
  533. root["hsv"] = lightColor(false);
  534. }
  535. }
  536. JsonArray& channels = root.createNestedArray("channels");
  537. for (unsigned char id=0; id < lightChannels(); id++) {
  538. channels.add(lightChannel(id));
  539. }
  540. }
  541. void _lightWebSocketOnAction(const char * action, JsonObject& data) {
  542. if (_light_has_color) {
  543. if (strcmp(action, "color") == 0) {
  544. if (data.containsKey("rgb")) {
  545. lightColor(data["rgb"], true);
  546. lightUpdate(true, true);
  547. }
  548. if (data.containsKey("hsv")) {
  549. lightColor(data["hsv"], false);
  550. lightUpdate(true, true);
  551. }
  552. if (data.containsKey("brightness")) {
  553. lightBrightness(data["brightness"]);
  554. lightUpdate(true, true);
  555. }
  556. }
  557. }
  558. if (strcmp(action, "channel") == 0) {
  559. if (data.containsKey("id") && data.containsKey("value")) {
  560. lightChannel(data["id"], data["value"]);
  561. lightUpdate(true, true);
  562. }
  563. }
  564. }
  565. void _lightAPISetup() {
  566. // API entry points (protected with apikey)
  567. if (_light_has_color) {
  568. // DEPRECATE
  569. apiRegister(MQTT_TOPIC_COLOR,
  570. [](char * buffer, size_t len) {
  571. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  572. _toRGB(buffer, len, false);
  573. } else {
  574. _toLong(buffer, len, false);
  575. }
  576. },
  577. [](const char * payload) {
  578. lightColor(payload, true);
  579. lightUpdate(true, true);
  580. }
  581. );
  582. apiRegister(MQTT_TOPIC_COLOR_RGB,
  583. [](char * buffer, size_t len) {
  584. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  585. _toRGB(buffer, len, false);
  586. } else {
  587. _toLong(buffer, len, false);
  588. }
  589. },
  590. [](const char * payload) {
  591. lightColor(payload, true);
  592. lightUpdate(true, true);
  593. }
  594. );
  595. apiRegister(MQTT_TOPIC_COLOR_HSV,
  596. [](char * buffer, size_t len) {
  597. _toHSV(buffer, len);
  598. },
  599. [](const char * payload) {
  600. lightColor(payload, false);
  601. lightUpdate(true, true);
  602. }
  603. );
  604. apiRegister(MQTT_TOPIC_BRIGHTNESS,
  605. [](char * buffer, size_t len) {
  606. snprintf_P(buffer, len, PSTR("%d"), _light_brightness);
  607. },
  608. [](const char * payload) {
  609. lightBrightness(atoi(payload));
  610. lightUpdate(true, true);
  611. }
  612. );
  613. apiRegister(MQTT_TOPIC_KELVIN,
  614. [](char * buffer, size_t len) {},
  615. [](const char * payload) {
  616. _fromKelvin(atol(payload));
  617. lightUpdate(true, true);
  618. }
  619. );
  620. apiRegister(MQTT_TOPIC_MIRED,
  621. [](char * buffer, size_t len) {},
  622. [](const char * payload) {
  623. _fromMireds(atol(payload));
  624. lightUpdate(true, true);
  625. }
  626. );
  627. }
  628. for (unsigned int id=0; id<lightChannels(); id++) {
  629. char key[15];
  630. snprintf_P(key, sizeof(key), PSTR("%s/%d"), MQTT_TOPIC_CHANNEL, id);
  631. apiRegister(key,
  632. [id](char * buffer, size_t len) {
  633. snprintf_P(buffer, len, PSTR("%d"), lightChannel(id));
  634. },
  635. [id](const char * payload) {
  636. lightChannel(id, atoi(payload));
  637. lightUpdate(true, true);
  638. }
  639. );
  640. }
  641. }
  642. #endif // WEB_SUPPORT
  643. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  644. unsigned long getIOMux(unsigned long gpio) {
  645. unsigned long muxes[16] = {
  646. PERIPHS_IO_MUX_GPIO0_U, PERIPHS_IO_MUX_U0TXD_U, PERIPHS_IO_MUX_GPIO2_U, PERIPHS_IO_MUX_U0RXD_U,
  647. PERIPHS_IO_MUX_GPIO4_U, PERIPHS_IO_MUX_GPIO5_U, PERIPHS_IO_MUX_SD_CLK_U, PERIPHS_IO_MUX_SD_DATA0_U,
  648. PERIPHS_IO_MUX_SD_DATA1_U, PERIPHS_IO_MUX_SD_DATA2_U, PERIPHS_IO_MUX_SD_DATA3_U, PERIPHS_IO_MUX_SD_CMD_U,
  649. PERIPHS_IO_MUX_MTDI_U, PERIPHS_IO_MUX_MTCK_U, PERIPHS_IO_MUX_MTMS_U, PERIPHS_IO_MUX_MTDO_U
  650. };
  651. return muxes[gpio];
  652. }
  653. unsigned long getIOFunc(unsigned long gpio) {
  654. unsigned long funcs[16] = {
  655. FUNC_GPIO0, FUNC_GPIO1, FUNC_GPIO2, FUNC_GPIO3,
  656. FUNC_GPIO4, FUNC_GPIO5, FUNC_GPIO6, FUNC_GPIO7,
  657. FUNC_GPIO8, FUNC_GPIO9, FUNC_GPIO10, FUNC_GPIO11,
  658. FUNC_GPIO12, FUNC_GPIO13, FUNC_GPIO14, FUNC_GPIO15
  659. };
  660. return funcs[gpio];
  661. }
  662. #endif
  663. void _lightConfigure() {
  664. _light_has_color = getSetting("useColor", LIGHT_USE_COLOR).toInt() == 1;
  665. if (_light_has_color && (_light_channel.size() < 3)) {
  666. _light_has_color = false;
  667. setSetting("useColor", _light_has_color);
  668. }
  669. _light_use_white = getSetting("useWhite", LIGHT_USE_WHITE).toInt() == 1;
  670. if (_light_use_white && (_light_channel.size() < 4)) {
  671. _light_use_white = false;
  672. setSetting("useWhite", _light_use_white);
  673. }
  674. _light_use_gamma = getSetting("useGamma", LIGHT_USE_GAMMA).toInt() == 1;
  675. _light_use_transitions = getSetting("useTransitions", LIGHT_USE_TRANSITIONS).toInt() == 1;
  676. }
  677. void lightSetup() {
  678. #ifdef LIGHT_ENABLE_PIN
  679. pinMode(LIGHT_ENABLE_PIN, OUTPUT);
  680. digitalWrite(LIGHT_ENABLE_PIN, HIGH);
  681. #endif
  682. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  683. _my92xx = new my92xx(MY92XX_MODEL, MY92XX_CHIPS, MY92XX_DI_PIN, MY92XX_DCKI_PIN, MY92XX_COMMAND);
  684. for (unsigned char i=0; i<LIGHT_CHANNELS; i++) {
  685. _light_channel.push_back((channel_t) {0, false, 0, 0, 0});
  686. }
  687. #endif
  688. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  689. #ifdef LIGHT_CH1_PIN
  690. _light_channel.push_back((channel_t) {LIGHT_CH1_PIN, LIGHT_CH1_INVERSE, 0, 0, 0});
  691. #endif
  692. #ifdef LIGHT_CH2_PIN
  693. _light_channel.push_back((channel_t) {LIGHT_CH2_PIN, LIGHT_CH2_INVERSE, 0, 0, 0});
  694. #endif
  695. #ifdef LIGHT_CH3_PIN
  696. _light_channel.push_back((channel_t) {LIGHT_CH3_PIN, LIGHT_CH3_INVERSE, 0, 0, 0});
  697. #endif
  698. #ifdef LIGHT_CH4_PIN
  699. _light_channel.push_back((channel_t) {LIGHT_CH4_PIN, LIGHT_CH4_INVERSE, 0, 0, 0});
  700. #endif
  701. #ifdef LIGHT_CH5_PIN
  702. _light_channel.push_back((channel_t) {LIGHT_CH5_PIN, LIGHT_CH5_INVERSE, 0, 0, 0});
  703. #endif
  704. uint32 pwm_duty_init[PWM_CHANNEL_NUM_MAX];
  705. uint32 io_info[PWM_CHANNEL_NUM_MAX][3];
  706. for (unsigned int i=0; i < _light_channel.size(); i++) {
  707. pwm_duty_init[i] = 0;
  708. io_info[i][0] = getIOMux(_light_channel[i].pin);
  709. io_info[i][1] = getIOFunc(_light_channel[i].pin);
  710. io_info[i][2] = _light_channel[i].pin;
  711. pinMode(_light_channel[i].pin, OUTPUT);
  712. }
  713. pwm_init(LIGHT_MAX_PWM, pwm_duty_init, PWM_CHANNEL_NUM_MAX, io_info);
  714. pwm_start();
  715. #endif
  716. DEBUG_MSG_P(PSTR("[LIGHT] LIGHT_PROVIDER = %d\n"), LIGHT_PROVIDER);
  717. DEBUG_MSG_P(PSTR("[LIGHT] Number of channels: %d\n"), _light_channel.size());
  718. _lightColorRestore();
  719. _lightConfigure();
  720. #if WEB_SUPPORT
  721. _lightAPISetup();
  722. wsOnSendRegister(_lightWebSocketOnSend);
  723. wsOnActionRegister(_lightWebSocketOnAction);
  724. wsOnAfterParseRegister([]() {
  725. #if LIGHT_SAVE_ENABLED == 0
  726. lightSave();
  727. #endif
  728. _lightConfigure();
  729. });
  730. #endif
  731. #if MQTT_SUPPORT
  732. mqttRegister(_lightMQTTCallback);
  733. #endif
  734. }
  735. void lightLoop(){
  736. }
  737. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE