rgpio(3)
π¦ rgpio π
okay bestie, welcome to the rgpio(3) page from the lg archive β¨ manual section 3 β she's long, she's iconic, and we're doing her in SEVEN parts so buckle up π
πΈ NAME πΈ
rgpio β a C library to manipulate a remote SBC's GPIO. no cap, that's the whole job description π
β¨ SYNOPSIS β¨
#include <rgpio.h>
gcc -Wall -o prog prog.c -lrgpio
./prog
π DESCRIPTION π
rgpio is a C library which allows remote control of the GPIO and other functions of Linux SBCs running the rgpiod daemon.
bestie listen π β the rgpiod daemon must be running on the SBCs you wish to control. no daemon, no vibes.
π Features π
- reading and writing GPIO singly and in groups
- software timed PWM and waves
- GPIO callbacks
- pipe notification of GPIO alerts
- I2C wrapper
- SPI wrapper
- serial link wrapper
- simple file handling
- creating and running scripts on the
rgpioddaemon - a simple interface to start and stop new threads
π Usage π
Include <rgpio.h> in your source files.
Assuming your source is in prog.c use the following command to build
gcc -Wall -o prog prog.c -lrgpio
to run make sure the rgpiod daemon is running
rgpiod&
./prog
For examples see the lg archive file.
π§ Notes π§
All the functions which return an int return < 0 on error
π OVERVIEW π
π« ESSENTIAL π«
rgpiod_startβ Connects to argpioddaemonrgpiod_stopβ Disconnects from argpioddaemon
π FILES π
file_openβ Opens a filefile_closeβ Closes a filefile_readβ Reads bytes from a filefile_writeβ Writes bytes to a filefile_seekβ Seeks to a position within a filefile_listβ List files which match a pattern
π GPIO π
gpiochip_openβ Opens a gpiochip devicegpiochip_closeβ Closes a gpiochip devicegpio_get_chip_infoβ Gets gpiochip informationgpio_get_line_infoβ Gets gpiochip line informationgpio_get_modeβ Gets the mode of a GPIOgpio_claim_inputβ Claims a GPIO for inputgpio_claim_outputβ Claims a GPIO for outputgpio_claim_alertβ Claims a GPIO for alertsgpio_freeβ Frees a GPIOgroup_claim_inputβ Claims a group of GPIO for inputsgroup_claim_outputβ Claims a group of GPIO for outputsgroup_freeβ Frees a group of GPIOgpio_readβ Reads a GPIOgpio_writeβ Writes a GPIOgroup_readβ Reads a group of GPIOgroup_writeβ Writes a group of GPIOtx_pulseβ Starts pulses on a GPIOtx_pwmβ Starts PWM on a GPIOtx_servoβ Starts servo pulses on a GPIO.tx_waveβ Starts a wave on a group of GPIOtx_busyβ See if tx is active on a GPIO or grouptx_roomβ See if more room for tx on a GPIO or groupgpio_set_debounce_timeβ Sets the debounce time for a GPIOgpio_set_watchdog_timeβ Sets the watchdog time for a GPIOcallbackβ Starts a GPIO callbackcallback_cancelβ Stops a GPIO callback
π¦ I2C π¦
i2c_openβ Opens an I2C devicei2c_closeβ Closes an I2C devicei2c_write_quickβ smbus write quicki2c_read_byteβ smbus read bytei2c_write_byteβ smbus write bytei2c_read_byte_dataβ smbus read byte datai2c_write_byte_dataβ smbus write byte datai2c_read_word_dataβ smbus read word datai2c_write_word_dataβ smbus write word datai2c_read_block_dataβ smbus read block datai2c_write_block_dataβ smbus write block datai2c_read_i2c_block_dataβ smbus read I2C block datai2c_write_i2c_block_dataβ smbus write I2C block datai2c_read_deviceβ Reads the raw I2C devicei2c_write_deviceβ Writes the raw I2C devicei2c_process_callβ smbus process calli2c_block_process_callβ smbus block process calli2c_zipβ Performs multiple I2C transactions
π NOTIFICATIONS π
notify_openβ Request a notification handlenotify_closeβ Close a notificationnotify_pauseβ Pause notificationsnotify_resumeβ Start notifications for selected GPIO
π SCRIPTS π
script_storeβ Store a scriptscript_runβ Run a stored scriptscript_updateβ Set a scripts parametersscript_statusβ Get script status and parametersscript_stopβ Stop a running scriptscript_deleteβ Delete a stored script
πΈ SERIAL πΈ
serial_openβ Opens a serial deviceserial_closeβ Closes a serial deviceserial_read_byteβ Reads a byte from a serial deviceserial_write_byteβ Writes a byte to a serial deviceserial_readβ Reads bytes from a serial deviceserial_writeβ Writes bytes to a serial deviceserial_data_availableβ Returns number of bytes ready to be read
π» SHELL π»
shellβ Executes a shell command
β SPI β
spi_openβ Opens a SPI devicespi_closeβ Closes a SPI devicespi_readβ Reads bytes from a SPI devicespi_writeβ Writes bytes to a SPI devicespi_xferβ Transfers bytes with a SPI device
π§΅ THREADS π§΅
thread_startβ Start a new threadthread_stopβ Stop a previously started thread
π οΈ UTILITIES π οΈ
lgu_get_sbc_nameβ Get the SBC namelgu_get_internalβ Get a SBC configuration valuelgu_set_internalβ Set a SBC configuration valuelgu_timeβ Returns the number of seconds since the epochlgu_timestampβ Returns the number of nanoseconds since the epochlgu_sleepβ Sleeps for a number of secondslgu_set_userβ Set the user (and associated permissions)lgu_set_share_idβ Set the share id for a resourcelgu_use_share_idβ Use this share id when asking for a resourcelgu_rgpio_versionβ Get thergpiolibrary versionlgu_error_textβ Get the error text for an error code
π FUNCTIONS π
π«Ά rgpiod_start π«Ά
int rgpiod_start(const char *addrStr, const char *portStr)
Connect to the rgpiod daemon. Reserving command and notification streams.
addrStr: specifies the host or IP address of the SBC running thergpioddaemon. It may beNULLin which caselocalhostis used unless overridden by theLG_ADDRenvironment variable.portStr: specifies the port address used by the SBC running thergpioddaemon. It may beNULLin which case"8889"is used unless overridden by theLG_PORTenvironment variable.
If OK returns a sbc (>= 0).
On failure returns a negative error code.
This sbc value is passed to the other functions to specify the SBC to be used.
If the LG_USER environment variable exists that user will be "logged in" using lgu_set_user. This only has an effect if the rgpiod daemon is running with access control enabled.
π«Ά rgpiod_stop π«Ά
void rgpiod_stop(int sbc)
Terminates the connection to a rgpiod daemon and frees resources used by the library.
sbc:>= 0(as returned byrgpiod_start).
π file_open π
int file_open(int sbc, const char *file, int mode)
This function returns a handle to a file opened in a specified mode.
heads up bestie π β this is a privileged command. See permits.
sbc:>= 0(as returned byrgpiod_start).file: the file to open.mode: the file open mode.
If OK returns a handle (>= 0).
On failure returns a negative error code.
ποΈ File
A file may only be opened if permission is granted by an entry in the [files] section of the permits file. This is intended to allow remote access to files in a controlled manner.
π Mode
The mode may have the following values.
LG_FILE_READβ value1β open file for readingLG_FILE_WRITEβ value2β open file for writingLG_FILE_RWβ value3β open file for reading and writing
The following values may be or'd into the mode.
LG_FILE_APPENDβ value4β Writes append data to the end of the fileLG_FILE_CREATEβ value8β The file is created if it doesn't existLG_FILE_TRUNCβ value16β The file is truncated
Newly created files are owned by the user who launched the daemon with permissions owner read and write.
π Example π
#include <stdio.h>
#include <rgpio.h>
int main(int argc, char *argv[])
{
int sbc, handle, c;
char buf[60000];
sbc = rgpiod_start(NULL, NULL);
if (sbc < 0) return 1;
handle = file_open(sbc, "/ram/lg.c", LG_FILE_READ);
if (handle >= 0)
{
while ((c=file_read(sbc, handle, buf, sizeof(buf)-1)))
{
buf[c] = 0;
printf("%s", buf);
}
file_close(sbc, handle);
}
rgpiod_stop(sbc);
}
π file_close π
int file_close(int sbc, int handle)
This function closes the file.
sbc:>= 0(as returned byrgpiod_start).handle:>= 0(as returned byfile_open).
If OK returns 0.
On failure returns a negative error code.
π Example π
file_close(sbc, handle);
βοΈ file_write βοΈ
int file_write(int sbc, int handle, const char *buf, int count)
This function writes count bytes from buf to the the file.
sbc:>= 0(as returned byrgpiod_start).handle:>= 0(as returned byfile_open).buf: the array of bytes to write.count: the number of bytes to write.
If OK returns 0.
On failure returns a negative error code.
π Example π
if (file_write(sbc, handle, buf, 100) == 0)
{
// file written okay
}
else
{
// error
}
π file_read π
int file_read(int sbc, int handle, char *buf, int count)
This function reads up to count bytes from the the file.
sbc:>= 0(as returned byrgpiod_start).handle:>= 0(as returned byfile_open).buf: an array to receive the read data.count: the maximum number of bytes to read.
If OK returns the count of bytes read and updates buf.
On failure returns a negative error code.
π Example π
bytes = file_read(sbc, handle, buf, sizeof(buf));
if (bytes >= 0)
{
// process read data
}
π file_seek π
int file_seek(int sbc, int handle, int32_t seekOffset, int seekFrom)
This function seeks to a position within the file.
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned byfile_open).seekOffsetβ the number of bytes to move. Positive offsets move forward, negative offsets backwards.seekFromβ one ofLG_FROM_START(0),LG_FROM_CURRENT(1), orLG_FROM_END(2).
If OK returns the new file position. Slay. π
On failure returns a negative error code. Not slay. π
Example
file_seek(sbc, handle, 123, LG_FROM_START); // Start plus 123
size = file_seek(sbc, handle, 0, LG_FROM_END); // End, return size
pos = file_seek(sbc, handle, 0, LG_FROM_CURRENT); // Current position
β¨ file_list β¨
int file_list(int sbc, const char *fpat, char *buf, int count)
This function returns a list of files which match a pattern. Bestie, itβs giving directory energy. π¦
sbcβ >= 0 (as returned byrgpiod_start).fpatβ file pattern to match.bufβ an array to receive the matching file names.countβ the maximum number of bytes to read.
If OK returns the count of bytes read and updates buf with the matching filenames (the filenames are separated by newline characters). Newline-separated, no cap. β¨
On failure returns a negative error code.
Example
#include <stdio.h>
#include <rgpio.h>
int main(int argc, char *argv[])
{
int sbc, handle, c;
char buf[60000];
sbc = rgpiod_start(NULL, NULL);
if (sbc < 0) return 1;
c = file_list(sbc, "/ram/p*.c", buf, sizeof(buf));
if (c >= 0)
{
buf[c] = 0;
printf("%s", buf);
}
rgpiod_stop(sbc);
}
π₯ gpiochip_open π₯
int gpiochip_open(int sbc, int gpioDev)
This returns a handle to a gpiochip device. Main character energy for your GPIO chip. π
This is a privileged command. See permits. Yes, permits. The bouncer at the club. π
sbcβ >= 0 (as returned byrgpiod_start).gpioDevβ >= 0
If OK returns a handle (>= 0).
On failure returns a negative error code.
Example
h = gpiochip_open(sbc, 0); // open gpiochip0
if (h >= 0)
{
// open ok
}
else
{
// open error
}
π gpiochip_close π
int gpiochip_close(int sbc, int handle)
This closes a gpiochip device. Say bye bestie, respectfully. πΈ
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).
If OK returns 0.
On failure returns a negative error code.
Example
status = gpiochip_close(sbc, h); // close gpiochip
if (status < 0)
{
// close failed
}
π gpio_get_chip_info π
int gpio_get_chip_info(int sbc, int handle, lgChipInfo_p chipInfo)
This returns summary information of an opened gpiochip. Tea time, but for chip facts. π
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).chipInfoβ address to store returned chip info.
If OK returns a list of okay status, number of lines, name, and label. Okay status, lines, name, label β the whole vibe check. β¨
On failure returns a negative error code.
π¦ gpio_get_line_info π¦
int gpio_get_line_info(int sbc, int handle, int gpio, lgLineInfo_p lineInfo)
This returns detailed information of a GPIO of an opened gpiochip. Itβs giving receipts for one specific pin. π
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).gpioβ the GPIO.lineInfoβ address to store returned line info.
If OK returns a list of okay status, offset, line flags, name, and user. Okay status, offset, flags, name, user. Iconic. π
The meaning of the line flags bits are as given for the mode by gpio_get_mode. Go read that one next, bestie. πΈ
On failure returns a negative error code.
β gpio_get_mode β
int gpio_get_mode(int sbc, int handle, int gpio)
This returns the mode of a GPIO. What is that pin going through? Letβs find out. π«Ά
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).gpioβ the GPIO to be read.
If OK returns the mode of the GPIO.
On failure returns a negative error code.
- Bit
0β Value1β Meaning: Kernel: In use by the kernel - Bit
1β Value2β Meaning: Kernel: Output - Bit
2β Value4β Meaning: Kernel: Active low - Bit
3β Value8β Meaning: Kernel: Open drain - Bit
4β Value16β Meaning: Kernel: Open source - Bit
5β Value32β Meaning: Kernel: Pull up set - Bit
6β Value64β Meaning: Kernel: Pull down set - Bit
7β Value128β Meaning: Kernel: Pulls off set - Bit
8β Value256β Meaning: LG: Input - Bit
9β Value512β Meaning: LG: Output - Bit
10β Value1024β Meaning: LG: Alert - Bit
11β Value2048β Meaning: LG: Group - Bit
12β Value4096β Meaning: LG: --- - Bit
13β Value8192β Meaning: LG: --- - Bit
14β Value16384β Meaning: LG: --- - Bit
15β Value32768β Meaning: LG: --- - Bit
16β Value65536β Meaning: Kernel: Input - Bit
17β Value1<<17β Meaning: Kernel: Rising edge alert - Bit
18β Value1<<18β Meaning: Kernel: Falling edge alert - Bit
19β Value1<<19β Meaning: Kernel: Realtime clock alert
The LG bits are only set if the query was made by the process that owns the GPIO. Owner privileges, periodt. π
π gpio_claim_input π
int gpio_claim_input(int sbc, int handle, int lFlags, int gpio)
This claims a GPIO for input. Mine now. No takebacks. π
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).lFlagsβ line flags for the GPIO.gpioβ the GPIO to be claimed.
If OK returns 0.
On failure returns a negative error code.
The line flags may be used to set the GPIO as active low, open drain, open source, pull up, pull down, pull off. So many little settings, so little time. β¨
Example
status = gpio_claim_input(sbc, h, 0, 23); // open GPIO 23 for input
πΈ gpio_claim_output πΈ
int gpio_claim_output(int sbc, int handle, int lFlags, int gpio, int value)
This claims a GPIO for output. Itβs giving βI will send signals, actually.β π₯
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).lFlagsβ line flags for the GPIO.gpioβ the GPIO to be claimed.valueβ the initial value for the GPIO.
If OK returns 0.
On failure returns a negative error code.
The line flags may be used to set the GPIO as active low, open drain, open source, pull up, pull down, pull off. Same glow-up options as input. π
If value is zero the GPIO will be initialised low (0). If any other value is used the GPIO will be initialised high (1). Zero = low, anything else = high. Thatβs the rule, bestie. π
Example
status = gpio_claim_output(sbc, h, 0, 35, 1); // open GPIO 35 for high output
π gpio_free π
int gpio_free(int sbc, int handle, int gpio)
This frees a GPIO. Release it back into the wild. π¦
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).gpioβ the GPIO to be freed.
If OK returns 0.
On failure returns a negative error code.
The GPIO may now be claimed by another user or for a different purpose. Sharing is caring, I guess. π«Ά
π group_claim_input π
int group_claim_input(int sbc, int handle, int lFlags, int count, const int *gpios)
This claims a group of GPIO for inputs. Squad goals but for pins. β¨
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).lFlagsβ line flags for each GPIO.countβ the number of GPIO to claim.gpiosβ the group GPIO.
If OK returns 0.
On failure returns a negative error code.
The line flags may be used to set the group as active low, open drain, open source, pull up, pull down, pull off. Group settings, but make it fashion. π
gpios is an array of one or more GPIO. The first GPIO in the array is called the group leader and is used to reference the group as a whole. Group leader = the main character of the group. π
π¦ group_claim_output π¦
int group_claim_output(int sbc, int handle, int lFlags, int count, const int *gpios, const int *values)
This claims a group of GPIO to be used as outputs. Theyβre all getting claimed, bestie. π
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).lFlagsβ line flags for each GPIO.countβ the number of GPIO to claim.gpiosβ the group GPIO.valuesβ the initial value for each GPIO.
If OK returns 0.
On failure returns a negative error code.
The line flags may be used to set the group as active low, open drain, open source, pull up, pull down, pull off. Same flag energy as before. β¨
gpios is an array of one or more GPIO. The first GPIO in the array is called the group leader and is used to reference the group as a whole. Group leader again. They run this group chat. π
values is a list of initialisation values for the GPIO. If a value is zero the corresponding GPIO will be initialised low (0). If any other value is used the corresponding GPIO will be initialised high (1). Zero = low, not-zero = high. Every single time. π
β group_free β
int group_free(int sbc, int handle, int gpio)
This frees all the group GPIO. The whole group is being dismissed. π
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).gpioβ the group leader.
If OK returns 0.
On failure returns a negative error code.
The GPIO may now be claimed by another user or for a different purpose. Theyβre free agents now. π¦
π gpio_read π
int gpio_read(int sbc, int handle, int gpio)
This returns the level of a GPIO. High or low? Letβs find out. β¨
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).gpioβ the GPIO to be read.
If OK returns 0 (low) or 1 (high).
On failure returns a negative error code.
This command will work for any claimed GPIO (even if a member of a group). For an output GPIO the value returned will be that last written to the GPIO. Last written, not necessarily actual physical level. Important detail, bestie. π
π gpio_write π
int gpio_write(int sbc, int handle, int gpio, int value)
This sets the level of an output GPIO. Time to send a signal, periodt. π₯
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).gpioβ the GPIO to be written.valueβ the value to write.
If OK returns 0.
On failure returns a negative error code.
This command will work for any GPIO claimed as an output (even if a member of a group). Group member? Still works. No drama. π
If level is zero the GPIO will be set low (0). If any other value is used the GPIO will be set high (1). Zero low, anything else high. Same rule as always. π
πΈ group_read πΈ
int group_read(int sbc, int handle, int gpio, uint64_t *groupBits)
This returns the levels read from a group. Whole group, one read. Efficient queen. π
sbcβ >= 0 (as returned byrgpiod_start).handleβ >= 0 (as returned bygpiochip_open).gpioβ the offset of a member of the GPIO group to be read.groupBitsβ a pointer to a 64-bit memory area for the returned value.
If OK returns the group size and updates groupBits.
On failure returns a negative error code.
This command will work for an output group as well as an input group. For an output group the value returned will be that last written to the group GPIO. Output group? Still works. Last written tea. β¨
Note that this command will also work on an individual GPIO claimed as an input or output as that is treated as a group with one member. Solo pin = group of one. Main character energy. β
After a successful read groupBits is set as follows.
Bit 0 is the level of the group leader. Bit 1 is the level of the second GPIO in the group. Bit x is the level of GPIO x+1 of the group. Bit 0 = leader, bit 1 = second, and so on. Math but cute. π
π group_write π
int group_write(int sbc, int handle, int gpio, uint64_t groupBits, uint64_t groupMask)
This sets the levels of an output output group. Yes, βoutput output.β We said what we said. π
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bygpiochip_open).gpioβ the offset of a member of the GPIO group to be written.groupBitsβ the level to set if the corresponding bit ingroupMaskis set.groupMaskβ a mask indicating the group GPIO to be updated.
If it goes well it returns 0. π
If it does not, you get a negative error code back. π
So the tea on how the levels actually get set: each GPIO in the group takes its value from the bits of group_bits.
Bit 0 sets the level of the group leader. Bit 1 sets the level of the second GPIO in the group. Bit x sets the level of GPIO x+1 in the group. It's giving very orderly, very demure. β¨
But β plot twist β group_mask can override all of that. A GPIO is only updated if the corresponding bit in the mask is 1. That's it, that's the whole vibe check. π
tx_pulse π
int tx_pulse(int sbc, int handle, int gpio, int pulse_on, int pulse_off,
int pulse_offset, int pulse_cycles)
This starts software timed pulses on an output GPIO. Pulsing, bestie. π«Ά
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bygpiochip_open).gpioβ GPIO to be written.pulse_onβ pulse high time in microseconds.pulse_offβ pulse low time in microseconds.pulse_offsetβ offset from nominal pulse start position.pulse_cyclesβ the number of pulses to be sent,0for infinite.
If OK it returns the number of entries left in the PWM queue for the GPIO. β¨
On failure it returns a negative error code.
If bothpulse_onandpulse_offare zero, pulses will be switched off for that GPIO. The active pulse, if any, will be stopped and any queued pulses will be deleted. Full stop, no leftovers. π
Each successful call to this function consumes one PWM queue entry.
pulse_cycles cycles are transmitted (0 means infinite). Each cycle consists of pulse_on microseconds of GPIO high followed by pulse_off microseconds of GPIO low. That's the whole loop, she's simple and she's iconic. π
PWM is characterised by two values β its frequency (number of cycles per second) and its duty cycle (percentage of high time per cycle).
The set frequency will be 1000000 / (pulse_on + pulse_off) Hz.
The set duty cycle will be pulse_on / (pulse_on + pulse_off) * 100 %.
E.g. if pulse_on is 50 and pulse_off is 100 the frequency will be 6666.67 Hz and the duty cycle will be 33.33 %. Math girlies rise up. π₯
pulse_offset is a microsecond offset from the natural start of the pulse cycle.
For instance if the PWM frequency is 10 Hz the natural start of each cycle is at seconds 0, then 0.1, 0.2, 0.3 etc. In this case if the offset is 20000 microseconds the cycle will start at seconds 0.02, 0.12, 0.22, 0.32 etc. See how she shifts? Effortless. π¦
Another pulse command may be issued to the GPIO before the last has finished.
If the last pulse had infinite cycles then it will be replaced by the new settings at the end of the current cycle. Otherwise it will be replaced by the new settings when all its cycles are complete.
Multiple pulse settings may be queued in this way. Queueing is self care, fr fr. πΈ
tx_pwm β
int tx_pwm(int sbc, int handle, int gpio, float pwmFrequency, float
pwmDutyCycle, int pwmOffset, int pwmCycles)
This starts software timed PWM on an output GPIO.
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bygpiochip_open)gpioβ the GPIO to be pulsedpwmFrequencyβ PWM frequency in Hz (0=off,0.1-10000)pwmDutyCycleβ PWM duty cycle in % (0-100)pwmOffsetβ offset from nominal pulse start positionpwmCyclesβ the number of pulses to be sent,0for infinite
If OK it returns the number of entries left in the PWM queue for the GPIO.
On failure it returns a negative error code.
Each successful call to this function consumes one PWM queue entry. One entry per call, that's the toll. π
PWM is characterised by two values β its frequency (number of cycles per second) and its duty cycle (percentage of high time per cycle).
Another PWM command may be issued to the GPIO before the last has finished.
If the last pulse had infinite cycles then it will be replaced by the new settings at the end of the current cycle. Otherwise it will be replaced by the new settings when all its cycles are complete.
Multiple PWM settings may be queued in this way. π
tx_servo π§
int tx_servo(int sbc, int handle, int gpio, int pulseWidth, int
servoFrequency, int servoOffset, int servoCycles)
This starts software timed servo pulses on an output GPIO.
Lowkey important: I would only use software timed servo pulses for testing purposes. The timing jitter will cause the servo to fidget. This may cause it to overheat and wear out prematurely. Girl is gonna burn out, and not in the cute way. π
handleβ>= 0(as returned bygpiochip_open)gpioβ the GPIO to be pulsedpulseWidthβ pulse high time in microseconds (0=0ff,500-2500)servoFrequencyβ the number of pulses per second (40-500)servoOffsetβ offset from nominal pulse start positionservoCyclesβ the number of pulses to be sent,0for infinite
If OK it returns the number of entries left in the PWM queue for the GPIO. β¨
On failure it returns a negative error code.
Each successful call to this function consumes one PWM queue entry.
Another servo command may be issued to the GPIO before the last has finished.
If the last pulse had infinite cycles then it will be replaced by the new settings at the end of the current cycle. Otherwise it will be replaced by the new settings when all its cycles are complete.
Multiple servo settings may be queued in this way. Stack 'em up, bestie. π
tx_wave π
int tx_wave(int sbc, int handle, int gpio, int count, lgPulse_p pulses)
This starts a software timed wave on an output group. Wave, like the emoji. πβ¨
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bygpiochip_open).gpioβ group leader.countβ the number of pulses in the wave.pulsesβ the pulses.
If OK it returns the number of entries left in the wave queue for the group.
On failure it returns a negative error code.
Each successful call to this function consumes one wave queue entry.
This command starts a wave of pulses.
pulses is an array of pulses to be transmitted on the group.
Each pulse is defined by the following triplet:
bitsβ the levels to set for the selected GPIOmaskβ the GPIO to selectdelayβ the delay in microseconds before the next pulse
Another wave command may be issued to the group before the last has finished transmission. The new wave will start when the previous wave has completed. No cutting the line, she waits her turn. π
Multiple waves may be queued in this way.
tx_busy π
int tx_busy(int sbc, int handle, int gpio, int kind)
This returns true if transmissions of the specified kind are active on the GPIO or group. Is she busy or is she free? Let's find out. π₯
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bygpiochip_open).gpioβ the GPIO or group to be tested.kindβLG_TX_PWMorLG_TX_WAVE.
If OK it returns 1 for busy and 0 for not busy.
On failure it returns a negative error code.
tx_room π
int tx_room(int sbc, int handle, int gpio, int kind)
This returns the number of entries there are to queue further transmissions of the specified kind on a GPIO or GPIO group. Basically: how much room is left in the queue, bestie? π«Ά
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bygpiochip_open).gpioβ the GPIO or group to be tested.kindβLG_TX_PWMorLG_TX_WAVE.
If OK it returns the number of free entries (0 for none). Zero room means no room, periodt.
On failure it returns a negative error code.
gpio_set_debounce_time πΈ
int gpio_set_debounce_time(int sbc, int handle, int gpio, int
debounce_us)
This sets the debounce time for a GPIO.
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bygpiochip_open).gpioβ the GPIO to be configured.debounce_usβ the debounce time in microseconds.
If OK it returns 0.
On failure it returns a negative error code.
This only affects alerts.
An alert will only be issued if the edge has been stable for at least debounce microseconds.
Generally this is used to debounce mechanical switches (e.g. contact bounce). Shaky switches, we're calming you down. π§
Suppose that a square wave at 5 Hz is being generated on a GPIO. Each edge will last 100000 microseconds. If a debounce time of 100001 is set no alerts will be generated, If a debounce time of 99999 is set 10 alerts will be generated per second.
Note that level changes will be timestamped debounce microseconds after the actual level change. The timestamps are late to the party, and that's on purpose. π
gpio_set_watchdog_time πΆ
int gpio_set_watchdog_time(int sbc, int handle, int gpio, int
watchdog_us)
This sets the watchdog time for a GPIO.
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bygpiochip_open).gpioβ the GPIO to be configured.watchdog_usβ the watchdog time in microseconds.
If OK it returns 0.
On failure it returns a negative error code.
This only affects alerts.
A watchdog alert will be sent if no edge alert has been issued for that GPIO in the previous watchdog microseconds. It's giving "bestie has been quiet for too long, someone check on her". π
Note that only one watchdog alert will be sent per stream of edge alerts. The watchdog is reset by the sending of a new edge alert.
The level is set to LG_TIMEOUT (2) for a watchdog alert. β°
gpio_claim_alert π¨
int gpio_claim_alert(int sbc, int handle, int lFlags, int eFlags, int
gpio, int nfyHandle)
This claims a GPIO to be used as a source of alerts on level changes. Claiming her, she's taken, back off. π
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bygpiochip_open).gpioβ>= 0, as legal for the gpiochip.lFlagsβ line flags for the GPIO.eFlagsβ event flags for the GPIO.nfyHandleβ>=0, a notification handle (use-1for callbacks).
If OK it returns 0.
On failure it returns a negative error code.
The line flags may be used to set the GPIO as active low, open drain, open source, pull up, pull down, pull off.
The event flags are used to generate alerts for a rising edge, falling edge, or both edges.
Use a notification handle of -1 unless you plan to read the alerts from a notification pipe you have opened. You have been warned, bestie. π
callback π
int callback(int sbc, int handle, int gpio, int edge, CBFunc_t f, void
*userdata)
This function initialises a new callback. So every time the thing happens, your lil function gets called. Obsessed. β¨
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0,(as returned bygpiochip_open).gpioβ>= 0, as legal for the gpiochip.edgeβRISING_EDGE,FALLING_EDGE, orBOTH_EDGES.fβ the callback function.userdataβ a pointer to arbitrary user data.
If OK it returns a callback id.
On failure it returns a negative error code.
Okay so like, your user-supplied callback receives the chip, GPIO, edge, timestamp, and the userdata pointer whenever the GPIO has the identified edge. π
The reported level will be one of (vibe check):
0: change to low (a falling edge)1: change to high (a rising edge)2: no level change (a watchdog timeout)
The timestamp is when the change happened, reported as the number of nanoseconds since the epoch (start of 1970). β³
If you want to track the level of more than one GPIO, do so by maintaining the state in the callback. Do not use gpio_read. Remember the alert that triggered the callback may have happened several milliseconds before and the GPIO may have changed level many times since then. So like, don't trust it for current state, bestie. π No cap.
int callback_cancel(int callback_id)
This function cancels a callback identified by its id. π Periodt.
callback_id: >= 0 (as returned bycallback).
If OK returns 0.
On failure returns a negative error code.
int i2c_open(int sbc, int i2c_bus, int i2c_addr, int i2c_flags)
This returns a handle for the device at address i2c_addr on bus i2c_bus. π It's giving privileged.
This is a privileged command. See permits.
sbc: >= 0 (as returned byrgpiod_start).i2c_bus: >= 0.i2c_addr: 0-0x7F.i2c_flags: 0.
If OK returns a handle (>= 0).
On failure returns a negative error code.
No flags are currently defined. This parameter should be set to zero.
For the SMBus commands the low level transactions are shown at the end of the function description. The following abbreviations are used.
S (1 bit) : Start bit
P (1 bit) : Stop bit
Rd/Wr (1 bit) : Read/Write bit. Rd equals 1, Wr equals 0.
A, NA (1 bit) : Accept and not accept bit.
Addr (7 bits): I2C 7 bit address.
i2c_reg (8 bits): A byte which often selects a register.
Data (8 bits): A data byte.
Count (8 bits): A byte defining the length of a block operation.
[..]: Data sent by the device.
int i2c_close(int sbc, int handle)
This closes the I2C device. π
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).
If OK returns 0.
On failure returns a negative error code.
int i2c_write_quick(int sbc, int handle, int bitVal)
This sends a single bit (in the Rd/Wr bit) to the device. β‘
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).bitVal: 0-1, the value to write.
If OK returns 0.
On failure returns a negative error code.
Quick command. SMBus 2.0 5.5.1
S Addr bit [A] P
int i2c_write_byte(int sbc, int handle, int byteVal)
This sends a single byte to the device. π¦
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).byteVal: 0-0xFF, the value to write.
If OK returns 0.
On failure returns a negative error code.
Send byte. SMBus 2.0 5.5.2
S Addr Wr [A] byteVal [A] P
int i2c_read_byte(int sbc, int handle)
This reads a single byte from the device. π
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).
If OK returns the byte read (0-255).
On failure returns a negative error code.
Receive byte. SMBus 2.0 5.5.3
S Addr Rd [A] [Data] NA P
int i2c_write_byte_data(int sbc, int handle, int i2c_reg, int byteVal)
This writes a single byte to the specified register of the device. βοΈ
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).i2c_reg: 0-255, the register to write.byteVal: 0-0xFF, the value to write.
If OK returns 0.
On failure returns a negative error code.
Write byte. SMBus 2.0 5.5.4
S Addr Wr [A] i2c_reg [A] byteVal [A] P
int i2c_write_word_data(int sbc, int handle, int i2c_reg, int wordVal)
This writes a single 16 bit word to the specified register of the device. πΎ
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).i2c_reg: 0-255, the register to write.wordVal: 0-0xFFFF, the value to write.
If OK returns 0.
On failure returns a negative error code.
Write word. SMBus 2.0 5.5.4
S Addr Wr [A] i2c_reg [A] wval_Low [A] wVal_High [A] P
int i2c_read_byte_data(int sbc, int handle, int i2c_reg)
This reads a single byte from the specified register of the device. π
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).i2c_reg: 0-255, the register to read.
If OK returns the read byte (0-255).
On failure returns a negative error code.
Read byte. SMBus 2.0 5.5.5
S Addr Wr [A] i2c_reg [A] S Addr Rd [A] [Data] NA P
int i2c_read_word_data(int sbc, int handle, int i2c_reg)
This reads a single 16 bit word from the specified register of the device. π
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).i2c_reg: 0-255, the register to read.
If OK returns the read word (0-65535).
On failure returns a negative error code.
Read word. SMBus 2.0 5.5.5
S Addr Wr [A] i2c_reg [A]
S Addr Rd [A] [DataLow] A [DataHigh] NA P
int i2c_process_call(int sbc, int handle, int i2c_reg, int wordVal)
This writes 16 bits of data to the specified register of the device and reads 16 bits of data in return. π
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).i2c_reg: 0-255, the register to write/read.wordVal: 0-0xFFFF, the value to write.
If OK returns the read word (0-65535).
On failure returns a negative error code.
Process call. SMBus 2.0 5.5.6
S Addr Wr [A] i2c_reg [A] wVal_Low [A] wVal_High [A]
S Addr Rd [A] [DataLow] A [DataHigh] NA P
int i2c_write_block_data(int sbc, int handle, int i2c_reg, const char *buf, int count)
This writes up to 32 bytes to the specified register of the device. π¦
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).i2c_reg: 0-255, the register to write.buf: an array with the data to send.count: 1-32, the number of bytes to write.
If OK returns 0.
On failure returns a negative error code.
Block write. SMBus 2.0 5.5.7
S Addr Wr [A] i2c_reg [A] count [A] buf0 [A] buf1 [A] ...
[A] bufn [A] P
int i2c_read_block_data(int sbc, int handle, int i2c_reg, char *buf)
This reads a block of up to 32 bytes from the specified register of the device. π
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).i2c_reg: 0-255, the register to read.buf: an array to receive the read data.
If OK returns the count of bytes read and updates buf.
On failure returns a negative error code.
The amount of returned data is set by the device.
Block read. SMBus 2.0 5.5.7
S Addr Wr [A] i2c_reg [A]
S Addr Rd [A] [Count] A [buf0] A [buf1] A ... A [bufn] NA P
int i2c_block_process_call(int sbc, int handle, int i2c_reg, char *buf, int count)
This writes data bytes to the specified register of the device and reads a device specified number of bytes of data in return. π
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).i2c_reg: 0-255, the register to write/read.buf: an array with the data to send and to receive the read data.count: 1-32, the number of bytes to write.
If OK returns the count of bytes read and updates buf.
On failure returns a negative error code.
The smbus 2.0 documentation states that a minimum of 1 byte may be sent and a minimum of 1 byte may be received. The total number of bytes sent/received must be 32 or less.
Block write-block read. SMBus 2.0 5.5.8
S Addr Wr [A] i2c_reg [A] count [A] buf0 [A] ...
S Addr Rd [A] [Count] A [Data] ... A P
int i2c_read_i2c_block_data(int sbc, int handle, int i2c_reg, char *buf, int count)
This reads count bytes from the specified register of the device. The count may be 1-32. π
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).i2c_reg: 0-255, the register to read.buf: an array to receive the read data.count: 1-32, the number of bytes to read.
If OK returns the count of bytes read and updates buf.
On failure returns a negative error code.
S Addr Wr [A] i2c_reg [A]
S Addr Rd [A] [buf0] A [buf1] A ... A [bufn] NA P
int i2c_write_i2c_block_data(int sbc, int handle, int i2c_reg, const char *buf, int count)
This writes 1 to 32 bytes to the specified register of the device. βοΈ
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).i2c_reg: 0-255, the register to write.buf: the data to write.count: 1-32, the number of bytes to write.
If OK returns 0.
On failure returns a negative error code.
S Addr Wr [A] i2c_reg [A] buf0 [A] buf1 [A] ... [A] bufn [A] P
int i2c_read_device(int sbc, int handle, char *buf, int count)
This reads count bytes from the raw device into buf. π
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).buf: an array to receive the read data bytes.count: >0, the number of bytes to read.
If OK returns the count of bytes read and updates buf.
On failure returns a negative error code.
S Addr Rd [A] [buf0] A [buf1] A ... A [bufn] NA P
int i2c_write_device(int sbc, int handle, const char *buf, int count)
This writes count bytes from buf to the raw device. βοΈ
sbc: >= 0 (as returned byrgpiod_start).handle: >= 0 (as returned byi2c_open).buf: an array containing the data bytes to write.count: >0, the number of bytes to write.
If OK returns 0.
On failure returns a negative error code.
S Addr Wr [A] buf0 [A] buf1 [A] ... [A] bufn [A] P
int i2c_zip(int sbc, int handle, const char *inBuf, int inCount, char *outBuf, int outCount)
This function executes a sequence of I2C operations. The operations to be performed are specified by the contents of inBuf which contains the concatenated command codes and associated data. π€―
sbcβ>= 0, and it's whateverrgpiod_starthanded you back.handleβ>= 0, the one you got from callinglgI2cOpen.inBufβ pointer to all your I2C commands smooshed together (deets below bestie, keep reading).inCountβ how big that command buffer is.outBufβ pointer to the buffer where the returned data goes.outCountβ how big the output buffer is.
If everything's good, it returns the count of bytes read and updates outBuf for you. π«Ά
If it fails, you get a negative error code back. No vibes.
Here's the command codes it speaks β literally the whole language:
Endβ cmd & data0β no more commands, we're done here periodt.Escapeβ cmd & data1β the nextPis two bytes.Onβ cmd & data2β switch the combined flag on.Offβ cmd & data3β switch the combined flag off.Addressβ cmd & data4 Pβ set the I2C address toP.Flagsβ cmd & data5 lsb msbβ set the I2C flags tolsb + (msb << 8).Readβ cmd & data6 Pβ readPbytes of data.Writeβ cmd & data7 P ...β writePbytes of data.
The address, read, and write commands all take a parameter P. Normally P is one byte (0-255). But if the command gets preceded by the Escape command, then P is two bytes (0-65535, least significant byte first). π
The address defaults to whatever's associated with the handle. The flags default to 0. Both the address and the flags just hold onto their previous value until something updates them β they're loyal like that.
The returned I2C data gets stored in consecutive locations of outBuf. β¨
Example
Set address 0x53, write 0x32, read 6 bytes Set address 0x1E, write 0x03, read 6 bytes Set address 0x68, write 0x1B, read 8 bytes End
0x04 0x53 0x07 0x01 0x32 0x06 0x06
0x04 0x1E 0x07 0x01 0x03 0x06 0x06
0x04 0x68 0x07 0x01 0x1B 0x06 0x08
0x00
int notify_open(int sbc)
Grab yourself a free notification handle. π
This is a privileged command. See permits.
sbcβ>= 0(as returned byrgpiod_start).
If it works you get a handle (>= 0).
If it fails you get a negative error code.
So like, a notification is the whole method for getting notified about GPIO state changes via a pipe. π¦
Pipes are only accessible from the local machine, so this function is genuinely pointless if you're using the library from a remote machine. Use the in-built (socket) notifications that callback gives you instead, bestie. π
The notification pipes get created in the library working directory.
Notifications for handle x will be available at the pipe named .lgd-nfyx (where x is the handle number). So if the function returns 15, you read the notifications from .lgd-nfy15.
Each notification occupies 16 bytes in the fifo and has this exact structure:
typedef struct
{
uint64_t timestamp; // alert time in nanoseconds
uint8_t chip; // gpiochip device number
uint8_t gpio; // offset into gpio device
uint8_t level; // 0=low, 1=high, 2=timeout
uint8_t flags; // none currently defined
} lgGpioReport_t;
timestampβ the number of nanoseconds since the epoch (start of 1970).chipβ the gpiochip device number (NOT the handle, ok?).gpioβ the GPIO.levelβ tells you the level of the GPIO.flagsβ no flags are currently defined.
For future proofing, it's probably best to just ignore any notification with non-zero flags. π
int notify_resume(int sbc, int handle)
Resume notifications on a handle. We're back, babes. β¨
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bynotify_open).
If OK it returns 0.
On failure it returns a negative error code.
int notify_pause(int sbc, int handle)
Pauses notifications on a handle. Shhh, it's napping. π΄
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bynotify_open).
If OK it returns 0.
On failure it returns a negative error code.
Notifications for the handle are suspended until notify_resume gets called. So don't leave it hanging fr fr.
int notify_close(int sbc, int handle)
Stop notifications and release the handle. It's giving closure. π
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned bynotify_open).
If OK it returns 0.
On failure it returns a negative error code.
int script_store(int sbc, const char *script)
This one stores a script so you can execute it later. Save it for your future self, iconic behaviour honestly. π
This is a privileged command. See permits.
See scripts.html for details. π
sbcβ>= 0(as returned byrgpiod_start).scriptβ the text of the script.
If OK it returns a handle (>=0).
On failure it returns a negative error code.
int script_run(int sbc, int handle, int count, const uint32_t *param)
This function runs a stored script. Go bestie go. π₯
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byscript_store).countβ0-10, the number of parameters.paramβ an array of parameters.
If OK it returns 0.
On failure it returns a negative error code.
param is an array of up to 10 parameters which may be referenced in the script as p0 to p9.
int script_update(int sbc, int handle, int count, const uint32_t *param)
This sets the parameters of a script. The script may or may not be running β no judgement either way, we support all states here. π The first numPar parameters of the script get overwritten with the new values.
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byscript_store).countβ0-10, the number of parameters.paramβ an array of parameters.
If OK it returns 0.
On failure it returns a negative error code.
param is an array of up to 10 parameters which may be referenced in the script as p0 to p9.
int script_status(int sbc, int handle, uint32_t *param)
This returns the run status of a stored script plus the current values of parameters 0 to 9. It's giving full transparency. π¦
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byscript_store).paramβ an array to hold the returned 10 parameters.
If OK it returns the script status and updates param.
On failure it returns a negative error code.
The script status may be:
LG_SCRIPT_INITINGLG_SCRIPT_READYLG_SCRIPT_RUNNINGLG_SCRIPT_WAITINGLG_SCRIPT_ENDEDLG_SCRIPT_HALTEDLG_SCRIPT_FAILED
The current value of script parameters 0 to 9 are returned in param.
int script_stop(int sbc, int handle)
This function stops a running script. Sit down. β
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byscript_store).
If OK it returns 0.
On failure it returns a negative error code.
int script_delete(int sbc, int handle)
This function deletes a stored script. Gone, and honestly? Not missed. π
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byscript_store).
If OK it returns 0.
On failure it returns a negative error code.
int serial_open(int sbc, const char *ser_tty, int ser_baud, int ser_flags)
Opens a serial device at a specified baud rate with specified flags. The device has to be present in /dev, no cap. π
This is a privileged command. See permits.
sbcβ>= 0(as returned byrgpiod_start).ser_ttyβ the serial device to open.ser_baudβ the baud rate in bits per second, see below.ser_flagsβ0.
If OK it returns 0.
On failure it returns a negative error code.
The baud rate must be one of 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, or 230400. That's the whole list, bestie β don't get creative. π
No flags are currently defined. This parameter should be set to zero.
int serial_close(int sbc, int handle)
This function closes the serial device.
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byserial_open).
If OK it returns 0.
On failure it returns a negative error code.
int serial_write_byte(int sbc, int handle, int byteVal)
This function writes byteVal to the serial port.
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byserial_open).
If OK it returns 0.
On failure it returns a negative error code.
int serial_read_byte(int sbc, int handle)
This function reads a byte from the serial port.
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byserial_open).
If OK it returns the read byte (0-255).
On failure it returns a negative error code.
int serial_write(int sbc, int handle, const char *buf, int count)
This function writes count bytes from buf to the the serial port.
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byserial_open).bufβ the array of bytes to write.countβ the number of bytes to write.
If OK it returns 0.
On failure it returns a negative error code.
int serial_read(int sbc, int handle, char *buf, int count)
This function reads up to count bytes from the the serial port and writes them to buf.
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byserial_open).bufβ an array to receive the read data.countβ the maximum number of bytes to read.
If OK it returns the count of bytes read and updates buf.
On failure it returns a negative error code.
If no data is ready, zero is returned. (Not an error, just quiet. π€«)
int serial_data_available(int sbc, int handle)
Returns the number of bytes available to be read from the device.
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byserial_open).
If OK it returns the count of bytes available.
On failure it returns a negative error code.
int shell(int sbc, const char *scriptName, const char *scriptString)
This function uses the system call to execute a shell script, with the given string as its parameter. π
This is a privileged command. See permits.
sbcβ>= 0(as returned byrgpiod_start).scriptNameβ the name of the script; only alphanumeric characters,-and_are allowed in the name.scriptStringβ the string to pass to the script.
If OK it returns 0.
On failure it returns a negative error code.
scriptName must exist in a directory named cgi in the daemon's configuration directory, and it must be executable. No exceptions, the rules are the rules. π
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.
And here's a lil table of some example returned statuses π
- script exit status
1β returned system call status256 - script exit status
5β returned system call status1280 - script exit status
10β returned system call status2560 - script exit status
200β returned system call status51200 - script exit status
script not foundβ returned system call status32512
Lowkey the numbers go up a lil aggressively, that's just how it is bestie, multiply-by-256 energy. π
Example β look at how you pass args:
// pass two parameters, hello and world
status = shell_(sbc, "scr1", "hello world");
// pass three parameters, hello, string with spaces, and world
status = shell_(sbc, "scr1", "hello 'string with spaces' world");
// pass one parameter, hello string with spaces world
status = shell_(sbc, "scr1", "
π int spi_open(int sbc, int spi_device, int spi_channel, int spi_baud, int spi_flags)
Okay so this one hands you back a handle for the SPI device on the channel. Data gets transferred at spi_baud bits per second. The flags? They're your little levers for tweaking the default behaviour. ποΈ
Not me reminding you: this is a privileged command. See permits. π
sbcβ>= 0(as returned byrgpiod_start).spi_deviceβ>= 0spi_channelβ>= 0spi_baudβ SPI speed in bits per second.spi_flagsβ see below.
If it's OK it returns a handle (>= 0). π«Ά
On failure it returns a negative error code. π
So spi_flags is literally just the least significant 2 bits, bestie:
1 0
m m
and mm is what defines the SPI mode. Iconic bitfield behaviour, honestly.
Mode POL PHA
0 0 0
1 0 1
2 1 0
3 1 1
The other bits in flags should be set to zero. Periodt. β¨
π¦ int spi_close(int sbc, int handle)
This function closes the SPI device identified by the handle. Closing things is self-care fr fr. π
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byspi_open).
If OK it returns 0.
On failure it returns a negative error code.
β int spi_read(int sbc, int handle, char *buf, int count)
This function reads count bytes of data from the SPI device. Nom nom bytes. π
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byspi_open).bufβ an array to receive the read data bytes.countβ the number of bytes to read.
If OK it returns the count of bytes read and updates buf. π«Ά
On failure it returns a negative error code.
π int spi_write(int sbc, int handle, const char *buf, int count)
This function writes count bytes of data from buf to the SPI device. Sending it, slay. β¨
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byspi_open).bufβ the data bytes to write.countβ the number of bytes to write.
If OK it returns the count of bytes written.
On failure it returns a negative error code.
π₯ int spi_xfer(int sbc, int handle, const char *txBuf, char *rxBuf, int count)
This function transfers count bytes of data from txBuf to the SPI device. Simultaneously count bytes of data are read from the device and placed in rxBuf. It's giving full duplex main character energy. π
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0(as returned byspi_open).txBufβ the data bytes to write.rxBufβ the received data bytes.countβ the number of bytes to transfer.
If OK it returns the count of bytes transferred and updates rxBuf. πΈ
On failure it returns a negative error code.
π§ pthread_t *thread_start(lgThreadFunc_t thread_func, void *userdata)
Starts a whole new thread of execution with thread_func as the main routine. Baby thread, go live your life. π
thread_funcβ the main function for the new thread.userdataβ a pointer to an arbitrary argument.
If OK it returns a pointer to a pthread_t.
On failure it returns NULL. π
The function is passed the single argument userdata.
The thread can be cancelled by passing the pointer to pthread_t to thread_stop.
π void thread_stop(pthread_t *pth)
Cancels the thread pointed at by pth. Rip bozo, respectfully. π
pthβ the thread to be stopped.
No value is returned.
The thread to be stopped should have been started with thread_start.
π int lgu_get_internal(int sbc, int config_id, uint64_t *config_value)
Returns the value of a sbc configuration item. Peeking behind the curtain, iykyk. π
This is a privileged command. See permits. π
sbcβ>= 0(as returned byrgpiod_start).config_idβ the configuration item.config_valueβ pointer for returned value.
If OK it returns 0 and updates config_value.
On failure it returns a negative error code.
π
int lgu_set_internal(int sbc, int config_id, uint64_t config_value)
Sets the value of a sbc configuration item. Now you're the one moving the levers. ποΈ
This is a privileged command. See permits. π
sbcβ>= 0(as returned byrgpiod_start).config_idβ the configuration item.config_valueβ the value to set.
If OK it returns 0.
On failure it returns a negative error code.
π¦ int lgu_get_sbc_name(int sbc, char *buf, int count)
Return the lgd server name. Identity check, who are you bestie. π
sbcβ>= 0(as returned byrgpiod_start).bufβ the server name is copied to this buffer.countβ the maximum number of characters to copy.
If OK it returns the count of bytes copied and updates buf.
On failure it returns a negative error code.
β int lgu_set_user(int sbc, const char *user, const char *secretsFile)
Sets the rgpiod daemon user. The user then has the associated permissions. Glow-up via permissions, honestly. β¨
sbcβ>= 0(as returned byrgpiod_start).userβ the user to set (""defaults to the default user).secretsFileβ the path to the shared secret file (""defaults to"~/.lg_secret").
If OK it returns 1 if the user was set, 0 otherwise.
On failure it returns a negative error code.
Example:
if (lgu_set_user(sbc, "gpio", "")
{
printf("using user gpio permissions");
}
else
{
printf("using default permissions");
}
π int lgu_set_share_id(int sbc, int handle, int share_id)
Sets the share id of an owned object. Sharing is caring, fr fr. π«Ά
sbcβ>= 0(as returned byrgpiod_start).handleβ>= 0share_idβ>= 0,0stops sharing.
If OK it returns 0.
On failure it returns a negative error code.
Normally objects associated with a handle are only accessible to the program which created them (and are automatically deleted when the program ends).
If a non-zero share is set the object is accessible to any software which knows the share and the handle (and are not automatically deleted when the program ends).
Example:
lgu_set_share_id(sbc, handle, 23);
π int lgu_use_share_id(int sbc, int share_id)
Sets the share id to be used when asking to use an object owned by another creator. Borrowing from a friend, we love to see it. π
sbcβ>= 0(as returned byrgpiod_start).share_idβ>= 0,0stops sharing.
If OK it returns 0.
On failure it returns a negative error code.
Normally objects associated with a handle are only accessible to the program which created them (and are automatically deleted when the program ends).
If a non-zero share is set the object is accessible to any software which knows the share and the handle.
Example:
lgu_use_share_id(sbc, 23);
π uint32_t lgu_rgpio_version(void)
Return the rgpio version. Version check, because knowing is half the battle. π
If OK it returns the rgpio version.
On failure it returns a negative error code.
πΈ const char *lgu_error_text(int errnum)
Return a text description for an error code. Translating the drama into words. π¬
errnumβ the error code.
π void lgu_sleep(double sleepSecs)
Delay execution for a given number of seconds. Nap time, deserved. π΄
sleepSecsβ the number of seconds to delay.
π§ double lgu_time(void)
Return the current time in seconds since the Epoch.
β uint64_t lgu_timestamp(void)
Return the current time in nanoseconds since the Epoch. Nanoseconds, because we're precise like that. π
β¨ PARAMETERS β¨
*addrStrβ A string specifying the host or IP address of the SBC running thergpioddaemon. It may beNULLin which case localhost is used unless overridden by theLG_ADDRenvironment variable.bitValβ A value of0or1.*bufβ A buffer to hold data being sent or being received.byteVal: 0-255 β An 8-bit byte value.callback_idβ A value>= 0, as returned by a call to the callback. The id is passed tocallback_cancelto cancel the callback.CBFunc_tβ
typedef void (*CBFunc_t)
(int sbc, int chip, int gpio, int level, uint64_t timestamp, void * userdata);
charβ A single character, an 8 bit quantity able to store0-255.chipInfoβ A pointer to algChipInfo_tobject.config_idβ A number identifying a configuration item. Values:LG_CFG_ID_DEBUG_LEVEL0,LG_CFG_ID_MIN_DELAY1.config_valueβ The value of a configuration item.*config_valueβ The value of a configuration item.countβ The number of bytes to be transferred in a file, I2C, SPI, or serial command.debounce_usβ The debounce time in microseconds.doubleβ A floating point number.edgeβ Used to identify a GPIO level transition of interest. A rising edge is a level change from0to1. A falling edge is a level change from1to0. Values:RISING_EDGE1,FALLING_EDGE2,BOTH_EDGES3.eFlagsβ The type of GPIO edge to generate an alert. Seegpio_claim_alert. Values:RISING_EDGE1,FALLING_EDGE2,BOTH_EDGES3.errnumβ A negative number indicating a function call failed and the nature of the error.fβ A function.*fileβ A full file path. To be accessible the path must match an entry in the[files]section of the permits file.floatβ A floating point number.*fpatβ A file path which may contain wildcards. To be accessible the path must match an entry in the[files]section of the permits file.gpioβ A 0 based offset of a GPIO within a gpiochip.gpioDev: >= 0 β The device number of a gpiochip.*gpiosβ An array of GPIO numbers.groupBitsβ A 64-bit value used to set the levels of a GPIO group. Set bit x to set GPIO x of the group high. Clear bit x to set GPIO x of the group low.*groupBitsβ A 64-bit value denoting the levels of a GPIO group. If bit x is set then GPIO x of the group is high.groupMaskβ A 64-bit value used to determine which members of a GPIO group should be updated. Set bit x to update GPIO x of the group. Clear bit x to leave GPIO x of the group unaltered.handle: >= 0 β A number referencing an object opened by one offile_open,gpiochip_open,i2c_open,notify_open,serial_openscript_store,spi_open.i2c_addr: 0-0x7F β The address of a device on the I2C bus.i2c_bus: >= 0 β An I2C bus number.i2c_flags: 0 β Flags which modify an I2C open command. None are currently defined.i2c_reg: 0-255 β A register of an I2C device.*inBufβ A buffer used to pass data to a function.inCountβ The size of an input buffer.intβ A whole number, negative or positive.int32_tβ A 32-bit signed value.kind:LG_TX_PWMorLG_TX_WAVEβ A type of transmission: PWM or wave.lFlagsβ Line flags for the GPIO. The following values may be or'd to form the value.LG_SET_ACTIVE_LOW,LG_SET_OPEN_DRAIN,LG_SET_OPEN_SOURCE,LG_SET_PULL_UP,LG_SET_PULL_DOWN,LG_SET_PULL_NONE.lgChipInfo_pβ A pointer to algChipInfo_tobject.
typedef struct lgChipInfo_s
{
uint32_t lines; // number of GPIO
char name[LG_GPIO_NAME_LEN]; // Linux name
char label[LG_GPIO_LABEL_LEN]; // functional name
} lgChipInfo_t, *lgChipInfo_p;
lgLineInfo_pβ A pointer to algLineInfo_tobject.
typedef struct lgLine_s
{
uint32_t offset; // GPIO number
uint32_t lFlags;
char name[LG_GPIO_NAME_LEN]; // GPIO name
char user[LG_GPIO_USER_LEN]; // user
} lgLineInfo_t, *lgLineInfo_p;
lgPulse_pβ A pointer to algPulse_tobject.
typedef struct lgPulse_s
{
uint64_t bits;
uint64_t mask;
int64_t delay;
} lgPulse_t, *lgPulse_p;
lgThreadFunc_tβ
typedef void *(lgThreadFunc_t) (void *);
lineInfoβ A pointer to algLineInfo_tobject.modeβ A file open mode. Values:LG_FILE_READ1,LG_FILE_WRITE2,LG_FILE_RW3. The following values can be or'd into the mode:LG_FILE_APPEND4,LG_FILE_CREATE8,LG_FILE_TRUNC16.nfyHandle: >= 0 β This associates a notification with a GPIO alert.*outBufβ A buffer used to return data from a function.outCountβ The size of an output buffer.*paramβ An array of script parameters.*portStrβ A string specifying the port address used by the SBC running thergpioddaemon. It may beNULLin which case"8889"is used unless overridden by theLG_PORTenvironment variable.*pthβ A thread identifier, returned bythread_start.pthread_tβ A thread identifier.pulse_cycles: >= 0 β The number of pulses to generate. A value of0means infinite.#pulse_off: >= 0 β The off period for a pulse in microseconds.pulse_offset: >= 0 β The offset in microseconds from the nominal pulse start.pulse_on: >= 0 β The on period for a pulse in microseconds.pulsesβ An pointer to an array oflgPulse_tobjects.pulseWidth: 0, 500-2500 microseconds β Servo pulse widthpwmCycles: >= 0 β The number of PWM pulses to generate. A value of0means infinite.pwmDutyCycle: 0-100 % β PWM duty cycle %pwmFrequency: 0.1-10000 Hz β PWM frequencypwmOffset: >= 0 β The offset in microseconds from the nominal PWM pulse start.*rxBufβ A pointer to a buffer to receive data.sbcβ An integer defining a connected SBC. The value is returned byrgpiod_startupon success.*scriptβ A pointer to the text of a script.*scriptNameβ The name of ashell_script to be executed. The script must be present in the cgi directory of the daemon's configuration directory and must have execute permission.*scriptStringβ The string to be passed to ashell_script to be executed.*secretsFileβ The file containing the shared secret for a user. If the shared secret for a user matches that known by thergpioddaemon the user can"log in"to the daemon.seekFromβ Values:LG_FROM_START0,LG_FROM_CURRENT1,LG_FROM_END2.seekOffsetβ The number of bytes to move forward (positive) or backwards (negative) from the seek position (start, current, or end of file).ser_baudβ The speed of serial communication in bits per second.ser_flagsβ Flags which modify a serial open command. None are currently defined.*ser_ttyβ The name of a serial tty device, e.g./dev/ttyAMA0,/dev/ttyUSB0,/dev/tty1.servoCycles: >= 0 β The number of servo pulses to generate. A value of0means infinite.servoFrequency: 40-500 Hz β Servo pulse frequencyservoOffset: >= 0 β The offset in microseconds from the nominal servo pulse start.share_idβ Objects created with a non-zeroshare_idare persistent and may be used by other software which knows theshare_id.sleepSecsβ The number of seconds to delay.spi_baudβ The speed of SPI communication in bits per second.spi_channel: >= 0 β A SPI channel.spi_device: >= 0 β A SPI device.spi_flagsβ Seespi_openandbb_spi_open.thread_funcβ A function of typegpioThreadFunc_tused as the main function of a thread.*txBufβ An array of bytes to transmit.uint32_tβ A 32-bit unsigned value.uint64_tβ A 64-bit unsigned value.*userβ A name known by thergpioddaemon and associated with a set of user permissions.*userdataβ A pointer to arbitrary user data. This may be used to identify the instance.
You must ensure that the pointer is in scope at the time it is processed. If it is a pointer to a global this is automatic. Do not pass the address of a local variable. If you want to pass a transient object then use the following technique.
In the calling function:
user_type *userdata;
user_type my_userdata;
userdata = malloc(sizeof(user_type));
*userdata = my_userdata;
In the receiving function:
user_type my_userdata = *(user_type*)userdata;
free(userdata);
value: 0-1 β A GPIO level.*valuesβ An array of GPIO values.voidβ Denoting no parameter is requiredwatchdog_usβ The watchdog time in microseconds.wordVal: 0-65535 β A 16-bit word value.
π rgpio Error Codes π
typedef enum
{
lgif_bad_send = -2000,
lgif_bad_recv = -2001,
lgif_bad_getaddrinfo = -2002,
lgif_bad_connect = -2003,
lgif_bad_socket = -2004,
lgif_bad_noib = -2005,
lgif_duplicate_callback = -2006,
lgif_bad_malloc = -2007,
lgif_bad_callback = -2008,
lgif_notify_failed = -2009,
lgif_callback_not_found = -2010,
lgif_unconnected_sbc = -2011,
lgif_too_many_pis = -2012,
} lgifError_t;
π¦ SEE ALSO π¦
Source: Linux, version/date: 2020-2021, page: rgpio(3). Stay iconic, bestie π
β¨
π¦ where this page comes from
- ubuntu noble: librgpio-dev 0.2.0.0-0ubuntu3 Β· LicenseRef-public-domain
- ubuntu questing: librgpio-dev 0.2.0.0-0ubuntu4
- ubuntu resolute: librgpio-dev 0.2.0.0-0ubuntu6 Β· LicenseRef-public-domain
- ubuntu stonking: librgpio-dev 0.2.0.0-0ubuntu6 Β· LicenseRef-public-domain
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms