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.

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