diff --git a/.cargo/config b/.cargo/config index 3022c42..89cc57c 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,2 +1,11 @@ [target.armv5te-unknown-linux-gnueabi] linker = "/usr/bin/arm-linux-gnueabi-gcc" + +[target.armv5te-unknown-linux-musleabi] +linker = "rust-lld" + +[source.crates-io] +replace-with = "vendored-sources" + +[source.vendored-sources] +directory = "vendor" diff --git a/Cargo.toml b/Cargo.toml index e426e99..05e5453 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ev3dev-lang-rust" -version = "0.11.1" +version = "0.12.0" edition = "2021" authors = ["Lars Westermann "] diff --git a/Makefile b/Makefile index e0fe079..31c54bf 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,23 @@ -build-example-attributes: - docker run --rm -v $(PWD):/ev3dev-lang-rust/ -w /ev3dev-lang-rust/examples/attributes pixix4/ev3dev-rust /bin/bash -c "cargo build --release --target armv5te-unknown-linux-gnueabi && /usr/bin/arm-linux-gnueabi-strip /ev3dev-lang-rust/target/armv5te-unknown-linux-gnueabi/release/attributes" +all: build strip -build-example-buttons: - docker run --rm -v $(PWD):/ev3dev-lang-rust/ -w /ev3dev-lang-rust/examples/buttons pixix4/ev3dev-rust /bin/bash -c "cargo build --release --target armv5te-unknown-linux-gnueabi && /usr/bin/arm-linux-gnueabi-strip /ev3dev-lang-rust/target/armv5te-unknown-linux-gnueabi/release/buttons" +build: + docker run --rm -v $(PWD):/build -w /build pixix4/ev3dev-rust:latest \ + cargo build --release --examples --target armv5te-unknown-linux-musleabi -build-example-color-sensor: - docker run --rm -v $(PWD):/ev3dev-lang-rust/ -w /ev3dev-lang-rust/examples/color-sensor pixix4/ev3dev-rust /bin/bash -c "cargo build --release --target armv5te-unknown-linux-gnueabi && /usr/bin/arm-linux-gnueabi-strip /ev3dev-lang-rust/target/armv5te-unknown-linux-gnueabi/release/color-sensor" +strip: strip-buttons strip-color-sensor strip-custom-attributes strip-infrared-sensor strip-motors -build-example-infrared-sensor: - docker run --rm -v $(PWD):/ev3dev-lang-rust/ -w /ev3dev-lang-rust/examples/infrared-sensor pixix4/ev3dev-rust /bin/bash -c "cargo build --release --target armv5te-unknown-linux-gnueabi && /usr/bin/arm-linux-gnueabi-strip /ev3dev-lang-rust/target/armv5te-unknown-linux-gnueabi/release/infrared-sensor" - -build-example-motors: - docker run --rm -v $(PWD):/ev3dev-lang-rust/ -w /ev3dev-lang-rust/examples/motors pixix4/ev3dev-rust /bin/bash -c "cargo build --release --target armv5te-unknown-linux-gnueabi && /usr/bin/arm-linux-gnueabi-strip /ev3dev-lang-rust/target/armv5te-unknown-linux-gnueabi/release/motors" - -build-example-screen: - docker run --rm -v $(PWD):/ev3dev-lang-rust/ -w /ev3dev-lang-rust/examples/screen pixix4/ev3dev-rust /bin/bash -c "cargo build --release --target armv5te-unknown-linux-gnueabi && /usr/bin/arm-linux-gnueabi-strip /ev3dev-lang-rust/target/armv5te-unknown-linux-gnueabi/release/screen" - -build-examples: build-example-attributes build-example-buttons build-example-color-sensor build-example-infrared-sensor build-example-screen - -clean: - cargo clean +strip-buttons: + docker run --rm -v $(PWD):/build -w /build pixix4/ev3dev-rust:latest \ + arm-linux-gnueabi-strip /build/target/armv5te-unknown-linux-musleabi/release/examples/buttons +strip-color-sensor: + docker run --rm -v $(PWD):/build -w /build pixix4/ev3dev-rust:latest \ + arm-linux-gnueabi-strip /build/target/armv5te-unknown-linux-musleabi/release/examples/color-sensor +strip-custom-attributes: + docker run --rm -v $(PWD):/build -w /build pixix4/ev3dev-rust:latest \ + arm-linux-gnueabi-strip /build/target/armv5te-unknown-linux-musleabi/release/examples/custom-attributes +strip-infrared-sensor: + docker run --rm -v $(PWD):/build -w /build pixix4/ev3dev-rust:latest \ + arm-linux-gnueabi-strip /build/target/armv5te-unknown-linux-musleabi/release/examples/infrared-sensor +strip-motors: + docker run --rm -v $(PWD):/build -w /build pixix4/ev3dev-rust:latest \ + arm-linux-gnueabi-strip /build/target/armv5te-unknown-linux-musleabi/release/examples/motors diff --git a/docker/Dockerfile b/docker/Dockerfile index fcaf24e..5af6811 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,25 +1,20 @@ -FROM debian:stretch +FROM debian:stretch-slim RUN dpkg --add-architecture armel -RUN apt update -# Fix debian package alias +RUN apt-get update 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 as cross compiler -RUN apt --yes install curl g++ g++-arm-linux-gnueabi crossbuild-essential-armel +RUN apt-get --yes install curl cmake pkg-config clang g++ g++-arm-linux-gnueabi crossbuild-essential-armel libssl-dev libssl-dev:armel libclang-dev \ + && rm -rf /var/lib/apt/lists/* + -# Instull rust for host platform RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y ENV PATH "$PATH:/root/.cargo/bin" -# Add stdlib for target platform -RUN rustup target add armv5te-unknown-linux-gnueabi - -# docker build . -t pixix4/ev3dev-rust --no-cache +RUN rustup target add armv5te-unknown-linux-musleabi armv5te-unknown-linux-gnueabi -# docker run -it --rm -v $PWD:/build/ -w /build pixix4/ev3dev-rust -# cargo build --release --target armv5te-unknown-linux-gnueabi +ENV PKG_CONFIG_SYSROOT_DIR /usr/arm-linux-gnueabi/ +ENV CC_armv5te_unknown_linux_gnueabi arm-linux-gnueabi-gcc +ENV CXX_armv5te_unknown_linux_gnueabi arm-linux-gnueabi-g++ diff --git a/src/attriute.rs b/src/attribute.rs similarity index 96% rename from src/attriute.rs rename to src/attribute.rs index b5e64e0..4b79961 100644 --- a/src/attriute.rs +++ b/src/attribute.rs @@ -39,7 +39,7 @@ impl Attribute { }) } - /// Create a new `Attribute` instance that wrappes + /// Create a new `Attribute` instance that wrap's /// the file `/sys/class/{class_name}/{name}{attribute_name}`. pub fn from_sys_class( class_name: &str, @@ -71,7 +71,7 @@ impl Attribute { /// println!("value0 of color sensor: {}", color_sensor_value.get::()?); /// /// // Get raw rotation count of motor in port `A`. - /// // See https://github.com/ev3dev/ev3dev/wiki/Internals:-ev3dev-stretch for more infomation. + /// // See https://github.com/ev3dev/ev3dev/wiki/Internals:-ev3dev-stretch for more information. /// let rotation_count = Attribute::from_path_with_discriminator( /// "/sys/bus/iio/devices", /// "in_count0_raw", @@ -105,7 +105,7 @@ impl Attribute { Err(Ev3Error::InternalError { msg: format!( - "Attribute `{}` at root path `{}` coult not be found!", + "Attribute `{}` at root path `{}` could not be found!", attribute_path, root_path ), }) @@ -165,7 +165,7 @@ impl Attribute { } /// Returns a string vector representation of the wrapped file. - /// The file value is splitet at whitespaces. + /// The file value is splitted at whitespace's. pub fn get_vec(&self) -> Ev3Result> { let value = self.get_str()?; let vec = value diff --git a/src/buttons.rs b/src/buttons.rs index 4dd403b..d5c6720 100644 --- a/src/buttons.rs +++ b/src/buttons.rs @@ -37,7 +37,7 @@ struct FileMapEntry { pub file: File, pub buffer_cache: [u8; KEY_BUF_LEN], } -// Manuelly implement Debug cause `buffer_cache` does not implement Debug. +// Manually implement Debug cause `buffer_cache` does not implement Debug. impl fmt::Debug for FileMapEntry { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("FileMapEntry") @@ -103,14 +103,14 @@ impl ButtonFileHandler { self.pressed_buttons.contains(name) } - /// Check for currenly pressed buttons. If the new state differs from the + /// Check for currently pressed buttons. If the new state differs from the /// old state, call the appropriate button event handlers. fn process(&mut self) { for entry in self.file_map.values_mut() { unsafe { libc::ioctl( entry.file.as_raw_fd(), - EVIOCGKEY.into(), + (EVIOCGKEY as i32).try_into().unwrap(), &mut entry.buffer_cache, ); } @@ -185,7 +185,7 @@ impl Ev3Button { }) } - /// Check for currenly pressed buttons. If the new state differs from the + /// Check for currently pressed buttons. If the new state differs from the /// old state, call the appropriate button event handlers. pub fn process(&self) { self.button_handler.borrow_mut().process() diff --git a/src/driver.rs b/src/driver.rs index 2e0d2d3..612f2c7 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -61,7 +61,7 @@ impl Driver { Err(Ev3Error::NotConnected { device: format!("{:?}", driver_name_vec), - port: Some(port.address()), + port: Some(port_address), }) } diff --git a/src/led.rs b/src/led.rs index 7805e24..1c77784 100644 --- a/src/led.rs +++ b/src/led.rs @@ -1,4 +1,4 @@ -//! The leds on top of the EV3 brick. +//! The led's on top of the EV3 brick. use std::fs; @@ -7,7 +7,7 @@ use crate::{utils::OrErr, Attribute, Ev3Result}; /// Color type. pub type Color = (u8, u8); -/// The leds on top of the EV3 brick. +/// The led's on top of the EV3 brick. #[derive(Debug, Clone)] pub struct Led { left_red: Attribute, diff --git a/src/lib.rs b/src/lib.rs index fdcaf84..5f96a61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ //! // Run motor. //! large_motor.set_duty_cycle_sp(50)?; //! -//! // Find color sensor. Always returns the first recognised one. +//! // Find color sensor. Always returns the first recognized one. //! let color_sensor = ColorSensor::find()?; //! //! // Switch to rgb mode. @@ -46,8 +46,8 @@ extern crate libc; #[macro_use] mod findable; -mod attriute; -pub use attriute::Attribute; +mod attribute; +pub use attribute::Attribute; mod driver; pub use driver::Driver; mod device; @@ -56,7 +56,6 @@ pub use device::Device; mod utils; pub use utils::{Ev3Error, Ev3Result, Port}; -#[cfg(target_os = "linux")] pub mod wait; pub mod motors; diff --git a/src/motors/large_motor.rs b/src/motors/large_motor.rs index baf8a26..e714824 100644 --- a/src/motors/large_motor.rs +++ b/src/motors/large_motor.rs @@ -1,7 +1,5 @@ use super::MotorPort; -#[cfg(target_os = "linux")] -use crate::wait; -use crate::{Attribute, Device, Driver, Ev3Error, Ev3Result}; +use crate::{wait, Attribute, Device, Driver, Ev3Error, Ev3Result}; use std::time::Duration; /// EV3/NXT large servo motor diff --git a/src/motors/medium_motor.rs b/src/motors/medium_motor.rs index a077e21..cf352c4 100644 --- a/src/motors/medium_motor.rs +++ b/src/motors/medium_motor.rs @@ -1,7 +1,5 @@ use super::MotorPort; -#[cfg(target_os = "linux")] -use crate::wait; -use crate::{Attribute, Device, Driver, Ev3Error, Ev3Result}; +use crate::{wait, Attribute, Device, Driver, Ev3Error, Ev3Result}; use std::time::Duration; /// EV3 medium servo motor diff --git a/src/motors/servo_motor_macro.rs b/src/motors/servo_motor_macro.rs index 2446ea1..b4e5d75 100644 --- a/src/motors/servo_motor_macro.rs +++ b/src/motors/servo_motor_macro.rs @@ -49,7 +49,7 @@ macro_rules! servo_motor { /// Returns the current mid pulse setpoint. /// Used to set the pulse size in milliseconds for the signal - /// that tells the servo to drive to the miniumum (counter-clockwise) position_sp. + /// that tells the servo to drive to the minimum (counter-clockwise) position_sp. /// Default value is 600. /// Valid values are 300 to 700. /// You must write to the position_sp attribute for changes to this attribute to take effect. @@ -59,7 +59,7 @@ macro_rules! servo_motor { /// Sets the mid pulse setpoint. /// Used to set the pulse size in milliseconds for the signal - /// that tells the servo to drive to the miniumum (counter-clockwise) position_sp. + /// that tells the servo to drive to the minimum (counter-clockwise) position_sp. /// Default value is 600. /// Valid values are 300 to 700. /// You must write to the position_sp attribute for changes to this attribute to take effect. @@ -69,7 +69,7 @@ macro_rules! servo_motor { /// Returns the current min pulse setpoint. /// Used to set the pulse size in milliseconds for the signal - /// that tells the servo to drive to the miniumum (counter-clockwise) position_sp. + /// that tells the servo to drive to the minimum (counter-clockwise) position_sp. /// Default value is 600. Valid values are 300 to 700. /// You must write to the position_sp attribute for changes to this attribute to take effect. pub fn get_min_pulse_sp(&self) -> Ev3Result { @@ -77,7 +77,7 @@ macro_rules! servo_motor { } /// Sets the min pulse setpoint. /// Used to set the pulse size in milliseconds for the signal - /// that tells the servo to drive to the miniumum (counter-clockwise) position_sp. + /// that tells the servo to drive to the minimum (counter-clockwise) position_sp. /// Default value is 600. Valid values are 300 to 700. /// You must write to the position_sp attribute for changes to this attribute to take effect. pub fn set_min_pulse_sp(&self, min_pulse_sp: i32) -> Ev3Result<()> { diff --git a/src/motors/tacho_motor.rs b/src/motors/tacho_motor.rs index acfc196..f80887e 100644 --- a/src/motors/tacho_motor.rs +++ b/src/motors/tacho_motor.rs @@ -208,7 +208,7 @@ impl TachoMotor { /// Returns the number of tacho counts in the full travel of the motor. /// - /// When combined with the count_per_m atribute, + /// When combined with the count_per_m attribute, /// you can use this value to calculate the maximum travel distance of the motor. /// (linear motors only) pub fn get_full_travel_count(&self) -> Ev3Result { @@ -910,7 +910,7 @@ impl TachoMotor { /// Wait until the motor is not moving or the timeout is reached. /// - /// This is euqal to `wait_while(STATE_RUNNING, timeout)`. + /// This is equal to `wait_while(STATE_RUNNING, timeout)`. /// If the `timeout` is `None` it will wait an infinite time. /// /// # Example diff --git a/src/motors/tacho_motor_macro.rs b/src/motors/tacho_motor_macro.rs index 5e3a1dd..778cf27 100644 --- a/src/motors/tacho_motor_macro.rs +++ b/src/motors/tacho_motor_macro.rs @@ -110,7 +110,7 @@ macro_rules! tacho_motor { /// Returns the number of tacho counts in the full travel of the motor. /// - /// When combined with the count_per_m atribute, + /// When combined with the count_per_m attribute, /// you can use this value to calculate the maximum travel distance of the motor. /// (linear motors only) pub fn get_full_travel_count(&self) -> Ev3Result { @@ -620,7 +620,6 @@ macro_rules! tacho_motor { /// # Ok(()) /// # } /// ``` - #[cfg(target_os = "linux")] pub fn wait(&self, cond: F, timeout: Option) -> bool where F: Fn() -> bool, @@ -651,7 +650,6 @@ macro_rules! tacho_motor { /// # Ok(()) /// # } /// ``` - #[cfg(target_os = "linux")] pub fn wait_while(&self, state: &str, timeout: Option) -> bool { let cond = || { self.get_state() @@ -684,7 +682,6 @@ macro_rules! tacho_motor { /// # Ok(()) /// # } /// ``` - #[cfg(target_os = "linux")] pub fn wait_until(&self, state: &str, timeout: Option) -> bool { let cond = || { self.get_state() @@ -697,7 +694,7 @@ macro_rules! tacho_motor { /// Wait until the motor is not moving or the timeout is reached. /// - /// This is euqal to `wait_while(STATE_RUNNING, timeout)`. + /// This is equal to `wait_while(STATE_RUNNING, timeout)`. /// If the `timeout` is `None` it will wait an infinite time. /// /// # Example @@ -718,7 +715,6 @@ macro_rules! tacho_motor { /// # Ok(()) /// # } /// ``` - #[cfg(target_os = "linux")] pub fn wait_until_not_moving(&self, timeout: Option) -> bool { self.wait_while(Self::STATE_RUNNING, timeout) } diff --git a/src/screen.rs b/src/screen.rs index 3526faa..9c96d34 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -7,13 +7,13 @@ use image::{Rgb, RgbImage}; use crate::Ev3Result; /// Represents the device screen. -/// Advanced drawing operations can be perfomed with the `imageproc` crate. +/// Advanced drawing operations can be performed with the `imageproc` crate. #[cfg(feature = "screen")] #[derive(Debug)] pub struct Screen { /// Direct reference to the framebuffer pub buffer: Framebuffer, - /// Convinience layer to access the framebuffer + /// Convenience layer to access the framebuffer /// For drawing operations the `imageproc` crate can be used. pub image: RgbImage, } diff --git a/src/sensors/color_sensor.rs b/src/sensors/color_sensor.rs index 5277392..a207369 100644 --- a/src/sensors/color_sensor.rs +++ b/src/sensors/color_sensor.rs @@ -85,7 +85,7 @@ impl ColorSensor { self.get_value2() } - /// Red, green and blue componets of the detected color, each in the range 0-1020 + /// Red, green and blue components of the detected color, each in the range 0-1020 pub fn get_rgb(&self) -> Ev3Result<(i32, i32, i32)> { let red = self.get_red()?; let green = self.get_green()?; diff --git a/src/sensors/infrared_sensor.rs b/src/sensors/infrared_sensor.rs index 162a28e..b84cebc 100644 --- a/src/sensors/infrared_sensor.rs +++ b/src/sensors/infrared_sensor.rs @@ -22,7 +22,7 @@ impl InfraredSensor { "lego-sensor", ["lego-ev3-ir"], SensorPort, - "InfraredrSensor", + "InfraredSensor", "in" ); @@ -101,7 +101,7 @@ pub struct RemoteControl { helper: Rc>, } -// Manuelly implement Debug cause `buffer_cache` does not implement Debug. +// Manually implement Debug cause `buffer_cache` does not implement Debug. impl fmt::Debug for RemoteControl { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("RemoteControl") @@ -148,7 +148,7 @@ impl RemoteControl { self.helper.borrow().contains("beacon") } - /// Check for currenly pressed buttons. If the new state differs from the + /// Check for currently pressed buttons. If the new state differs from the /// old state, call the appropriate button event handlers. pub fn process(&self) -> Ev3Result<()> { let buttons = self.sensor.get_value(self.channel)?; diff --git a/src/sensors/sensor.rs b/src/sensors/sensor.rs index 8a3f8f2..900a63d 100644 --- a/src/sensors/sensor.rs +++ b/src/sensors/sensor.rs @@ -58,7 +58,7 @@ pub trait Sensor: Device { /// Returns the polling period of the sensor in milliseconds. /// Returns `-EOPNOTSUPP` if changing polling is not supported. - /// Note: Setting poll_ms too high can cause the input port autodetection to fail. + /// Note: Setting poll_ms too high can cause the input port auto detection to fail. /// If this happens, use the mode attribute of the port to force the port to `nxt-i2c mode`. Values must not be negative. fn get_poll_ms(&self) -> Ev3Result { self.get_attribute("poll_ms").get() @@ -66,7 +66,7 @@ pub trait Sensor: Device { /// Sets the polling period of the sensor in milliseconds. /// Setting to 0 disables polling. - /// Note: Setting poll_ms too high can cause the input port autodetection to fail. + /// Note: Setting poll_ms too high can cause the input port auto detection to fail. /// If this happens, use the mode attribute of the port to force the port to `nxt-i2c mode`. Values must not be negative. fn set_poll_ms(&self, poll_ms: i32) -> Ev3Result<()> { self.get_attribute("poll_ms").set(poll_ms) diff --git a/src/sound.rs b/src/sound.rs index 64ccc1f..326fda8 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -1,7 +1,7 @@ //! Sound-related functions. It can beep, play wav files, or convert text to //! speech. //! -//! Note that all methods of the meodule spawn system processes and return +//! Note that all methods of the module spawn system processes and return //! `std::process::Child` objects. The methods are asynchronous (they return //! immediately after child process was spawned, without waiting for its //! completion), but you can call wait() on the returned result. diff --git a/src/wait.rs b/src/wait.rs index d635e87..20c638a 100644 --- a/src/wait.rs +++ b/src/wait.rs @@ -1,6 +1,7 @@ -//! Utility functions for cpu efficent `wait` commands. +//! Utility functions for cpu efficient `wait` commands. //! Uses the `libc::epoll_wait` that only works on linux systems. +#[cfg(target_os = "linux")] use libc; use std::os::unix::io::RawFd; use std::time::{Duration, Instant}; @@ -66,6 +67,7 @@ where } /// Wrapper for `libc::epoll_wait` +#[cfg(target_os = "linux")] fn wait_file_changes(fd: RawFd, timeout: i32) -> bool { let mut buf: [libc::epoll_event; 10] = [libc::epoll_event { events: 0, u64: 0 }; 10]; @@ -80,3 +82,10 @@ fn wait_file_changes(fd: RawFd, timeout: i32) -> bool { result > 0 } + +/// Stub method for non linux os's +#[cfg(not(target_os = "linux"))] +fn wait_file_changes(_fd: RawFd, _timeout: i32) -> bool { + std::thread::sleep(Duration::from_millis(100)); + false +}