158 lines
5.0 KiB
C++
Raw Normal View History

2013-10-10 20:01:51 -04:00
//****************************************************************************
2018-10-25 11:13:01 -04:00
// Product: Board Support Package (BSP) for the Calculator example
// Last Updated for Version: 6.3.6
// Date of the Last Update: 2018-10-14
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
//
2018-10-25 11:13:01 -04:00
// Copyright (C) 2005-2018 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
//****************************************************************************
2018-10-25 11:13:01 -04:00
#include "qpcpp.h"
2013-10-10 20:01:51 -04:00
#include "bsp.h"
#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;
}
}
//............................................................................
void BSP_negate(void) {
BSP_clear();
l_display[DISP_WIDTH - 2] = '-';
}
//............................................................................
void BSP_display(void) {
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);
}
//............................................................................
int BSP_eval(double operand1, int oper, double operand2) {
int ok = 1;
2015-06-06 18:30:55 -04:00
double result = 0.0;
2013-10-10 20:01:51 -04:00
switch (oper) {
case KEY_PLUS: {
result = operand1 + operand2;
break;
}
case KEY_MINUS: {
result = operand1 - operand2;
break;
}
case KEY_MULT: {
result = operand1 * operand2;
break;
}
case KEY_DIVIDE: {
if ((operand2 < -1e-30) || (1e-30 < operand2)) {
result = operand1 / operand2;
}
else {
2016-09-01 11:58:57 -04:00
strcpy(l_display, " Error 0 "); // error: divide by zero
2013-10-10 20:01:51 -04:00
ok = 0;
}
break;
}
}
if (ok) {
if ((-0.000001 < result) && (result < 0.000001)) {
result = 0.0;
}
if ((-99999999.0 < result) && (result < 99999999.0)) {
sprintf(l_display, "%9.6g", result);
}
else {
2016-09-01 11:58:57 -04:00
strcpy(l_display, " Error 1 "); // error: out of range
2013-10-10 20:01:51 -04:00
ok = 0;
}
}
return ok;
}
//............................................................................
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
}