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.

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