main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*****************************************************************************
  2. * Product: calc Example
  3. * Last updated for version 6.9.1
  4. * Last updated on 2020-09-11
  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 "calc.h"
  37. #include "safe_std.h" /* portable "safe" <stdio.h>/<string.h> facilities */
  38. Q_DEFINE_THIS_FILE
  39. /*..........................................................................*/
  40. int main() {
  41. QF_init();
  42. QF_onStartup();
  43. PRINTF_S("Calculator example, QP version: %s\n"
  44. "Press '0' .. '9' to enter a digit\n"
  45. "Press '.' to enter the decimal point\n"
  46. "Press '+' to add\n"
  47. "Press '-' to subtract or negate a number\n"
  48. "Press '*' to multiply\n"
  49. "Press '/' to divide\n"
  50. "Press '=' or <Enter> to get the result\n"
  51. "Press 'c' or 'C' to Cancel\n"
  52. "Press 'e' or 'E' to Cancel Entry\n"
  53. "Press <Esc> to quit.\n\n",
  54. QP_VERSION_STR);
  55. Calc_ctor(); /* explicitly instantiate the calculator object */
  56. QHSM_INIT(the_calc, (void *)0, 0U); /* trigger initial transition */
  57. for (;;) { /* event loop */
  58. CalcEvt e; /* Calculator event */
  59. BSP_display(); /* show the display */
  60. PRINTF_S("%s", " : ");
  61. fflush(stdout);
  62. e.key_code = (uint8_t)QF_consoleWaitForKey();
  63. PRINTF_S("%c ", (e.key_code >= ' ') ? e.key_code : 'X');
  64. switch (e.key_code) {
  65. case 'c': /* intentionally fall through */
  66. case 'C': {
  67. e.super.sig = C_SIG;
  68. break;
  69. }
  70. case 'e': /* intentionally fall through */
  71. case 'E': {
  72. e.super.sig = CE_SIG;
  73. break;
  74. }
  75. case '0': {
  76. e.super.sig = DIGIT_0_SIG;
  77. break;
  78. }
  79. case '1': /* intentionally fall through */
  80. case '2': /* intentionally fall through */
  81. case '3': /* intentionally fall through */
  82. case '4': /* intentionally fall through */
  83. case '5': /* intentionally fall through */
  84. case '6': /* intentionally fall through */
  85. case '7': /* intentionally fall through */
  86. case '8': /* intentionally fall through */
  87. case '9': {
  88. e.super.sig = DIGIT_1_9_SIG;
  89. break;
  90. }
  91. case '.': {
  92. e.super.sig = POINT_SIG;
  93. break;
  94. }
  95. case '+': /* intentionally fall through */
  96. case '-': /* intentionally fall through */
  97. case '*': /* intentionally fall through */
  98. case '/': {
  99. e.super.sig = OPER_SIG;
  100. break;
  101. }
  102. case '=': /* intentionally fall through */
  103. case '\r': { /* Enter key */
  104. e.super.sig = EQUALS_SIG;
  105. break;
  106. }
  107. case '\33': { /* ESC key */
  108. e.super.sig = OFF_SIG;
  109. break;
  110. }
  111. default: {
  112. e.super.sig = 0; /* invalid event */
  113. break;
  114. }
  115. }
  116. if (e.super.sig != 0) { /* valid event generated? */
  117. QHSM_DISPATCH(the_calc, &e.super, 0U); /* dispatch event */
  118. }
  119. }
  120. QF_onCleanup();
  121. return 0;
  122. }