#ifndef qpbob_h
#define qpbob_h
//(1)
enum DPPSignals {
READY = Q_USER_SIG,
GET,
TIMEOUT,
TERMINATE_SIG,
MAX_PUB_SIG,
MAX_SIG
};
//(2)
extern QActive * const AO_BOB;
extern QActive * const AO_CONVEYOR;
#endif
* This source code was highlighted with Source Code Highlighter .
#ifndef bsp_h
#define bsp_h
//(1)
#define BSP_TICKS_PER_SEC 1
//(2)
void BSP_init( int argc, char *argv[]);
void BSP_print( char const *str);
#endif // bsp_h
* This source code was highlighted with Source Code Highlighter .
#include "qp_port.h"
#include "qpbob.h"
#include "bsp.h"
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
Q_DEFINE_THIS_FILE
//(1)
unsigned int TimeCounter;
//(2)
void BSP_timeStamp()
{
printf( "%d - " ,TimeCounter);
}
//(3) idle-
static uint8_t l_running;
//(4) idle-
static DWORD WINAPI idleThread(LPVOID par) {
TimeCounter=0;
( void )par;
l_running = (uint8_t)1;
while (l_running) {
Sleep(100);
TimeCounter++;
//(5) Esc
if (_kbhit()) {
if (_getch() == '\33' ) {
QF::publish(Q_NEW(QEvent, TERMINATE_SIG));
BSP_print( "Esc exit" );
}
}
}
return 0;
}
//(6)
void BSP_init( int argc, char *argv[]) {
HANDLE hIdle;
//(7)
QF_setTickRate(BSP_TICKS_PER_SEC);
//(8) idle
hIdle = CreateThread(NULL, 1024, &idleThread, ( void *)0, 0, NULL);
Q_ASSERT(hIdle != (HANDLE)0);
SetThreadPriority(hIdle, THREAD_PRIORITY_IDLE);
printf( "QP example"
"\nQEP %s\nQF %s\n"
"Press ESC to quit...\n" ,
QEP::getVersion(),
QF::getVersion());
}
//............................................................................
void QF::onStartup( void ) {
}
//(9)..........................................................................
void QF::onCleanup( void ) {
l_running = (uint8_t)0;
}
//............................................................................
void BSP_print( char const *str)
{
BSP_timeStamp();
printf( "%s\n" , str);
}
//(10)
void Q_onAssert( char const Q_ROM * const Q_ROM_VAR file, int line) {
fprintf(stderr, "Assertion failed in %s, line %d" , file, line);
exit(0);
}
* This source code was highlighted with Source Code Highlighter .
#include "qp_port.h"
#include "qpbob.h"
#include "bsp.h"
//(1)
static QEvent const *l_bobQueueSto[10];
static QEvent const *l_conQueueSto[10];
//(2)
static QSubscrList l_subscrSto[MAX_PUB_SIG];
//(3) ,
static union SmallEvents {
void *min_size;
} l_smlPoolSto[10];
int main( int argc, char *argv[]) {
//(4) BSP
BSP_init(argc, argv);
//(5)
QF::init();
//(6)
QS_FILTER_ON(QS_ALL_RECORDS);
QS_FILTER_OFF(QS_QF_INT_LOCK);
QS_FILTER_OFF(QS_QF_INT_UNLOCK);
QS_FILTER_OFF(QS_QF_ISR_ENTRY);
QS_FILTER_OFF(QS_QF_ISR_EXIT);
QS_FILTER_OFF(QS_QF_TICK);
QS_FILTER_OFF(QS_QK_SCHEDULE);
//(7)
QS_OBJ_DICTIONARY(l_smlPoolSto);
//(8)
QF::psInit(l_subscrSto, Q_DIM(l_subscrSto));
//(9)
QF::poolInit(l_smlPoolSto, sizeof (l_smlPoolSto), sizeof (l_smlPoolSto[0]));
//(10)
AO_CONVEYOR->start(2,
l_conQueueSto, Q_DIM(l_conQueueSto),
( void *)0, 1024, (QEvent *)0);
AO_BOB->start(3,
l_bobQueueSto, Q_DIM(l_bobQueueSto),
( void *)0, 1024, (QEvent *)0);
//(11)
QF::run();
return 0;
}
* This source code was highlighted with Source Code Highlighter .
#include "qp_port.h"
#include "qpbob.h"
#include "bsp.h"
Q_DEFINE_THIS_FILE
class Bob : public QActive {
private :
//(1)
QTimeEvt m_timeEvt;
public :
Bob();
private :
//(2)
//
static QState initial(Bob *me, QEvent const *e);
static QState free(Bob *me, QEvent const *e);
static QState work(Bob *me, QEvent const *e);
};
static Bob l_Bob;
#define TIMEOUT_TIME 1
//(3)
enum InternalSignals {
TIMEOUT_SIG = MAX_SIG
};
QActive * const AO_BOB = &l_Bob;
//............................................................................
Bob::Bob() : QActive((QStateHandler)&Bob::initial),
m_timeEvt(TIMEOUT_SIG){ //(4)
}
//............................................................................
QState Bob::initial(Bob *me, QEvent const *) {
BSP_print( "Bob initial" );
//
QS_OBJ_DICTIONARY(&l_Bob);
QS_OBJ_DICTIONARY(&l_Bob.m_timeEvt);
//
QS_FUN_DICTIONARY(&QHsm::top);
QS_FUN_DICTIONARY(&Bob::initial);
QS_FUN_DICTIONARY(&Bob::free);
QS_FUN_DICTIONARY(&Bob::work);
//
QS_SIG_DICTIONARY(READY, me);
QS_SIG_DICTIONARY(TIMEOUT_SIG, me);
//(5)
me->subscribe(READY);
me->subscribe(TERMINATE_SIG);
//(6)
me->m_timeEvt.postIn(me, TIMEOUT_TIME);
//(7)
return Q_TRAN(&Bob::free);
}
//............................................................................
QState Bob::free(Bob *me, QEvent const *e) {
BSP_print( "Bob free" );
//(8)
switch (e->sig) {
//(9)
case Q_ENTRY_SIG: {
return Q_HANDLED();
}
//(10)
case READY:
{
BSP_print( "O my box READY. Going work" );
return Q_TRAN(&Bob::work);
}
case TIMEOUT_SIG:
{
BSP_print( "Tick Free - Bob" );
//(11)
me->m_timeEvt.postIn(me, TIMEOUT_TIME);
//(12)
QEvent* be=Q_NEW(QEvent,TIMEOUT);
QF::publish(be);
return Q_HANDLED();
}
//(13)
case TERMINATE_SIG: {
BSP_print( "Bob terminate.----------------------------------------------" );
QF::stop(); //(14)
//(15)
return Q_HANDLED();
}
}
return Q_SUPER(&QHsm::top);
}
QState Bob::work(Bob *me, QEvent const *e) {
BSP_print( "Bob work" );
switch (e->sig) {
case Q_ENTRY_SIG: {
QEvent* be=Q_NEW(QEvent,GET);
QF::publish(be);
BSP_print( "Bob public GET" );
return Q_HANDLED();
}
case TIMEOUT_SIG:
{
BSP_print( "Tick Work - Bob" );
BSP_print( "I am going to free." );
me->m_timeEvt.postIn(me, TIMEOUT_TIME);
QEvent* be=Q_NEW(QEvent,TIMEOUT);
QF::publish(be);
return Q_TRAN(&Bob::free);
}
case TERMINATE_SIG: {
BSP_print( "Bob terminate.----------------------------------------------" );
QF::stop();
return Q_HANDLED();
}
}
return Q_SUPER(&QHsm::top);
}
* This source code was highlighted with Source Code Highlighter .
#include "qp_port.h"
#include "qpbob.h"
#include "bsp.h"
Q_DEFINE_THIS_FILE
class Conveyor : public QActive {
private :
public :
Conveyor();
private :
//
static QState initial(Conveyor *me, QEvent const *e);
static QState empty(Conveyor *me, QEvent const *e);
static QState full(Conveyor *me, QEvent const *e);
};
static Conveyor l_Conveyor;
QActive * const AO_CONVEYOR = &l_Conveyor;
//............................................................................
Conveyor::Conveyor() : QActive((QStateHandler)&Conveyor::initial)
{
}
//............................................................................
QState Conveyor::initial(Conveyor *me, QEvent const *) {
BSP_print( "Conveyor initial" );
QS_OBJ_DICTIONARY(&l_Conveyor);
QS_FUN_DICTIONARY(&QHsm::top);
QS_FUN_DICTIONARY(&Conveyor::initial);
QS_FUN_DICTIONARY(&Conveyor::empty);
QS_FUN_DICTIONARY(&Conveyor::full);
QS_SIG_DICTIONARY(GET, me);
QS_SIG_DICTIONARY(TIMEOUT, me);
me->subscribe(GET);
me->subscribe(TIMEOUT);
return Q_TRAN(&Conveyor::empty);
}
//............................................................................
QState Conveyor::empty(Conveyor *me, QEvent const *e) {
BSP_print( "Conveyor empty" );
switch (e->sig) {
case TIMEOUT:
{
BSP_print( "Tick Empty - Conveyor" );
BSP_print( "Conveyor going to full." );
return Q_TRAN(&Conveyor::full);
}
}
return Q_SUPER(&QHsm::top);
}
QState Conveyor::full(Conveyor *me, QEvent const *e) {
BSP_print( "Conveyor full" );
switch (e->sig) {
//(1)
case Q_ENTRY_SIG: {
QEvent* be=Q_NEW(QEvent,READY);
QF::publish(be);
BSP_print( "Conveyor READY." );
}
case GET:
{
BSP_print( "Bob GET box." );
return Q_TRAN(&Conveyor::empty);
}
}
return Q_SUPER(&QHsm::top);
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/102024/