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

205 lines
7.5KB

  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. #if !defined(EventResponder_h) && defined(__cplusplus)
  33. #define EventResponder_h
  34. #include <Arduino.h>
  35. /* EventResponder lets you control how your program responds to an event.
  36. * Imagine a basketball or football (American soccer) player who gets the
  37. * ball. Usually they will pass to another player who has the best
  38. * opportunity to score. Similarly in Arduino programming, events are
  39. * often triggered within interrupts or other timing sensitive code.
  40. * EventResponder can call your function a short time later, giving you
  41. * the ability to use Arduino functions and libraries which would not
  42. * be safe to use from an interrupt. However, some situations do call
  43. * for the most immediate response, even if doing so is more difficult.
  44. * EventResponder lets you choose how your function will be called,
  45. * without editing the timers or libraries which trigger the events.
  46. */
  47. class EventResponder;
  48. typedef EventResponder& EventResponderRef;
  49. typedef void (*EventResponderFunction)(EventResponderRef);
  50. class EventResponder
  51. {
  52. public:
  53. constexpr EventResponder() {
  54. }
  55. ~EventResponder() {
  56. detach();
  57. }
  58. enum EventType { // these are not meant for public consumption...
  59. EventTypeDetached = 0, // no function is called
  60. EventTypeYield, // function is called from yield()
  61. EventTypeImmediate, // function is called immediately
  62. EventTypeInterrupt, // function is called from interrupt
  63. EventTypeThread // function is run as a new thread
  64. };
  65. // Attach a function to be called from yield(). This should be the
  66. // default way to use EventResponder. Calls from yield() allow use
  67. // of Arduino libraries, String, Serial, etc.
  68. void attach(EventResponderFunction function) {
  69. detach();
  70. _function = function;
  71. _type = EventTypeYield;
  72. }
  73. // Attach a function to be called immediately. This provides the
  74. // fastest possible response, but your function must be carefully
  75. // designed.
  76. void attachImmediate(EventResponderFunction function) {
  77. detach();
  78. _function = function;
  79. _type = EventTypeImmediate;
  80. }
  81. // Attach a function to be called from a low priority interrupt.
  82. // Boards not supporting software triggered interrupts will implement
  83. // this as attachImmediate. On ARM and other platforms with software
  84. // interrupts, this allow fast interrupt-based response, but with less
  85. // disruption to other libraries requiring their own interrupts.
  86. void attachInterrupt(EventResponderFunction function) {
  87. detach();
  88. _function = function;
  89. _type = EventTypeInterrupt;
  90. // TODO: configure PendSV
  91. }
  92. // Attach a function to be called as its own thread. Boards not running
  93. // a RTOS or pre-emptive scheduler shall implement this as attach().
  94. void attachThread(EventResponderFunction function, void *param=nullptr) {
  95. attach(function); // for non-RTOS usage, compile as default attach
  96. }
  97. // Do not call any function. The user's program must occasionally check
  98. // whether the event has occurred, or use one of the wait functions.
  99. void detach();
  100. // Trigger the event. An optional status code and data may be provided.
  101. // The code triggering the event does NOT control which of the above
  102. // response methods will be used.
  103. virtual void triggerEvent(int status=0, void *data=nullptr) {
  104. _status = status;
  105. _data = data;
  106. if (_type == EventTypeImmediate) {
  107. (*_function)(*this);
  108. } else {
  109. triggerEventNotImmediate();
  110. }
  111. }
  112. // Clear an event which has been triggered, but has not yet caused a
  113. // function to be called.
  114. bool clearEvent();
  115. // Get the event's status code. Typically this will indicate if the event was
  116. // triggered due to successful completion, or how much data was successfully
  117. // processed (positive numbers) or an error (negative numbers). The
  118. // exact meaning of this status code depends on the code or library which
  119. // triggers the event.
  120. int getStatus() { return _status; }
  121. // Get the optional data pointer associated with the event. Often this
  122. // will be NULL, or will be the object instance which triggered the event.
  123. // Some libraries may use this to pass data associated with the event.
  124. void * getData() { return _data; }
  125. // An optional "context" may be associated with each EventResponder.
  126. // When more than one EventResponder has the same function attached, these
  127. // may be used to allow the function to obtain extra information needed
  128. // depending on which EventResponder called it.
  129. void setContext(void *context) { _context = context; }
  130. void * getContext() { return _context; }
  131. // Wait for event(s) to occur. These are most likely to be useful when
  132. // used with a scheduler or RTOS.
  133. bool waitForEvent(EventResponderRef event, int timeout);
  134. EventResponder * waitForEvent(EventResponder *list, int listsize, int timeout);
  135. static void runFromYield() {
  136. EventResponder *first = firstYield;
  137. if (first && !runningFromYield) {
  138. runningFromYield = true;
  139. firstYield = first->_next;
  140. if (firstYield) firstYield->_prev = nullptr;
  141. first->_pending = false;
  142. (*(first->_function))(*first);
  143. runningFromYield = false;
  144. }
  145. }
  146. static void runFromInterrupt();
  147. operator bool() { return _pending; }
  148. protected:
  149. void triggerEventNotImmediate();
  150. int _status = 0;
  151. EventResponderFunction _function = nullptr;
  152. void *_data = nullptr;
  153. void *_context = nullptr;
  154. EventResponder *_next = nullptr;
  155. EventResponder *_prev = nullptr;
  156. EventType _type = EventTypeDetached;
  157. bool _pending = false;
  158. static EventResponder *firstYield;
  159. static EventResponder *lastYield;
  160. static EventResponder *firstInterrupt;
  161. static EventResponder *lastInterrupt;
  162. static bool runningFromYield;
  163. };
  164. class MillisTimer
  165. {
  166. public:
  167. constexpr MillisTimer() {
  168. }
  169. ~MillisTimer() {
  170. end();
  171. }
  172. void begin(unsigned long milliseconds, EventResponderRef event);
  173. void beginRepeat(unsigned long milliseconds, EventResponderRef event);
  174. void end();
  175. static void runFromTimer();
  176. private:
  177. void addToList();
  178. unsigned long _ms = 0;
  179. unsigned long _reload = 0;
  180. MillisTimer *_next = nullptr;
  181. MillisTimer *_prev = nullptr;
  182. EventResponder *_event = nullptr;
  183. bool isQueued = false;
  184. static MillisTimer *list;
  185. };
  186. #endif