Class Events
Provides event notification warapper APIs for the pigpio library. These methods represent an internal event system that is unused in the managed model of this library.
Inherited Members
Namespace: Unosquare.PiGpio.NativeMethods
Syntax
public static class Events
Methods
EventMonitor(UIntPtr, BitMask)
This function selects the events to be reported on a previously opened handle.
A report is sent each time an event is triggered providing the corresponding bit in bits is set.
See GpioNotifyBegin(UIntPtr, BitMask) for the notification format.
Declaration
public static ResultCode EventMonitor(UIntPtr handle, BitMask bitMask)
Parameters
Type | Name | Description |
---|---|---|
UIntPtr | handle |
|
BitMask | bitMask | a bit mask indicating the events of interest. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_HANDLE. |
Examples
// Start reporting events 3, 6, and 7.
// bit 76543210
// (0xC8 = 0b11001000)
eventMonitor(h, 0xC8);
EventSetFunc(UInt32, PiGpioEventDelegate)
Registers a function to be called (a callback) when the specified event occurs.
One function may be registered per event.
The function is passed the event, and the tick.
The callback may be cancelled by passing NULL as the function.
Declaration
public static ResultCode EventSetFunc(uint eventId, PiGpioEventDelegate callback)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | eventId | 0-31. |
PiGpioEventDelegate | callback | the callback function. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_EVENT_ID. |
EventSetFuncEx(UInt32, PiGpioEventExDelegate, UIntPtr)
Registers a function to be called (a callback) when the specified event occurs.
One function may be registered per event.
The function is passed the event, the tick, and the ueserdata pointer.
The callback may be cancelled by passing NULL as the function.
Only one of EventSetFunc(UInt32, PiGpioEventDelegate) EventSetFuncEx(UInt32, PiGpioEventExDelegate, UIntPtr) can be registered per event.
Declaration
public static ResultCode EventSetFuncEx(uint eventId, PiGpioEventExDelegate callback, UIntPtr userData)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | eventId | 0-31. |
PiGpioEventExDelegate | callback | the callback function. |
UIntPtr | userData | pointer to arbitrary user data. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_EVENT_ID. |
EventTrigger(UInt32)
This function signals the occurrence of an event.
An event is a signal used to inform one or more consumers to start an action. Each consumer which has registered an interest in the event (e.g. by calling EventSetFunc(UInt32, PiGpioEventDelegate)) will be informed by a callback.
One event, PI_EVENT_BSC (31) is predefined. This event is auto generated on BSC slave activity.
The meaning of other events is arbitrary.
Note that other than its id and its tick there is no data associated with an event.
Declaration
public static ResultCode EventTrigger(uint eventId)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | eventId | 0-31, the event. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_EVENT_ID. |
GpioNotifyBegin(UIntPtr, BitMask)
This function starts notifications on a previously opened handle.
The notification sends state changes for each GPIO whose corresponding bit in bits is set.
Each notification occupies 12 bytes in the fifo and has the following structure.
seqno: starts at 0 each time the handle is opened and then increments by one for each report.
flags: three flags are defined, PI_NTFY_FLAGS_WDOG, PI_NTFY_FLAGS_ALIVE, and PI_NTFY_FLAGS_EVENT.
If bit 5 is set (PI_NTFY_FLAGS_WDOG) then bits 0-4 of the flags indicate a GPIO which has had a watchdog timeout.
If bit 6 is set (PI_NTFY_FLAGS_ALIVE) this indicates a keep alive signal on the pipe/socket and is sent once a minute in the absence of other notification activity.
If bit 7 is set (PI_NTFY_FLAGS_EVENT) then bits 0-4 of the flags indicate an event which has been triggered.
tick: the number of microseconds since system boot. It wraps around after 1h12m.
level: indicates the level of each GPIO. If bit 1<<x is set then GPIO x is high.
Declaration
public static ResultCode GpioNotifyBegin(UIntPtr handle, BitMask bitMask)
Parameters
Type | Name | Description |
---|---|---|
UIntPtr | handle |
|
BitMask | bitMask | a bit mask indicating the GPIO of interest. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_HANDLE. |
Remarks
typedef struct { uint16_t seqno; uint16_t flags; uint tick; uint level; } gpioReport_t;.
Examples
// Start notifications for GPIO 1, 4, 6, 7, 10.
// 1
// 0 76 4 1
// (1234 = 0x04D2 = 0b0000010011010010)
gpioNotifyBegin(h, 1234);
GpioNotifyClose(UIntPtr)
This function stops notifications on a previously opened handle and releases the handle for reuse.
Declaration
public static ResultCode GpioNotifyClose(UIntPtr handle)
Parameters
Type | Name | Description |
---|---|---|
UIntPtr | handle |
|
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_HANDLE. |
Examples
gpioNotifyClose(h);
GpioNotifyOpen()
This function requests a free notification handle.
A notification is a method for being notified of GPIO state changes via a pipe or socket.
Pipe notifications for handle x will be available at the pipe named /dev/pigpiox (where x is the handle number). E.g. if the function returns 15 then the notifications must be read from /dev/pigpio15.
Socket notifications are returned to the socket which requested the handle.
Declaration
public static UIntPtr GpioNotifyOpen()
Returns
Type | Description |
---|---|
UIntPtr | Returns a handle greater than or equal to zero if OK, otherwise PI_NO_HANDLE. |
Examples
h = gpioNotifyOpen();
if (h >= 0)
{
sprintf(str, "/dev/pigpio%d", h);
fd = open(str, O_RDONLY);
if (fd >= 0)
{
// Okay.
}
else
{
// Error.
}
}
else
{
// Error.
}
GpioNotifyOpenWithSize(Int32)
This function requests a free notification handle.
It differs from GpioNotifyOpen() in that the pipe size may be specified, whereas GpioNotifyOpen() uses the default pipe size.
See GpioNotifyOpen() for further details.
Declaration
public static UIntPtr GpioNotifyOpenWithSize(int bufferSize)
Parameters
Type | Name | Description |
---|---|---|
Int32 | bufferSize | The pipe size of the the buffer. |
Returns
Type | Description |
---|---|
UIntPtr | The result code. 0 for success. See the ResultCode enumeration. |
GpioNotifyPause(UIntPtr)
This function pauses notifications on a previously opened handle.
Notifications for the handle are suspended until GpioNotifyBegin(UIntPtr, BitMask) is called again.
Declaration
public static ResultCode GpioNotifyPause(UIntPtr handle)
Parameters
Type | Name | Description |
---|---|---|
UIntPtr | handle |
|
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_HANDLE. |
Examples
gpioNotifyPause(h);