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.

428 lines
14 KiB

6 years ago
6 years ago
6 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
6 years ago
6 years ago
6 years ago
  1. /*
  2. IR MODULE
  3. Copyright (C) 2018 by Alexander Kolesnikov (raw and MQTT implementation)
  4. Copyright (C) 2017-2019 by François Déchery
  5. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  6. -----------------------------------------------------------------------------
  7. Configuration
  8. -----------------------------------------------------------------------------
  9. To enable transmit functions define IR_TX_PIN
  10. To enable receiver functions define IR_RX_PIN
  11. MQTT input topic: {root}/irin
  12. MQTT output topic: {root}/irout/set
  13. --------------------------------------------------------------------------------
  14. MQTT messages
  15. --------------------------------------------------------------------------------
  16. Decoded messages:
  17. Transmitting:
  18. Payload: 2:121944:32:1 (<type>:<code>:<bits>[:<repeat>])
  19. The repeat value is optional and defaults to 1
  20. Receiving:
  21. Payload: 2:121944:32 (<type>:<code>:<bits>)
  22. Raw messages:
  23. Transmitting:
  24. Payload: 1000,1000,1000,1000,1000,DELAY,COUNT,FREQ:500,500,500,500,500
  25. | IR codes | | IR repeat codes |
  26. codes - time in microseconds when IR LED On/Off. First value - ON, second - Off ...
  27. DELAY - delay in milliseconds between sending repeats
  28. COUNT - how many repeats send. Max 120.
  29. FREQ - modulation frequency. Usually 38kHz. You may set 38, it means 38kHz or set 38000, it meant same.
  30. Repeat codes is optional. You may omit ":" and codes. In this case if repeat count > 0 we repeat main code.
  31. Receiving:
  32. Payload: 1000,1000,1000,1000,1000
  33. | IR codes |
  34. --------------------------------------------------------------------------------
  35. */
  36. #include "ir.h"
  37. #if IR_SUPPORT
  38. #include <IRremoteESP8266.h>
  39. #include <IRrecv.h>
  40. #include <IRsend.h>
  41. #include "ir_button.h"
  42. #include "light.h"
  43. #include "mqtt.h"
  44. #include "relay.h"
  45. #if defined(IR_RX_PIN)
  46. IRrecv _ir_receiver(IR_RX_PIN, IR_BUFFER_SIZE, IR_TIMEOUT, true);
  47. decode_results _ir_results;
  48. #endif // defined(IR_RX_PIN)
  49. #if defined(IR_TX_PIN)
  50. IRsend _ir_sender(IR_TX_PIN);
  51. #if IR_USE_RAW
  52. uint16_t _ir_freq = 38; // IR modulation freq. for sending codes and repeat codes
  53. uint8_t _ir_repeat_size = 0; // size of repeat array
  54. uint16_t * _ir_raw; // array for sending codes and repeat codes
  55. #else
  56. decode_type_t _ir_type = UNUSED; // Type of encoding
  57. uint64_t _ir_code = 0; // Code to transmit
  58. uint16_t _ir_bits = 0; // Code bits
  59. #endif
  60. uint8_t _ir_repeat = 0; // count of times repeating of repeat_code
  61. uint32_t _ir_delay = IR_DELAY; // delay between repeat codes
  62. #endif // defined(IR_TX_PIN)
  63. // MQTT to IR
  64. #if MQTT_SUPPORT && defined(IR_TX_PIN)
  65. void _irMqttCallback(unsigned int type, const char * topic, const char * payload) {
  66. if (type == MQTT_CONNECT_EVENT) {
  67. mqttSubscribe(MQTT_TOPIC_IROUT);
  68. }
  69. if (type == MQTT_MESSAGE_EVENT) {
  70. String t = mqttMagnitude((char *) topic);
  71. // Match topic
  72. if (t.equals(MQTT_TOPIC_IROUT)) {
  73. String data = String(payload);
  74. unsigned int len = data.length();
  75. int col = data.indexOf(":"); // position of ":" which means repeat_code
  76. #if IR_USE_RAW
  77. unsigned char count = 1; // count of code values for allocating array
  78. if (col > 2) { // count & validating repeat code
  79. _ir_repeat_size = 1;
  80. // count & validate repeat-string
  81. for(unsigned int i = col+1; i < len; i++) {
  82. if (i < len-1) {
  83. if ( payload[i] == ',' && isDigit(payload[i+1]) && i>0 ) { //validate string
  84. _ir_repeat_size++;
  85. } else if (!isDigit(payload[i])) {
  86. // Error in repeat_code. Use comma separated unsigned integer values.
  87. // Last three is repeat delay, repeat count(<120) and frequency.
  88. // After all you may write ':' and specify repeat code followed by comma.
  89. DEBUG_MSG_P(PSTR("[IR] Error in repeat code.\n"));
  90. return;
  91. }
  92. }
  93. }
  94. len = col; //cut repeat code from main code processing
  95. } // end of counting & validating repeat code
  96. // count & validate main code string
  97. for(unsigned int i = 0; i < len; i++) {
  98. if (i<len-1) {
  99. if ( payload[i] == ',' && isDigit(payload[i+1]) && i>0 ) { //validate string
  100. count++;
  101. } else if (!isDigit(payload[i])) {
  102. // Error in main code. Use comma separated unsigned integer values.
  103. // Last three is repeat delay, repeat count(<120) and frequency.
  104. // After all you may write ':' and specify repeat code followed by comma.
  105. DEBUG_MSG_P(PSTR("[IR] Error in main code.\n"));
  106. return;
  107. }
  108. }
  109. }
  110. _ir_raw = (uint16_t*)calloc(count, sizeof(uint16_t)); // allocating array for main codes
  111. String value = ""; // for populating values of array from comma separated string
  112. int j = 0; // for populating values of array from comma separated string
  113. // populating main code array from part of MQTT string
  114. for (unsigned int i = 0; i < len; i++) {
  115. if (payload[i] != ',') {
  116. value = value + data[i];
  117. }
  118. if ((payload[i] == ',') || (i == len - 1)) {
  119. _ir_raw[j]= value.toInt();
  120. value = "";
  121. j++;
  122. }
  123. }
  124. // if count>3 then we have values, repeat delay, count and modulation frequency
  125. _ir_repeat=0;
  126. if (count>3) {
  127. if (_ir_raw[count-2] <= 120) { // if repeat count > 120 it's to long and ussualy unusual. maybe we get raw code without this parameters and just use defaults for freq.
  128. _ir_freq = _ir_raw[count-1];
  129. _ir_repeat = _ir_raw[count-2];
  130. _ir_delay = _ir_raw[count-3];
  131. count = count - 3;
  132. }
  133. }
  134. DEBUG_MSG_P(PSTR("[IR] Raw IR output %d codes, repeat %d times on %d(k)Hz freq.\n"), count, _ir_repeat, _ir_freq);
  135. #if defined(IR_RX_PIN)
  136. _ir_receiver.disableIRIn();
  137. #endif
  138. _ir_sender.sendRaw(_ir_raw, count, _ir_freq);
  139. if (_ir_repeat==0) { // no repeat, cleaning array, enabling receiver
  140. free(_ir_raw);
  141. #if defined(IR_RX_PIN)
  142. _ir_receiver.enableIRIn();
  143. #endif
  144. } else if (col>2) { // repeat with repeat_code
  145. DEBUG_MSG_P(PSTR("[IR] Repeat codes count: %d\n"), _ir_repeat_size);
  146. free(_ir_raw);
  147. _ir_raw = (uint16_t*)calloc(_ir_repeat_size, sizeof(uint16_t));
  148. String value = ""; // for populating values of array from comma separated string
  149. int j = 0; // for populating values of array from comma separated string
  150. len = data.length(); //redifining length to full lenght
  151. // populating repeat code array from part of MQTT string
  152. for (unsigned int i = col+1; i < len; i++) {
  153. value = value + data[i];
  154. if ((payload[i] == ',') || (i == len - 1)) {
  155. _ir_raw[j]= value.toInt();
  156. value = "";
  157. j++;
  158. }
  159. }
  160. } else { // if repeat code not specified (col<=2) repeat with current main code
  161. _ir_repeat_size = count;
  162. }
  163. #else
  164. _ir_repeat = 0;
  165. if (col > 0) {
  166. _ir_type = static_cast<decode_type_t>(data.toInt());
  167. _ir_code = strtoul(data.substring(col+1).c_str(), NULL, 10);
  168. col = data.indexOf(":", col+1);
  169. if (col > 0) {
  170. _ir_bits = data.substring(col+1).toInt();
  171. col = data.indexOf(":", col+1);
  172. if (col > 2) {
  173. _ir_repeat = data.substring(col+1).toInt();
  174. } else {
  175. _ir_repeat = IR_REPEAT;
  176. }
  177. }
  178. }
  179. if (_ir_repeat > 0) {
  180. DEBUG_MSG_P(PSTR("[IR] IROUT: %d:%lu:%d:%d\n"), _ir_type, (unsigned long) _ir_code, _ir_bits, _ir_repeat);
  181. } else {
  182. DEBUG_MSG_P(PSTR("[IR] Wrong MQTT payload format (%s)\n"), payload);
  183. }
  184. #endif // IR_USE_RAW
  185. } // end of match topic
  186. } // end of MQTT message
  187. } //end of function
  188. void _irTXLoop() {
  189. static uint32_t last = 0;
  190. if ((_ir_repeat > 0) && (millis() - last > _ir_delay)) {
  191. last = millis();
  192. // Send message
  193. #if IR_USE_RAW
  194. _ir_sender.sendRaw(_ir_raw, _ir_repeat_size, _ir_freq);
  195. #else
  196. _ir_sender.send(_ir_type, _ir_code, _ir_bits);
  197. #endif
  198. // Update repeat count
  199. --_ir_repeat;
  200. if (0 == _ir_repeat) {
  201. #if IR_USE_RAW
  202. free(_ir_raw);
  203. #endif
  204. #if defined(IR_RX_PIN)
  205. _ir_receiver.enableIRIn();
  206. #endif
  207. }
  208. }
  209. }
  210. #endif // MQTT_SUPPORT && defined(IR_TX_PIN)
  211. // Receiving
  212. #if defined(IR_RX_PIN)
  213. void _irProcess(unsigned char type, unsigned long code) {
  214. #if IR_BUTTON_SET > 0
  215. boolean found = false;
  216. for (unsigned char i = 0; i < IR_BUTTON_COUNT ; i++) {
  217. uint32_t button_code = pgm_read_dword(&IR_BUTTON[i][0]);
  218. if (code == button_code) {
  219. unsigned long button_action = pgm_read_dword(&IR_BUTTON[i][1]);
  220. unsigned long button_value = pgm_read_dword(&IR_BUTTON[i][2]);
  221. switch (button_action) {
  222. #if RELAY_SUPPORT
  223. case IR_BUTTON_ACTION_STATE:
  224. relayStatus(0, button_value);
  225. break;
  226. case IR_BUTTON_ACTION_TOGGLE:
  227. relayToggle(button_value);
  228. break;
  229. #endif // RELAY_SUPPORT == 1
  230. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  231. case IR_BUTTON_ACTION_BRIGHTER:
  232. lightBrightnessStep(button_value ? 1 : -1);
  233. lightUpdate();
  234. nice_delay(150); //debounce
  235. break;
  236. case IR_BUTTON_ACTION_RGB:
  237. lightColor(button_value);
  238. lightUpdate();
  239. break;
  240. /*
  241. #if LIGHT_PROVIDER == LIGHT_PROVIDER_FASTLED
  242. case IR_BUTTON_ACTION_EFFECT:
  243. _buttonAnimMode(button_value);
  244. break;
  245. #endif
  246. */
  247. /*
  248. case IR_BUTTON_ACTION_HSV:
  249. lightColor(button_value);
  250. break;
  251. */
  252. }
  253. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  254. found = true;
  255. break;
  256. }
  257. }
  258. if (!found) {
  259. DEBUG_MSG_P(PSTR("[IR] Code does not match any action\n"));
  260. }
  261. #endif
  262. }
  263. void _irRXLoop() {
  264. if (_ir_receiver.decode(&_ir_results)) {
  265. _ir_receiver.resume(); // Receive the next value
  266. // Debounce
  267. static unsigned long last_time = 0;
  268. if (millis() - last_time < IR_DEBOUNCE) return;
  269. last_time = millis();
  270. #if IR_USE_RAW
  271. // Check code
  272. if (_ir_results.rawlen < 1) return;
  273. char * payload;
  274. String value = "";
  275. for (int i = 1; i < _ir_results.rawlen; i++) {
  276. if (i>1) value = value + ",";
  277. value = value + String(_ir_results.rawbuf[i] * RAWTICK);
  278. }
  279. payload = const_cast<char*>(value.c_str());
  280. #else
  281. // Check code
  282. if (_ir_results.value < 1) return;
  283. if (_ir_results.decode_type < 1) return;
  284. if (_ir_results.bits < 1) return;
  285. char payload[32];
  286. snprintf_P(payload, sizeof(payload), PSTR("%u:%lu:%u"), _ir_results.decode_type, (unsigned long) _ir_results.value, _ir_results.bits);
  287. #endif
  288. DEBUG_MSG_P(PSTR("[IR] IRIN: %s\n"), payload);
  289. #if not IR_USE_RAW
  290. _irProcess(_ir_results.decode_type, (unsigned long) _ir_results.value);
  291. #endif
  292. #if MQTT_SUPPORT
  293. if (strlen(payload)>0) {
  294. mqttSend(MQTT_TOPIC_IRIN, (const char *) payload);
  295. }
  296. #endif
  297. }
  298. }
  299. #endif // defined(IR_RX_PIN)
  300. // -----------------------------------------------------------------------------
  301. void _irLoop() {
  302. #if defined(IR_RX_PIN)
  303. _irRXLoop();
  304. #endif
  305. #if MQTT_SUPPORT && defined(IR_TX_PIN)
  306. _irTXLoop();
  307. #endif
  308. }
  309. void irSetup() {
  310. #if defined(IR_RX_PIN)
  311. _ir_receiver.enableIRIn();
  312. DEBUG_MSG_P(PSTR("[IR] Receiver initialized \n"));
  313. #endif
  314. #if MQTT_SUPPORT && defined(IR_TX_PIN)
  315. _ir_sender.begin();
  316. mqttRegister(_irMqttCallback);
  317. DEBUG_MSG_P(PSTR("[IR] Transmitter initialized \n"));
  318. #endif
  319. espurnaRegisterLoop(_irLoop);
  320. }
  321. #endif // IR_SUPPORT