Teensy 4.1 core updated for C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

EventResponder.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* EventResponder - Simple event-based programming for Arduino
  2. * Copyright 2017 Paul Stoffregen
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. /* EventResponder is an experimental API, almost certain to
  25. * incompatibly change as it develops. Please understand any
  26. * programs you write now using EventResponder may need to be
  27. * updated as EventResponder develops.
  28. *
  29. * Please post EventResponder post your feedback here:
  30. * https://forum.pjrc.com/threads/44723-Arduino-Events
  31. */
  32. #include "EventResponder.h"
  33. EventResponder * EventResponder::firstYield = nullptr;
  34. EventResponder * EventResponder::lastYield = nullptr;
  35. EventResponder * EventResponder::firstInterrupt = nullptr;
  36. EventResponder * EventResponder::lastInterrupt = nullptr;
  37. bool EventResponder::runningFromYield = false;
  38. // TODO: interrupt disable/enable needed in many places!!!
  39. void EventResponder::triggerEventNotImmediate()
  40. {
  41. if (_pending) {
  42. // already triggered
  43. return;
  44. }
  45. if (_type == EventTypeYield) {
  46. // normal type, called from yield()
  47. if (firstYield == nullptr) {
  48. _next = nullptr;
  49. _prev = nullptr;
  50. firstYield = this;
  51. lastYield = this;
  52. } else {
  53. _next = nullptr;
  54. _prev = lastYield;
  55. lastYield = this;
  56. }
  57. } else if (_type == EventTypeInterrupt) {
  58. // interrupt, called from software interrupt
  59. if (firstInterrupt == nullptr) {
  60. _next = nullptr;
  61. _prev = nullptr;
  62. firstInterrupt = this;
  63. lastInterrupt = this;
  64. } else {
  65. _next = nullptr;
  66. _prev = lastInterrupt;
  67. lastInterrupt = this;
  68. }
  69. // TODO set interrupt pending
  70. } else {
  71. // detached, easy :-)
  72. }
  73. _pending = true;
  74. }
  75. void pendablesrvreq_isr(void)
  76. {
  77. EventResponder::runFromInterrupt();
  78. }
  79. void EventResponder::runFromInterrupt()
  80. {
  81. for (EventResponder *first=firstInterrupt; first; first = first->_next) {
  82. first->_pending = false;
  83. (*(first->_function))(*first);
  84. }
  85. firstInterrupt = nullptr;
  86. lastInterrupt = nullptr;
  87. }
  88. bool EventResponder::clearEvent()
  89. {
  90. if (_pending) {
  91. if (_type == EventTypeYield) {
  92. if (_prev) {
  93. _prev->_next = _next;
  94. } else {
  95. firstYield = _next;
  96. }
  97. if (_next) {
  98. _next->_prev = _prev;
  99. } else {
  100. lastYield = _prev;
  101. }
  102. } else if (_type == EventTypeInterrupt) {
  103. if (_prev) {
  104. _prev->_next = _next;
  105. } else {
  106. firstInterrupt = _next;
  107. }
  108. if (_next) {
  109. _next->_prev = _prev;
  110. } else {
  111. lastInterrupt = _prev;
  112. }
  113. }
  114. _pending = false;
  115. return true;
  116. }
  117. return false;
  118. }
  119. void EventResponder::detach()
  120. {
  121. if (_type == EventTypeYield) {
  122. if (_pending) {
  123. if (_prev) {
  124. _prev->_next = _next;
  125. } else {
  126. firstYield = _next;
  127. }
  128. if (_next) {
  129. _next->_prev = _prev;
  130. } else {
  131. lastYield = _prev;
  132. }
  133. }
  134. _type = EventTypeDetached;
  135. } else if (_type == EventTypeInterrupt) {
  136. if (_pending) {
  137. if (_prev) {
  138. _prev->_next = _next;
  139. } else {
  140. firstInterrupt = _next;
  141. }
  142. if (_next) {
  143. _next->_prev = _prev;
  144. } else {
  145. lastInterrupt = _prev;
  146. }
  147. }
  148. _type = EventTypeDetached;
  149. }
  150. }
  151. //-------------------------------------------------------------
  152. MillisTimer * MillisTimer::list = nullptr;
  153. void MillisTimer::begin(unsigned long milliseconds, EventResponderRef event)
  154. {
  155. end();
  156. if (!milliseconds) return;
  157. _event = &event;
  158. _ms = milliseconds;
  159. _reload = 0;
  160. addToList();
  161. }
  162. void MillisTimer::beginRepeat(unsigned long milliseconds, EventResponderRef event)
  163. {
  164. end();
  165. if (!milliseconds) return;
  166. _event = &event;
  167. _ms = milliseconds;
  168. _reload = milliseconds;
  169. addToList();
  170. }
  171. void MillisTimer::addToList()
  172. {
  173. if (list == nullptr) {
  174. // list is empty, easy case
  175. _next = nullptr;
  176. _prev = nullptr;
  177. list = this;
  178. } else if (_ms < list->_ms) {
  179. // this timer triggers before any on the list
  180. _next = list;
  181. _prev = nullptr;
  182. list->_prev = this;
  183. list = this;
  184. } else {
  185. // add this timer somewhere after the first already on the list
  186. MillisTimer *timer = list;
  187. while (timer->_next) {
  188. _ms -= timer->_ms;
  189. timer = timer->_next;
  190. if (_ms < timer->_ms) {
  191. // found the right place in the middle of list
  192. _next = timer;
  193. _prev = timer->_prev;
  194. timer->_prev = this;
  195. _prev->_next = this;
  196. isQueued = true;
  197. return;
  198. }
  199. }
  200. // add this time at the end of the list
  201. _ms -= timer->_ms;
  202. _next = nullptr;
  203. _prev = timer;
  204. timer->_next = this;
  205. }
  206. isQueued = true;
  207. }
  208. void MillisTimer::end()
  209. {
  210. if (isQueued) {
  211. if (_next) {
  212. _next->_prev = _prev;
  213. }
  214. if (_prev) {
  215. _prev->_next = _next;
  216. } else {
  217. list = _next;
  218. }
  219. isQueued = false;
  220. }
  221. }
  222. void MillisTimer::runFromTimer()
  223. {
  224. MillisTimer *timer = list;
  225. while (timer) {
  226. if (timer->_ms > 0) {
  227. timer->_ms--;
  228. break;
  229. } else {
  230. MillisTimer *next = timer->_next;
  231. if (next) next->_prev = nullptr;
  232. list = next;
  233. timer->isQueued = false;
  234. EventResponderRef event = *(timer->_event);
  235. event.triggerEvent(0, timer);
  236. if (timer->_reload) {
  237. timer->_ms = timer->_reload;
  238. timer->addToList();
  239. }
  240. timer = list;
  241. }
  242. }
  243. }
  244. extern "C" volatile uint32_t systick_millis_count;
  245. void systick_isr(void)
  246. {
  247. systick_millis_count++;
  248. MillisTimer::runFromTimer();
  249. }