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.

857 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_MY92XX
  27. #include <my92xx.h>
  28. my92xx * _my92xx;
  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_MY92XX
  303. ARRAYINIT(unsigned char, channels, MY92XX_MAPPING);
  304. for (unsigned char i=0; i<_channels.size(); i++) {
  305. _my92xx->setChannel(channels[i], _toPWM(i));
  306. }
  307. _my92xx->setState(_lightState);
  308. _my92xx->update();
  309. #endif
  310. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  311. for (unsigned int i=0; i < _channels.size(); i++) {
  312. pwm_set_duty(_toPWM(i), i);
  313. }
  314. pwm_start();
  315. #endif
  316. }
  317. // -----------------------------------------------------------------------------
  318. // PERSISTANCE
  319. // -----------------------------------------------------------------------------
  320. void _lightColorSave() {
  321. for (unsigned int i=0; i < _channels.size(); i++) {
  322. setSetting("ch", i, _channels[i].value);
  323. }
  324. setSetting("brightness", _brightness);
  325. saveSettings();
  326. }
  327. void _lightColorRestore() {
  328. for (unsigned int i=0; i < _channels.size(); i++) {
  329. _channels[i].value = getSetting("ch", i, i==0 ? 255 : 0).toInt();
  330. }
  331. _brightness = getSetting("brightness", LIGHT_MAX_BRIGHTNESS).toInt();
  332. lightUpdate(false, false);
  333. }
  334. // -----------------------------------------------------------------------------
  335. // MQTT
  336. // -----------------------------------------------------------------------------
  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. // -----------------------------------------------------------------------------
  391. // API
  392. // -----------------------------------------------------------------------------
  393. unsigned char lightChannels() {
  394. return _channels.size();
  395. }
  396. bool lightHasColor() {
  397. bool useColor = getSetting("useColor", LIGHT_USE_COLOR).toInt() == 1;
  398. return useColor && (_channels.size() > 2);
  399. }
  400. unsigned char lightWhiteChannels() {
  401. return _channels.size() % 3;
  402. }
  403. void lightMQTT() {
  404. char buffer[12];
  405. if (lightHasColor()) {
  406. // Color
  407. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  408. _toRGB(buffer, sizeof(buffer), false);
  409. } else {
  410. _toLong(buffer, sizeof(buffer), false);
  411. }
  412. mqttSend(MQTT_TOPIC_COLOR, buffer); // DEPRECATE
  413. mqttSend(MQTT_TOPIC_COLOR_RGB, buffer);
  414. _toHSV(buffer, sizeof(buffer));
  415. mqttSend(MQTT_TOPIC_COLOR_HSV, buffer);
  416. // Brightness
  417. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _brightness);
  418. mqttSend(MQTT_TOPIC_BRIGHTNESS, buffer);
  419. }
  420. // Channels
  421. for (unsigned int i=0; i < _channels.size(); i++) {
  422. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _channels[i].value);
  423. mqttSend(MQTT_TOPIC_CHANNEL, i, buffer);
  424. }
  425. }
  426. void lightUpdate(bool save, bool forward) {
  427. _lightProviderUpdate();
  428. // Report color & brightness to MQTT broker
  429. if (forward) lightMQTT();
  430. // Report color to WS clients (using current brightness setting)
  431. #if WEB_SUPPORT
  432. {
  433. DynamicJsonBuffer jsonBuffer;
  434. JsonObject& root = jsonBuffer.createObject();
  435. root["colorVisible"] = 1;
  436. root["useColor"] = getSetting("useColor", LIGHT_USE_COLOR).toInt() == 1;
  437. root["useWhite"] = getSetting("useWhite", LIGHT_USE_WHITE).toInt() == 1;
  438. root["useGamma"] = getSetting("useGamma", LIGHT_USE_GAMMA).toInt() == 1;
  439. if (lightHasColor()) {
  440. bool useRGB = getSetting("useRGB", LIGHT_USE_RGB).toInt() == 1;
  441. if (useRGB) {
  442. root["rgb"] = lightColor(true);
  443. root["brightness"] = lightBrightness();
  444. } else {
  445. root["hsv"] = lightColor(false);
  446. }
  447. }
  448. JsonArray& channels = root.createNestedArray("channels");
  449. for (unsigned char id=0; id < lightChannels(); id++) {
  450. channels.add(lightChannel(id));
  451. }
  452. String output;
  453. root.printTo(output);
  454. wsSend(output.c_str());
  455. }
  456. #endif
  457. #if LIGHT_SAVE_ENABLED
  458. // Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
  459. if (save) colorTicker.once(LIGHT_SAVE_DELAY, _lightColorSave);
  460. #endif
  461. };
  462. #if LIGHT_SAVE_ENABLED == 0
  463. void lightSave() {
  464. _lightColorSave();
  465. }
  466. #endif
  467. void lightState(bool state) {
  468. _lightState = state;
  469. }
  470. bool lightState() {
  471. return _lightState;
  472. }
  473. void lightColor(const char * color, bool rgb) {
  474. DEBUG_MSG_P(PSTR("[LIGHT] %s: %s\n"), rgb ? "RGB" : "HSV", color);
  475. if (rgb) {
  476. _fromRGB(color);
  477. } else {
  478. _fromHSV(color);
  479. }
  480. }
  481. void lightColor(const char * color) {
  482. lightColor(color, true);
  483. }
  484. void lightColor(unsigned long color) {
  485. _fromLong(color, false);
  486. }
  487. String lightColor(bool rgb) {
  488. char str[12];
  489. if (rgb) {
  490. _toRGB(str, sizeof(str), false);
  491. } else {
  492. _toHSV(str, sizeof(str));
  493. }
  494. return String(str);
  495. }
  496. String lightColor() {
  497. return lightColor(true);
  498. }
  499. unsigned int lightChannel(unsigned char id) {
  500. if (id <= _channels.size()) {
  501. return _channels[id].value;
  502. }
  503. return 0;
  504. }
  505. void lightChannel(unsigned char id, unsigned int value) {
  506. if (id <= _channels.size()) {
  507. _channels[id].value = constrain(value, 0, LIGHT_MAX_VALUE);
  508. }
  509. }
  510. unsigned int lightBrightness() {
  511. return _brightness;
  512. }
  513. void lightBrightness(int b) {
  514. _brightness = constrain(b, 0, LIGHT_MAX_BRIGHTNESS);
  515. }
  516. void lightBrightnessStep(int steps) {
  517. lightBrightness(_brightness + steps * LIGHT_STEP);
  518. }
  519. // -----------------------------------------------------------------------------
  520. // SETUP
  521. // -----------------------------------------------------------------------------
  522. void _lightAPISetup() {
  523. #if WEB_SUPPORT
  524. // API entry points (protected with apikey)
  525. if (lightHasColor()) {
  526. // DEPRECATE
  527. apiRegister(MQTT_TOPIC_COLOR, MQTT_TOPIC_COLOR,
  528. [](char * buffer, size_t len) {
  529. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  530. _toRGB(buffer, len, false);
  531. } else {
  532. _toLong(buffer, len, false);
  533. }
  534. },
  535. [](const char * payload) {
  536. lightColor(payload, true);
  537. lightUpdate(true, true);
  538. }
  539. );
  540. apiRegister(MQTT_TOPIC_COLOR_RGB, MQTT_TOPIC_COLOR_RGB,
  541. [](char * buffer, size_t len) {
  542. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  543. _toRGB(buffer, len, false);
  544. } else {
  545. _toLong(buffer, len, false);
  546. }
  547. },
  548. [](const char * payload) {
  549. lightColor(payload, true);
  550. lightUpdate(true, true);
  551. }
  552. );
  553. apiRegister(MQTT_TOPIC_COLOR_HSV, MQTT_TOPIC_COLOR_HSV,
  554. [](char * buffer, size_t len) {
  555. _toHSV(buffer, len);
  556. },
  557. [](const char * payload) {
  558. lightColor(payload, false);
  559. lightUpdate(true, true);
  560. }
  561. );
  562. apiRegister(MQTT_TOPIC_BRIGHTNESS, MQTT_TOPIC_BRIGHTNESS,
  563. [](char * buffer, size_t len) {
  564. snprintf_P(buffer, len, PSTR("%d"), _brightness);
  565. },
  566. [](const char * payload) {
  567. lightBrightness(atoi(payload));
  568. lightUpdate(true, true);
  569. }
  570. );
  571. apiRegister(MQTT_TOPIC_KELVIN, MQTT_TOPIC_KELVIN,
  572. [](char * buffer, size_t len) {},
  573. [](const char * payload) {
  574. _fromKelvin(atol(payload));
  575. lightUpdate(true, true);
  576. }
  577. );
  578. apiRegister(MQTT_TOPIC_MIRED, MQTT_TOPIC_MIRED,
  579. [](char * buffer, size_t len) {},
  580. [](const char * payload) {
  581. _fromMireds(atol(payload));
  582. lightUpdate(true, true);
  583. }
  584. );
  585. }
  586. for (unsigned int id=0; id<lightChannels(); id++) {
  587. char url[15];
  588. snprintf_P(url, sizeof(url), PSTR("%s/%d"), MQTT_TOPIC_CHANNEL, id);
  589. char key[10];
  590. snprintf_P(key, sizeof(key), PSTR("%s%d"), MQTT_TOPIC_CHANNEL, id);
  591. apiRegister(url, key,
  592. [id](char * buffer, size_t len) {
  593. snprintf_P(buffer, len, PSTR("%d"), lightChannel(id));
  594. },
  595. [id](const char * payload) {
  596. lightChannel(id, atoi(payload));
  597. lightUpdate(true, true);
  598. }
  599. );
  600. }
  601. #endif // WEB_SUPPORT
  602. }
  603. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  604. unsigned long getIOMux(unsigned long gpio) {
  605. unsigned long muxes[16] = {
  606. PERIPHS_IO_MUX_GPIO0_U, PERIPHS_IO_MUX_U0TXD_U, PERIPHS_IO_MUX_GPIO2_U, PERIPHS_IO_MUX_U0RXD_U,
  607. PERIPHS_IO_MUX_GPIO4_U, PERIPHS_IO_MUX_GPIO5_U, PERIPHS_IO_MUX_SD_CLK_U, PERIPHS_IO_MUX_SD_DATA0_U,
  608. PERIPHS_IO_MUX_SD_DATA1_U, PERIPHS_IO_MUX_SD_DATA2_U, PERIPHS_IO_MUX_SD_DATA3_U, PERIPHS_IO_MUX_SD_CMD_U,
  609. PERIPHS_IO_MUX_MTDI_U, PERIPHS_IO_MUX_MTCK_U, PERIPHS_IO_MUX_MTMS_U, PERIPHS_IO_MUX_MTDO_U
  610. };
  611. return muxes[gpio];
  612. }
  613. unsigned long getIOFunc(unsigned long gpio) {
  614. unsigned long funcs[16] = {
  615. FUNC_GPIO0, FUNC_GPIO1, FUNC_GPIO2, FUNC_GPIO3,
  616. FUNC_GPIO4, FUNC_GPIO5, FUNC_GPIO6, FUNC_GPIO7,
  617. FUNC_GPIO8, FUNC_GPIO9, FUNC_GPIO10, FUNC_GPIO11,
  618. FUNC_GPIO12, FUNC_GPIO13, FUNC_GPIO14, FUNC_GPIO15
  619. };
  620. return funcs[gpio];
  621. }
  622. #endif
  623. void lightSetup() {
  624. #ifdef LIGHT_ENABLE_PIN
  625. pinMode(LIGHT_ENABLE_PIN, OUTPUT);
  626. #endif
  627. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  628. _my92xx = new my92xx(MY92XX_MODEL, MY92XX_CHIPS, MY92XX_DI_PIN, MY92XX_DCKI_PIN, MY92XX_COMMAND);
  629. for (unsigned char i=0; i<LIGHT_CHANNELS; i++) {
  630. _channels.push_back((channel_t) {0, false, 0});
  631. }
  632. #endif
  633. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  634. #ifdef LIGHT_CH1_PIN
  635. _channels.push_back((channel_t) {LIGHT_CH1_PIN, LIGHT_CH1_INVERSE, 0});
  636. #endif
  637. #ifdef LIGHT_CH2_PIN
  638. _channels.push_back((channel_t) {LIGHT_CH2_PIN, LIGHT_CH2_INVERSE, 0});
  639. #endif
  640. #ifdef LIGHT_CH3_PIN
  641. _channels.push_back((channel_t) {LIGHT_CH3_PIN, LIGHT_CH3_INVERSE, 0});
  642. #endif
  643. #ifdef LIGHT_CH4_PIN
  644. _channels.push_back((channel_t) {LIGHT_CH4_PIN, LIGHT_CH4_INVERSE, 0});
  645. #endif
  646. #ifdef LIGHT_CH5_PIN
  647. _channels.push_back((channel_t) {LIGHT_CH5_PIN, LIGHT_CH5_INVERSE, 0});
  648. #endif
  649. uint32 pwm_duty_init[PWM_CHANNEL_NUM_MAX];
  650. uint32 io_info[PWM_CHANNEL_NUM_MAX][3];
  651. for (unsigned int i=0; i < _channels.size(); i++) {
  652. pwm_duty_init[i] = 0;
  653. io_info[i][0] = getIOMux(_channels[i].pin);
  654. io_info[i][1] = getIOFunc(_channels[i].pin);
  655. io_info[i][2] = _channels[i].pin;
  656. pinMode(_channels[i].pin, OUTPUT);
  657. }
  658. pwm_init(LIGHT_MAX_PWM, pwm_duty_init, PWM_CHANNEL_NUM_MAX, io_info);
  659. pwm_start();
  660. #endif
  661. DEBUG_MSG_P(PSTR("[LIGHT] LIGHT_PROVIDER = %d\n"), LIGHT_PROVIDER);
  662. DEBUG_MSG_P(PSTR("[LIGHT] Number of channels: %d\n"), _channels.size());
  663. _lightColorRestore();
  664. _lightAPISetup();
  665. mqttRegister(_lightMQTTCallback);
  666. }
  667. void lightLoop(){
  668. }
  669. #endif // LIGHT_PROVIDER_EXPERIMENTAL_RGB_ONLY_HSV_IR
  670. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE