reminder2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*$file${.::reminder2.c} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  2. /*
  3. * Model: reminder2.qm
  4. * File: ${.::reminder2.c}
  5. *
  6. * This code has been generated by QM 5.2.4 <www.state-machine.com/qm>.
  7. * DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
  8. *
  9. * SPDX-License-Identifier: GPL-3.0-or-later
  10. *
  11. * This generated code is open source software: you can redistribute it under
  12. * the terms of the GNU General Public License as published by the Free
  13. * Software Foundation.
  14. *
  15. * This code is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * NOTE:
  21. * Alternatively, this generated code may be distributed under the terms
  22. * of Quantum Leaps commercial licenses, which expressly supersede the GNU
  23. * General Public License and are specifically designed for licensees
  24. * interested in retaining the proprietary status of their code.
  25. *
  26. * Contact information:
  27. * <www.state-machine.com/licensing>
  28. * <info@state-machine.com>
  29. */
  30. /*$endhead${.::reminder2.c} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  31. #include "qpc.h"
  32. #include "bsp.h"
  33. #include "safe_std.h" /* portable "safe" <stdio.h>/<string.h> facilities */
  34. #include <stdlib.h>
  35. Q_DEFINE_THIS_FILE
  36. /*..........................................................................*/
  37. enum ReminderSignals {
  38. CRUNCH_SIG = Q_USER_SIG, /* the invented reminder signal */
  39. ECHO_SIG, /* check the responsiveness of the system */
  40. TERMINATE_SIG /* terminate the application */
  41. };
  42. /*$declare${Events::ReminderEvt} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  43. /*${Events::ReminderEvt} ...................................................*/
  44. typedef struct {
  45. /* protected: */
  46. QEvt super;
  47. /* public: */
  48. /* the next iteration to perform */
  49. uint32_t iter;
  50. } ReminderEvt;
  51. /*$enddecl${Events::ReminderEvt} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  52. /*..........................................................................*/
  53. /*$declare${Components::Cruncher} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  54. /*${Components::Cruncher} ..................................................*/
  55. typedef struct {
  56. /* protected: */
  57. QActive super;
  58. /* private: */
  59. // internal variable
  60. double sum;
  61. } Cruncher;
  62. /* public: */
  63. static void Cruncher_ctor(Cruncher * const me);
  64. /* protected: */
  65. static QState Cruncher_initial(Cruncher * const me, void const * const par);
  66. static QState Cruncher_processing(Cruncher * const me, QEvt const * const e);
  67. static QState Cruncher_final(Cruncher * const me, QEvt const * const e);
  68. /*$enddecl${Components::Cruncher} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  69. /*$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  70. /* Check for the minimum required QP version */
  71. #if (QP_VERSION < 700U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
  72. #error qpc version 7.0.0 or higher required
  73. #endif
  74. /*$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  75. /*$define${Components::Cruncher} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  76. /*${Components::Cruncher} ..................................................*/
  77. /*${Components::Cruncher::ctor} ............................................*/
  78. static void Cruncher_ctor(Cruncher * const me) {
  79. QActive_ctor(&me->super, Q_STATE_CAST(&Cruncher_initial));
  80. }
  81. /*${Components::Cruncher::SM} ..............................................*/
  82. static QState Cruncher_initial(Cruncher * const me, void const * const par) {
  83. /*${Components::Cruncher::SM::initial} */
  84. (void)par; /* unused parameter */
  85. QS_FUN_DICTIONARY(&Cruncher_processing);
  86. QS_FUN_DICTIONARY(&Cruncher_final);
  87. return Q_TRAN(&Cruncher_processing);
  88. }
  89. /*${Components::Cruncher::SM::processing} ..................................*/
  90. static QState Cruncher_processing(Cruncher * const me, QEvt const * const e) {
  91. QState status_;
  92. switch (e->sig) {
  93. /*${Components::Cruncher::SM::processing} */
  94. case Q_ENTRY_SIG: {
  95. ReminderEvt *reminder = Q_NEW(ReminderEvt, CRUNCH_SIG);
  96. reminder->iter = 0U;
  97. QACTIVE_POST(&me->super, &reminder->super, me);
  98. me->sum = 0.0;
  99. status_ = Q_HANDLED();
  100. break;
  101. }
  102. /*${Components::Cruncher::SM::processing::CRUNCH} */
  103. case CRUNCH_SIG: {
  104. uint32_t i = Q_EVT_CAST(ReminderEvt)->iter;
  105. uint32_t n = i;
  106. i += 100U;
  107. for (; n < i; ++n) {
  108. if ((n & 1) == 0) {
  109. me->sum += 1.0/(2*n + 1);
  110. }
  111. else {
  112. me->sum -= 1.0/(2*n + 1);
  113. }
  114. }
  115. /*${Components::Cruncher::SM::processing::CRUNCH::[i<0x07000000U]} */
  116. if (i < 0x07000000U) {
  117. ReminderEvt *reminder = Q_NEW(ReminderEvt, CRUNCH_SIG);
  118. reminder->iter = i;
  119. QACTIVE_POST(&me->super, &reminder->super, me);
  120. status_ = Q_HANDLED();
  121. }
  122. /*${Components::Cruncher::SM::processing::CRUNCH::[else]} */
  123. else {
  124. PRINTF_S("pi=%16.14f\n", 4.0 * me->sum);
  125. status_ = Q_TRAN(&Cruncher_processing);
  126. }
  127. break;
  128. }
  129. /*${Components::Cruncher::SM::processing::ECHO} */
  130. case ECHO_SIG: {
  131. PRINTF_S("Echo! pi=%16.14f\n", 4.0 * me->sum);
  132. status_ = Q_HANDLED();
  133. break;
  134. }
  135. /*${Components::Cruncher::SM::processing::TERMINATE} */
  136. case TERMINATE_SIG: {
  137. status_ = Q_TRAN(&Cruncher_final);
  138. break;
  139. }
  140. default: {
  141. status_ = Q_SUPER(&QHsm_top);
  142. break;
  143. }
  144. }
  145. return status_;
  146. }
  147. /*${Components::Cruncher::SM::final} .......................................*/
  148. static QState Cruncher_final(Cruncher * const me, QEvt const * const e) {
  149. QState status_;
  150. switch (e->sig) {
  151. /*${Components::Cruncher::SM::final} */
  152. case Q_ENTRY_SIG: {
  153. PRINTF_S("%s\n", "final-ENTRY;");
  154. QF_stop(); /* terminate the application */
  155. status_ = Q_HANDLED();
  156. break;
  157. }
  158. default: {
  159. status_ = Q_SUPER(&QHsm_top);
  160. break;
  161. }
  162. }
  163. return status_;
  164. }
  165. /*$enddef${Components::Cruncher} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
  166. /* test harness ============================================================*/
  167. /* Local-scope objects -----------------------------------------------------*/
  168. static Cruncher l_cruncher; /* the Cruncher active object */
  169. QEvt const *l_cruncherQSto[10]; /* Event queue storage for Cruncher AO */
  170. static QF_MPOOL_EL(ReminderEvt) l_smlPoolSto[20]; /* storage for small pool */
  171. /*..........................................................................*/
  172. int main(int argc, char *argv[]) {
  173. PRINTF_S("Reminder state pattern\nQP version: %s\n"
  174. "Press 'e' to echo the current value...\n"
  175. "Press ESC to quit...\n",
  176. QP_VERSION_STR);
  177. fflush(stdout);
  178. Cruncher_ctor(&l_cruncher);
  179. BSP_init(argc, argv); /* initialize the BSP */
  180. QF_init();/* initialize the framework and the underlying RT kernel */
  181. /* publish-subscribe not used, no call to QF_psInit() */
  182. QF_poolInit(l_smlPoolSto, sizeof(l_smlPoolSto), sizeof(l_smlPoolSto[0]));
  183. /* instantiate and start the active objects... */
  184. QACTIVE_START(&l_cruncher.super,
  185. 1U,
  186. l_cruncherQSto, Q_DIM(l_cruncherQSto),
  187. (void *)0, 0U, (QEvt *)0);
  188. return QF_run(); /* run the QF application */
  189. }
  190. /*..........................................................................*/
  191. void BSP_onKeyboardInput(uint8_t key) {
  192. switch (key) {
  193. case 'e': { /* echo the progress so far */
  194. static QEvt const echoEvt = { ECHO_SIG, 0U, 0U };
  195. QACTIVE_POST((QActive *)&l_cruncher, &echoEvt, (void *)0);
  196. break;
  197. }
  198. case '\033': { /* ESC pressed? */
  199. /* NOTE: this constant event is statically pre-allocated.
  200. * It can be posted/published as any other event.
  201. */
  202. static QEvt const terminateEvt = { TERMINATE_SIG, 0U, 0U };
  203. QACTIVE_POST((QActive *)&l_cruncher, &terminateEvt, (void *)0);
  204. break;
  205. }
  206. }
  207. }