8.3. Event callback mechanism
Note: requires kernel version no less than: v1.8.7
8.3.2. Headers
8.3.3. Data types
The event callback mechanism relies on the PikaEventListener
event listener, which records the ID of each registered event. When a signal is sent to the event listener, the event listener will call the corresponding Python callback function based on the event ID, and pass the semaphore.
The core of the event model is the PikaEventListener
event listener.
The PikaEventListener
model is shown above. After registering an event to the event listener, an event item Event Item
will be recorded inside the PikaEventListener
, including.
Event ID
the unique ID of the eventEvent Handler Object
event object, which records all the information about the event itemEvent CallBack
event callback function ( Python function )
When the Event Signal
event signal arrives, the event listener will match the Event ID
to find the corresponding event item, then pass the signal code Event Code
to Event CallBak
to trigger the callback function.
8.3.5. Event callback mechanism flow
Initialize the event listener
register callback functions in Python
the callback function registered in Python is executed
8.3.6. Support event callbacks via PikaStdDevice
Inheriting PikaStdDevice is the easiest way to support event callbacks, the PikaStdDevice.BaseDev
device base class already supports the event registration method addEventCallBack
.
class BaseDev:
def addEventCallBack(self, eventCallback: any): ...
# need override
def platformGetEventId(self): ...
- The device classes in
PikaStdDevice
(e.g. GPIO) all inherit fromBaseDev
, so they all get theaddEventCallBack
method and can register callbacks.
/package/PikaStdDevice/PikaStdDevice.pyi
After the platform driver inherits from PikaStdDevice.GPIO
, it also gets the addEventCallBack
method.
# TemplateDevice.pyi
class GPIO(PikaStdDevice.GPIO):
# overrid
...
...
Just override the platformGetEventId
platform method to be able to support registration callbacks.
For example.
/package/TemplateDevice/TemplateDevice_GPIO.c
const uint32_t GPIO_PA8_EVENT_ID = 0x08;
char* pin = obj_getStr(self, "pin");
if (strEqu(pin, "PA8")) {
obj_setInt(self, "eventId", GPIO_PA8_EVENT_ID);
}
}
- Define a callback function
callBack1
that takes an input parametersignal
,signal
can receive the incoming signal number.
/examples/TemplateDevice/gpio_cb.py
8.3.8. Signal triggering
Send a signal to PikaEventListener
when an event callback needs to be triggered.
Example: /port/linux/test/event-test.cpp
Get the event listener provided by
PikaStdDevice
viaextern PikaEventListener* g_pika_device_event_listener
.
extern PikaEventListener* g_pika_device_event_listener;
#define EVENT_SIGAL_IO_RISING_EDGE 0x01
#define EVENT_SIGAL_IO_FALLING_EDGE 0x02
#define GPIO_PA8_EVENT_ID 0x08
TEST(event, gpio) {
/* init */
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
pikaVM_runFile(pikaMain, "... /... /examples/TemplateDevice/gpio_cb.py");
/* simulate run in the call back */
pks_eventLisener_sendSignal(g_pika_device_event_listener, GPIO_PA8_EVENT_ID,
EVENT_SIGAL_IO_RISING_EDGE);
pks_eventLisener_sendSignal(g_pika_device_event_listener, GPIO_PA8_EVENT_ID,
EVENT_SIGAL_IO_FALLING_EDGE);
...
}
get rising edge!
get falling edg!
8.3.9. Advanced: Custom event registration functions
In addition to event callbacks supported by , you can also customize event registration functions, which is an advanced part.
Custom event registration requires a better understanding of PikaScript’s C-module mechanism and object mechanism.
Define a Python interface to a C module that receives incoming event callback functions.
For example.
/package/PikaStdDevice/PikaStdDevice.pyi
The type annotation for the event callback function is any
.
- Registering events in the C module implementation
Example:
PikaEventListener* g_pika_device_event_listener;
void PikaStdDevice_BaseDev_addEventCallBack(PikaObj* self, Arg* eventCallBack) {
obj_setArg(self, "eventCallBack", eventCallBack);
/* init event_listener for the first time */
if (NULL == g_pika_device_event_listener) {
pks_eventLisener_init(&g_pika_device_event_listener);
}
if (PIKA_RES_OK != obj_runNativeMethod(self, "platformGetEventId", NULL)) {
obj_setErrorCode(self, 1);
__platform_printf("Error: Method %s no found.\r\n",
"platformGetEventId");
}
uint32_t eventId = obj_getInt(self, "eventId");
pks_eventLicener_registEvent(g_pika_device_event_listener, eventId, self);
}
Create a global
PikaEventListener
:g_pika_device_event_listener
.Pass
self
asevent handler object
andevnetCallBack
intoself
.Get
evnetID
.This example gets the
eventID
by calling theplatformGetEventId()
platform function, which requiresBaseDev
inheritance, then rewritesplatformGetEventId()
and setsself. eventId
in the overriddenplatformGetEventId()
.For example: /package/TemplateDevice/TemplateDevice_GPIO.c