Class IO
Defines fundamental IO methods for the GPIO Pins. The bulk of the managed pin functionality is supplied by these methods.
Inherited Members
Namespace: Unosquare.PiGpio.NativeMethods
Syntax
public static class IO
Methods
GpioGetMode(SystemGpio)
Gest the current mode for the given GPIO.
Declaration
public static PinMode GpioGetMode(SystemGpio gpio)
Parameters
Type | Name | Description |
---|---|---|
SystemGpio | gpio | The gpio. |
Returns
Type | Description |
---|---|
PinMode | The port mode. |
GpioGetPad(GpioPadId)
This function returns the pad drive strength in mA.
Pad @ GPIO 0 @ 0-27 1 @ 28-45 2 @ 46-53.
Declaration
public static GpioPadStrength GpioGetPad(GpioPadId pad)
Parameters
Type | Name | Description |
---|---|---|
GpioPadId | pad | 0-2, the pad to get. |
Returns
Type | Description |
---|---|
GpioPadStrength | Returns the pad drive strength if OK, otherwise PI_BAD_PAD. |
Examples
strength = gpioGetPad(1); // get pad 1 strength
GpioGlitchFilter(UserGpio, UInt32)
Sets a glitch filter on a GPIO.
Level changes on the GPIO are not reported unless the level
has been stable for at least steadyMicroseconds
microseconds. The
level is then reported. Level changes of less than steadyMicroseconds
microseconds are ignored.
This filter affects the GPIO samples returned to callbacks set up with GpioSetAlertFunc(UserGpio, PiGpioAlertDelegate), GpioSetAlertFuncEx(UserGpio, PiGpioAlertExDelegate, UIntPtr), GpioSetGetSamplesFunc(PiGpioGetSamplesDelegate, BitMask), and GpioSetGetSamplesFuncEx(PiGpioGetSamplesExDelegate, BitMask, UIntPtr).
It does not affect interrupts set up with GpioSetIsrFunc(SystemGpio, EdgeDetection, Int32, PiGpioIsrDelegate), GpioSetIsrFuncEx(SystemGpio, EdgeDetection, Int32, PiGpioIsrExDelegate, UIntPtr), or levels read by GpioRead(SystemGpio), GpioReadBits00To31(), or GpioReadBits32To53().
Each (stable) edge will be timestamped steadyMicroseconds
microseconds
after it was first detected.
Declaration
public static ResultCode GpioGlitchFilter(UserGpio userGpio, uint steadyMicroseconds)
Parameters
Type | Name | Description |
---|---|---|
UserGpio | userGpio | 0-31. |
UInt32 | steadyMicroseconds | 0-300000. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER. |
GpioNoiseFilter(UserGpio, UInt32, UInt32)
Sets a noise filter on a GPIO.
Level changes on the GPIO are ignored until a level which has
been stable for steadyMicroseconds
microseconds is detected. Level changes
on the GPIO are then reported for activeMicroseconds
microseconds after
which the process repeats.
This filter affects the GPIO samples returned to callbacks set up with GpioSetAlertFunc(UserGpio, PiGpioAlertDelegate), GpioSetAlertFuncEx(UserGpio, PiGpioAlertExDelegate, UIntPtr), GpioSetGetSamplesFunc(PiGpioGetSamplesDelegate, BitMask), and GpioSetGetSamplesFuncEx(PiGpioGetSamplesExDelegate, BitMask, UIntPtr).
It does not affect interrupts set up with GpioSetIsrFunc(SystemGpio, EdgeDetection, Int32, PiGpioIsrDelegate), GpioSetIsrFuncEx(SystemGpio, EdgeDetection, Int32, PiGpioIsrExDelegate, UIntPtr), or levels read by GpioRead(SystemGpio), GpioReadBits00To31(), or GpioReadBits32To53().
Level changes before and after the active period may be reported. Your software must be designed to cope with such reports.
Declaration
public static ResultCode GpioNoiseFilter(UserGpio userGpio, uint steadyMicroseconds, uint activeMicroseconds)
Parameters
Type | Name | Description |
---|---|---|
UserGpio | userGpio | 0-31. |
UInt32 | steadyMicroseconds | 0-300000. |
UInt32 | activeMicroseconds | 0-1000000. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER. |
GpioRead(SystemGpio)
Reads the value of the GPIO.
Declaration
public static bool GpioRead(SystemGpio gpio)
Parameters
Type | Name | Description |
---|---|---|
SystemGpio | gpio | The gpio. |
Returns
Type | Description |
---|---|
Boolean | The digital value. |
GpioReadBits00To31()
Returns the current level of GPIO 0-31.
Declaration
public static uint GpioReadBits00To31()
Returns
Type | Description |
---|---|
UInt32 | The current level of GPIO 0-31. |
GpioReadBits32To53()
Returns the current level of GPIO 32-53.
Declaration
public static uint GpioReadBits32To53()
Returns
Type | Description |
---|---|
UInt32 | The current level of GPIO 32-53. |
GpioSetAlertFunc(UserGpio, PiGpioAlertDelegate)
Registers a function to be called (a callback) when the specified GPIO changes state.
One callback may be registered per GPIO.
The callback is passed the GPIO, the new level, and the tick.
The alert may be cancelled by passing NULL as the function.
The GPIO are sampled at a rate set when the library is started.
If a value isn't specifically set the default of 5 us is used.
The number of samples per second is given in the following table.
Level changes shorter than the sample rate may be missed.
The thread which calls the alert functions is triggered nominally 1000 times per second. The active alert functions will be called once per level change since the last time the thread was activated. i.e. The active alert functions will get all level changes but there will be a latency.
The tick value is the time stamp of the sample in microseconds, see GpioTick() for more details.
Declaration
public static ResultCode GpioSetAlertFunc(UserGpio userGpio, PiGpioAlertDelegate callback)
Parameters
Type | Name | Description |
---|---|---|
UserGpio | userGpio | 0-31. |
PiGpioAlertDelegate | callback | the callback function. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_USER_GPIO. |
Remarks
Parameter Value Meaning
GPIO 0-31 The GPIO which has changed state
level 0-2 0 = change to low (a falling edge) 1 = change to high (a rising edge) 2 = no level change (a watchdog timeout)
tick 32 bit The number of microseconds since boot WARNING: this wraps around from 4294967295 to 0 roughly every 72 minutes samples per sec
1 1,000,000
2 500,000
sample 4 250,000 rate 5 200,000 (us) 8 125,000 10 100,000.
Examples
void aFunction(int gpio, int level, uint tick)
{
printf("GPIO %d became %d at %d", gpio, level, tick);
}
// call aFunction whenever GPIO 4 changes state
gpioSetAlertFunc(4, aFunction);
GpioSetAlertFuncEx(UserGpio, PiGpioAlertExDelegate, UIntPtr)
Registers a function to be called (a callback) when the specified GPIO changes state.
One callback may be registered per GPIO.
The callback is passed the GPIO, the new level, the tick, and the userData pointer.
See GpioSetAlertFunc(UserGpio, PiGpioAlertDelegate) for further details.
Only one of GpioSetAlertFunc(UserGpio, PiGpioAlertDelegate) or GpioSetAlertFuncEx(UserGpio, PiGpioAlertExDelegate, UIntPtr) can be registered per GPIO.
Declaration
public static ResultCode GpioSetAlertFuncEx(UserGpio userGpio, PiGpioAlertExDelegate callback, UIntPtr userData)
Parameters
Type | Name | Description |
---|---|---|
UserGpio | userGpio | 0-31. |
PiGpioAlertExDelegate | callback | the callback function. |
UIntPtr | userData | pointer to arbitrary user data. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_USER_GPIO. |
Remarks
Parameter Value Meaning
GPIO 0-31 The GPIO which has changed state
level 0-2 0 = change to low (a falling edge) 1 = change to high (a rising edge) 2 = no level change (a watchdog timeout)
tick 32 bit The number of microseconds since boot WARNING: this wraps around from 4294967295 to 0 roughly every 72 minutes
userData pointer Pointer to an arbitrary object.
GpioSetGetSamplesFunc(PiGpioGetSamplesDelegate, BitMask)
Registers a function to be called (a callback) every millisecond with the latest GPIO samples.
The function is passed a pointer to the samples (an array of GpioSample), and the number of samples.
Only one function can be registered.
The callback may be cancelled by passing NULL as the function.
The samples returned will be the union of bits, plus any active alerts, plus any active notifications.
e.g. if there are alerts for GPIO 7, 8, and 9, notifications for GPIO 8, 10, 23, 24, and bits is (1<<23)|(1<<17) then samples for GPIO 7, 8, 9, 10, 17, 23, and 24 will be reported.
Declaration
public static ResultCode GpioSetGetSamplesFunc(PiGpioGetSamplesDelegate callback, BitMask bits)
Parameters
Type | Name | Description |
---|---|---|
PiGpioGetSamplesDelegate | callback | the function to call. |
BitMask | bits | the GPIO of interest. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK. |
GpioSetGetSamplesFuncEx(PiGpioGetSamplesExDelegate, BitMask, UIntPtr)
Registers a function to be called (a callback) every millisecond with the latest GPIO samples.
The function is passed a pointer to the samples (an array of GpioSample), the number of samples, and the userData pointer.
Only one of GpioSetGetSamplesFunc(PiGpioGetSamplesDelegate, BitMask) or GpioSetGetSamplesFuncEx(PiGpioGetSamplesExDelegate, BitMask, UIntPtr) can be registered.
See GpioSetGetSamplesFunc(PiGpioGetSamplesDelegate, BitMask) for further details.
Declaration
public static ResultCode GpioSetGetSamplesFuncEx(PiGpioGetSamplesExDelegate callback, BitMask bits, UIntPtr userData)
Parameters
Type | Name | Description |
---|---|---|
PiGpioGetSamplesExDelegate | callback | the function to call. |
BitMask | bits | the GPIO of interest. |
UIntPtr | userData | a pointer to arbitrary user data. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK. |
GpioSetIsrFunc(SystemGpio, EdgeDetection, Int32, PiGpioIsrDelegate)
Registers a function to be called (a callback) whenever the specified GPIO interrupt occurs.
One function may be registered per GPIO.
The function is passed the GPIO, the current level, and the current tick. The level will be PI_TIMEOUT if the optional interrupt timeout expires.
The underlying Linux sysfs GPIO interface is used to provide the interrupt services.
The first time the function is called, with a non-NULL f, the GPIO is exported, set to be an input, and set to interrupt on the given edge and timeout.
Subsequent calls, with a non-NULL f, can vary one or more of the edge, timeout, or function.
The ISR may be cancelled by passing a NULL f, in which case the GPIO is unexported.
The tick is that read at the time the process was informed of the interrupt. This will be a variable number of microseconds after the interrupt occurred. Typically the latency will be of the order of 50 microseconds. The latency is not guaranteed and will vary with system load.
The level is that read at the time the process was informed of the interrupt, or PI_TIMEOUT if the optional interrupt timeout expired. It may not be the same as the expected edge as interrupts happening in rapid succession may be missed by the kernel (i.e. this mechanism can not be used to capture several interrupts only a few microseconds apart).
Declaration
public static ResultCode GpioSetIsrFunc(SystemGpio gpio, EdgeDetection edge, int timeout, PiGpioIsrDelegate callback)
Parameters
Type | Name | Description |
---|---|---|
SystemGpio | gpio | 0-53. |
EdgeDetection | edge | RISING_EDGE, FALLING_EDGE, or EITHER_EDGE. |
Int32 | timeout | interrupt timeout in milliseconds (<=0 to cancel). |
PiGpioIsrDelegate | callback | the callback function. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_GPIO, PI_BAD_EDGE, or PI_BAD_ISR_INIT. |
Remarks
Parameter Value Meaning
GPIO 0-53 The GPIO which has changed state
level 0-2 0 = change to low (a falling edge) 1 = change to high (a rising edge) 2 = no level change (interrupt timeout)
tick 32 bit The number of microseconds since boot WARNING: this wraps around from 4294967295 to 0 roughly every 72 minutes.
GpioSetIsrFuncEx(SystemGpio, EdgeDetection, Int32, PiGpioIsrExDelegate, UIntPtr)
Registers a function to be called (a callback) whenever the specified GPIO interrupt occurs.
The function is passed the GPIO, the current level, the current tick, and the userData pointer.
Only one of GpioSetIsrFunc(SystemGpio, EdgeDetection, Int32, PiGpioIsrDelegate) or GpioSetIsrFuncEx(SystemGpio, EdgeDetection, Int32, PiGpioIsrExDelegate, UIntPtr) can be registered per GPIO.
See GpioSetIsrFunc(SystemGpio, EdgeDetection, Int32, PiGpioIsrDelegate) for further details.
Declaration
public static ResultCode GpioSetIsrFuncEx(SystemGpio gpio, EdgeDetection edge, int timeout, PiGpioIsrExDelegate callback, UIntPtr userData)
Parameters
Type | Name | Description |
---|---|---|
SystemGpio | gpio | 0-53. |
EdgeDetection | edge | RISING_EDGE, FALLING_EDGE, or EITHER_EDGE. |
Int32 | timeout | interrupt timeout in milliseconds (<=0 to cancel). |
PiGpioIsrExDelegate | callback | the callback function. |
UIntPtr | userData | pointer to arbitrary user data. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_GPIO, PI_BAD_EDGE, or PI_BAD_ISR_INIT. |
Remarks
Parameter Value Meaning
GPIO 0-53 The GPIO which has changed state
level 0-2 0 = change to low (a falling edge) 1 = change to high (a rising edge) 2 = no level change (interrupt timeout)
tick 32 bit The number of microseconds since boot WARNING: this wraps around from 4294967295 to 0 roughly every 72 minutes
userData pointer Pointer to an arbitrary object.
GpioSetMode(SystemGpio, PinMode)
Sets the GPIO mode, typically input or output.
Arduino style: pinMode.
See [[http://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf]] page 102 for an overview of the modes.
Declaration
public static ResultCode GpioSetMode(SystemGpio gpio, PinMode mode)
Parameters
Type | Name | Description |
---|---|---|
SystemGpio | gpio | 0-53. |
PinMode | mode | 0-7. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_GPIO or PI_BAD_MODE. |
Examples
gpioSetMode(17, PI_INPUT); // Set GPIO17 as input.
gpioSetMode(18, PI_OUTPUT); // Set GPIO18 as output.
gpioSetMode(22,PI_ALT0); // Set GPIO22 to alternative mode 0.
GpioSetPad(GpioPadId, GpioPadStrength)
This function sets the pad drive strength in mA.
Pad @ GPIO 0 @ 0-27 1 @ 28-45 2 @ 46-53.
Declaration
public static ResultCode GpioSetPad(GpioPadId pad, GpioPadStrength padStrength)
Parameters
Type | Name | Description |
---|---|---|
GpioPadId | pad | 0-2, the pad to set. |
GpioPadStrength | padStrength | 1-16 mA. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_PAD, or PI_BAD_STRENGTH. |
Examples
gpioSetPad(0, 16); // set pad 0 strength to 16 mA
GpioSetPullUpDown(SystemGpio, GpioPullMode)
Sets or clears resistor pull ups or downs on the GPIO.
Declaration
public static ResultCode GpioSetPullUpDown(SystemGpio gpio, GpioPullMode pullMode)
Parameters
Type | Name | Description |
---|---|---|
SystemGpio | gpio | 0-53. |
GpioPullMode | pullMode | 0-2. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_GPIO or PI_BAD_PUD. |
Examples
gpioSetPullUpDown(17, PI_PUD_UP); // Sets a pull-up.
gpioSetPullUpDown(18, PI_PUD_DOWN); // Sets a pull-down.
gpioSetPullUpDown(23, PI_PUD_OFF); // Clear any pull-ups/downs.
GpioSetWatchdog(UserGpio, UInt32)
Sets a watchdog for a GPIO.
The watchdog is nominally in milliseconds.
One watchdog may be registered per GPIO.
The watchdog may be cancelled by setting timeout to 0.
Until cancelled a timeout will be reported every timeout milliseconds after the last GPIO activity.
In particular:
1) any registered alert function for the GPIO will be called with the level set to PI_TIMEOUT.
2) any notification for the GPIO will have a report written to the fifo with the flags set to indicate a watchdog timeout.
Declaration
public static ResultCode GpioSetWatchdog(UserGpio userGpio, uint timeoutMilliseconds)
Parameters
Type | Name | Description |
---|---|---|
UserGpio | userGpio | 0-31. |
UInt32 | timeoutMilliseconds | 0-60000. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_USER_GPIO or PI_BAD_WDOG_TIMEOUT. |
Examples
void aFunction(int gpio, int level, uint tick)
{
printf("GPIO %d became %d at %d", gpio, level, tick);
}
// call aFunction whenever GPIO 4 changes state
gpioSetAlertFunc(4, aFunction);
// or approximately every 5 millis
gpioSetWatchdog(4, 5);
GpioTrigger(UserGpio, UInt32, Boolean)
This function sends a trigger pulse to a GPIO. The GPIO is set to level for pulseLen microseconds and then reset to not level.
or PI_BAD_PULSELEN.
Declaration
public static ResultCode GpioTrigger(UserGpio userGpio, uint pulseLength, bool value)
Parameters
Type | Name | Description |
---|---|---|
UserGpio | userGpio | 0-31. |
UInt32 | pulseLength | 1-100. |
Boolean | value | 0,1. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_LEVEL, or PI_BAD_PULSELEN. |
GpioWrite(SystemGpio, Boolean)
Sets the GPIO level, on or off.
If PWM or servo pulses are active on the GPIO they are switched off.
Arduino style: digitalWrite.
Declaration
public static ResultCode GpioWrite(SystemGpio gpio, bool value)
Parameters
Type | Name | Description |
---|---|---|
SystemGpio | gpio | 0-53. |
Boolean | value | 0-1. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_GPIO or PI_BAD_LEVEL. |
Examples
gpioWrite(24, 1); // Set GPIO24 high.
GpioWriteBits00To31Clear(BitMask)
Clears GPIO 0-31 if the corresponding bit in bits is set.
Declaration
public static ResultCode GpioWriteBits00To31Clear(BitMask bits)
Parameters
Type | Name | Description |
---|---|---|
BitMask | bits | a bit mask of GPIO to clear. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK. |
Examples
// To clear (set to 0) GPIO 4, 7, and 15
gpioWrite_Bits_0_31_Clear( (1<<4) | (1<<7) | (1<<15) );
GpioWriteBits00To31Set(BitMask)
Sets GPIO 0-31 if the corresponding bit in bits is set.
Declaration
public static ResultCode GpioWriteBits00To31Set(BitMask bits)
Parameters
Type | Name | Description |
---|---|---|
BitMask | bits | a bit mask of GPIO to set. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK. |
GpioWriteBits32To53Clear(BitMask)
Clears GPIO 32-53 if the corresponding bit (0-31) in bits is set.
Declaration
public static ResultCode GpioWriteBits32To53Clear(BitMask bits)
Parameters
Type | Name | Description |
---|---|---|
BitMask | bits | a bit mask of GPIO to clear. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK. |
GpioWriteBits32To53Set(BitMask)
Sets GPIO 32-53 if the corresponding bit (0-21) in bits is set.
Declaration
public static ResultCode GpioWriteBits32To53Set(BitMask bits)
Parameters
Type | Name | Description |
---|---|---|
BitMask | bits | a bit mask of GPIO to set. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK. |
Examples
// To set (set to 1) GPIO 32, 40, and 53
gpioWrite_Bits_32_53_Set((1<<(32-32)) | (1<<(40-32)) | (1<<(53-32)));