140 lines
4.9 KiB
C++
Raw Normal View History

2013-10-10 20:01:51 -04:00
//****************************************************************************
2016-09-01 11:58:57 -04:00
// Product: Calculator Example
2019-03-28 11:59:31 -04:00
// Last Updated for Version: 6.5.0
// Date of the Last Update: 2019-03-21
2012-08-14 18:00:48 -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
2012-08-14 18:00:48 -04:00
//
2019-03-28 11:59:31 -04:00
// Copyright (C) 2005-2019 Quantum Leaps, LLC. All rights reserved.
2012-08-14 18:00:48 -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
2012-08-14 18:00:48 -04:00
// (at your option) any later version.
//
// Alternatively, this program may be distributed and modified under the
// terms of Quantum Leaps commercial licenses, which expressly supersede
// 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
2019-12-31 15:56:23 -05:00
// along with this program. If not, see <www.gnu.org/licenses/>.
2012-08-14 18:00:48 -04:00
//
// Contact information:
2019-12-31 15:56:23 -05:00
// <www.state-machine.com/licensing>
// <info@state-machine.com>
2013-10-10 20:01:51 -04:00
//****************************************************************************
2019-10-27 12:26:31 -04:00
#include "qpcpp.hpp" // QP API
#include "bsp.hpp" // board support package
#include "calc1.hpp" // application
2012-08-14 18:00:48 -04:00
2014-04-13 21:35:34 -04:00
#include <iostream>
2012-08-14 18:00:48 -04:00
2018-10-25 11:13:01 -04:00
using namespace QP;
2014-04-13 21:35:34 -04:00
using namespace std;
2012-08-14 18:00:48 -04:00
//............................................................................
int main() {
2018-10-25 11:13:01 -04:00
QF::init();
QF::onStartup();
2016-09-01 11:58:57 -04:00
cout << "Calculator example, QEP version: "
2014-04-13 21:35:34 -04:00
<< QP::QEP::getVersion() << endl
2012-08-14 18:00:48 -04:00
<< "Press '0' .. '9' to enter a digit\n"
"Press '.' to enter the decimal point\n"
"Press '+' or '#' to add\n"
"Press '-' to subtract or negate a number\n"
"Press '*' to multiply\n"
"Press '/' to divide\n"
"Press '=' or <Enter> to get the result\n"
"Press 'c' or 'C' to Cancel\n"
"Press 'e' or 'E' to Cancel Entry\n"
"Press <Esc> to quit.\n\n";
2015-05-14 16:05:04 -04:00
the_calc->init(); // trigger initial transition
2012-08-14 18:00:48 -04:00
2015-05-14 16:05:04 -04:00
for (;;) { // event loop
CalcEvt e; // Calculator event
2012-08-14 18:00:48 -04:00
2019-03-28 11:59:31 -04:00
BSP_show_display(); // show the display
2012-08-14 18:00:48 -04:00
cout << ": ";
2014-04-13 21:35:34 -04:00
cout.flush();
2018-10-25 11:13:01 -04:00
e.key_code = (uint8_t)QF_consoleWaitForKey();
2012-08-14 18:00:48 -04:00
switch (e.key_code) {
2015-05-14 16:05:04 -04:00
case 'c': // intentionally fall through
2012-08-14 18:00:48 -04:00
case 'C': {
2014-04-13 21:35:34 -04:00
static_cast<QP::QEvt *>(&e)->sig = C_SIG;
2012-08-14 18:00:48 -04:00
break;
}
2015-05-14 16:05:04 -04:00
case 'e': // intentionally fall through
2012-08-14 18:00:48 -04:00
case 'E': {
2014-04-13 21:35:34 -04:00
static_cast<QP::QEvt *>(&e)->sig = CE_SIG;
2012-08-14 18:00:48 -04:00
break;
}
case '0': {
2014-04-13 21:35:34 -04:00
static_cast<QP::QEvt *>(&e)->sig = DIGIT_0_SIG;
2012-08-14 18:00:48 -04:00
break;
}
2015-05-14 16:05:04 -04:00
case '1': // intentionally fall through
case '2': // intentionally fall through
case '3': // intentionally fall through
case '4': // intentionally fall through
case '5': // intentionally fall through
case '6': // intentionally fall through
case '7': // intentionally fall through
case '8': // intentionally fall through
2012-08-14 18:00:48 -04:00
case '9': {
2014-04-13 21:35:34 -04:00
static_cast<QP::QEvt *>(&e)->sig = DIGIT_1_9_SIG;
2012-08-14 18:00:48 -04:00
break;
}
case '.': {
2014-04-13 21:35:34 -04:00
static_cast<QP::QEvt *>(&e)->sig = POINT_SIG;
2012-08-14 18:00:48 -04:00
break;
}
2015-05-14 16:05:04 -04:00
case '+': // intentionally fall through
case '-': // intentionally fall through
case '*': // intentionally fall through
2012-08-14 18:00:48 -04:00
case '/': {
2014-04-13 21:35:34 -04:00
static_cast<QP::QEvt *>(&e)->sig = OPER_SIG;
2012-08-14 18:00:48 -04:00
break;
}
2015-05-14 16:05:04 -04:00
case '#': { // alternative '+'
2014-04-13 21:35:34 -04:00
static_cast<QP::QEvt *>(&e)->sig = OPER_SIG;
2012-08-14 18:00:48 -04:00
e.key_code = '+';
break;
}
2015-05-14 16:05:04 -04:00
case '=': // intentionally fall through
case '\r': { // Enter key
2014-04-13 21:35:34 -04:00
static_cast<QP::QEvt *>(&e)->sig = EQUALS_SIG;
break;
}
2015-05-14 16:05:04 -04:00
case '\33': { // ESC key
2014-04-13 21:35:34 -04:00
static_cast<QP::QEvt *>(&e)->sig = OFF_SIG;
2012-08-14 18:00:48 -04:00
break;
}
default: {
2015-05-14 16:05:04 -04:00
static_cast<QP::QEvt *>(&e)->sig = 0; // invalid event
2012-08-14 18:00:48 -04:00
break;
}
}
2016-09-01 11:58:57 -04:00
if (static_cast<QP::QEvt *>(&e)->sig != 0) { // valid event generated?
2015-05-14 16:05:04 -04:00
the_calc->dispatch(&e); // dispatch event
2012-08-14 18:00:48 -04:00
}
}
2018-10-25 11:13:01 -04:00
QF::onCleanup();
2012-08-14 18:00:48 -04:00
return 0;
}