From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Kent Gibson <warthog618@gmail.com>,
Linus Walleij <linus.walleij@linaro.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Viresh Kumar <viresh.kumar@linaro.org>
Cc: linux-gpio@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: [libgpiod][PATCH v2 8/8] bindings: rust: provide line_config.set_output_values()
Date: Fri, 20 Jan 2023 10:45:15 +0100 [thread overview]
Message-ID: <20230120094515.40464-9-brgl@bgdev.pl> (raw)
In-Reply-To: <20230120094515.40464-1-brgl@bgdev.pl>
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Add a new function to line config allowing to set a list of output values
for requested lines. This works very similarily to the C++ version of the
new C interface.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
bindings/rust/libgpiod/src/lib.rs | 1 +
bindings/rust/libgpiod/src/line_config.rs | 24 +++++++-
bindings/rust/libgpiod/tests/line_config.rs | 62 +++++++++++++++++++++
3 files changed, 86 insertions(+), 1 deletion(-)
diff --git a/bindings/rust/libgpiod/src/lib.rs b/bindings/rust/libgpiod/src/lib.rs
index a5d018c..f268ceb 100644
--- a/bindings/rust/libgpiod/src/lib.rs
+++ b/bindings/rust/libgpiod/src/lib.rs
@@ -71,6 +71,7 @@ pub enum OperationType {
InfoEventGetLineInfo,
LineConfigNew,
LineConfigAddSettings,
+ LineConfigSetOutputValues,
LineConfigGetOffsets,
LineConfigGetSettings,
LineRequestReconfigLines,
diff --git a/bindings/rust/libgpiod/src/line_config.rs b/bindings/rust/libgpiod/src/line_config.rs
index 3f42dad..32f057f 100644
--- a/bindings/rust/libgpiod/src/line_config.rs
+++ b/bindings/rust/libgpiod/src/line_config.rs
@@ -7,7 +7,7 @@ use std::collections::HashMap;
use super::{
gpiod,
- line::{Offset, Settings},
+ line::{Offset, Settings, Value},
Error, OperationType, Result,
};
@@ -77,6 +77,28 @@ impl Config {
}
}
+ /// Set output values for a number of lines.
+ pub fn set_output_values(&mut self, values: &[Value]) -> Result<&mut Self> {
+ let mut mapped_values = Vec::new();
+ for value in values {
+ mapped_values.push(value.value());
+ }
+
+ let ret = unsafe {
+ gpiod::gpiod_line_config_set_output_values(self.config, mapped_values.as_ptr(),
+ values.len() as u64)
+ };
+
+ if ret == -1 {
+ Err(Error::OperationFailed(
+ OperationType::LineConfigSetOutputValues,
+ errno::errno(),
+ ))
+ } else {
+ Ok(self)
+ }
+ }
+
/// Get a mapping of offsets to line settings stored by this object.
pub fn line_settings(&self) -> Result<HashMap<Offset, Settings>> {
let mut map: HashMap<Offset, Settings> = HashMap::new();
diff --git a/bindings/rust/libgpiod/tests/line_config.rs b/bindings/rust/libgpiod/tests/line_config.rs
index 92a7af3..96ce127 100644
--- a/bindings/rust/libgpiod/tests/line_config.rs
+++ b/bindings/rust/libgpiod/tests/line_config.rs
@@ -5,9 +5,11 @@
mod common;
mod line_config {
+ use libgpiod::chip::Chip;
use libgpiod::line::{
self, Bias, Direction, Drive, Edge, EventClock, SettingKind, SettingVal, Value,
};
+ use gpiosim_sys::Sim;
#[test]
fn settings() {
@@ -76,4 +78,64 @@ mod line_config {
SettingVal::OutputValue(Value::Active)
);
}
+
+ #[test]
+ fn set_global_output_values() {
+ let sim = Sim::new(Some(4), None, true).unwrap();
+ let mut settings = line::Settings::new().unwrap();
+ settings.set_direction(Direction::Output).unwrap();
+
+ let mut config = line::Config::new().unwrap();
+ config
+ .add_line_settings(&[0, 1, 2, 3], settings)
+ .unwrap()
+ .set_output_values(&[
+ Value::Active,
+ Value::InActive,
+ Value::Active,
+ Value::InActive
+ ])
+ .unwrap();
+
+ let chip = Chip::open(&sim.dev_path()).unwrap();
+ let _request = chip.request_lines(None, &config);
+
+ assert_eq!(sim.val(0).unwrap(), gpiosim_sys::Value::Active);
+ assert_eq!(sim.val(1).unwrap(), gpiosim_sys::Value::InActive);
+ assert_eq!(sim.val(2).unwrap(), gpiosim_sys::Value::Active);
+ assert_eq!(sim.val(3).unwrap(), gpiosim_sys::Value::InActive);
+ }
+
+ #[test]
+ fn read_back_global_output_values() {
+ let mut settings = line::Settings::new().unwrap();
+ settings
+ .set_direction(Direction::Output)
+ .unwrap()
+ .set_output_value(Value::Active)
+ .unwrap();
+
+ let mut config = line::Config::new().unwrap();
+ config
+ .add_line_settings(&[0, 1, 2, 3], settings)
+ .unwrap()
+ .set_output_values(&[
+ Value::Active,
+ Value::InActive,
+ Value::Active,
+ Value::InActive,
+ ])
+ .unwrap();
+
+ assert_eq!(
+ config
+ .line_settings()
+ .unwrap()
+ .get(&1)
+ .unwrap()
+ .output_value()
+ .unwrap(),
+ Value::InActive
+ );
+ }
}
--
2.37.2
next prev parent reply other threads:[~2023-01-20 9:45 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-20 9:45 [libgpiod][PATCH v2 0/8] treewide: continue beating libgpiod v2 into shape for an upcoming release Bartosz Golaszewski
2023-01-20 9:45 ` [libgpiod][PATCH v2 1/8] README: update for libgpiod v2 Bartosz Golaszewski
2023-01-20 9:45 ` [libgpiod][PATCH v2 2/8] treewide: unify gpiod_line_config/request_get_offsets() functions Bartosz Golaszewski
2023-01-20 9:45 ` [libgpiod][PATCH v2 3/8] core: provide gpiod_line_config_set_output_values() Bartosz Golaszewski
2023-01-20 9:45 ` [libgpiod][PATCH v2 4/8] gpioset: use gpiod_line_config_set_output_values() Bartosz Golaszewski
2023-01-20 9:45 ` [libgpiod][PATCH v2 5/8] bindings: cxx: add line_config.set_output_values() Bartosz Golaszewski
2023-01-20 9:45 ` [libgpiod][PATCH v2 6/8] bindings: python: add the output_values argument to Chip.request_lines() Bartosz Golaszewski
2023-01-20 9:45 ` [libgpiod][PATCH v2 7/8] bindings: rust: make mutators return &mut self Bartosz Golaszewski
2023-01-25 9:25 ` Viresh Kumar
2023-01-20 9:45 ` Bartosz Golaszewski [this message]
2023-01-25 9:25 ` [libgpiod][PATCH v2 8/8] bindings: rust: provide line_config.set_output_values() Viresh Kumar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230120094515.40464-9-brgl@bgdev.pl \
--to=brgl@bgdev.pl \
--cc=andriy.shevchenko@linux.intel.com \
--cc=bartosz.golaszewski@linaro.org \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=viresh.kumar@linaro.org \
--cc=warthog618@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.