RKH
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Identifying, generating and sending events
rkh_bunner.jpg


Prev: Preparing the application files
Next: Identifying and classifying states and pseudostates
The events in the my state machine have been identified in the state diagram in Figure 1. Note that events consist really of two parts. The part of the event called the signal conveys the type of the occurrence (what happened). For example, the ONE signal conveys the arrival of a press key '1'. An event can also contain additional quantitative information about the occurrence in form of event parameters. In my state machine, all signals are accompanied by the parameter (ts) that contain the quantitative information as to timestamp. In RKH, events are represented as instances of the RKH_EVT_T structure provided by the framework. Specifically, the RKH_EVT_T structure contains the member e, to represent the signal of that event. Event parameters are added in the process of inheritance .


Identifying events

Because events are explicitly shared among most of the application components, it is convenient to declare them in the separate header file "my.h", which is shown below.

/* -------------------------- Development history -------------------------- */
/*
* 2016.12.06 LeFr v2.4.05 Initial version
*/
/* -------------------------------- Authors -------------------------------- */
/*
* LeFr Leandro Francucci francuccilea@gmail.com
*/
/* --------------------------------- Notes --------------------------------- */
/* --------------------------------- Module -------------------------------- */
#ifndef __MY_H__
#define __MY_H__
/* ----------------------------- Include files ----------------------------- */
#include "rkh.h"
...
/* ................................ Signals ................................ */
typedef enum Signals Signals;
enum Signals
{
ZERO, /* press the key '0' on the keyboard */
ONE, /* press the key '1' on the keyboard */
TWO, /* press the key '2' on the keyboard */
THREE, /* press the key '3' on the keyboard */
FOUR, /* press the key '4' on the keyboard */
FIVE, /* press the key '5' on the keyboard */
SIX, /* press the key '6' on the keyboard */
TERM /* press the key escape on the keyboard */
};
/* ................................. Events ................................ */
typedef struct
{
RKH_EVT_T event;
rui16_t ts;
} MyEvt;
...

Generating and posting events

RKH supports the simple mechanism of direct event posting supported through the functions rkh_sma_post_fifo() and rkh_sma_post_lifo(), where the producer of an event directly posts the event to the event queue of the consumer active object (SMA). In RKH, any part of the system can produce events, not necessarily only the SMAs. For example, interrupt service routines (ISRs) or device drivers can also produce events. On the other hand, only SMAs can consume events, because only SMAs have event queues. The following listing provides examples of posting dynamic and static events from the interrupt service routines (ISRs) of the ahsm demo application version for the win32 single thread, "bsp.c" file.

#define ESC 0x1B
#define kbmap(c) ((c) - '0')
(1) static RKH_ROM_STATIC_EVENT(eterm, TERM);
...
static DWORD WINAPI
(2) isr_kbd_thread(LPVOID par) /* Win32 thread to emulate keyboard ISR */
{
int c;
MYEVT_T *mye;
(void)par;
while (running)
{
c = _getch();
if (c == ESC)
{
(3) RKH_SMA_POST_FIFO(my, &eterm, 0);
}
else
{
(4) mye = RKH_ALLOC_EVT(MYEVT_T, kbmap(c));
(5) mye->ts = (rui16_t)rand();
}
}
return 0;
}
  • (1) The TERM event never changes, so it can be statically allocated just once. The RKH_ROM_STATIC_EVENT() macro declares and initializes the event structure eterm with TERM signal and establishes it as one static event. The created event object is explicitly placed in ROM.
  • (2) Win32 thread to emulate keyboard ISR.
  • (3) The static event is posted directly to the my SMA.
  • (4) The macro RKH_ALLOC_EVT() dinamically allocates an instance of MYEVT_T event from an event pool. The macro also performs the association between the signal and the allocated event. The signals are generated from the key strokes by means of kbmap() macro.
  • (5) The ts parameter of the event is assigned.
  • (6) The dynamic event is posted directly to the my SMA..


Prev: Preparing the application files
Next: Identifying and classifying states and pseudostates