Class Utilities
Provides various utility methods to retrieve hardware and software versions, time, shell commands, and bitwise masking.
Inherited Members
Namespace: Unosquare.PiGpio.NativeMethods
Syntax
public static class Utilities
Methods
ApplyBits(Int32, Boolean, Int32[])
Applies bit values according to the indexes.
Declaration
public static int ApplyBits(this int flags, bool value, params int[] indexes)
Parameters
Type | Name | Description |
---|---|---|
Int32 | flags | The flags. |
Boolean | value | True to set the 1 bits. False to clear the 1 bits. |
Int32[] | indexes | The indexes. |
Returns
Type | Description |
---|---|
Int32 | The applied bitmask. |
GetBit(Byte, Int32)
Gets a bit at the given position index from right to left.
Declaration
public static bool GetBit(this byte flags, int index)
Parameters
Type | Name | Description |
---|---|---|
Byte | flags | The flags. |
Int32 | index | The index. |
Returns
Type | Description |
---|---|
Boolean | The value of the bit at the given position index. |
GetBit(Int16, Int32)
Gets a bit at the given position index from right to left.
Declaration
public static bool GetBit(this short flags, int index)
Parameters
Type | Name | Description |
---|---|---|
Int16 | flags | The flags. |
Int32 | index | The index. |
Returns
Type | Description |
---|---|
Boolean | The value of the bit at the given position index. |
GetBit(Int32, Int32)
Gets a bit at the given position index from right to left.
Declaration
public static bool GetBit(this int flags, int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | flags | The flags. |
Int32 | index | The index. |
Returns
Type | Description |
---|---|
Boolean | The value of the bit at the given position index. |
GetBit(UInt16, Int32)
Gets a bit at the given position index from right to left.
Declaration
public static bool GetBit(this ushort flags, int index)
Parameters
Type | Name | Description |
---|---|---|
UInt16 | flags | The flags. |
Int32 | index | The index. |
Returns
Type | Description |
---|---|
Boolean | The value of the bit at the given position index. |
GetBit(UInt32, Int32)
Gets a bit at the given position index from right to left.
Declaration
public static bool GetBit(this uint flags, int index)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | flags | The flags. |
Int32 | index | The index. |
Returns
Type | Description |
---|---|
Boolean | The value of the bit at the given position index. |
GetBitInBytes(Int32, Byte[], Int32)
Gets the bit value at the bit position.
Declaration
public static int GetBitInBytes(int bitPos, byte[] buf, int numBits)
Parameters
Type | Name | Description |
---|---|---|
Int32 | bitPos | bit index from the start of buf. |
Byte[] | buf | array of bits. |
Int32 | numBits | number of valid bits in buf. |
Returns
Type | Description |
---|---|
Int32 | Returns the value of the bit bitPos bits from the start of buf. Returns 0 if bitPos is greater than or equal to numBits. |
GpioHardwareRevision()
If the hardware revision can not be found or is not a valid hexadecimal number the function returns 0.
The hardware revision is the last few characters on the Revision line of /proc/cpuinfo.
The revision number can be used to determine the assignment of GPIO to pins (see IO).
There are at least three types of board.
Type 1 boards have hardware revision numbers of 2 and 3.
Type 2 boards have hardware revision numbers of 4, 5, 6, and 15.
Type 3 boards have hardware revision numbers of 16 or greater.
for "Revision : 0002" the function returns 2. for "Revision : 000f" the function returns 15. for "Revision : 000g" the function returns 0.
Declaration
public static uint GpioHardwareRevision()
Returns
Type | Description |
---|---|
UInt32 | Returns the hardware revision. |
GpioSetSignalFunc(UInt32, PiGpioSignalDelegate)
Registers a function to be called (a callback) when a signal occurs.
The function is passed the signal number.
One function may be registered per signal.
The callback may be cancelled by passing NULL.
By default all signals are treated as fatal and cause the library to call gpioTerminate and then exit.
Declaration
public static ResultCode GpioSetSignalFunc(uint signalNumber, PiGpioSignalDelegate f)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | signalNumber | 0-63. |
PiGpioSignalDelegate | f | the callback function. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_signalNumber. |
GpioSetSignalFuncEx(UInt32, PiGpioSignalExDelegate, UIntPtr)
Registers a function to be called (a callback) when a signal occurs.
The function is passed the signal number and the userData pointer.
Only one of gpioSetSignalFunc or gpioSetSignalFuncEx can be registered per signal.
See gpioSetSignalFunc for further details.
Declaration
public static ResultCode GpioSetSignalFuncEx(uint signalNumber, PiGpioSignalExDelegate callback, UIntPtr userData)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | signalNumber | 0-63. |
PiGpioSignalExDelegate | callback | the callback function. |
UIntPtr | userData | a pointer to arbitrary user data. |
Returns
Type | Description |
---|---|
ResultCode | Returns 0 if OK, otherwise PI_BAD_signalNumber. |
GpioTick()
Tick is the number of microseconds since system boot.
As tick is an unsigned 32 bit quantity it wraps around after 2^32 microseconds, which is approximately 1 hour 12 minutes.
You don't need to worry about the wrap around as long as you take a tick (uint) from another tick, i.e. the following code will always provide the correct difference.
Declaration
public static uint GpioTick()
Returns
Type | Description |
---|---|
UInt32 | Returns the current system tick. |
Examples
uint startTick, endTick;
int diffTick;
startTick = gpioTick();
// do some processing
endTick = gpioTick();
diffTick = endTick - startTick;
printf("some processing took %d microseconds", diffTick);
GpioTime(TimeType, out Int32, out Int32)
Retrieves the seconds and micros variables with the current time.
If timetype is PI_TIME_ABSOLUTE updates seconds and micros with the number of seconds and microseconds since the epoch (1st January 1970).
If timetype is PI_TIME_RELATIVE updates seconds and micros with the number of seconds and microseconds since the library was initialised.
Declaration
public static int GpioTime(TimeType timeType, out int seconds, out int microseconds)
Parameters
Type | Name | Description |
---|---|---|
TimeType | timeType | 0 (relative), 1 (absolute). |
Int32 | seconds | a pointer to an int to hold seconds. |
Int32 | microseconds | a pointer to an int to hold microseconds. |
Returns
Type | Description |
---|---|
Int32 | Returns 0 if OK, otherwise PI_BAD_TIMETYPE. |
Examples
int secs, mics;
// print the number of seconds since the library was started
gpioTime(PI_TIME_RELATIVE, &secs, &mics);
printf("library started %d.%03d seconds ago", secs, mics/1000);
GpioVersion()
Returns the pigpio version number.
Declaration
public static uint GpioVersion()
Returns
Type | Description |
---|---|
UInt32 | Returns the pigpio version. |
PutBitInBytes(Int32, Byte[], Int32)
Sets the bit bitPos bits from the start of buf to bit.
Declaration
public static void PutBitInBytes(int bitPos, byte[] buf, int bit)
Parameters
Type | Name | Description |
---|---|---|
Int32 | bitPos | bit index from the start of buf. |
Byte[] | buf | array of bits. |
Int32 | bit | 0-1, value to set. |
RaiseSignal(Int32)
Raises the given UNIX signal number (0 to 63).
Declaration
public static int RaiseSignal(int signalNumber)
Parameters
Type | Name | Description |
---|---|---|
Int32 | signalNumber | The UNIX signal number from 0 to 63. |
Returns
Type | Description |
---|---|
Int32 | 0 for success. |
SetBit(Byte, Int32, Boolean)
Sets a bit at the given position index from right to left.
Declaration
public static byte SetBit(this byte flags, int index, bool value)
Parameters
Type | Name | Description |
---|---|---|
Byte | flags | The flags. |
Int32 | index | The index. |
Boolean | value | if set to |
Returns
Type | Description |
---|---|
Byte | The flags with the bit set at the given position. |
SetBit(Int16, Int32, Boolean)
Sets a bit at the given position index from right to left.
Declaration
public static short SetBit(this short flags, int index, bool value)
Parameters
Type | Name | Description |
---|---|---|
Int16 | flags | The flags. |
Int32 | index | The index. |
Boolean | value | if set to |
Returns
Type | Description |
---|---|
Int16 | The flags with the bit set at the given position. |
SetBit(Int32, Int32, Boolean)
Sets a bit at the given position index from right to left.
Declaration
public static int SetBit(this int flags, int index, bool value)
Parameters
Type | Name | Description |
---|---|---|
Int32 | flags | The flags. |
Int32 | index | The index. |
Boolean | value | if set to |
Returns
Type | Description |
---|---|
Int32 | The flags with the bit set at the given position. |
SetBit(UInt16, Int32, Boolean)
Sets a bit at the given position index from right to left.
Declaration
public static ushort SetBit(this ushort flags, int index, bool value)
Parameters
Type | Name | Description |
---|---|---|
UInt16 | flags | The flags. |
Int32 | index | The index. |
Boolean | value | if set to |
Returns
Type | Description |
---|---|
UInt16 | The flags with the bit set at the given position. |
SetBit(UInt32, Int32, Boolean)
Sets a bit at the given position index from right to left.
Declaration
public static uint SetBit(this uint flags, int index, bool value)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | flags | The flags. |
Int32 | index | The index. |
Boolean | value | if set to |
Returns
Type | Description |
---|---|
UInt32 | The flags with the bit set at the given position. |
Shell(String, String)
This function uses the system call to execute a shell script with the given string as its parameter.
The exit status of the system call is returned if OK, otherwise PI_BAD_SHELL_STATUS.
scriptName must exist in /opt/pigpio/cgi and must be executable.
The returned exit status is normally 256 times that set by the shell script exit function. If the script can't be found 32512 will be returned.
The following table gives some example returned statuses.
Script exit status @ Returned system call status 1 @ 256 5 @ 1280 10 @ 2560 200 @ 51200 script not found @ 32512.
Declaration
public static ResultCode Shell(string scriptName, string scriptString)
Parameters
Type | Name | Description |
---|---|---|
String | scriptName | the name of the script, only alphanumeric characters,. |
String | scriptString | the string to pass to the script. |
Returns
Type | Description |
---|---|
ResultCode | The result code. 0 for success. See the ResultCode enumeration. |
Remarks
'-' and '_' are allowed in the name.
Examples
// pass two parameters, hello and world
status = shell("scr1", "hello world");
// pass three parameters, hello, string with spaces, and world
status = shell("scr1", "hello 'string with spaces' world");
// pass one parameter, hello string with spaces world
status = shell("scr1", "\"hello string with spaces world\"");
TimeTime()
Return the current time in seconds since the Epoch.
Declaration
public static double TimeTime()
Returns
Type | Description |
---|---|
Double | The result code. 0 for success. See the ResultCode enumeration. |