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.

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