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.

938 lines
27 KiB

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