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.

855 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. #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. void _lightAPISetup() {
  522. #if WEB_SUPPORT
  523. // API entry points (protected with apikey)
  524. if (lightHasColor()) {
  525. // DEPRECATE
  526. apiRegister(MQTT_TOPIC_COLOR, MQTT_TOPIC_COLOR,
  527. [](char * buffer, size_t len) {
  528. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  529. _toRGB(buffer, len, false);
  530. } else {
  531. _toLong(buffer, len, false);
  532. }
  533. },
  534. [](const char * payload) {
  535. lightColor(payload, true);
  536. lightUpdate(true, true);
  537. }
  538. );
  539. apiRegister(MQTT_TOPIC_COLOR_RGB, MQTT_TOPIC_COLOR_RGB,
  540. [](char * buffer, size_t len) {
  541. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  542. _toRGB(buffer, len, false);
  543. } else {
  544. _toLong(buffer, len, false);
  545. }
  546. },
  547. [](const char * payload) {
  548. lightColor(payload, true);
  549. lightUpdate(true, true);
  550. }
  551. );
  552. apiRegister(MQTT_TOPIC_COLOR_HSV, MQTT_TOPIC_COLOR_HSV,
  553. [](char * buffer, size_t len) {
  554. _toHSV(buffer, len);
  555. },
  556. [](const char * payload) {
  557. lightColor(payload, false);
  558. lightUpdate(true, true);
  559. }
  560. );
  561. apiRegister(MQTT_TOPIC_BRIGHTNESS, MQTT_TOPIC_BRIGHTNESS,
  562. [](char * buffer, size_t len) {
  563. snprintf_P(buffer, len, PSTR("%d"), _brightness);
  564. },
  565. [](const char * payload) {
  566. lightBrightness(atoi(payload));
  567. lightUpdate(true, true);
  568. }
  569. );
  570. apiRegister(MQTT_TOPIC_KELVIN, MQTT_TOPIC_KELVIN,
  571. [](char * buffer, size_t len) {},
  572. [](const char * payload) {
  573. _fromKelvin(atol(payload));
  574. lightUpdate(true, true);
  575. }
  576. );
  577. apiRegister(MQTT_TOPIC_MIRED, MQTT_TOPIC_MIRED,
  578. [](char * buffer, size_t len) {},
  579. [](const char * payload) {
  580. _fromMireds(atol(payload));
  581. lightUpdate(true, true);
  582. }
  583. );
  584. }
  585. for (unsigned int id=0; id<lightChannels(); id++) {
  586. char url[15];
  587. snprintf_P(url, sizeof(url), PSTR("%s/%d"), MQTT_TOPIC_CHANNEL, id);
  588. char key[10];
  589. snprintf_P(key, sizeof(key), PSTR("%s%d"), MQTT_TOPIC_CHANNEL, id);
  590. apiRegister(url, key,
  591. [id](char * buffer, size_t len) {
  592. snprintf_P(buffer, len, PSTR("%d"), lightChannel(id));
  593. },
  594. [id](const char * payload) {
  595. lightChannel(id, atoi(payload));
  596. lightUpdate(true, true);
  597. }
  598. );
  599. }
  600. #endif // WEB_SUPPORT
  601. }
  602. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  603. unsigned long getIOMux(unsigned long gpio) {
  604. unsigned long muxes[16] = {
  605. PERIPHS_IO_MUX_GPIO0_U, PERIPHS_IO_MUX_U0TXD_U, PERIPHS_IO_MUX_GPIO2_U, PERIPHS_IO_MUX_U0RXD_U,
  606. PERIPHS_IO_MUX_GPIO4_U, PERIPHS_IO_MUX_GPIO5_U, PERIPHS_IO_MUX_SD_CLK_U, PERIPHS_IO_MUX_SD_DATA0_U,
  607. PERIPHS_IO_MUX_SD_DATA1_U, PERIPHS_IO_MUX_SD_DATA2_U, PERIPHS_IO_MUX_SD_DATA3_U, PERIPHS_IO_MUX_SD_CMD_U,
  608. PERIPHS_IO_MUX_MTDI_U, PERIPHS_IO_MUX_MTCK_U, PERIPHS_IO_MUX_MTMS_U, PERIPHS_IO_MUX_MTDO_U
  609. };
  610. return muxes[gpio];
  611. }
  612. unsigned long getIOFunc(unsigned long gpio) {
  613. unsigned long funcs[16] = {
  614. FUNC_GPIO0, FUNC_GPIO1, FUNC_GPIO2, FUNC_GPIO3,
  615. FUNC_GPIO4, FUNC_GPIO5, FUNC_GPIO6, FUNC_GPIO7,
  616. FUNC_GPIO8, FUNC_GPIO9, FUNC_GPIO10, FUNC_GPIO11,
  617. FUNC_GPIO12, FUNC_GPIO13, FUNC_GPIO14, FUNC_GPIO15
  618. };
  619. return funcs[gpio];
  620. }
  621. #endif
  622. void lightSetup() {
  623. #ifdef LIGHT_ENABLE_PIN
  624. pinMode(LIGHT_ENABLE_PIN, OUTPUT);
  625. #endif
  626. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
  627. _my92xx = new my92xx(MY92XX_MODEL, MY92XX_CHIPS, MY92XX_DI_PIN, MY92XX_DCKI_PIN, MY92XX_COMMAND);
  628. for (unsigned char i=0; i<LIGHT_CHANNELS; i++) {
  629. _channels.push_back((channel_t) {0, false, 0});
  630. }
  631. #endif
  632. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  633. #ifdef LIGHT_CH1_PIN
  634. _channels.push_back((channel_t) {LIGHT_CH1_PIN, LIGHT_CH1_INVERSE, 0});
  635. #endif
  636. #ifdef LIGHT_CH2_PIN
  637. _channels.push_back((channel_t) {LIGHT_CH2_PIN, LIGHT_CH2_INVERSE, 0});
  638. #endif
  639. #ifdef LIGHT_CH3_PIN
  640. _channels.push_back((channel_t) {LIGHT_CH3_PIN, LIGHT_CH3_INVERSE, 0});
  641. #endif
  642. #ifdef LIGHT_CH4_PIN
  643. _channels.push_back((channel_t) {LIGHT_CH4_PIN, LIGHT_CH4_INVERSE, 0});
  644. #endif
  645. #ifdef LIGHT_CH5_PIN
  646. _channels.push_back((channel_t) {LIGHT_CH5_PIN, LIGHT_CH5_INVERSE, 0});
  647. #endif
  648. uint32 pwm_duty_init[PWM_CHANNEL_NUM_MAX];
  649. uint32 io_info[PWM_CHANNEL_NUM_MAX][3];
  650. for (unsigned int i=0; i < _channels.size(); i++) {
  651. pwm_duty_init[i] = 0;
  652. io_info[i][0] = getIOMux(_channels[i].pin);
  653. io_info[i][1] = getIOFunc(_channels[i].pin);
  654. io_info[i][2] = _channels[i].pin;
  655. pinMode(_channels[i].pin, OUTPUT);
  656. }
  657. pwm_init(LIGHT_MAX_PWM, pwm_duty_init, PWM_CHANNEL_NUM_MAX, io_info);
  658. pwm_start();
  659. #endif
  660. DEBUG_MSG_P(PSTR("[LIGHT] LIGHT_PROVIDER = %d\n"), LIGHT_PROVIDER);
  661. DEBUG_MSG_P(PSTR("[LIGHT] Number of channels: %d\n"), _channels.size());
  662. _lightColorRestore();
  663. _lightAPISetup();
  664. mqttRegister(_lightMQTTCallback);
  665. }
  666. void lightLoop(){
  667. }
  668. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE