352 lines
12 KiB
C++
Raw Normal View History

//============================================================================
2020-03-17 21:33:58 -04:00
// Product: DPP example with emWin/uC/GUI, NO Window Manager
2020-10-01 12:50:17 -04:00
// Last updated for version 6.9.1
// Last updated on 2020-09-21
2016-06-10 21:51:18 -04:00
//
2020-10-01 12:50:17 -04:00
// Q u a n t u m L e a P s
// ------------------------
// Modern Embedded Software
2016-06-10 21:51:18 -04:00
//
2020-10-01 12:50:17 -04:00
// Copyright (C) 2005-2020 Quantum Leaps. All rights reserved.
2016-06-10 21:51:18 -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.
//
// 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
2020-10-01 12:50:17 -04:00
// along with this program. If not, see <www.gnu.org/licenses>.
2016-06-10 21:51:18 -04:00
//
// Contact information:
2019-12-31 15:56:23 -05:00
// <www.state-machine.com/licensing>
// <info@state-machine.com>
//============================================================================
2019-10-27 12:26:31 -04:00
#include "qpcpp.hpp"
#include "dpp.hpp"
#include "bsp.hpp"
2016-06-10 21:51:18 -04:00
extern "C" {
#include "GUI.h"
2022-11-17 14:13:33 -05:00
#include "LCD_SIM.h"
2016-06-10 21:51:18 -04:00
#include "DIALOG.h"
2020-03-17 21:33:58 -04:00
#include "WM.h" // emWin Windows Manager
2016-06-10 21:51:18 -04:00
}
Q_DEFINE_THIS_FILE
// Active object class -------------------------------------------------------
class Table : public QActive {
private:
uint8_t m_fork[N_PHILO];
uint8_t m_isHungry[N_PHILO];
public:
Table();
private:
2020-03-17 21:33:58 -04:00
Q_STATE_DECL(initial);
Q_STATE_DECL(ready);
Q_STATE_DECL(serving);
Q_STATE_DECL(paused);
2016-06-10 21:51:18 -04:00
};
#define RIGHT(n_) ((uint8_t)(((n_) + (N_PHILO - 1)) % N_PHILO))
#define LEFT(n_) ((uint8_t)(((n_) + 1) % N_PHILO))
enum m_forkState { FREE, USED };
// Local objects -------------------------------------------------------------
2020-03-17 21:33:58 -04:00
static Table l_table; // local Table object
2016-06-10 21:51:18 -04:00
// Public-scope objects ------------------------------------------------------
2020-03-17 21:33:58 -04:00
QActive * const AO_Table = &l_table; // "opaque" AO pointer
2016-06-10 21:51:18 -04:00
// GUI definition ============================================================
static WM_HWIN l_hDlg;
static WM_CALLBACK *l_cb_WM_HBKWIN;
static const GUI_WIDGET_CREATE_INFO l_dialog[] = {
{ &FRAMEWIN_CreateIndirect, "Dining Philosopher Problem",
0, 30, 30, 260, 180, FRAMEWIN_CF_MOVEABLE },
{ &TEXT_CreateIndirect, "Philosopher 0",
GUI_ID_TEXT9, 50, 10, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "Philosopher 1",
GUI_ID_TEXT9, 50, 30, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "Philosopher 2",
GUI_ID_TEXT9, 50, 50, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "Philosopher 3",
GUI_ID_TEXT9, 50, 70, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "Philosopher 4",
GUI_ID_TEXT9, 50, 90, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "Table",
GUI_ID_TEXT9, 50, 110, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "thinking",
GUI_ID_TEXT0, 130, 10, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "thinking",
GUI_ID_TEXT1, 130, 30, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "thinking",
GUI_ID_TEXT2, 130, 50, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "thinking",
GUI_ID_TEXT3, 130, 70, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "thinking",
GUI_ID_TEXT4, 130, 90, 0, 0, TEXT_CF_LEFT },
{ &TEXT_CreateIndirect, "serving",
GUI_ID_TEXT5, 130, 110, 0, 0, TEXT_CF_LEFT },
{ &BUTTON_CreateIndirect,"PAUSE",
GUI_ID_BUTTON0, 160, 130, 80, 30 }
};
2020-10-01 12:50:17 -04:00
//............................................................................
2016-06-10 21:51:18 -04:00
static void onMainWndGUI(WM_MESSAGE* pMsg) {
switch (pMsg->MsgId) {
case WM_PAINT: {
GUI_SetBkColor(GUI_GRAY);
GUI_Clear();
GUI_SetColor(GUI_BLACK);
GUI_SetFont(&GUI_Font24_ASCII);
GUI_DispStringHCenterAt("Dining Philosophers - Demo", 160, 5);
break;
}
default: {
WM_DefaultProc(pMsg);
break;
}
}
}
2020-10-01 12:50:17 -04:00
//............................................................................
2016-06-10 21:51:18 -04:00
static void onDialogGUI(WM_MESSAGE * pMsg) {
switch (pMsg->MsgId) {
case WM_INIT_DIALOG: {
break;
}
case WM_NOTIFY_PARENT: {
switch (pMsg->Data.v) {
2020-10-01 12:50:17 -04:00
case WM_NOTIFICATION_RELEASED: { // react only if released
2016-06-10 21:51:18 -04:00
switch (WM_GetId(pMsg->hWinSrc)) {
case GUI_ID_BUTTON0: {
2020-10-01 12:50:17 -04:00
// static PAUSE event for the Table AO
2019-12-31 15:56:23 -05:00
static QEvt const pauseEvt = { PAUSE_SIG, 0 };
2016-06-10 21:51:18 -04:00
AO_Table->POST(&pauseEvt, &l_onDialogGUI);
break;
}
}
break;
}
}
break;
}
default: {
WM_DefaultProc(pMsg);
break;
}
}
}
2020-10-01 12:50:17 -04:00
//............................................................................
2016-06-10 21:51:18 -04:00
static void displyPhilStat(uint8_t n, char const *stat) {
TEXT_SetText(WM_GetDialogItem(l_hDlg, GUI_ID_TEXT0 + n), stat);
2020-10-01 12:50:17 -04:00
WM_Exec(); // update the screen and invoke WM callbacks
2016-06-10 21:51:18 -04:00
QS_BEGIN_ID(PHILO_STAT, AO_Philo[n]->getPrio()) // app-specific record begin
2020-10-01 12:50:17 -04:00
QS_U8(1, n); // Philosopher number
QS_STR(stat); // Philosopher status
2016-06-10 21:51:18 -04:00
QS_END()
}
2020-10-01 12:50:17 -04:00
//............................................................................
2016-06-10 21:51:18 -04:00
static void displyTableStat(char const *stat) {
TEXT_SetText(WM_GetDialogItem(l_hDlg, GUI_ID_TEXT5), stat);
2020-10-01 12:50:17 -04:00
WM_Exec(); // update the screen and invoke WM callbacks
2016-06-10 21:51:18 -04:00
QS_BEGIN_ID(TABLE_STAT, AO_Table->getPrio()) // app-specific record begin
2020-10-01 12:50:17 -04:00
QS_STR(stat); // Philosopher status
2016-06-10 21:51:18 -04:00
QS_END()
}
//............................................................................
Table::Table() : QActive((QStateHandler)&Table::initial) {
uint8_t n;
for (n = 0; n < N_PHILO; ++n) {
m_fork[n] = FREE;
m_isHungry[n] = 0;
}
}
//............................................................................
2020-03-17 21:33:58 -04:00
Q_STATE_DEF(Table, initial) {
2016-06-10 21:51:18 -04:00
2020-03-17 21:33:58 -04:00
GUI_Init(); // initialize the embedded GUI
2016-06-10 21:51:18 -04:00
2020-03-17 21:33:58 -04:00
subscribe(DONE_SIG);
subscribe(PAUSE_SIG);
2016-06-10 21:51:18 -04:00
2020-03-17 21:33:58 -04:00
return tran(&ready);
2016-06-10 21:51:18 -04:00
}
//............................................................................
2020-03-17 21:33:58 -04:00
Q_STATE_DEF(Table, ready) {
2016-06-10 21:51:18 -04:00
switch (e->sig) {
case Q_ENTRY_SIG: {
l_cb_WM_HBKWIN = WM_SetCallback(WM_HBKWIN, &onMainWndGUI);
2020-03-17 21:33:58 -04:00
// create the diaglog box and return right away...
2016-06-10 21:51:18 -04:00
l_hDlg = GUI_CreateDialogBox(l_dialog, GUI_COUNTOF(l_dialog),
&onDialogGUI, 0, 0, 0);
2020-03-17 21:33:58 -04:00
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
case Q_EXIT_SIG: {
GUI_EndDialog(l_hDlg, 0);
WM_SetCallback(WM_HBKWIN, l_cb_WM_HBKWIN);
2020-03-17 21:33:58 -04:00
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
case Q_INIT_SIG: {
2020-03-17 21:33:58 -04:00
return tran(&serving);
2016-06-10 21:51:18 -04:00
}
2020-03-17 21:33:58 -04:00
case MOUSE_CHANGE_SIG: { // mouse change (move or click) event
2016-06-10 21:51:18 -04:00
GUI_PID_STATE mouse;
2020-03-17 21:33:58 -04:00
mouse.x = Q_EVT_CAST(MouseEvt)->x;
mouse.y = Q_EVT_CAST(MouseEvt)->y;
mouse.Pressed = Q_EVT_CAST(MouseEvt)->Pressed;
mouse.Layer = Q_EVT_CAST(MouseEvt)->Layer;
GUI_PID_StoreState(&mouse); // update the state of the Mouse PID
2016-06-10 21:51:18 -04:00
2020-10-01 12:50:17 -04:00
WM_Exec(); // update the screen and invoke WM callbacks
2020-03-17 21:33:58 -04:00
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
// ... hardkey events ...
2020-03-17 21:33:58 -04:00
case KEY_LEFT_REL_SIG: { // hardkey LEFT released
2016-06-10 21:51:18 -04:00
WM_MoveWindow(l_hDlg, -5, 0);
2020-03-17 21:33:58 -04:00
WM_Exec(); // update the screen and invoke WM callbacks
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
2020-03-17 21:33:58 -04:00
case KEY_RIGHT_REL_SIG: { // hardkey RIGHT released
2016-06-10 21:51:18 -04:00
WM_MoveWindow(l_hDlg, 5, 0);
2020-03-17 21:33:58 -04:00
WM_Exec(); // update the screen and invoke WM callbacks
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
2020-03-17 21:33:58 -04:00
case KEY_DOWN_REL_SIG: { // hardkey DOWN released
2016-06-10 21:51:18 -04:00
WM_MoveWindow(l_hDlg, 0, 5);
2020-03-17 21:33:58 -04:00
WM_Exec(); // update the screen and invoke WM callbacks
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
2020-03-17 21:33:58 -04:00
case KEY_UP_REL_SIG: { // hardkey UP released
2016-06-10 21:51:18 -04:00
WM_MoveWindow(l_hDlg, 0, -5);
2020-03-17 21:33:58 -04:00
WM_Exec(); // update the screen and invoke WM callbacks
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
}
2020-03-17 21:33:58 -04:00
return super(&top);
2016-06-10 21:51:18 -04:00
}
//............................................................................
2020-03-17 21:33:58 -04:00
Q_STATE_DEF(Table, serving) {
2016-06-10 21:51:18 -04:00
uint8_t n, m;
TableEvt *pe;
switch (e->sig) {
case Q_ENTRY_SIG: {
displyTableStat("serving");
2020-03-17 21:33:58 -04:00
for (n = 0; n < N_PHILO; ++n) { // give permissions to eat...
if (m_isHungry[n]
&& (m_fork[LEFT(n)] == FREE)
&& (m_fork[n] == FREE))
2016-06-10 21:51:18 -04:00
{
2020-03-17 21:33:58 -04:00
m_fork[LEFT(n)] = m_fork[n] = USED;
2016-06-10 21:51:18 -04:00
pe = Q_NEW(TableEvt, EAT_SIG);
pe->philoNum = n;
QF::PUBLISH(pe, me);
2020-03-17 21:33:58 -04:00
m_isHungry[n] = 0;
2016-06-10 21:51:18 -04:00
displyPhilStat(n, "eating ");
}
}
2020-03-17 21:33:58 -04:00
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
case HUNGRY_SIG: {
n = ((TableEvt *)e)->philoNum;
2020-03-17 21:33:58 -04:00
Q_ASSERT(n < N_PHILO && !m_isHungry[n]);
2016-06-10 21:51:18 -04:00
displyPhilStat(n, "hungry ");
m = LEFT(n);
2020-03-17 21:33:58 -04:00
if (m_fork[m] == FREE && m_fork[n] == FREE) {
m_fork[m] = m_fork[n] = USED;
2016-06-10 21:51:18 -04:00
pe = Q_NEW(TableEvt, EAT_SIG);
pe->philoNum = n;
QF::PUBLISH(pe, me);
displyPhilStat(n, "eating ");
}
else {
2020-03-17 21:33:58 -04:00
m_isHungry[n] = 1;
2016-06-10 21:51:18 -04:00
}
2020-03-17 21:33:58 -04:00
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
case DONE_SIG: {
n = ((TableEvt *)e)->philoNum;
Q_ASSERT(n < N_PHILO);
displyPhilStat(n, "thinking");
2020-03-17 21:33:58 -04:00
m_fork[LEFT(n)] = m_fork[n] = FREE;
m = RIGHT(n); // check the right neighbor
if (m_isHungry[m] && m_fork[m] == FREE) {
m_fork[n] = m_fork[m] = USED;
m_isHungry[m] = 0;
2016-06-10 21:51:18 -04:00
pe = Q_NEW(TableEvt, EAT_SIG);
pe->philoNum = m;
QF::PUBLISH(pe, me);
displyPhilStat(m, "eating ");
}
2020-03-17 21:33:58 -04:00
m = LEFT(n); // check the left neighbor
2016-06-10 21:51:18 -04:00
n = LEFT(m);
2020-03-17 21:33:58 -04:00
if (m_isHungry[m] && m_fork[n] == FREE) {
m_fork[m] = m_fork[n] = USED;
m_isHungry[m] = 0;
2016-06-10 21:51:18 -04:00
pe = Q_NEW(TableEvt, EAT_SIG);
pe->philoNum = m;
QF::PUBLISH(pe, me);
displyPhilStat(m, "eating ");
}
2020-03-17 21:33:58 -04:00
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
2020-03-17 21:33:58 -04:00
case PAUSE_SIG: // "Toggle" button pressed
case KEY_CENTER_PRESS_SIG: { // hardkey CENTER pressed
return tran(&paused);
2016-06-10 21:51:18 -04:00
}
}
2020-03-17 21:33:58 -04:00
return super(&ready);
2016-06-10 21:51:18 -04:00
}
//............................................................................
2020-03-17 21:33:58 -04:00
Q_STATE_DEF(Table, paused) {
2016-06-10 21:51:18 -04:00
uint8_t n;
switch (e->sig) {
case Q_ENTRY_SIG: {
displyTableStat("paused");
2020-03-17 21:33:58 -04:00
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
case HUNGRY_SIG: {
n = ((TableEvt *)e)->philoNum;
2020-03-17 21:33:58 -04:00
Q_ASSERT(n < N_PHILO && !m_isHungry[n]);
2016-06-10 21:51:18 -04:00
displyPhilStat(n, "hungry ");
2020-03-17 21:33:58 -04:00
m_isHungry[n] = 1;
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
case DONE_SIG: {
n = ((TableEvt *)e)->philoNum;
Q_ASSERT(n < N_PHILO);
displyPhilStat(n, "thinking");
2020-03-17 21:33:58 -04:00
m_fork[LEFT(n)] = m_fork[n] = FREE;
return Q_RET_HANDLED;
2016-06-10 21:51:18 -04:00
}
2020-03-17 21:33:58 -04:00
case PAUSE_SIG: // "Toggle" button pressed
case KEY_CENTER_REL_SIG: { // hardkey CENTER released
return tran(&serving);
2016-06-10 21:51:18 -04:00
}
}
2020-03-17 21:33:58 -04:00
return super(&ready);
2016-06-10 21:51:18 -04:00
}