Skip to content

Commit 47aad16

Browse files
authored
Merge pull request #10 from fpagliughi/multi-bit
Add multi-line support.
2 parents 0bf05a9 + b1c720f commit 47aad16

File tree

10 files changed

+461
-91
lines changed

10 files changed

+461
-91
lines changed

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@ Versioning](https://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
1010

11-
## 0.1.0 - 2018-09-28
11+
## v0.2.0 - 2018-12-12
12+
13+
Adds the ability to create a collection of lines from a single chip and read or write those lines simultaneously with a single stystem call.
14+
15+
- A new `Lines` object (plural) was added. It is a collection of individual `Line` objects on a single `Chip` which can be read or written simultaneously with a single system call.
16+
- A `Line` now just contains the reference to the Chip and the offset number. No system call is incurred when one is created.
17+
- Information about an individual line is now represented by a separate `LineInfo` struct which can be obtained from the function `Line::info()`. This incurs a system call to retrieve the information.
18+
- Creating a `Line` can't fail unless the caller specifies an offset that is out of range of the chip.
19+
- The `LineIterator` can not fail since it checks the offset range. So now its item is just a `Line`, and not `Result<Line>`.
20+
- There was no longer a need for `Line::refresh()` so it was removed.
21+
- Since a `Line` object is trivial to create, it is now OK to have `Lines` be a simple collection of `Line` structs.
22+
23+
## v0.1.0 - 2018-09-28
1224

1325
- Initial release of the library with basic operations centered around operating
1426
on a single line at a time.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "gpio-cdev"
3-
version = "0.1.0"
4-
authors = ["Paul Osborne <[email protected]>"]
3+
version = "0.2.0"
4+
authors = ["Paul Osborne <[email protected]>", "Frank Pagliughi <[email protected]>"]
55
description = "Linux GPIO Character Device Support (/dev/gpiochipN)"
66
homepage = "https://github.com/posborne/rust-gpio-cdev"
77
repository = "https://github.com/posborne/rust-gpio-cdev"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Add the following to your Cargo.toml
2323

2424
```
2525
[dependencies]
26-
gpio-cdev = "0.1"
26+
gpio-cdev = "0.2"
2727
```
2828

2929
## Examples

examples/blinky.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn do_main(args: Cli) -> errors::Result<()> {
3434
// setting it separately is not required
3535
let handle = chip
3636
.get_line(args.line)?
37-
.request(LineRequestFlags::OUTPUT, 1, "readinput")?;
37+
.request(LineRequestFlags::OUTPUT, 1, "blinky")?;
3838

3939
let duration = Duration::from_millis(args.duration_ms);
4040
let start_time = Instant::now();

examples/driveoutput.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn do_main(args: Cli) -> errors::Result<()> {
3030
// setting it separately is not required
3131
let _handle =
3232
chip.get_line(args.line)?
33-
.request(LineRequestFlags::OUTPUT, args.value, "readinput")?;
33+
.request(LineRequestFlags::OUTPUT, args.value, "driveoutput")?;
3434

3535
println!("Output being driven... Enter to exit");
3636
let mut buf = String::new();

examples/lsgpio.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@ fn main() {
3131
chip.num_lines()
3232
);
3333
for line in chip.lines() {
34-
match line {
35-
Ok(line) => {
34+
match line.info() {
35+
Ok(info) => {
3636
let mut flags = vec![];
3737

38-
if line.is_kernel() {
38+
if info.is_kernel() {
3939
flags.push("kernel");
4040
}
4141

42-
if line.direction() == LineDirection::Out {
42+
if info.direction() == LineDirection::Out {
4343
flags.push("output");
4444
}
4545

46-
if line.is_active_low() {
46+
if info.is_active_low() {
4747
flags.push("active-low");
4848
}
49-
if line.is_open_drain() {
49+
if info.is_open_drain() {
5050
flags.push("open-drain");
5151
}
52-
if line.is_open_source() {
52+
if info.is_open_source() {
5353
flags.push("open-source");
5454
}
5555

@@ -61,15 +61,16 @@ fn main() {
6161

6262
println!(
6363
"\tline {lineno:>3}: {name} {consumer} {usage}",
64-
lineno = line.offset(),
65-
name = line.name().unwrap_or("unused"),
66-
consumer = line.consumer().unwrap_or("unused"),
64+
lineno = info.line().offset(),
65+
name = info.name().unwrap_or("unused"),
66+
consumer = info.consumer().unwrap_or("unused"),
6767
usage = usage,
6868
);
6969
}
70-
Err(e) => println!("\tError getting line: {:?}", e),
70+
Err(e) => println!("\tError getting line info: {:?}", e),
7171
}
7272
}
73+
println!();
7374
}
7475
}
7576
}

examples/multioutput.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2018 The rust-gpio-cdev Project Developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
extern crate gpio_cdev;
10+
#[macro_use]
11+
extern crate quicli;
12+
13+
use gpio_cdev::*;
14+
use quicli::prelude::*;
15+
16+
#[derive(Debug, StructOpt)]
17+
struct Cli {
18+
/// The gpiochip device (e.g. /dev/gpiochip0)
19+
chip: String,
20+
/// The offset and value of each GPIO line for the provided chip
21+
/// in the form "off=<0|1>"
22+
line_values: Vec<String>,
23+
}
24+
25+
// Use like:
26+
// muiltioutput /dev/gpiochip0 0=1 1=1 2=0 3=1 4=0
27+
//
28+
// to set lines 0, 1, & 3 high
29+
// 2 & 4 low
30+
//
31+
fn do_main(args: Cli) -> errors::Result<()> {
32+
let mut chip = Chip::new(args.chip)?;
33+
let mut offsets = Vec::new();
34+
let mut values = Vec::new();
35+
36+
for arg in args.line_values {
37+
let lv: Vec<&str> = arg.split("=").collect();
38+
offsets.push(lv[0].parse::<u32>().unwrap());
39+
values.push(lv[1].parse::<u8>().unwrap());
40+
}
41+
42+
// NOTE: we set the default values to the desired states so
43+
// setting them separately is not required
44+
let _handle =
45+
chip.get_lines(&offsets)?
46+
.request(LineRequestFlags::OUTPUT, &values, "multioutput")?;
47+
48+
println!("Output lines being driven... Enter to exit");
49+
let mut buf = String::new();
50+
::std::io::stdin().read_line(&mut buf)?;
51+
52+
Ok(())
53+
}
54+
55+
main!(|args: Cli| match do_main(args) {
56+
Ok(()) => {}
57+
Err(e) => {
58+
println!("Error: {:?}", e);
59+
}
60+
});

examples/multiread.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2018 The rust-gpio-cdev Project Developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
extern crate gpio_cdev;
10+
#[macro_use]
11+
extern crate quicli;
12+
13+
use gpio_cdev::*;
14+
use quicli::prelude::*;
15+
16+
#[derive(Debug, StructOpt)]
17+
struct Cli {
18+
/// The gpiochip device (e.g. /dev/gpiochip0)
19+
chip: String,
20+
/// The offset of the GPIO lines for the provided chip
21+
lines: Vec<u32>,
22+
}
23+
24+
fn do_main(args: Cli) -> errors::Result<()> {
25+
let mut chip = Chip::new(args.chip)?;
26+
let ini_vals = vec![ 0; args.lines.len() ];
27+
let handle = chip
28+
.get_lines(&args.lines)?
29+
.request(LineRequestFlags::INPUT, &ini_vals, "multiread")?;
30+
println!("Values: {:?}", handle.get_values()?);
31+
32+
Ok(())
33+
}
34+
35+
main!(|args: Cli| match do_main(args) {
36+
Ok(()) => {}
37+
Err(e) => {
38+
println!("Error: {:?}", e);
39+
}
40+
});

examples/readall.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2018 The rust-gpio-cdev Project Developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
extern crate gpio_cdev;
10+
#[macro_use]
11+
extern crate quicli;
12+
13+
use gpio_cdev::*;
14+
use quicli::prelude::*;
15+
16+
#[derive(Debug, StructOpt)]
17+
struct Cli {
18+
/// The gpiochip device (e.g. /dev/gpiochip0)
19+
chip: String,
20+
}
21+
22+
fn do_main(args: Cli) -> errors::Result<()> {
23+
let mut chip = Chip::new(args.chip)?;
24+
let ini_vals = vec![ 0; chip.num_lines() as usize ];
25+
let handle = chip
26+
.get_all_lines()?
27+
.request(LineRequestFlags::INPUT, &ini_vals, "readall")?;
28+
println!("Values: {:?}", handle.get_values()?);
29+
30+
Ok(())
31+
}
32+
33+
main!(|args: Cli| match do_main(args) {
34+
Ok(()) => {}
35+
Err(e) => {
36+
println!("Error: {:?}", e);
37+
}
38+
});

0 commit comments

Comments
 (0)