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.

925 lines
26 KiB

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