144 lines
5.1 KiB
C++
Raw Normal View History

2013-10-10 20:01:51 -04:00
//****************************************************************************
2014-04-13 21:35:34 -04:00
// Product: Calculator2 Example
2015-06-06 18:30:55 -04:00
// Last Updated for Version: 5.4.2
// Date of the Last Update: 2015-06-06
2012-08-14 18:00:48 -04:00
//
// Q u a n t u m L e a P s
// ---------------------------
// innovating embedded systems
//
2014-04-13 21:35:34 -04:00
// Copyright (C) 2002-2013 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
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Contact information:
2015-05-14 16:05:04 -04:00
// Web : http://www.state-machine.com
// Email: info@state-machine.com
2013-10-10 20:01:51 -04:00
//****************************************************************************
2015-05-14 16:05:04 -04:00
#include "qpcpp.h" // the port of the QP framework
#include "bsp.h" // board support package
2014-04-13 21:35:34 -04:00
#include "calc2.h"
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
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
2014-04-13 21:35:34 -04:00
using namespace std;
2012-08-14 18:00:48 -04:00
//............................................................................
int main() {
2014-04-13 21:35:34 -04:00
cout << "Calculator2 example, QEP version: "
<< 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"
2015-06-06 18:30:55 -04:00
"Press '%' to calculate percentage\n"
2012-08-14 18:00:48 -04:00
"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
2015-05-14 16:05:04 -04:00
BSP_display(); // show the display
2012-08-14 18:00:48 -04:00
cout << ": ";
2014-04-13 21:35:34 -04:00
cout.flush();
2015-05-14 16:05:04 -04:00
e.key_code = (uint8_t)_getche(); // get a char with echo
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 '%': { // Percent key
2014-04-13 21:35:34 -04:00
static_cast<QP::QEvt *>(&e)->sig = PERCENT_SIG;
2012-08-14 18:00:48 -04:00
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;
}
}
2015-05-14 16:05:04 -04:00
// valid event generated?
if (static_cast<QP::QEvt *>(&e)->sig != 0) {
the_calc->dispatch(&e); // dispatch event
2012-08-14 18:00:48 -04:00
}
}
return 0;
}