Event Boilerplate Code

Discussions and support about ChibiOS/RT, the free embedded RTOS.
JSStabl
Posts: 78
Joined: Tue Feb 25, 2020 4:06 pm
Has thanked: 3 times
Been thanked: 2 times

Event Boilerplate Code

Postby JSStabl » Thu Aug 13, 2020 10:07 am

I'm using events to signal some changes to my statemachine running in its own thread, I might as well use messaging or direct signaling but events seems to work well.

The thing that bothers me is the amount of boilerplate I have to write to add a single event. (Code is an excerpt from different files in my program)

Code: Select all

// declare eventsource
EVENTSOURCE_DECL(es_inverter_init);

//make available in header file
extern event_source_t es_inverter_init;

//declare enum for event mask
typedef enum  {
    COMMAND_INIT
}

// event listener in thread
event_listener_t init_listener;

//map listener to event source and enum
chEvtRegisterMask(&es_inverter_init, &init_listener, EVENT_MASK(COMMAND_INIT));

//Send event from other thread
chEvtBroadcast(&es_inverter_init);

//receive event somewhere else
eventmask_t evt = chEvtWaitAny(EVENT_MASK(COMMAND_INIT);

//decide to do something
if(evt & EVENT_MASK(COMMAND_INIT))     { return some other state; }


Having 4 or 5 different COMMANDS for the state switches makes that process produce a lot of boilerplate code. Am I doing something wrong? Maybe theres a better tool for this that I'm missing :)

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Event Boilerplate Code

Postby Giovanni » Thu Aug 13, 2020 10:32 am

Note that you can signal events to a specific thread without having to use listeners and sources, just one command chEvtSignal().

Giovanni


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 5 guests