124 lines
4.2 KiB
C++
Raw Normal View History

2013-10-10 20:01:51 -04:00
//****************************************************************************
2019-03-28 11:59:31 -04:00
// Product: Board Support Package (BSP) for the Calculator example
// Last Updated for Version: 6.5.0
// Date of the Last Update: 2019-03-21
2013-10-10 20:01:51 -04:00
//
2018-10-25 11:13:01 -04:00
// Q u a n t u m L e a P s
// ------------------------
// Modern Embedded Software
2013-10-10 20:01:51 -04:00
//
2019-03-28 11:59:31 -04:00
// Copyright (C) 2005-2019 Quantum Leaps, LLC. All rights reserved.
2013-10-10 20:01:51 -04:00
//
2016-09-01 11:58:57 -04:00
// This program is open source software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2013-10-10 20:01:51 -04:00
//
2016-09-01 11:58:57 -04:00
// Alternatively, this program may be distributed and modified under the
2013-10-10 20:01:51 -04:00
// terms of Quantum Leaps commercial licenses, which expressly supersede
2016-09-01 11:58:57 -04:00
// the GNU General Public License and are specifically designed for
// licensees interested in retaining the proprietary status of their code.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-10-10 20:01:51 -04:00
//
// Contact information:
2018-10-25 11:13:01 -04:00
// https://www.state-machine.com
// mailto:info@state-machine.com
2013-10-10 20:01:51 -04:00
//****************************************************************************
2019-10-27 12:26:31 -04:00
#include "qpcpp.hpp"
#include "bsp.hpp"
2013-10-10 20:01:51 -04:00
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
2018-10-25 11:13:01 -04:00
#include <string.h>
2013-10-10 20:01:51 -04:00
2018-10-25 11:13:01 -04:00
using namespace QP;
2013-10-10 20:01:51 -04:00
using namespace std;
#define DISP_WIDTH 9
2016-09-01 11:58:57 -04:00
static char l_display[DISP_WIDTH + 1]; // the calculator display
static int l_len; // number of displayed characters
2013-10-10 20:01:51 -04:00
//............................................................................
void BSP_clear(void) {
memset(l_display, ' ', DISP_WIDTH - 1);
l_display[DISP_WIDTH - 1] = '0';
l_display[DISP_WIDTH] = '\0';
l_len = 0;
}
//............................................................................
void BSP_insert(int keyId) {
if (l_len == 0) {
l_display[DISP_WIDTH - 1] = (char)keyId;
++l_len;
}
else if (l_len < (DISP_WIDTH - 1)) {
memmove(&l_display[0], &l_display[1], DISP_WIDTH - 1);
l_display[DISP_WIDTH - 1] = (char)keyId;
++l_len;
}
}
//............................................................................
2019-03-28 11:59:31 -04:00
void BSP_display(double value) {
sprintf(l_display, "%9.6g", value);
}
//............................................................................
void BSP_display_error(char const *err) {
strcpy(l_display, err);
}
//............................................................................
2013-10-10 20:01:51 -04:00
void BSP_negate(void) {
BSP_clear();
l_display[DISP_WIDTH - 2] = '-';
}
//............................................................................
2019-03-28 11:59:31 -04:00
void BSP_show_display(void) {
2013-10-10 20:01:51 -04:00
cout << endl << '[' << l_display << "] ";
}
//............................................................................
void BSP_exit(void) {
cout << endl << "Bye! Bye" << endl;
2018-10-25 11:13:01 -04:00
QF::onCleanup();
exit(0);
2013-10-10 20:01:51 -04:00
}
//............................................................................
double BSP_get_value(void) {
return strtod(l_display, (char **)0);
}
//............................................................................
void BSP_message(char const *msg) {
cout << msg;
}
2018-10-25 11:13:01 -04:00
namespace QP {
/*..........................................................................*/
void QF::onStartup(void) {
QF_consoleSetup();
}
/*..........................................................................*/
void QF::onCleanup(void) {
QF_consoleCleanup();
}
/*..........................................................................*/
void QF_onClockTick(void) {
}
} // namespace QP
2013-10-10 20:01:51 -04:00
//............................................................................
// this function is used by the QP embedded systems-friendly assertions
2018-10-25 11:13:01 -04:00
extern "C" void Q_onAssert(char const * const module, int loc) {
cout << "Assertion failed in " << module << ':' << loc
2013-10-10 20:01:51 -04:00
<< flush << endl;
2018-10-25 11:13:01 -04:00
QF::onCleanup();
exit(-1);
2013-10-10 20:01:51 -04:00
}