bsp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*****************************************************************************
  2. * Product: Board Support Package (BSP) for the Calculator example
  3. * Last updated for version 6.9.0
  4. * Last updated on 2020-06-24
  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. #include <stdlib.h> /* for exit() */
  38. #define DISP_WIDTH 15
  39. /* helper macros to "stringify" values */
  40. #define VAL(x) #x
  41. #define STRINGIFY(x) VAL(x)
  42. static char l_display[DISP_WIDTH + 1]; /* the calculator display */
  43. static int l_len; /* number of displayed characters */
  44. /*..........................................................................*/
  45. void BSP_clear(void) {
  46. memset(l_display, ' ', DISP_WIDTH - 1);
  47. l_display[DISP_WIDTH - 1] = '0';
  48. l_display[DISP_WIDTH] = '\0';
  49. l_len = 0;
  50. }
  51. /*..........................................................................*/
  52. void BSP_insert(int keyId) {
  53. if (l_len == 0) {
  54. l_display[DISP_WIDTH - 1] = (char)keyId;
  55. ++l_len;
  56. }
  57. else if (l_len < (DISP_WIDTH - 1)) {
  58. MEMMOVE_S(&l_display[0], DISP_WIDTH, &l_display[1], DISP_WIDTH - 1);
  59. l_display[DISP_WIDTH - 1] = (char)keyId;
  60. ++l_len;
  61. }
  62. }
  63. /*..........................................................................*/
  64. void BSP_display(double value) {
  65. SNPRINTF_S(l_display, Q_DIM(l_display),
  66. "%" STRINGIFY(DISP_WIDTH) ".6g", value);
  67. }
  68. /*..........................................................................*/
  69. void BSP_display_error(char const *err) {
  70. STRNCPY_S(l_display, DISP_WIDTH, err);
  71. }
  72. /*..........................................................................*/
  73. void BSP_negate(void) {
  74. BSP_clear();
  75. l_display[DISP_WIDTH - 2] = '-';
  76. }
  77. /*..........................................................................*/
  78. void BSP_show_display(void) {
  79. PRINTF_S("\n[%" STRINGIFY(DISP_WIDTH) "s] ", l_display);
  80. }
  81. /*..........................................................................*/
  82. void BSP_exit(void) {
  83. PRINTF_S("\n%s\n", "Bye! Bye!");
  84. fflush(stdout);
  85. QF_onCleanup();
  86. exit(0);
  87. }
  88. /*..........................................................................*/
  89. double BSP_get_value(void) {
  90. return strtod(l_display, (char **)0);
  91. }
  92. /*..........................................................................*/
  93. void BSP_message(char const *msg) {
  94. PRINTF_S("%s", msg);
  95. }
  96. /*..........................................................................*/
  97. void QF_onStartup(void) {
  98. QF_consoleSetup();
  99. }
  100. /*..........................................................................*/
  101. void QF_onCleanup(void) {
  102. QF_consoleCleanup();
  103. }
  104. /*..........................................................................*/
  105. void QF_onClockTick(void) {
  106. }
  107. /*..........................................................................*/
  108. /* this function is used by the QP embedded systems-friendly assertions */
  109. Q_NORETURN Q_onAssert(char const * const module, int_t const loc) {
  110. FPRINTF_S(stderr, "Assertion failed in %s:%d", module, loc);
  111. QF_onCleanup();
  112. exit(-1);
  113. }