Page 1 of 1

Event Boilerplate Code

Posted: Thu Aug 13, 2020 10:07 am
by JSStabl
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 :)

Re: Event Boilerplate Code

Posted: Thu Aug 13, 2020 10:32 am
by Giovanni
Note that you can signal events to a specific thread without having to use listeners and sources, just one command chEvtSignal().

Giovanni