reminder.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*****************************************************************************
  2. * Product: Reminder state pattern example
  3. * Last updated for version 6.8.0
  4. * Last updated on 2020-04-01
  5. *
  6. * Q u a n t u m L e a P s
  7. * ------------------------
  8. * Modern Embedded Software
  9. *
  10. * Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved.
  11. *
  12. * This program is open source software: you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as published
  14. * by the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * Alternatively, this program may be distributed and modified under the
  18. * terms of Quantum Leaps commercial licenses, which expressly supersede
  19. * the GNU General Public License and are specifically designed for
  20. * licensees interested in retaining the proprietary status of their code.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see <www.gnu.org/licenses/>.
  29. *
  30. * Contact information:
  31. * <www.state-machine.com/licensing>
  32. * <info@state-machine.com>
  33. *****************************************************************************/
  34. #include "qpc.h"
  35. #include "bsp.h"
  36. #include "safe_std.h" /* portable "safe" <stdio.h>/<string.h> facilities */
  37. Q_DEFINE_THIS_FILE
  38. /*..........................................................................*/
  39. enum SensorSignals {
  40. TIMEOUT_SIG = Q_USER_SIG, /* the periodic timeout signal */
  41. DATA_READY_SIG, /* the invented reminder signal */
  42. TERMINATE_SIG /* terminate the application */
  43. };
  44. /*..........................................................................*/
  45. typedef struct SensorTag { /* the Sensor active object */
  46. QActive super; /* inherit QActive */
  47. QTimeEvt timeEvt; /* private time event generator */
  48. uint16_t pollCtr;
  49. uint16_t procCtr;
  50. } Sensor;
  51. void Sensor_ctor(Sensor * const me);
  52. /* hierarchical state machine ... */
  53. static QState Sensor_initial (Sensor * const me, QEvt const * const e);
  54. static QState Sensor_polling (Sensor * const me, QEvt const * const e);
  55. static QState Sensor_processing(Sensor * const me, QEvt const * const e);
  56. static QState Sensor_idle (Sensor * const me, QEvt const * const e);
  57. static QState Sensor_busy (Sensor * const me, QEvt const * const e);
  58. static QState Sensor_final (Sensor * const me, QEvt const * const e);
  59. /*..........................................................................*/
  60. void Sensor_ctor(Sensor * const me) {
  61. QActive_ctor(&me->super, (QStateHandler)&Sensor_initial);
  62. QTimeEvt_ctorX(&me->timeEvt, &me->super, TIMEOUT_SIG, 0U);
  63. }
  64. /* HSM definition ----------------------------------------------------------*/
  65. QState Sensor_initial(Sensor * const me, QEvt const * const e) {
  66. (void)e; /* unused parameter */
  67. me->pollCtr = 0U;
  68. me->procCtr = 0U;
  69. return Q_TRAN(&Sensor_polling);
  70. }
  71. /*..........................................................................*/
  72. QState Sensor_final(Sensor * const me, QEvt const * const e) {
  73. QState status;
  74. switch (e->sig) {
  75. case Q_ENTRY_SIG: {
  76. QF_stop(); /* terminate the application */
  77. status = Q_HANDLED();
  78. break;
  79. }
  80. default: {
  81. status = Q_SUPER(&QHsm_top);
  82. break;
  83. }
  84. }
  85. return status;
  86. }
  87. /*..........................................................................*/
  88. QState Sensor_polling(Sensor * const me, QEvt const * const e) {
  89. QState status;
  90. switch (e->sig) {
  91. case Q_ENTRY_SIG: {
  92. /* periodic timeout in 1/2 second and every 1/2 second */
  93. QTimeEvt_armX(&me->timeEvt, BSP_TICKS_PER_SEC/2, BSP_TICKS_PER_SEC/2);
  94. status = Q_HANDLED();
  95. break;
  96. }
  97. case Q_EXIT_SIG: {
  98. QTimeEvt_disarm(&me->timeEvt);
  99. status = Q_HANDLED();
  100. break;
  101. }
  102. case Q_INIT_SIG: {
  103. status = Q_TRAN(&Sensor_processing);
  104. break;
  105. }
  106. case TIMEOUT_SIG: {
  107. /* NOTE: this constant event is statically pre-allocated.
  108. * It can be posted/published as any other event.
  109. */
  110. static const QEvt reminderEvt = { DATA_READY_SIG, 0U, 0U };
  111. ++me->pollCtr;
  112. PRINTF_S("polling %3d\n", me->pollCtr);
  113. if ((me->pollCtr & 0x3U) == 0U) { /* modulo 4 */
  114. QACTIVE_POST(&me->super, &reminderEvt, me);
  115. }
  116. status = Q_HANDLED();
  117. break;
  118. }
  119. case TERMINATE_SIG: {
  120. status = Q_TRAN(&Sensor_final);
  121. break;
  122. }
  123. default: {
  124. status = Q_SUPER(&QHsm_top);
  125. break;
  126. }
  127. }
  128. return status;
  129. }
  130. /*..........................................................................*/
  131. QState Sensor_processing(Sensor * const me, QEvt const * const e) {
  132. QState status;
  133. switch (e->sig) {
  134. case Q_INIT_SIG: {
  135. status = Q_TRAN(&Sensor_idle);
  136. break;
  137. }
  138. default: {
  139. status = Q_SUPER(&Sensor_polling);
  140. break;
  141. }
  142. }
  143. return status;
  144. }
  145. /*..........................................................................*/
  146. QState Sensor_idle(Sensor * const me, QEvt const * const e) {
  147. QState status;
  148. switch (e->sig) {
  149. case Q_ENTRY_SIG: {
  150. PRINTF_S("%s\n", "idle-ENTRY;");
  151. status = Q_HANDLED();
  152. break;
  153. }
  154. case DATA_READY_SIG: {
  155. status = Q_TRAN(&Sensor_busy);
  156. break;
  157. }
  158. default: {
  159. status = Q_SUPER(&Sensor_processing);
  160. break;
  161. }
  162. }
  163. return status;
  164. }
  165. /*..........................................................................*/
  166. QState Sensor_busy(Sensor * const me, QEvt const * const e) {
  167. QState status;
  168. switch (e->sig) {
  169. case Q_ENTRY_SIG: {
  170. PRINTF_S("%s\n", "busy-ENTRY;");
  171. status = Q_HANDLED();
  172. break;
  173. }
  174. case TIMEOUT_SIG: {
  175. ++me->procCtr;
  176. PRINTF_S("processing %3d\n", me->procCtr);
  177. if ((me->procCtr & 0x1U) == 0U) { /* modulo 2 */
  178. status = Q_TRAN(&Sensor_idle);
  179. }
  180. else {
  181. status = Q_HANDLED();
  182. }
  183. break;
  184. }
  185. default: {
  186. status = Q_SUPER(&Sensor_processing);
  187. break;
  188. }
  189. }
  190. return status;
  191. }
  192. /* test harness ============================================================*/
  193. /* Local-scope objects -----------------------------------------------------*/
  194. static Sensor l_sensor; /* the Sensor active object */
  195. static QEvt const *l_sensorQSto[10]; /* Event queue storage for Sensor */
  196. /*..........................................................................*/
  197. int main(int argc, char *argv[]) {
  198. PRINTF_S("Reminder state pattern\nQP version: %s\n"
  199. "Press ESC to quit...\n",
  200. QP_VERSION_STR);
  201. BSP_init(argc, argv); /* initialize the BSP */
  202. QF_init(); /* initialize the framework and the underlying RT kernel */
  203. /* publish-subscribe not used, no call to QF_psInit() */
  204. /* dynamic event allocation not used, no call to QF_poolInit() */
  205. /* instantiate and start the active objects... */
  206. Sensor_ctor(&l_sensor);
  207. QACTIVE_START(&l_sensor.super,
  208. 1U,
  209. l_sensorQSto, Q_DIM(l_sensorQSto),
  210. (void *)0, 0U, (QEvt *)0);
  211. return QF_run(); /* run the QF application */
  212. }
  213. /*..........................................................................*/
  214. void BSP_onKeyboardInput(uint8_t key) {
  215. switch (key) {
  216. case '\033': { /* ESC pressed? */
  217. /* NOTE: this constant event is statically pre-allocated.
  218. * It can be posted/published as any other event.
  219. */
  220. static QEvt const terminateEvt = { TERMINATE_SIG, 0U, 0U };
  221. QACTIVE_POST((QActive *)&l_sensor, &terminateEvt, (void *)0);
  222. break;
  223. }
  224. }
  225. }