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.

376 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. char rgb[8];
  184. _color_array2rgb(_lightColor, brightness, rgb);
  185. // Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
  186. if (save) colorTicker.once(LIGHT_SAVE_DELAY, _lightColorSave);
  187. // Report color to MQTT broker
  188. if (forward) {
  189. mqttSend(MQTT_TOPIC_COLOR, rgb);
  190. }
  191. // Report color to WS clients
  192. char message[64];
  193. sprintf(message, "{\"color\": \"%s\", \"brightness\": %d}", rgb, (int) (brightness * LIGHT_MAX_BRIGHTNESS));
  194. wsSend(message);
  195. }
  196. String lightColor(float b) {
  197. char rgb[8];
  198. _color_array2rgb(_lightColor, b, rgb);
  199. return String(rgb);
  200. }
  201. String lightColor() {
  202. return lightColor(brightness);
  203. }
  204. // -----------------------------------------------------------------------------
  205. // PERSISTANCE
  206. // -----------------------------------------------------------------------------
  207. void _lightColorSave() {
  208. setSetting("color", lightColor(1.0));
  209. setSetting("brightness", brightness * LIGHT_MAX_BRIGHTNESS);
  210. saveSettings();
  211. }
  212. void _lightColorRestore() {
  213. String color = getSetting("color", LIGHT_DEFAULT_COLOR);
  214. _color_string2array(color.c_str(), _lightColor);
  215. brightness = getSetting("brightness", 1).toFloat() / LIGHT_MAX_BRIGHTNESS;
  216. }
  217. // -----------------------------------------------------------------------------
  218. // MQTT
  219. // -----------------------------------------------------------------------------
  220. void lightMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  221. if (type == MQTT_CONNECT_EVENT) {
  222. mqttSubscribe(MQTT_TOPIC_BRIGHTNESS);
  223. mqttSubscribe(MQTT_TOPIC_COLORTEMP);
  224. mqttSubscribe(MQTT_TOPIC_COLOR);
  225. }
  226. if (type == MQTT_MESSAGE_EVENT) {
  227. // Match topic
  228. String t = mqttSubtopic((char *) topic);
  229. // Color temperature
  230. if (t.equals(MQTT_TOPIC_COLORTEMP)) {
  231. char buffer[10];
  232. sprintf(buffer, "%sK", payload);
  233. parseColor(buffer);
  234. lightColor(true, mqttForward());
  235. }
  236. // Color
  237. if (t.equals(MQTT_TOPIC_COLOR)) {
  238. parseColor(payload);
  239. lightColor(true, mqttForward());
  240. }
  241. // Brightness
  242. if (t.equals(MQTT_TOPIC_BRIGHTNESS)) {
  243. brightness = (float) atoi(payload) / LIGHT_MAX_BRIGHTNESS;
  244. lightColor(true, mqttForward());
  245. }
  246. }
  247. }
  248. // -----------------------------------------------------------------------------
  249. // SETUP
  250. // -----------------------------------------------------------------------------
  251. void lightSetup() {
  252. #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
  253. _my9291 = new my9291(MY9291_DI_PIN, MY9291_DCKI_PIN, MY9291_COMMAND);
  254. #endif
  255. #if (LIGHT_PROVIDER == LIGHT_PROVIDER_RGB) || (LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW)
  256. analogWriteRange(LIGHT_PWM_RANGE);
  257. analogWriteFreq(LIGHT_PWM_FREQUENCY);
  258. pinMode(RGBW_RED_PIN, OUTPUT);
  259. pinMode(RGBW_GREEN_PIN, OUTPUT);
  260. pinMode(RGBW_BLUE_PIN, OUTPUT);
  261. #if LIGHT_PROVIDER == LIGHT_PROVIDER_RGBW
  262. pinMode(RGBW_WHITE_PIN, OUTPUT);
  263. #endif
  264. #if LIGHT_PROVIDER == LIGHT_PROVIDER_RGB2W
  265. pinMode(RGBW_WHITE_PIN, OUTPUT);
  266. pinMode(RGBW_WHITE2_PIN, OUTPUT);
  267. #endif
  268. #endif
  269. _lightColorRestore();
  270. // API entry points (protected with apikey)
  271. apiRegister(MQTT_TOPIC_COLOR, MQTT_TOPIC_COLOR,
  272. [](char * buffer, size_t len) {
  273. snprintf(buffer, len, "%s", lightColor().c_str());
  274. },
  275. [](const char * payload) {
  276. parseColor(payload);
  277. lightColor(true, mqttForward());
  278. }
  279. );
  280. apiRegister(MQTT_TOPIC_BRIGHTNESS, MQTT_TOPIC_BRIGHTNESS,
  281. NULL,
  282. [](const char * payload) {
  283. lightColor(true, mqttForward());
  284. }
  285. );
  286. mqttRegister(lightMQTTCallback);
  287. }
  288. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE