From 584ec6e05aac77866f3061cb24ecd71a7a01b401 Mon Sep 17 00:00:00 2001 From: Lars Westermann Date: Sat, 20 Mar 2021 10:47:48 +0100 Subject: [PATCH] Change driver_name type from `&str` to `&Vec<&str>` This allows alternative values for driver names, eg. the support for both ev3 and nxt hardware. --- Cargo.toml | 2 +- examples/color-sensor/.dockerignore | 14 -------------- examples/color-sensor/Dockerfile | 26 -------------------------- examples/infrared-sensor/.dockerignore | 14 -------------- examples/infrared-sensor/Dockerfile | 26 -------------------------- examples/screen/.dockerignore | 14 -------------- examples/screen/Dockerfile | 26 -------------------------- src/driver.rs | 21 +++++++++++---------- src/findable.rs | 23 +++++++++++++++++++---- src/motors/large_motor.rs | 2 +- src/motors/medium_motor.rs | 2 +- src/sensors/color_sensor.rs | 2 +- src/sensors/compass_sensor.rs | 2 +- src/sensors/gyro_sensor.rs | 2 +- src/sensors/infrared_sensor.rs | 2 +- src/sensors/ir_seeker_sensor.rs | 2 +- src/sensors/light_sensor.rs | 2 +- src/sensors/touch_sensor.rs | 2 +- src/sensors/ultrasonic_sensor.rs | 2 +- 19 files changed, 41 insertions(+), 145 deletions(-) delete mode 100644 examples/color-sensor/.dockerignore delete mode 100644 examples/color-sensor/Dockerfile delete mode 100644 examples/infrared-sensor/.dockerignore delete mode 100644 examples/infrared-sensor/Dockerfile delete mode 100644 examples/screen/.dockerignore delete mode 100644 examples/screen/Dockerfile diff --git a/Cargo.toml b/Cargo.toml index 6cd4056..4c5bfbb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ev3dev-lang-rust" -version = "0.10.0" +version = "0.10.1" authors = ["Lars Westermann "] description = "Rust language bindings for ev3dev" diff --git a/examples/color-sensor/.dockerignore b/examples/color-sensor/.dockerignore deleted file mode 100644 index f895bc1..0000000 --- a/examples/color-sensor/.dockerignore +++ /dev/null @@ -1,14 +0,0 @@ -# Generated by Cargo -# will have compiled files and executables -/target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - -# These are backup files generated by rustfmt -**/*.rs.bk - -vendor/ -.vscode/ -client*/ \ No newline at end of file diff --git a/examples/color-sensor/Dockerfile b/examples/color-sensor/Dockerfile deleted file mode 100644 index dd03882..0000000 --- a/examples/color-sensor/Dockerfile +++ /dev/null @@ -1,26 +0,0 @@ -FROM debian:stretch - -RUN dpkg --add-architecture armel -RUN apt update - -# Fix debian package alias -RUN sed -i "s#deb http://security.debian.org/debian-security stretch/updates main#deb http://deb.debian.org/debian-security stretch/updates main#g" /etc/apt/sources.list - -# Install curl for rust installation -# Install g++ as buildscript compiler -# Install g++-arm-linux-gnueabi, crossbuild-essential-armel as cross compiler -# Install libmosquitto-dev libssl-dev cmake for mqtt linking support -RUN apt --yes install curl g++ g++-arm-linux-gnueabi crossbuild-essential-armel - -# Instull rust for host platform -RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y - -# Export rustc/cargo bin path -ENV PATH "$PATH:/root/.cargo/bin" - -# Add stdlib for target platform -RUN rustup target add armv5te-unknown-linux-gnueabi - -# docker run -it --rm -v $PWD:/build/ -w /build pixix4/ev3dev-rust -# docker run -it --rm -v $PWD:/build/ -v $PWD/../ev3dev-lang-rust/:/ev3dev-lang-rust/ -w /build pixix4/ev3dev-rust -# cargo build --release --target armv5te-unknown-linux-gnueabi diff --git a/examples/infrared-sensor/.dockerignore b/examples/infrared-sensor/.dockerignore deleted file mode 100644 index f895bc1..0000000 --- a/examples/infrared-sensor/.dockerignore +++ /dev/null @@ -1,14 +0,0 @@ -# Generated by Cargo -# will have compiled files and executables -/target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - -# These are backup files generated by rustfmt -**/*.rs.bk - -vendor/ -.vscode/ -client*/ \ No newline at end of file diff --git a/examples/infrared-sensor/Dockerfile b/examples/infrared-sensor/Dockerfile deleted file mode 100644 index dd03882..0000000 --- a/examples/infrared-sensor/Dockerfile +++ /dev/null @@ -1,26 +0,0 @@ -FROM debian:stretch - -RUN dpkg --add-architecture armel -RUN apt update - -# Fix debian package alias -RUN sed -i "s#deb http://security.debian.org/debian-security stretch/updates main#deb http://deb.debian.org/debian-security stretch/updates main#g" /etc/apt/sources.list - -# Install curl for rust installation -# Install g++ as buildscript compiler -# Install g++-arm-linux-gnueabi, crossbuild-essential-armel as cross compiler -# Install libmosquitto-dev libssl-dev cmake for mqtt linking support -RUN apt --yes install curl g++ g++-arm-linux-gnueabi crossbuild-essential-armel - -# Instull rust for host platform -RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y - -# Export rustc/cargo bin path -ENV PATH "$PATH:/root/.cargo/bin" - -# Add stdlib for target platform -RUN rustup target add armv5te-unknown-linux-gnueabi - -# docker run -it --rm -v $PWD:/build/ -w /build pixix4/ev3dev-rust -# docker run -it --rm -v $PWD:/build/ -v $PWD/../ev3dev-lang-rust/:/ev3dev-lang-rust/ -w /build pixix4/ev3dev-rust -# cargo build --release --target armv5te-unknown-linux-gnueabi diff --git a/examples/screen/.dockerignore b/examples/screen/.dockerignore deleted file mode 100644 index f895bc1..0000000 --- a/examples/screen/.dockerignore +++ /dev/null @@ -1,14 +0,0 @@ -# Generated by Cargo -# will have compiled files and executables -/target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - -# These are backup files generated by rustfmt -**/*.rs.bk - -vendor/ -.vscode/ -client*/ \ No newline at end of file diff --git a/examples/screen/Dockerfile b/examples/screen/Dockerfile deleted file mode 100644 index dd03882..0000000 --- a/examples/screen/Dockerfile +++ /dev/null @@ -1,26 +0,0 @@ -FROM debian:stretch - -RUN dpkg --add-architecture armel -RUN apt update - -# Fix debian package alias -RUN sed -i "s#deb http://security.debian.org/debian-security stretch/updates main#deb http://deb.debian.org/debian-security stretch/updates main#g" /etc/apt/sources.list - -# Install curl for rust installation -# Install g++ as buildscript compiler -# Install g++-arm-linux-gnueabi, crossbuild-essential-armel as cross compiler -# Install libmosquitto-dev libssl-dev cmake for mqtt linking support -RUN apt --yes install curl g++ g++-arm-linux-gnueabi crossbuild-essential-armel - -# Instull rust for host platform -RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y - -# Export rustc/cargo bin path -ENV PATH "$PATH:/root/.cargo/bin" - -# Add stdlib for target platform -RUN rustup target add armv5te-unknown-linux-gnueabi - -# docker run -it --rm -v $PWD:/build/ -w /build pixix4/ev3dev-rust -# docker run -it --rm -v $PWD:/build/ -v $PWD/../ev3dev-lang-rust/:/ev3dev-lang-rust/ -w /build pixix4/ev3dev-rust -# cargo build --release --target armv5te-unknown-linux-gnueabi diff --git a/src/driver.rs b/src/driver.rs index df41b75..2694210 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -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 { let port_address = port.address(); @@ -52,15 +52,15 @@ impl Driver { if address.get::()?.contains(&port_address) { let driver = Attribute::new(class_name, name, "driver_name")?; - - if driver.get::()? == driver_name { + let driver_name = driver.get::()?; + 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()), }) } @@ -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 { - 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 { + 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> { + pub fn find_names_by_driver(class_name: &str, driver_name_vec: &Vec<&str>) -> Ev3Result> { let paths = fs::read_dir(format!("{}{}", ROOT_PATH, class_name))?; let mut found_names = Vec::new(); @@ -98,7 +98,8 @@ impl Driver { let driver = Attribute::new(class_name, name, "driver_name")?; - if driver.get::()? == driver_name { + let driver_name = driver.get::()?; + if driver_name_vec.iter().any(|n| &driver_name == n) { found_names.push(name.to_owned()); } } diff --git a/src/findable.rs b/src/findable.rs index d2badb8..b408462 100644 --- a/src/findable.rs +++ b/src/findable.rs @@ -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, @@ -42,7 +42,12 @@ 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 { - 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))) @@ -50,15 +55,25 @@ macro_rules! findable { /// Try to find a `Self`. Only returns a motor if their is exactly one connected, `Error::NotFound` otherwise. pub fn find() -> Ev3Result { + 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> { - 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()) diff --git a/src/motors/large_motor.rs b/src/motors/large_motor.rs index f45e403..baf8a26 100644 --- a/src/motors/large_motor.rs +++ b/src/motors/large_motor.rs @@ -17,7 +17,7 @@ impl LargeMotor { findable!( "tacho-motor", - "lego-ev3-l-motor", + ["lego-ev3-l-motor", "lego-nxt-motor"], MotorPort, "LargeMotor", "out" diff --git a/src/motors/medium_motor.rs b/src/motors/medium_motor.rs index 1335416..a077e21 100644 --- a/src/motors/medium_motor.rs +++ b/src/motors/medium_motor.rs @@ -17,7 +17,7 @@ impl MediumMotor { findable!( "tacho-motor", - "lego-ev3-m-motor", + ["lego-ev3-m-motor"], MotorPort, "MediumMotor", "out" diff --git a/src/sensors/color_sensor.rs b/src/sensors/color_sensor.rs index d0e3a41..5277392 100644 --- a/src/sensors/color_sensor.rs +++ b/src/sensors/color_sensor.rs @@ -16,7 +16,7 @@ impl ColorSensor { findable!( "lego-sensor", - "lego-ev3-color", + ["lego-ev3-color"], SensorPort, "ColorSensor", "in" diff --git a/src/sensors/compass_sensor.rs b/src/sensors/compass_sensor.rs index 5c36868..1feaf99 100644 --- a/src/sensors/compass_sensor.rs +++ b/src/sensors/compass_sensor.rs @@ -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"; diff --git a/src/sensors/gyro_sensor.rs b/src/sensors/gyro_sensor.rs index 23bb4f6..824efbc 100644 --- a/src/sensors/gyro_sensor.rs +++ b/src/sensors/gyro_sensor.rs @@ -16,7 +16,7 @@ impl GyroSensor { findable!( "lego-sensor", - "lego-ev3-gyro", + ["lego-ev3-gyro"], SensorPort, "GyroSensor", "in" diff --git a/src/sensors/infrared_sensor.rs b/src/sensors/infrared_sensor.rs index dceade5..162a28e 100644 --- a/src/sensors/infrared_sensor.rs +++ b/src/sensors/infrared_sensor.rs @@ -20,7 +20,7 @@ impl InfraredSensor { findable!( "lego-sensor", - "lego-ev3-ir", + ["lego-ev3-ir"], SensorPort, "InfraredrSensor", "in" diff --git a/src/sensors/ir_seeker_sensor.rs b/src/sensors/ir_seeker_sensor.rs index 00c7926..2109151 100644 --- a/src/sensors/ir_seeker_sensor.rs +++ b/src/sensors/ir_seeker_sensor.rs @@ -16,7 +16,7 @@ impl IrSeekerSensor { findable!( "lego-sensor", - "ht-nxt-ir-seek-v2", + ["ht-nxt-ir-seek-v2"], SensorPort, "IrSeeker", "in" diff --git a/src/sensors/light_sensor.rs b/src/sensors/light_sensor.rs index ff6ed63..39f8500 100644 --- a/src/sensors/light_sensor.rs +++ b/src/sensors/light_sensor.rs @@ -23,7 +23,7 @@ impl LightSensor { findable!( "lego-sensor", - "lego-nxt-light", + ["lego-nxt-light"], SensorPort, "LightSensor", "in" diff --git a/src/sensors/touch_sensor.rs b/src/sensors/touch_sensor.rs index 14972b1..367508c 100644 --- a/src/sensors/touch_sensor.rs +++ b/src/sensors/touch_sensor.rs @@ -16,7 +16,7 @@ impl TouchSensor { findable!( "lego-sensor", - "lego-ev3-touch", + ["lego-ev3-touch", "lego-nxt-touch"], SensorPort, "TouchSensor", "in" diff --git a/src/sensors/ultrasonic_sensor.rs b/src/sensors/ultrasonic_sensor.rs index bd8674d..748f156 100644 --- a/src/sensors/ultrasonic_sensor.rs +++ b/src/sensors/ultrasonic_sensor.rs @@ -23,7 +23,7 @@ impl UltrasonicSensor { findable!( "lego-sensor", - "lego-ev3-us", + ["lego-ev3-us", "lego-nxt-us"], SensorPort, "UltrasonicSensor", "in"