xsns_interface.ino 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. xsns_interface.ino - Sensor interface support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends inspired by ESPEasy
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef XFUNC_PTR_IN_ROM
  16. boolean (* const xsns_func_ptr[])(byte) PROGMEM = { // Sensor Function Pointers for simple implementation of sensors
  17. #else
  18. boolean (* const xsns_func_ptr[])(byte) = { // Sensor Function Pointers for simple implementation of sensors
  19. #endif
  20. #ifdef XSNS_01
  21. &Xsns01,
  22. #endif
  23. #ifdef XSNS_02
  24. &Xsns02,
  25. #endif
  26. #ifdef XSNS_03
  27. &Xsns03,
  28. #endif
  29. #ifdef XSNS_04
  30. &Xsns04,
  31. #endif
  32. #ifdef XSNS_05
  33. &Xsns05,
  34. #endif
  35. #ifdef XSNS_06
  36. &Xsns06,
  37. #endif
  38. #ifdef XSNS_07
  39. &Xsns07,
  40. #endif
  41. #ifdef XSNS_08
  42. &Xsns08,
  43. #endif
  44. #ifdef XSNS_09
  45. &Xsns09,
  46. #endif
  47. #ifdef XSNS_10
  48. &Xsns10,
  49. #endif
  50. #ifdef XSNS_11
  51. &Xsns11,
  52. #endif
  53. #ifdef XSNS_12
  54. &Xsns12,
  55. #endif
  56. #ifdef XSNS_13
  57. &Xsns13,
  58. #endif
  59. #ifdef XSNS_14
  60. &Xsns14,
  61. #endif
  62. #ifdef XSNS_15
  63. &Xsns15,
  64. #endif
  65. #ifdef XSNS_16
  66. &Xsns16,
  67. #endif
  68. #ifdef XSNS_17
  69. &Xsns17,
  70. #endif
  71. #ifdef XSNS_18
  72. &Xsns18,
  73. #endif
  74. #ifdef XSNS_19
  75. &Xsns19,
  76. #endif
  77. #ifdef XSNS_20
  78. &Xsns20,
  79. #endif
  80. #ifdef XSNS_21
  81. &Xsns21,
  82. #endif
  83. #ifdef XSNS_22
  84. &Xsns22,
  85. #endif
  86. #ifdef XSNS_23
  87. &Xsns23,
  88. #endif
  89. #ifdef XSNS_24
  90. &Xsns24,
  91. #endif
  92. #ifdef XSNS_25
  93. &Xsns25,
  94. #endif
  95. #ifdef XSNS_26
  96. &Xsns26,
  97. #endif
  98. #ifdef XSNS_27
  99. &Xsns27,
  100. #endif
  101. #ifdef XSNS_28
  102. &Xsns28,
  103. #endif
  104. #ifdef XSNS_29
  105. &Xsns29,
  106. #endif
  107. #ifdef XSNS_30
  108. &Xsns30,
  109. #endif
  110. #ifdef XSNS_31
  111. &Xsns31,
  112. #endif
  113. #ifdef XSNS_32
  114. &Xsns32,
  115. #endif
  116. #ifdef XSNS_33
  117. &Xsns33,
  118. #endif
  119. #ifdef XSNS_34
  120. &Xsns34,
  121. #endif
  122. #ifdef XSNS_35
  123. &Xsns35,
  124. #endif
  125. #ifdef XSNS_36
  126. &Xsns36,
  127. #endif
  128. #ifdef XSNS_37
  129. &Xsns37,
  130. #endif
  131. #ifdef XSNS_38
  132. &Xsns38,
  133. #endif
  134. #ifdef XSNS_39
  135. &Xsns39,
  136. #endif
  137. #ifdef XSNS_40
  138. &Xsns40,
  139. #endif
  140. #ifdef XSNS_41
  141. &Xsns41,
  142. #endif
  143. #ifdef XSNS_42
  144. &Xsns42,
  145. #endif
  146. #ifdef XSNS_43
  147. &Xsns43,
  148. #endif
  149. #ifdef XSNS_44
  150. &Xsns44,
  151. #endif
  152. #ifdef XSNS_45
  153. &Xsns45,
  154. #endif
  155. #ifdef XSNS_46
  156. &Xsns46,
  157. #endif
  158. #ifdef XSNS_47
  159. &Xsns47,
  160. #endif
  161. #ifdef XSNS_48
  162. &Xsns48,
  163. #endif
  164. #ifdef XSNS_49
  165. &Xsns49,
  166. #endif
  167. #ifdef XSNS_50
  168. &Xsns50,
  169. #endif
  170. // Optional user defined sensors in range 91 - 99
  171. #ifdef XSNS_91
  172. &Xsns91,
  173. #endif
  174. #ifdef XSNS_92
  175. &Xsns92,
  176. #endif
  177. #ifdef XSNS_93
  178. &Xsns93,
  179. #endif
  180. #ifdef XSNS_94
  181. &Xsns94,
  182. #endif
  183. #ifdef XSNS_95
  184. &Xsns95,
  185. #endif
  186. #ifdef XSNS_96
  187. &Xsns96,
  188. #endif
  189. #ifdef XSNS_97
  190. &Xsns97,
  191. #endif
  192. #ifdef XSNS_98
  193. &Xsns98,
  194. #endif
  195. #ifdef XSNS_99
  196. &Xsns99
  197. #endif
  198. };
  199. const uint8_t xsns_present = sizeof(xsns_func_ptr) / sizeof(xsns_func_ptr[0]); // Number of External Sensors found
  200. /*********************************************************************************************\
  201. * Function call to all xsns
  202. \*********************************************************************************************/
  203. boolean XsnsNextCall(byte Function, uint8_t &xsns_index)
  204. {
  205. xsns_index++;
  206. if (xsns_index == xsns_present) { xsns_index = 0; }
  207. #ifdef USE_DEBUG_DRIVER
  208. while (!XsnsEnabled(xsns_index) && !xsns_index) { // Perform at least first sensor (counter)
  209. xsns_index++;
  210. if (xsns_index == xsns_present) { xsns_index = 0; }
  211. }
  212. #endif
  213. // WifiAddDelayWhenDisconnected();
  214. return xsns_func_ptr[xsns_index](Function);
  215. }
  216. boolean XsnsCall(byte Function)
  217. {
  218. boolean result = false;
  219. #ifdef PROFILE_XSNS_EVERY_SECOND
  220. uint32_t profile_start_millis = millis();
  221. #endif // PROFILE_XSNS_EVERY_SECOND
  222. for (byte x = 0; x < xsns_present; x++) {
  223. #ifdef USE_DEBUG_DRIVER
  224. if (XsnsEnabled(x)) {
  225. #endif
  226. #ifdef PROFILE_XSNS_SENSOR_EVERY_SECOND
  227. uint32_t profile_start_millis = millis();
  228. #endif // PROFILE_XSNS_SENSOR_EVERY_SECOND
  229. // WifiAddDelayWhenDisconnected();
  230. result = xsns_func_ptr[x](Function);
  231. #ifdef PROFILE_XSNS_SENSOR_EVERY_SECOND
  232. uint32_t profile_millis = millis() - profile_start_millis;
  233. if (profile_millis) {
  234. if (FUNC_EVERY_SECOND == Function) {
  235. snprintf_P(log_data, sizeof(log_data), PSTR("PRF: At %08u XsnsCall %d to Sensor %d took %u mS"), uptime, Function, x, profile_millis);
  236. AddLog(LOG_LEVEL_DEBUG);
  237. }
  238. }
  239. #endif // PROFILE_XSNS_SENSOR_EVERY_SECOND
  240. if (result) break;
  241. #ifdef USE_DEBUG_DRIVER
  242. }
  243. #endif
  244. }
  245. #ifdef PROFILE_XSNS_EVERY_SECOND
  246. uint32_t profile_millis = millis() - profile_start_millis;
  247. if (profile_millis) {
  248. if (FUNC_EVERY_SECOND == Function) {
  249. snprintf_P(log_data, sizeof(log_data), PSTR("PRF: At %08u XsnsCall %d took %u mS"), uptime, Function, profile_millis);
  250. AddLog(LOG_LEVEL_DEBUG);
  251. }
  252. }
  253. #endif // PROFILE_XSNS_EVERY_SECOND
  254. return result;
  255. }