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.

390 lines
12 KiB

  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. Ticker colorTicker;
  8. bool _lightState = false;
  9. float brightness = 1.0;
  10. unsigned int _lightColor[3] = {0};
  11. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
  12. #include <my9291.h>
  13. my9291 * _my9291;
  14. #endif
  15. #if ENABLE_GAMMA_CORRECTION
  16. #define GAMMA_TABLE_SIZE (256)
  17. #undef LIGHT_PWM_RANGE
  18. #define LIGHT_PWM_RANGE (4095)
  19. // Gamma Correction lookup table for gamma=2.8 and 12 bit (4095) full scale
  20. const unsigned short gamma_table[GAMMA_TABLE_SIZE] = {
  21. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  22. 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11,
  23. 12, 13, 15, 16, 17, 18, 20, 21, 23, 25, 26, 28, 30, 32, 34, 36,
  24. 38, 40, 43, 45, 48, 50, 53, 56, 59, 62, 65, 68, 71, 75, 78, 82,
  25. 85, 89, 93, 97, 101, 105, 110, 114, 119, 123, 128, 133, 138, 143, 149, 154,
  26. 159, 165, 171, 177, 183, 189, 195, 202, 208, 215, 222, 229, 236, 243, 250, 258,
  27. 266, 273, 281, 290, 298, 306, 315, 324, 332, 341, 351, 360, 369, 379, 389, 399,
  28. 409, 419, 430, 440, 451, 462, 473, 485, 496, 508, 520, 532, 544, 556, 569, 582,
  29. 594, 608, 621, 634, 648, 662, 676, 690, 704, 719, 734, 749, 764, 779, 795, 811,
  30. 827, 843, 859, 876, 893, 910, 927, 944, 962, 980, 998,1016,1034,1053,1072,1091,
  31. 1110,1130,1150,1170,1190,1210,1231,1252,1273,1294,1316,1338,1360,1382,1404,1427,
  32. 1450,1473,1497,1520,1544,1568,1593,1617,1642,1667,1693,1718,1744,1770,1797,1823,
  33. 1850,1877,1905,1932,1960,1988,2017,2045,2074,2103,2133,2162,2192,2223,2253,2284,
  34. 2315,2346,2378,2410,2442,2474,2507,2540,2573,2606,2640,2674,2708,2743,2778,2813,
  35. 2849,2884,2920,2957,2993,3030,3067,3105,3143,3181,3219,3258,3297,3336,3376,3416,
  36. 3456,3496,3537,3578,3619,3661,3703,3745,3788,3831,3874,3918,3962,4006,4050,4095 };
  37. #endif
  38. #ifndef LIGHT_PWM_FREQUENCY
  39. #define LIGHT_PWM_FREQUENCY (1000)
  40. #endif
  41. #ifndef LIGHT_PWM_RANGE
  42. #define LIGHT_PWM_RANGE (255)
  43. #endif
  44. // -----------------------------------------------------------------------------
  45. // UTILS
  46. // -----------------------------------------------------------------------------
  47. void _color_string2array(const char * rgb, unsigned int * array) {
  48. char * p = (char *) rgb;
  49. if (strlen(p) == 0) return;
  50. // if color begins with a # then assume HEX RGB
  51. if (p[0] == '#') {
  52. ++p;
  53. unsigned long value = strtol(p, NULL, 16);
  54. // RGBA values are interpreted like RGB + brightness
  55. if (strlen(p) > 7) {
  56. array[0] = (value >> 24) & 0xFF;
  57. array[1] = (value >> 16) & 0xFF;
  58. array[2] = (value >> 8) & 0xFF;
  59. brightness =float(value & 0xFF) / 255;
  60. } else {
  61. array[0] = (value >> 16) & 0xFF;
  62. array[1] = (value >> 8) & 0xFF;
  63. array[2] = (value) & 0xFF;
  64. }
  65. // it's a temperature
  66. } else if (p[strlen(p)-1] == 'K') {
  67. p[strlen(p)-1] = 0;
  68. unsigned long temperature = atol(p);
  69. _color_temperature2array(temperature, array);
  70. // otherwise assume decimal values separated by commas
  71. } else {
  72. char * tok;
  73. tok = strtok(p, ",");
  74. array[0] = atoi(tok);
  75. tok = strtok(NULL, ",");
  76. // if there are more than one value assume R,G,B
  77. if (tok != NULL) {
  78. array[1] = atoi(tok);
  79. tok = strtok(NULL, ",");
  80. if (tok != NULL) {
  81. array[2] = atoi(tok);
  82. } else {
  83. array[2] = 0;
  84. }
  85. // only one value set red, green and blue to the same value
  86. } else {
  87. array[2] = array[1] = array[0];
  88. }
  89. }
  90. }
  91. void _color_array2rgb(unsigned int * array, float brightness, char * rgb) {
  92. unsigned long value = array[0] * brightness;
  93. value = (value << 8) + array[1] * brightness;
  94. value = (value << 8) + array[2] * brightness;
  95. sprintf(rgb, "#%06X", value);
  96. }
  97. // Thanks to Sacha Telgenhof for sharing this code in his AiLight library
  98. // Color temperature is measured in mireds (kelvin = 1e6/mired)
  99. // https://github.com/stelgenhof/AiLight
  100. void _color_temperature2array(unsigned long mireds, unsigned int * array) {
  101. // Force boundaries and conversion
  102. if (mireds == 0) mireds = 1;
  103. unsigned long kelvin = constrain(1000000UL / mireds, 1000, 40000) / 100;
  104. // Calculate colors
  105. unsigned int red = (kelvin <= 66)
  106. ? LIGHT_MAX_VALUE
  107. : 329.698727446 * pow((kelvin - 60), -0.1332047592);
  108. unsigned int green = (kelvin <= 66)
  109. ? 99.4708025861 * log(kelvin) - 161.1195681661
  110. : 288.1221695283 * pow(kelvin, -0.0755148492);
  111. unsigned int blue = (kelvin >= 66)
  112. ? LIGHT_MAX_VALUE
  113. : ((kelvin <= 19)
  114. ? 0
  115. : 138.5177312231 * log(kelvin - 10) - 305.0447927307);
  116. // Save values
  117. array[0] = constrain(red, 0, LIGHT_MAX_VALUE);
  118. array[1] = constrain(green, 0, LIGHT_MAX_VALUE);
  119. array[2] = constrain(blue, 0, LIGHT_MAX_VALUE);
  120. }
  121. // Converts a color intensity value (0..255) to a pwm value
  122. // This takes care of positive or negative logic and brightness
  123. unsigned int _intensity2pwm(unsigned int intensity, float brightness) {
  124. intensity = brightness * intensity;
  125. #if ENABLE_GAMMA_CORRECTION
  126. unsigned int pwm = (intensity < GAMMA_TABLE_SIZE) ? gamma_table[intensity] : LIGHT_PWM_RANGE;
  127. #else
  128. unsigned int pwm = intensity;
  129. #endif
  130. #if RGBW_INVERSE_LOGIC != 1
  131. pwm = LIGHT_PWM_RANGE - pwm;
  132. #endif
  133. return pwm;
  134. }
  135. unsigned int _intensity2pwm(unsigned int intensity) {
  136. return _intensity2pwm(intensity, LIGHT_MAX_VALUE);
  137. }
  138. // -----------------------------------------------------------------------------
  139. // PROVIDER
  140. // -----------------------------------------------------------------------------
  141. void _lightProviderSet(bool state, unsigned int red, unsigned int green, unsigned int blue, float brightness) {
  142. unsigned int white = 0;
  143. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W)
  144. // If all set to the same value use white instead
  145. if ((red == green) && (green == blue)) {
  146. white = red;
  147. red = green = blue = 0;
  148. }
  149. #endif
  150. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
  151. _my9291->setState(state);
  152. red *= brightness;
  153. green *= brightness;
  154. blue *= brightness;
  155. white *= brightness;
  156. _my9291->setColor((my9291_color_t) { red, green, blue, white });
  157. #endif
  158. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W)
  159. // Check state
  160. if (!state) red = green = blue = white = 0;
  161. analogWrite(RGBW_RED_PIN, _intensity2pwm(red, brightness));
  162. analogWrite(RGBW_GREEN_PIN, _intensity2pwm(green, brightness));
  163. analogWrite(RGBW_BLUE_PIN, _intensity2pwm(blue, brightness));
  164. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW)
  165. analogWrite(RGBW_WHITE_PIN, _intensity2pwm(white, brightness));
  166. #endif
  167. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W)
  168. analogWrite(RGBW_WHITE_PIN, _intensity2pwm(white, brightness));
  169. analogWrite(RGBW_WHITE2_PIN, _intensity2pwm(white, brightness));
  170. #endif
  171. #endif
  172. }
  173. // -----------------------------------------------------------------------------
  174. // LIGHT MANAGEMENT
  175. // -----------------------------------------------------------------------------
  176. void lightState(bool state) {
  177. _lightState = state;
  178. _lightProviderSet(_lightState, _lightColor[0], _lightColor[1], _lightColor[2], brightness);
  179. }
  180. bool lightState() {
  181. return _lightState;
  182. }
  183. void parseColor(const char * color) {
  184. brightness = 1.0;
  185. _color_string2array(color, _lightColor);
  186. }
  187. void lightColor(bool save, bool forward) {
  188. _lightProviderSet(_lightState, _lightColor[0], _lightColor[1], _lightColor[2], brightness);
  189. // Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
  190. if (save) colorTicker.once(LIGHT_SAVE_DELAY, _lightColorSave);
  191. // Report color & brightness to MQTT broker
  192. if (forward) {
  193. char rgb[8];
  194. _color_array2rgb(_lightColor, 1.0, rgb);
  195. mqttSend(MQTT_TOPIC_COLOR, rgb);
  196. char buffer[5];
  197. sprintf(buffer, "%d", (int) (brightness * LIGHT_MAX_BRIGHTNESS));
  198. mqttSend(MQTT_TOPIC_BRIGHTNESS, buffer);
  199. }
  200. // Report color to WS clients
  201. {
  202. char rgb[8];
  203. _color_array2rgb(_lightColor, brightness, rgb);
  204. char message[64];
  205. sprintf(message, "{\"color\": \"%s\"}", rgb);
  206. wsSend(message);
  207. }
  208. }
  209. String lightColor(float b) {
  210. char rgb[8];
  211. _color_array2rgb(_lightColor, b, rgb);
  212. return String(rgb);
  213. }
  214. String lightColor() {
  215. return lightColor(brightness);
  216. }
  217. // -----------------------------------------------------------------------------
  218. // PERSISTANCE
  219. // -----------------------------------------------------------------------------
  220. void _lightColorSave() {
  221. setSetting("color", lightColor(1.0));
  222. setSetting("brightness", brightness * LIGHT_MAX_BRIGHTNESS);
  223. saveSettings();
  224. }
  225. void _lightColorRestore() {
  226. String color = getSetting("color", LIGHT_DEFAULT_COLOR);
  227. _color_string2array(color.c_str(), _lightColor);
  228. brightness = getSetting("brightness", 1).toFloat() / LIGHT_MAX_BRIGHTNESS;
  229. }
  230. // -----------------------------------------------------------------------------
  231. // MQTT
  232. // -----------------------------------------------------------------------------
  233. void lightMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  234. if (type == MQTT_CONNECT_EVENT) {
  235. mqttSubscribe(MQTT_TOPIC_BRIGHTNESS);
  236. mqttSubscribe(MQTT_TOPIC_COLORTEMP);
  237. mqttSubscribe(MQTT_TOPIC_COLOR);
  238. }
  239. if (type == MQTT_MESSAGE_EVENT) {
  240. // Match topic
  241. String t = mqttSubtopic((char *) topic);
  242. // Color temperature
  243. if (t.equals(MQTT_TOPIC_COLORTEMP)) {
  244. char buffer[10];
  245. sprintf(buffer, "%sK", payload);
  246. parseColor(buffer);
  247. lightColor(true, mqttForward());
  248. }
  249. // Color
  250. if (t.equals(MQTT_TOPIC_COLOR)) {
  251. parseColor(payload);
  252. lightColor(true, mqttForward());
  253. }
  254. // Brightness
  255. if (t.equals(MQTT_TOPIC_BRIGHTNESS)) {
  256. brightness = (float) atoi(payload) / LIGHT_MAX_BRIGHTNESS;
  257. lightColor(true, mqttForward());
  258. }
  259. }
  260. }
  261. // -----------------------------------------------------------------------------
  262. // SETUP
  263. // -----------------------------------------------------------------------------
  264. void lightSetup() {
  265. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
  266. _my9291 = new my9291(MY9291_DI_PIN, MY9291_DCKI_PIN, MY9291_COMMAND);
  267. #endif
  268. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW)
  269. analogWriteRange(LIGHT_PWM_RANGE);
  270. analogWriteFreq(LIGHT_PWM_FREQUENCY);
  271. pinMode(RGBW_RED_PIN, OUTPUT);
  272. pinMode(RGBW_GREEN_PIN, OUTPUT);
  273. pinMode(RGBW_BLUE_PIN, OUTPUT);
  274. #if LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW
  275. pinMode(RGBW_WHITE_PIN, OUTPUT);
  276. #endif
  277. #if LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W
  278. pinMode(RGBW_WHITE_PIN, OUTPUT);
  279. pinMode(RGBW_WHITE2_PIN, OUTPUT);
  280. #endif
  281. #endif
  282. _lightColorRestore();
  283. // API entry points (protected with apikey)
  284. apiRegister(MQTT_TOPIC_COLOR, MQTT_TOPIC_COLOR,
  285. [](char * buffer, size_t len) {
  286. snprintf(buffer, len, "%s", lightColor().c_str());
  287. },
  288. [](const char * payload) {
  289. parseColor(payload);
  290. lightColor(true, true);
  291. }
  292. );
  293. apiRegister(MQTT_TOPIC_BRIGHTNESS, MQTT_TOPIC_BRIGHTNESS,
  294. [](char * buffer, size_t len) {
  295. snprintf(buffer, len, "%d", (int) (brightness * LIGHT_MAX_BRIGHTNESS));
  296. },
  297. [](const char * payload) {
  298. brightness = (float) atoi(payload) / LIGHT_MAX_BRIGHTNESS;
  299. lightColor(true, true);
  300. }
  301. );
  302. mqttRegister(lightMQTTCallback);
  303. }
  304. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE