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.

686 lines
20 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. void _toLong(char * color, size_t len, bool applyBrightness) {
  121. if (!lightHasColor()) return;
  122. float b = applyBrightness ? (float) _brightness / LIGHT_MAX_BRIGHTNESS : 1;
  123. snprintf_P(color, len, PSTR("%d,%d,%d"),
  124. (int) (_channels[0].value * b),
  125. (int) (_channels[1].value * b),
  126. (int) (_channels[2].value * b)
  127. );
  128. }
  129. void _toLong(char * color, size_t len) {
  130. _toLong(color, len, false);
  131. }
  132. // Thanks to Sacha Telgenhof for sharing this code in his AiLight library
  133. // https://github.com/stelgenhof/AiLight
  134. void _fromKelvin(unsigned long kelvin) {
  135. // Check we have RGB channels
  136. if (!lightHasColor()) return;
  137. // Calculate colors
  138. unsigned int red = (kelvin <= 66)
  139. ? LIGHT_MAX_VALUE
  140. : 329.698727446 * pow((kelvin - 60), -0.1332047592);
  141. unsigned int green = (kelvin <= 66)
  142. ? 99.4708025861 * log(kelvin) - 161.1195681661
  143. : 288.1221695283 * pow(kelvin, -0.0755148492);
  144. unsigned int blue = (kelvin >= 66)
  145. ? LIGHT_MAX_VALUE
  146. : ((kelvin <= 19)
  147. ? 0
  148. : 138.5177312231 * log(kelvin - 10) - 305.0447927307);
  149. // Save values
  150. _channels[0].value = constrain(red, 0, LIGHT_MAX_VALUE);
  151. _channels[1].value = constrain(green, 0, LIGHT_MAX_VALUE);
  152. _channels[2].value = constrain(blue, 0, LIGHT_MAX_VALUE);
  153. }
  154. // Color temperature is measured in mireds (kelvin = 1e6/mired)
  155. void _fromMireds(unsigned long mireds) {
  156. if (mireds == 0) mireds = 1;
  157. unsigned long kelvin = constrain(1000000UL / mireds, 1000, 40000) / 100;
  158. _fromKelvin(kelvin);
  159. }
  160. unsigned int _toPWM(unsigned long value, bool bright, bool gamma, bool reverse) {
  161. value = constrain(value, 0, LIGHT_MAX_VALUE);
  162. if (bright) value *= ((float) _brightness / LIGHT_MAX_BRIGHTNESS);
  163. if (gamma) value = gamma_table[value];
  164. if (LIGHT_MAX_VALUE != LIGHT_LIMIT_PWM) value = map(value, 0, LIGHT_MAX_VALUE, 0, LIGHT_LIMIT_PWM);
  165. if (reverse) value = LIGHT_LIMIT_PWM - value;
  166. return value;
  167. }
  168. // Returns a PWM valule for the given channel ID
  169. unsigned int _toPWM(unsigned char id) {
  170. if (id < _channels.size()) {
  171. bool isColor = lightHasColor() && (id < 3);
  172. bool bright = isColor;
  173. bool gamma = isColor & (getSetting("useGamma", LIGHT_USE_GAMMA).toInt() == 1);
  174. return _toPWM(_channels[id].shadow, bright, gamma, _channels[id].reverse);
  175. }
  176. return 0;
  177. }
  178. // -----------------------------------------------------------------------------
  179. // PROVIDER
  180. // -----------------------------------------------------------------------------
  181. void _shadow() {
  182. for (unsigned int i=0; i < _channels.size(); i++) {
  183. _channels[i].shadow = _lightState ? _channels[i].value : 0;
  184. }
  185. if (lightHasColor()) {
  186. bool useWhite = getSetting("useWhite", LIGHT_USE_WHITE).toInt() == 1;
  187. if (_lightState && useWhite && (_channels.size() > 3)) {
  188. if (_channels[0].shadow == _channels[1].shadow && _channels[1].shadow == _channels[2].shadow ) {
  189. _channels[3].shadow = _channels[0].shadow * ((float) _brightness / LIGHT_MAX_BRIGHTNESS);
  190. _channels[2].shadow = 0;
  191. _channels[1].shadow = 0;
  192. _channels[0].shadow = 0;
  193. }
  194. }
  195. }
  196. }
  197. void _lightProviderUpdate() {
  198. _shadow();
  199. #ifdef LIGHT_ENABLE_PIN
  200. digitalWrite(LIGHT_ENABLE_PIN, _lightState);
  201. #endif
  202. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
  203. if (_lightState) {
  204. unsigned int red = _toPWM(0);
  205. unsigned int green = _toPWM(1);
  206. unsigned int blue = _toPWM(2);
  207. unsigned int white = _toPWM(3)
  208. unsigned int warm = _toPWM(4);
  209. _my9291->setColor((my9291_color_t) { red, green, blue, white, warm });
  210. _my9291->setState(true);
  211. } else {
  212. _my9291->setColor((my9291_color_t) { 0, 0, 0, 0, 0 });
  213. _my9291->setState(false);
  214. }
  215. #endif
  216. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  217. for (unsigned int i=0; i < _channels.size(); i++) {
  218. pwm_set_duty(_toPWM(i), i);
  219. }
  220. pwm_start();
  221. #endif
  222. }
  223. // -----------------------------------------------------------------------------
  224. // PERSISTANCE
  225. // -----------------------------------------------------------------------------
  226. void _lightColorSave() {
  227. for (unsigned int i=0; i < _channels.size(); i++) {
  228. setSetting("ch", i, _channels[i].value);
  229. }
  230. setSetting("brightness", _brightness);
  231. saveSettings();
  232. }
  233. void _lightColorRestore() {
  234. for (unsigned int i=0; i < _channels.size(); i++) {
  235. _channels[i].value = getSetting("ch", i, i==0 ? 255 : 0).toInt();
  236. }
  237. _brightness = getSetting("brightness", LIGHT_MAX_BRIGHTNESS).toInt();
  238. lightUpdate(false, false);
  239. }
  240. // -----------------------------------------------------------------------------
  241. // MQTT
  242. // -----------------------------------------------------------------------------
  243. void _lightMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  244. if (type == MQTT_CONNECT_EVENT) {
  245. if (lightHasColor()) {
  246. mqttSubscribe(MQTT_TOPIC_BRIGHTNESS);
  247. mqttSubscribe(MQTT_TOPIC_MIRED);
  248. mqttSubscribe(MQTT_TOPIC_KELVIN);
  249. mqttSubscribe(MQTT_TOPIC_COLOR);
  250. }
  251. char buffer[strlen(MQTT_TOPIC_CHANNEL) + 3];
  252. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_CHANNEL);
  253. mqttSubscribe(buffer);
  254. }
  255. if (type == MQTT_MESSAGE_EVENT) {
  256. // Match topic
  257. String t = mqttSubtopic((char *) topic);
  258. // Color temperature in mireds
  259. if (t.equals(MQTT_TOPIC_MIRED)) {
  260. _fromMireds(atol(payload));
  261. lightUpdate(true, mqttForward());
  262. }
  263. // Color temperature in kelvins
  264. if (t.equals(MQTT_TOPIC_KELVIN)) {
  265. _fromKelvin(atol(payload));
  266. lightUpdate(true, mqttForward());
  267. }
  268. // Color
  269. if (t.equals(MQTT_TOPIC_COLOR)) {
  270. lightColor(payload);
  271. lightUpdate(true, mqttForward());
  272. }
  273. // Brightness
  274. if (t.equals(MQTT_TOPIC_BRIGHTNESS)) {
  275. _brightness = constrain(atoi(payload), 0, LIGHT_MAX_BRIGHTNESS);
  276. lightUpdate(true, mqttForward());
  277. }
  278. // Channel
  279. if (t.startsWith(MQTT_TOPIC_CHANNEL)) {
  280. unsigned int channelID = t.substring(strlen(MQTT_TOPIC_CHANNEL)+1).toInt();
  281. if (channelID >= _channels.size()) {
  282. DEBUG_MSG_P(PSTR("[LIGHT] Wrong channelID (%d)\n"), channelID);
  283. return;
  284. }
  285. lightChannel(channelID, atoi(payload));
  286. lightUpdate(true, mqttForward());
  287. }
  288. }
  289. }
  290. // -----------------------------------------------------------------------------
  291. // API
  292. // -----------------------------------------------------------------------------
  293. unsigned char lightChannels() {
  294. return _channels.size();
  295. }
  296. bool lightHasColor() {
  297. bool useColor = getSetting("useColor", LIGHT_USE_COLOR).toInt() == 1;
  298. return useColor && (_channels.size() > 2);
  299. }
  300. unsigned char lightWhiteChannels() {
  301. return _channels.size() % 3;
  302. }
  303. void lightMQTT() {
  304. char buffer[12];
  305. if (lightHasColor()) {
  306. // Color
  307. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  308. _toRGB(buffer, 12, false);
  309. } else {
  310. _toLong(buffer, 12, false);
  311. }
  312. mqttSend(MQTT_TOPIC_COLOR, buffer);
  313. // Brightness
  314. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _brightness);
  315. mqttSend(MQTT_TOPIC_BRIGHTNESS, buffer);
  316. }
  317. // Channels
  318. for (unsigned int i=0; i < _channels.size(); i++) {
  319. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _channels[i].value);
  320. mqttSend(MQTT_TOPIC_CHANNEL, i, buffer);
  321. }
  322. }
  323. void lightUpdate(bool save, bool forward) {
  324. _lightProviderUpdate();
  325. // Report color & brightness to MQTT broker
  326. if (forward) lightMQTT();
  327. // Report color to WS clients (using current brightness setting)
  328. #if WEB_SUPPORT
  329. {
  330. DynamicJsonBuffer jsonBuffer;
  331. JsonObject& root = jsonBuffer.createObject();
  332. root["colorVisible"] = 1;
  333. root["useColor"] = getSetting("useColor", LIGHT_USE_COLOR).toInt() == 1;
  334. root["useWhite"] = getSetting("useWhite", LIGHT_USE_WHITE).toInt() == 1;
  335. root["useGamma"] = getSetting("useGamma", LIGHT_USE_GAMMA).toInt() == 1;
  336. if (lightHasColor()) {
  337. root["color"] = lightColor();
  338. root["brightness"] = lightBrightness();
  339. }
  340. JsonArray& channels = root.createNestedArray("channels");
  341. for (unsigned char id=0; id < lightChannels(); id++) {
  342. channels.add(lightChannel(id));
  343. }
  344. String output;
  345. root.printTo(output);
  346. wsSend(output.c_str());
  347. }
  348. #endif
  349. #if LIGHT_SAVE_ENABLED
  350. // Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
  351. if (save) colorTicker.once(LIGHT_SAVE_DELAY, _lightColorSave);
  352. #endif
  353. };
  354. #if LIGHT_SAVE_ENABLED == 0
  355. void lightSave() {
  356. _lightColorSave();
  357. }
  358. #endif
  359. void lightState(bool state) {
  360. _lightState = state;
  361. }
  362. bool lightState() {
  363. return _lightState;
  364. }
  365. void lightColor(const char * color) {
  366. _fromRGB(color);
  367. }
  368. void lightColor(unsigned long color) {
  369. _fromLong(color, false);
  370. }
  371. String lightColor() {
  372. char rgb[8];
  373. _toRGB(rgb, 8, false);
  374. return String(rgb);
  375. }
  376. unsigned int lightChannel(unsigned char id) {
  377. if (id <= _channels.size()) {
  378. return _channels[id].value;
  379. }
  380. return 0;
  381. }
  382. void lightChannel(unsigned char id, unsigned int value) {
  383. if (id <= _channels.size()) {
  384. _channels[id].value = constrain(value, 0, LIGHT_MAX_VALUE);
  385. }
  386. }
  387. unsigned int lightBrightness() {
  388. return _brightness;
  389. }
  390. void lightBrightness(int b) {
  391. _brightness = constrain(b, 0, LIGHT_MAX_BRIGHTNESS);
  392. }
  393. void lightBrightnessStep(int steps) {
  394. lightBrightness(_brightness + steps * LIGHT_STEP);
  395. }
  396. // -----------------------------------------------------------------------------
  397. // SETUP
  398. // -----------------------------------------------------------------------------
  399. void _lightAPISetup() {
  400. #if WEB_SUPPORT
  401. // API entry points (protected with apikey)
  402. if (lightHasColor()) {
  403. apiRegister(MQTT_TOPIC_COLOR, MQTT_TOPIC_COLOR,
  404. [](char * buffer, size_t len) {
  405. if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) {
  406. _toRGB(buffer, len, false);
  407. } else {
  408. _toLong(buffer, len, false);
  409. }
  410. },
  411. [](const char * payload) {
  412. lightColor(payload);
  413. lightUpdate(true, true);
  414. }
  415. );
  416. apiRegister(MQTT_TOPIC_BRIGHTNESS, MQTT_TOPIC_BRIGHTNESS,
  417. [](char * buffer, size_t len) {
  418. snprintf_P(buffer, len, PSTR("%d"), _brightness);
  419. },
  420. [](const char * payload) {
  421. lightBrightness(atoi(payload));
  422. lightUpdate(true, true);
  423. }
  424. );
  425. apiRegister(MQTT_TOPIC_KELVIN, MQTT_TOPIC_KELVIN,
  426. [](char * buffer, size_t len) {},
  427. [](const char * payload) {
  428. _fromKelvin(atol(payload));
  429. lightUpdate(true, true);
  430. }
  431. );
  432. apiRegister(MQTT_TOPIC_MIRED, MQTT_TOPIC_MIRED,
  433. [](char * buffer, size_t len) {},
  434. [](const char * payload) {
  435. _fromMireds(atol(payload));
  436. lightUpdate(true, true);
  437. }
  438. );
  439. }
  440. for (unsigned int id=0; id<lightChannels(); id++) {
  441. char url[15];
  442. snprintf_P(url, sizeof(url), PSTR("%s/%d"), MQTT_TOPIC_CHANNEL, id);
  443. char key[10];
  444. snprintf_P(key, sizeof(key), PSTR("%s%d"), MQTT_TOPIC_CHANNEL, id);
  445. apiRegister(url, key,
  446. [id](char * buffer, size_t len) {
  447. snprintf_P(buffer, len, PSTR("%d"), lightChannel(id));
  448. },
  449. [id](const char * payload) {
  450. lightChannel(id, atoi(payload));
  451. lightUpdate(true, true);
  452. }
  453. );
  454. }
  455. #endif // WEB_SUPPORT
  456. }
  457. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  458. unsigned long getIOMux(unsigned long gpio) {
  459. unsigned long muxes[16] = {
  460. PERIPHS_IO_MUX_GPIO0_U, PERIPHS_IO_MUX_U0TXD_U, PERIPHS_IO_MUX_GPIO2_U, PERIPHS_IO_MUX_U0RXD_U,
  461. PERIPHS_IO_MUX_GPIO4_U, PERIPHS_IO_MUX_GPIO5_U, PERIPHS_IO_MUX_SD_CLK_U, PERIPHS_IO_MUX_SD_DATA0_U,
  462. PERIPHS_IO_MUX_SD_DATA1_U, PERIPHS_IO_MUX_SD_DATA2_U, PERIPHS_IO_MUX_SD_DATA3_U, PERIPHS_IO_MUX_SD_CMD_U,
  463. PERIPHS_IO_MUX_MTDI_U, PERIPHS_IO_MUX_MTCK_U, PERIPHS_IO_MUX_MTMS_U, PERIPHS_IO_MUX_MTDO_U
  464. };
  465. return muxes[gpio];
  466. }
  467. unsigned long getIOFunc(unsigned long gpio) {
  468. unsigned long funcs[16] = {
  469. FUNC_GPIO0, FUNC_GPIO1, FUNC_GPIO2, FUNC_GPIO3,
  470. FUNC_GPIO4, FUNC_GPIO5, FUNC_GPIO6, FUNC_GPIO7,
  471. FUNC_GPIO8, FUNC_GPIO9, FUNC_GPIO10, FUNC_GPIO11,
  472. FUNC_GPIO12, FUNC_GPIO13, FUNC_GPIO14, FUNC_GPIO15
  473. };
  474. return funcs[gpio];
  475. }
  476. #endif
  477. void lightSetup() {
  478. #ifdef LIGHT_ENABLE_PIN
  479. pinMode(LIGHT_ENABLE_PIN, OUTPUT);
  480. #endif
  481. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
  482. _my9291 = new my9291(MY9291_DI_PIN, MY9291_DCKI_PIN, MY9291_COMMAND, MY9291_CHANNELS);
  483. for (unsigned char i=0; i<MY9291_CHANNELS; i++) {
  484. _channels.push_back((channel_t) {0, false, 0});
  485. }
  486. #endif
  487. #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
  488. #ifdef LIGHT_CH1_PIN
  489. _channels.push_back((channel_t) {LIGHT_CH1_PIN, LIGHT_CH1_INVERSE, 0});
  490. #endif
  491. #ifdef LIGHT_CH2_PIN
  492. _channels.push_back((channel_t) {LIGHT_CH2_PIN, LIGHT_CH2_INVERSE, 0});
  493. #endif
  494. #ifdef LIGHT_CH3_PIN
  495. _channels.push_back((channel_t) {LIGHT_CH3_PIN, LIGHT_CH3_INVERSE, 0});
  496. #endif
  497. #ifdef LIGHT_CH4_PIN
  498. _channels.push_back((channel_t) {LIGHT_CH4_PIN, LIGHT_CH4_INVERSE, 0});
  499. #endif
  500. #ifdef LIGHT_CH5_PIN
  501. _channels.push_back((channel_t) {LIGHT_CH5_PIN, LIGHT_CH5_INVERSE, 0});
  502. #endif
  503. uint32 pwm_duty_init[PWM_CHANNEL_NUM_MAX];
  504. uint32 io_info[PWM_CHANNEL_NUM_MAX][3];
  505. for (unsigned int i=0; i < _channels.size(); i++) {
  506. pwm_duty_init[i] = 0;
  507. io_info[i][0] = getIOMux(_channels[i].pin);
  508. io_info[i][1] = getIOFunc(_channels[i].pin);
  509. io_info[i][2] = _channels[i].pin;
  510. pinMode(_channels[i].pin, OUTPUT);
  511. }
  512. pwm_init(LIGHT_MAX_PWM, pwm_duty_init, PWM_CHANNEL_NUM_MAX, io_info);
  513. pwm_start();
  514. #endif
  515. DEBUG_MSG_P(PSTR("[LIGHT] LIGHT_PROVIDER = %d\n"), LIGHT_PROVIDER);
  516. DEBUG_MSG_P(PSTR("[LIGHT] Number of channels: %d\n"), _channels.size());
  517. _lightColorRestore();
  518. _lightAPISetup();
  519. mqttRegister(_lightMQTTCallback);
  520. }
  521. void lightLoop(){
  522. }
  523. #endif // LIGHT_PROVIDER_EXPERIMENTAL_RGB_ONLY_HSV_IR
  524. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE