////////////////////////////////////////////////////////////////////////////// // Product: Calculator Example with inheritance of the whole state model // Last Updated for Version: 4.5.00 // Date of the Last Update: May 20, 2012 // // Q u a n t u m L e a P s // --------------------------- // innovating embedded systems // // Copyright (C) 2002-2012 Quantum Leaps, LLC. All rights reserved. // // 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 2 of the License, or // (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 . // // Contact information: // Quantum Leaps Web sites: http://www.quantum-leaps.com // http://www.state-machine.com // e-mail: info@quantum-leaps.com ////////////////////////////////////////////////////////////////////////////// #include "qp_port.h" // the port of the QP framework #include "bsp.h" // board support package #include "calc1.h" #include #include #include #include #include // Local objects ------------------------------------------------------------- static Calc1 l_calc; // instantiate Calculator1 //............................................................................ int main() { cout << "Calculator1 example, QEP version: " << QEP::getVersion() << endl << "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 to get the result\n" "Press 'c' or 'C' to Cancel\n" "Press 'e' or 'E' to Cancel Entry\n" "Press to quit.\n\n"; l_calc.init(); // trigger initial transition for (;;) { // event loop CalcEvt e; // Calculator event BSP_display(); // show the display cout << ": "; fflush(stdout); // cout.flush() doesn't work in Watcom! e.key_code = (uint8_t)_getche(); // get a char with echo switch (e.key_code) { case 'c': // intentionally fall through case 'C': { ((QEvt *)&e)->sig = C_SIG; break; } case 'e': // intentionally fall through case 'E': { ((QEvt *)&e)->sig = CE_SIG; break; } case '0': { ((QEvt *)&e)->sig = DIGIT_0_SIG; break; } 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 case '9': { ((QEvt *)&e)->sig = DIGIT_1_9_SIG; break; } case '.': { ((QEvt *)&e)->sig = POINT_SIG; break; } case '+': // intentionally fall through case '-': // intentionally fall through case '*': // intentionally fall through case '/': { ((QEvt *)&e)->sig = OPER_SIG; break; } case '#': { // alternative '+' ((QEvt *)&e)->sig = OPER_SIG; e.key_code = '+'; break; } case '=': // intentionally fall through case '\r': { // Enter key ((QEvt *)&e)->sig = EQUALS_SIG; break; } case '\33': { // ESC key ((QEvt *)&e)->sig = OFF_SIG; break; } default: { ((QEvt *)&e)->sig = 0; // invalid event break; } } if (((QEvt *)&e)->sig != 0) { // valid event generated? l_calc.dispatch(&e); // dispatch event } } return 0; }