Skip to content

Commit 16fedc8

Browse files
Merge #30
30: Improve documentation about maintaining LineHandles r=nastevens a=kmdouglass This commit addresses #29 by expanding the documentation to explain the importance of maintaining valid LineHandles in a program that uses this library. - A comment was added to the Read State code example in the README to explain that the LineHandle must be maintained for the line to work - A similar comment was added to the `driveoutput.rs` example - A brief explanation was added to the section entitled "Exporting" a GPIO of the README about the file descriptor that is associated with a line Co-authored-by: Kyle M. Douglass <[email protected]>
2 parents caf09c4 + 872abf5 commit 16fedc8

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ Using the sysfs API, one would write the global GPIO number to the "export" file
139139
to perform further operations using new files on the filesystem. Using the
140140
gpiochip character device, a handle for performing operations on one or more
141141
GPIO offsets within a chip are available via a "linehandle" fd created using the
142-
`GPIO_GET_LINEHANDLE_IOCTL`.
142+
`GPIO_GET_LINEHANDLE_IOCTL`. A consequence of this is that a line will remember
143+
its state only for as long as the fd is open; the line's state will be reset
144+
once the fd is closed.
143145

144146
When a linehandle is requested, additional information is also included about
145147
how the individual GPIOs will be used (input, output, as-is, active-low, open

examples/driveoutput.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ fn do_main(args: Cli) -> std::result::Result<(), errors::Error> {
2727
let mut chip = Chip::new(args.chip)?;
2828

2929
// NOTE: we set the default value to the desired state so
30-
// setting it separately is not required
30+
// setting it separately is not required. The LineHandle
31+
// instance that is returned by request must be owned by a
32+
// variable for the duration of the time that the line will
33+
// be used. If the instance is not assigned to a variable,
34+
// then the LineHandle will be immediately dropped after
35+
// request returns and the pin will appear to do nothing.
3136
let _handle =
3237
chip.get_line(args.line)?
3338
.request(LineRequestFlags::OUTPUT, args.value, "driveoutput")?;

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
//! # fn main() -> Result<()> {
7070
//! // Read the state of GPIO4 on a raspberry pi. /dev/gpiochip0
7171
//! // maps to the driver for the SoC (builtin) GPIO controller.
72+
//! // The LineHandle returned by request must be assigned to a
73+
//! // variable (in this case the variable handle) to ensure that
74+
//! // the corresponding file descriptor is not closed.
7275
//! let mut chip = Chip::new("/dev/gpiochip0")?;
7376
//! let handle = chip
7477
//! .get_line(4)?

0 commit comments

Comments
 (0)