Class Threads
PRovides threading and delay operations.
Inherited Members
Namespace: Unosquare.PiGpio.NativeMethods
Syntax
public static class Threads
Methods
GpioDelay(UInt32)
Delays for at least the number of microseconds specified by micros. Delays of 100 microseconds or less use busy waits.
Declaration
public static uint GpioDelay(uint micros)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | micros | the number of microseconds to sleep. |
Returns
Type | Description |
---|---|
UInt32 | Returns the actual length of the delay in microseconds. |
GpioSetTimerFunc(TimerId, UInt32, PiGpioTimerDelegate)
Registers a function to be called (a callback) every millis milliseconds.
10 timers are supported numbered 0 to 9.
One function may be registered per timer.
The timer may be cancelled by passing NULL as the function.
Declaration
public static ResultCode GpioSetTimerFunc(TimerId timer, uint periodMilliseconds, PiGpioTimerDelegate callback)
Parameters
Type | Name | Description |
---|---|---|
TimerId | timer | 0-9. |
UInt32 | periodMilliseconds | 10-60000. |
PiGpioTimerDelegate | callback | the function to call. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_TIMER, PI_BAD_MS, or PI_TIMER_FAILED. |
Examples
void bFunction(void)
{
printf("two seconds have elapsed");
}
// call bFunction every 2000 milliseconds
gpioSetTimerFunc(0, 2000, bFunction);
GpioSetTimerFuncEx(TimerId, UInt32, PiGpioTimerExDelegate, UIntPtr)
Registers a function to be called (a callback) every millis milliseconds.
The function is passed the userData pointer.
Only one of GpioSetTimerFunc(TimerId, UInt32, PiGpioTimerDelegate) or GpioSetTimerFuncEx(TimerId, UInt32, PiGpioTimerExDelegate, UIntPtr) can be registered per timer.
See GpioSetTimerFunc(TimerId, UInt32, PiGpioTimerDelegate) for further details.
Declaration
public static ResultCode GpioSetTimerFuncEx(TimerId timer, uint millisecondsTimeout, PiGpioTimerExDelegate callback, UIntPtr userData)
Parameters
Type | Name | Description |
---|---|---|
TimerId | timer | 0-9. |
UInt32 | millisecondsTimeout | 10-60000. |
PiGpioTimerExDelegate | callback | the function to call. |
UIntPtr | userData | a pointer to arbitrary user data. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_TIMER, PI_BAD_MS, or PI_TIMER_FAILED. |
GpioSleep(TimeType, Int32, Int32)
Sleeps for the number of seconds and microseconds specified by seconds and micros.
If timetype is PI_TIME_ABSOLUTE the sleep ends when the number of seconds and microseconds since the epoch (1st January 1970) has elapsed. System clock changes are taken into account.
If timetype is PI_TIME_RELATIVE the sleep is for the specified number of seconds and microseconds. System clock changes do not effect the sleep length.
For short delays (say, 50 microseonds or less) use GpioDelay(UInt32).
Declaration
public static ResultCode GpioSleep(TimeType timeType, int seconds, int micros)
Parameters
Type | Name | Description |
---|---|---|
TimeType | timeType | 0 (relative), 1 (absolute). |
Int32 | seconds | seconds to sleep. |
Int32 | micros | microseconds to sleep. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_TIMETYPE, PI_BAD_SECONDS, or PI_BAD_MICROS. |
Examples
gpioSleep(PI_TIME_RELATIVE, 2, 500000); // sleep for 2.5 seconds
gpioSleep(PI_TIME_RELATIVE, 0, 100000); // sleep for 0.1 seconds
gpioSleep(PI_TIME_RELATIVE, 60, 0); // sleep for one minute
GpioStartThread(PiGpioThreadDelegate, UIntPtr)
Starts a new thread of execution with callback
as the main routine.
The function is passed the single argument arg.
The thread can be cancelled by passing the pointer to pthread_t to GpioStopThread(UIntPtr).
Declaration
public static UIntPtr GpioStartThread(PiGpioThreadDelegate callback, UIntPtr userData)
Parameters
Type | Name | Description |
---|---|---|
PiGpioThreadDelegate | callback | the main function for the new thread. |
UIntPtr | userData | a pointer to arbitrary user data. |
Returns
Type | Description |
---|---|
UIntPtr | Returns a pointer to pthread_t if OK, otherwise NULL. |
Examples
#include <stdio.h>
#include <pigpio.h>
void *myfunc(void *arg)
{
while (1)
{
printf("%s", arg);
sleep(1);
}
}
int main(int argc, char *argv[])
{
pthread_t *p1, *p2, *p3;
if (gpioInitialise() < 0) return 1;
p1 = gpioStartThread(myfunc, "thread 1"); sleep(3);
p2 = gpioStartThread(myfunc, "thread 2"); sleep(3);
p3 = gpioStartThread(myfunc, "thread 3"); sleep(3);
gpioStopThread(p3); sleep(3);
gpioStopThread(p2); sleep(3);
gpioStopThread(p1); sleep(3);
gpioTerminate();
}
GpioStopThread(UIntPtr)
Cancels the thread pointed at by threadHandle.
No value is returned.
The thread to be stopped should have been started with GpioStartThread(PiGpioThreadDelegate, UIntPtr).
Declaration
public static void GpioStopThread(UIntPtr handle)
Parameters
Type | Name | Description |
---|---|---|
UIntPtr | handle | a thread pointer returned by GpioStartThread(PiGpioThreadDelegate, UIntPtr). |
TimeSleep(Double)
Delay execution for a given number of seconds.
Declaration
public static void TimeSleep(double seconds)
Parameters
Type | Name | Description |
---|---|---|
Double | seconds | the number of seconds to sleep. |