Boost Embedded Performance with the Lightweight C Event Library lwevt
This article introduces lwevt, a lightweight C event‑management library for embedded systems, covering its design goals, key features, X‑Macro‑based type definitions, thread‑safety considerations, and step‑by‑step usage examples with full source code snippets.
Overview
lwevt is a lightweight event‑management library written in C for embedded systems. It provides a platform‑independent mechanism for defining custom event types and associated data, with a simple API for registration and dispatch.
Key Features
Platform‑independent, no architecture‑specific code.
Customizable event types and associated data defined by the application.
Easy to use and maintain.
MIT license.
Getting Started
Clone the repository:
git clone --recurse-submodules https://github.com/MaJerle/lwevtBasic Usage
#include <stdio.h>
#include <string.h>
#include "lwevt/lwevt.h"
static void prv_evt_fn_1(lwevt_t *e) {
switch ((unsigned)e->type) {
case LWEVT_TYPE_MY_EXT_1:
printf("Event fn 1, LWEVT_TYPE_MY_EXT_1 - with data: a: %d, b: %d
",
(int)e->msg.a.a, (int)e->msg.a.b);
break;
default:
break;
}
}
static void prv_evt_fn_2(lwevt_t *e) {
switch ((unsigned)e->type) {
case LWEVT_TYPE_MY_EXT_2:
printf("Event fn 2, LWEVT_TYPE_MY_EXT_2 - with data: a: %d, b: %d
",
(int)e->msg.b.a, (int)e->msg.b.b);
break;
default:
break;
}
}
int main(void) {
lwevt_t *evt;
lwevt_t evt_local;
lwevt_init();
lwevt_register(prv_evt_fn_1);
lwevt_register(prv_evt_fn_2);
evt = lwevt_get_handle();
evt->msg.a.a = 1;
evt->msg.a.b = 2;
lwevt_dispatch(LWEVT_TYPE_MY_EXT_1);
evt_local.msg.b.a = 3;
evt_local.msg.b.b = 4;
lwevt_dispatch_ex(&evt_local, LWEVT_TYPE_MY_EXT_2);
return 0;
}The library provides a global static event handle accessed via lwevt_get_handle(). Functions lwevt_dispatch() and lwevt_dispatch_ex() are available when LWEVT_CFG_ENABLE_DEFAULT_HANDLE is enabled.
Thread‑Safety
In multithreaded environments the caller must ensure that accesses to the default handle and dispatch functions are protected. Using local lwevt_t variables with lwevt_dispatch_ex() avoids global synchronization.
Event Type Definition (X‑Macro)
Event types and payload structures are defined in a user‑maintained lwevt_types.h using the X‑Macro technique, for example:
LWEVT_TYPE_BASIC(LWEVT_TYPE_MY_BASIC_1)
LWEVT_TYPE_BASIC(LWEVT_TYPE_MY_BASIC_2)
LWEVT_TYPE_EXT(LWEVT_TYPE_MY_EXT_1, struct { int a; int b; } a)
LWEVT_TYPE_EXT(LWEVT_TYPE_MY_EXT_2, struct { int a; int b; } b)The main header lwevt.h expands these macros to generate the lwevt_type_t enum and the lwevt_t structure, which contains a type field and a union msg that holds the event data.
Generated Event Type Enum
typedef enum {
#define LWEVT_TYPE_BASIC(name) name,
#define LWEVT_TYPE_EXT(name, data) name,
#include "lwevt/lwevt_type.h"
LWEVT_TYPE_LAST
} lwevt_type_t;Event Structure
typedef struct {
lwevt_type_t type; /* Event type */
union {
#define LWEVT_TYPE_EXT(name, data) data;
#include "lwevt/lwevt_type.h"
const unsigned int dummy; /* Fallback */
} msg; /* Payload */
} lwevt_t;Repository: https://github.com/MaJerle/lwevt
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
