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.

386 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. kelvin = constrain(1000000UL / mireds, 1000, 40000) / 100;
  103. // Calculate colors
  104. unsigned int red = (kelvin <= 66)
  105. ? LIGHT_MAX_VALUE
  106. : 329.698727446 * pow((kelvin - 60), -0.1332047592);
  107. unsigned int green = (kelvin <= 66)
  108. ? 99.4708025861 * log(kelvin) - 161.1195681661
  109. : 288.1221695283 * pow(kelvin, -0.0755148492);
  110. unsigned int blue = (kelvin >= 66)
  111. ? LIGHT_MAX_VALUE
  112. : ((kelvin <= 19)
  113. ? 0
  114. : 138.5177312231 * log(kelvin - 10) - 305.0447927307);
  115. // Save values
  116. array[0] = constrain(red, 0, LIGHT_MAX_VALUE);
  117. array[1] = constrain(green, 0, LIGHT_MAX_VALUE);
  118. array[2] = constrain(blue, 0, LIGHT_MAX_VALUE);
  119. }
  120. // Converts a color intensity value (0..255) to a pwm value
  121. // This takes care of positive or negative logic and brightness
  122. unsigned int _intensity2pwm(unsigned int intensity, float brightness) {
  123. intensity = brightness * intensity;
  124. #if ENABLE_GAMMA_CORRECTION
  125. unsigned int pwm = (intensity < GAMMA_TABLE_SIZE) ? gamma_table[intensity] : LIGHT_PWM_RANGE;
  126. #else
  127. unsigned int pwm = intensity;
  128. #endif
  129. #if RGBW_INVERSE_LOGIC != 1
  130. pwm = LIGHT_PWM_RANGE - pwm;
  131. #endif
  132. return pwm;
  133. }
  134. unsigned int _intensity2pwm(unsigned int intensity) {
  135. return _intensity2pwm(intensity, LIGHT_MAX_VALUE);
  136. }
  137. // -----------------------------------------------------------------------------
  138. // PROVIDER
  139. // -----------------------------------------------------------------------------
  140. void _lightProviderSet(bool state, unsigned int red, unsigned int green, unsigned int blue, float brightness) {
  141. unsigned int white = 0;
  142. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W)
  143. // If all set to the same value use white instead
  144. if ((red == green) && (green == blue)) {
  145. white = red;
  146. red = green = blue = 0;
  147. }
  148. #endif
  149. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
  150. _my9291->setState(state);
  151. _my9291->setColor((my9291_color_t) { red * brightness, green * brightness, blue * brightness, white * brightness });
  152. #endif
  153. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W)
  154. // Check state
  155. if (!state) red = green = blue = white = 0;
  156. analogWrite(RGBW_RED_PIN, _intensity2pwm(red, brightness));
  157. analogWrite(RGBW_GREEN_PIN, _intensity2pwm(green, brightness));
  158. analogWrite(RGBW_BLUE_PIN, _intensity2pwm(blue, brightness));
  159. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW)
  160. analogWrite(RGBW_WHITE_PIN, _intensity2pwm(white, brightness));
  161. #endif
  162. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W)
  163. analogWrite(RGBW_WHITE_PIN, _intensity2pwm(white, brightness));
  164. analogWrite(RGBW_WHITE2_PIN, _intensity2pwm(white,brightness));
  165. #endif
  166. #endif
  167. }
  168. // -----------------------------------------------------------------------------
  169. // LIGHT MANAGEMENT
  170. // -----------------------------------------------------------------------------
  171. void lightState(bool state) {
  172. _lightState = state;
  173. _lightProviderSet(_lightState, _lightColor[0], _lightColor[1], _lightColor[2], brightness);
  174. }
  175. bool lightState() {
  176. return _lightState;
  177. }
  178. void parseColor(const char * color) {
  179. brightness = 1.0;
  180. _color_string2array(color, _lightColor);
  181. }
  182. void lightColor(bool save, bool forward) {
  183. _lightProviderSet(_lightState, _lightColor[0], _lightColor[1], _lightColor[2], brightness);
  184. // Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
  185. if (save) colorTicker.once(LIGHT_SAVE_DELAY, _lightColorSave);
  186. // Report color & brightness to MQTT broker
  187. if (forward) {
  188. char rgb[8];
  189. _color_array2rgb(_lightColor, 1.0, rgb);
  190. mqttSend(MQTT_TOPIC_COLOR, rgb);
  191. char buffer[5];
  192. sprintf(buffer, "%d", (int) (brightness * LIGHT_MAX_BRIGHTNESS));
  193. mqttSend(MQTT_TOPIC_BRIGHTNESS, buffer);
  194. }
  195. // Report color to WS clients
  196. {
  197. char rgb[8];
  198. _color_array2rgb(_lightColor, brightness, rgb);
  199. char message[64];
  200. sprintf(message, "{\"color\": \"%s\"}", rgb);
  201. wsSend(message);
  202. }
  203. }
  204. String lightColor(float b) {
  205. char rgb[8];
  206. _color_array2rgb(_lightColor, b, rgb);
  207. return String(rgb);
  208. }
  209. String lightColor() {
  210. return lightColor(brightness);
  211. }
  212. // -----------------------------------------------------------------------------
  213. // PERSISTANCE
  214. // -----------------------------------------------------------------------------
  215. void _lightColorSave() {
  216. setSetting("color", lightColor(1.0));
  217. setSetting("brightness", brightness * LIGHT_MAX_BRIGHTNESS);
  218. saveSettings();
  219. }
  220. void _lightColorRestore() {
  221. String color = getSetting("color", LIGHT_DEFAULT_COLOR);
  222. _color_string2array(color.c_str(), _lightColor);
  223. brightness = getSetting("brightness", 1).toFloat() / LIGHT_MAX_BRIGHTNESS;
  224. }
  225. // -----------------------------------------------------------------------------
  226. // MQTT
  227. // -----------------------------------------------------------------------------
  228. void lightMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  229. if (type == MQTT_CONNECT_EVENT) {
  230. mqttSubscribe(MQTT_TOPIC_BRIGHTNESS);
  231. mqttSubscribe(MQTT_TOPIC_COLORTEMP);
  232. mqttSubscribe(MQTT_TOPIC_COLOR);
  233. }
  234. if (type == MQTT_MESSAGE_EVENT) {
  235. // Match topic
  236. String t = mqttSubtopic((char *) topic);
  237. // Color temperature
  238. if (t.equals(MQTT_TOPIC_COLORTEMP)) {
  239. char buffer[10];
  240. sprintf(buffer, "%sK", payload);
  241. parseColor(buffer);
  242. lightColor(true, mqttForward());
  243. }
  244. // Color
  245. if (t.equals(MQTT_TOPIC_COLOR)) {
  246. parseColor(payload);
  247. lightColor(true, mqttForward());
  248. }
  249. // Brightness
  250. if (t.equals(MQTT_TOPIC_BRIGHTNESS)) {
  251. brightness = (float) atoi(payload) / LIGHT_MAX_BRIGHTNESS;
  252. lightColor(true, mqttForward());
  253. }
  254. }
  255. }
  256. // -----------------------------------------------------------------------------
  257. // SETUP
  258. // -----------------------------------------------------------------------------
  259. void lightSetup() {
  260. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
  261. _my9291 = new my9291(MY9291_DI_PIN, MY9291_DCKI_PIN, MY9291_COMMAND);
  262. #endif
  263. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW)
  264. analogWriteRange(LIGHT_PWM_RANGE);
  265. analogWriteFreq(LIGHT_PWM_FREQUENCY);
  266. pinMode(RGBW_RED_PIN, OUTPUT);
  267. pinMode(RGBW_GREEN_PIN, OUTPUT);
  268. pinMode(RGBW_BLUE_PIN, OUTPUT);
  269. #if LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW
  270. pinMode(RGBW_WHITE_PIN, OUTPUT);
  271. #endif
  272. #if LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W
  273. pinMode(RGBW_WHITE_PIN, OUTPUT);
  274. pinMode(RGBW_WHITE2_PIN, OUTPUT);
  275. #endif
  276. #endif
  277. _lightColorRestore();
  278. // API entry points (protected with apikey)
  279. apiRegister(MQTT_TOPIC_COLOR, MQTT_TOPIC_COLOR,
  280. [](char * buffer, size_t len) {
  281. snprintf(buffer, len, "%s", lightColor().c_str());
  282. },
  283. [](const char * payload) {
  284. parseColor(payload);
  285. lightColor(true, true);
  286. }
  287. );
  288. apiRegister(MQTT_TOPIC_BRIGHTNESS, MQTT_TOPIC_BRIGHTNESS,
  289. [](char * buffer, size_t len) {
  290. snprintf(buffer, len, "%d", (int) (brightness * LIGHT_MAX_BRIGHTNESS));
  291. },
  292. [](const char * payload) {
  293. brightness = (float) atoi(payload) / LIGHT_MAX_BRIGHTNESS;
  294. lightColor(true, true);
  295. }
  296. );
  297. mqttRegister(lightMQTTCallback);
  298. }
  299. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE