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.

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