Teensy 4.1 core updated for C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

162 lines
3.9KB

  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. }