Skip to content

Commit

Permalink
Change driver_name type from &str to &Vec<&str>
Browse files Browse the repository at this point in the history
This allows alternative values for driver names, eg. the support for
both ev3 and nxt hardware.
  • Loading branch information
pixix4 committed Mar 20, 2021
1 parent c1fcd43 commit 584ec6e
Show file tree
Hide file tree
Showing 19 changed files with 41 additions and 145 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ev3dev-lang-rust"
version = "0.10.0"
version = "0.10.1"
authors = ["Lars Westermann <[email protected]>"]

description = "Rust language bindings for ev3dev"
Expand Down
14 changes: 0 additions & 14 deletions examples/color-sensor/.dockerignore

This file was deleted.

26 changes: 0 additions & 26 deletions examples/color-sensor/Dockerfile

This file was deleted.

14 changes: 0 additions & 14 deletions examples/infrared-sensor/.dockerignore

This file was deleted.

26 changes: 0 additions & 26 deletions examples/infrared-sensor/Dockerfile

This file was deleted.

14 changes: 0 additions & 14 deletions examples/screen/.dockerignore

This file was deleted.

26 changes: 0 additions & 26 deletions examples/screen/Dockerfile

This file was deleted.

21 changes: 11 additions & 10 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Driver {
pub fn find_name_by_port_and_driver(
class_name: &str,
port: &dyn Port,
driver_name: &str,
driver_name_vec: &Vec<&str>,
) -> Ev3Result<String> {
let port_address = port.address();

Expand All @@ -52,15 +52,15 @@ impl Driver {

if address.get::<String>()?.contains(&port_address) {
let driver = Attribute::new(class_name, name, "driver_name")?;

if driver.get::<String>()? == driver_name {
let driver_name = driver.get::<String>()?;
if driver_name_vec.iter().any(|n| &driver_name == n) {
return Ok(name.to_owned());
}
}
}

Err(Ev3Error::NotConnected {
device: driver_name.to_owned(),
device: format!("{:?}", driver_name_vec),
port: Some(port.address()),
})
}
Expand All @@ -69,26 +69,26 @@ impl Driver {
///
/// Returns `Ev3Error::NotFound` if no such device exists.
/// Returns `Ev3Error::MultipleMatches` if more then one matching device exists.
pub fn find_name_by_driver(class_name: &str, driver_name: &str) -> Ev3Result<String> {
let mut names = Driver::find_names_by_driver(class_name, driver_name)?;
pub fn find_name_by_driver(class_name: &str, driver_name_vec: &Vec<&str>) -> Ev3Result<String> {
let mut names = Driver::find_names_by_driver(class_name, &driver_name_vec)?;

match names.len() {
0 => Err(Ev3Error::NotConnected {
device: driver_name.to_owned(),
device: format!("{:?}", driver_name_vec),
port: None,
}),
1 => Ok(names
.pop()
.expect("Name vector should contains exactly one element")),
_ => Err(Ev3Error::MultipleMatches {
device: driver_name.to_owned(),
device: format!("{:?}", driver_name_vec),
ports: names,
}),
}
}

/// Returns the names of the devices with the given `class_name`.
pub fn find_names_by_driver(class_name: &str, driver_name: &str) -> Ev3Result<Vec<String>> {
pub fn find_names_by_driver(class_name: &str, driver_name_vec: &Vec<&str>) -> Ev3Result<Vec<String>> {
let paths = fs::read_dir(format!("{}{}", ROOT_PATH, class_name))?;

let mut found_names = Vec::new();
Expand All @@ -98,7 +98,8 @@ impl Driver {

let driver = Attribute::new(class_name, name, "driver_name")?;

if driver.get::<String>()? == driver_name {
let driver_name = driver.get::<String>()?;
if driver_name_vec.iter().any(|n| &driver_name == n) {
found_names.push(name.to_owned());
}
}
Expand Down
23 changes: 19 additions & 4 deletions src/findable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/// ```
#[macro_export]
macro_rules! findable {
($class_name:expr, $driver_name:expr, $port: ty, $debug_name:expr, $port_prefix:expr) => {
($class_name:expr, [$( $driver_name:expr ),*], $port: ty, $debug_name:expr, $port_prefix:expr) => {
fn map_error(e: Ev3Error) -> Ev3Error {
match e {
e @ Ev3Error::InternalError { .. } => e,
Expand All @@ -42,23 +42,38 @@ macro_rules! findable {

/// Try to get a `Self` on the given port. Returns `None` if port is not used or another device is connected.
pub fn get(port: $port) -> Ev3Result<Self> {
let name = Driver::find_name_by_port_and_driver($class_name, &port, $driver_name)
let mut driver_name_vec = Vec::new();
$(
driver_name_vec.push($driver_name);
)*

let name = Driver::find_name_by_port_and_driver($class_name, &port, &driver_name_vec)
.map_err(Self::map_error)?;

Ok(Self::new(Driver::new($class_name, &name)))
}

/// Try to find a `Self`. Only returns a motor if their is exactly one connected, `Error::NotFound` otherwise.
pub fn find() -> Ev3Result<Self> {
let mut driver_name_vec = Vec::new();
$(
driver_name_vec.push($driver_name);
)*

let name =
Driver::find_name_by_driver($class_name, $driver_name).map_err(Self::map_error)?;
Driver::find_name_by_driver($class_name, &driver_name_vec).map_err(Self::map_error)?;

Ok(Self::new(Driver::new($class_name, &name)))
}

/// Extract list of connected 'Self'
pub fn list() -> Ev3Result<Vec<Self>> {
Ok(Driver::find_names_by_driver($class_name, $driver_name)?
let mut driver_name_vec = Vec::new();
$(
driver_name_vec.push($driver_name);
)*

Ok(Driver::find_names_by_driver($class_name, &driver_name_vec)?
.iter()
.map(|name| Self::new(Driver::new($class_name, &name)))
.collect())
Expand Down
2 changes: 1 addition & 1 deletion src/motors/large_motor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl LargeMotor {

findable!(
"tacho-motor",
"lego-ev3-l-motor",
["lego-ev3-l-motor", "lego-nxt-motor"],
MotorPort,
"LargeMotor",
"out"
Expand Down
2 changes: 1 addition & 1 deletion src/motors/medium_motor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl MediumMotor {

findable!(
"tacho-motor",
"lego-ev3-m-motor",
["lego-ev3-m-motor"],
MotorPort,
"MediumMotor",
"out"
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/color_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl ColorSensor {

findable!(
"lego-sensor",
"lego-ev3-color",
["lego-ev3-color"],
SensorPort,
"ColorSensor",
"in"
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/compass_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl CompassSensor {
Self { driver, origin: 0 }
}

findable!("lego-sensor", "ht-nxt-compass", SensorPort, "Compass", "in");
findable!("lego-sensor", ["ht-nxt-compass"], SensorPort, "Compass", "in");

/// Command for starting the calibration
pub const COMMAND_START_CALIBRATION: &'static str = "BEGIN-CAL";
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/gyro_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl GyroSensor {

findable!(
"lego-sensor",
"lego-ev3-gyro",
["lego-ev3-gyro"],
SensorPort,
"GyroSensor",
"in"
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/infrared_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl InfraredSensor {

findable!(
"lego-sensor",
"lego-ev3-ir",
["lego-ev3-ir"],
SensorPort,
"InfraredrSensor",
"in"
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/ir_seeker_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl IrSeekerSensor {

findable!(
"lego-sensor",
"ht-nxt-ir-seek-v2",
["ht-nxt-ir-seek-v2"],
SensorPort,
"IrSeeker",
"in"
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/light_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl LightSensor {

findable!(
"lego-sensor",
"lego-nxt-light",
["lego-nxt-light"],
SensorPort,
"LightSensor",
"in"
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/touch_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl TouchSensor {

findable!(
"lego-sensor",
"lego-ev3-touch",
["lego-ev3-touch", "lego-nxt-touch"],
SensorPort,
"TouchSensor",
"in"
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/ultrasonic_sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl UltrasonicSensor {

findable!(
"lego-sensor",
"lego-ev3-us",
["lego-ev3-us", "lego-nxt-us"],
SensorPort,
"UltrasonicSensor",
"in"
Expand Down

0 comments on commit 584ec6e

Please sign in to comment.