diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 13559be..fb72dc9 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -10,7 +10,7 @@ jobs: strategy: fail-fast: false matrix: - rust: [1.62.0, stable] + rust: [1.63.0, stable] features: ['', '--all-features'] runs-on: ubuntu-latest steps: diff --git a/CHANGELOG.md b/CHANGELOG.md index c11b074..b1c4118 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - ReleaseDate ### Changed -* Due to dependency updates the MSRV has been updated from 1.60 to 1.62. This should only be relevant if you use the `defmt` feature, but we now only test with 1.62 and not older releases, so it's not guaranteed to work otherwise. +* Due to dependency updates the MSRV has been updated from 1.60 to 1.63. This should only be relevant if you use the `defmt` feature, but we now only test with 1.63 and not older releases, so it's not guaranteed to work otherwise. +* Breaking: the API was migrated from `embedded-hal:0.2` to `embedded-hal:1.0.0-rc1`. + If your HAL does not yet implement this, then please use the previous release of the library. [Unreleased]: https://github.com/rursprung/tb6612fng-rs/compare/v0.1.1...HEAD diff --git a/Cargo.toml b/Cargo.toml index 0fbf67d..d1431b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "tb6612fng" version = "0.1.0" edition = "2021" -rust-version = "1.62" +rust-version = "1.63" description = "A `no_std` driver for the TB6612FNG motor driver." repository = "https://github.com/rursprung/tb6612fng-rs" @@ -11,9 +11,9 @@ keywords = ["tb6612fng", "driver", "motor", "controller", "embedded-hal-driver"] license = "MIT OR Apache-2.0" [dependencies] -embedded-hal = "0.2" +embedded-hal = "1.0.0-rc.1" defmt = { version = "0.3", optional = true } [dev-dependencies] -embedded-hal-mock = "0.9" +embedded-hal-mock = { version = "0.10.0-rc.2", features = ["eh1"] } diff --git a/README.md b/README.md index c2acd30..be51a77 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,8 @@ A simple example for the STM32F4 microcontrollers is [available](examples/stm32f For the changelog please see the dedicated [CHANGELOG.md](CHANGELOG.md). ## Roadmap to v1.0.0 -This crate is already stable, however it's based on a 0.x version of [`embedded-hal`](https://github.com/rust-embedded/embedded-hal/), making the API unstable (the change from 0.x to 1.x of e-h will be breaking). +This crate is already stable, however it's based on athe v1.0.0 release candidate version of [`embedded-hal`](https://github.com/rust-embedded/embedded-hal/), +making the API unstable (the change from 1.0.0-rc.1 to 1.0.0 of e-h will be breaking from a dependency management point of view). See [the tracking issue](https://github.com/rursprung/tb6612fng-rs/issues/4) for the roadmap to v1.0.0. ## Minimum Supported Rust Version (MSRV) diff --git a/examples/stm32f4-single-motor-example/Cargo.lock b/examples/stm32f4-single-motor-example/Cargo.lock index 96c471f..daee8ee 100644 --- a/examples/stm32f4-single-motor-example/Cargo.lock +++ b/examples/stm32f4-single-motor-example/Cargo.lock @@ -46,9 +46,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "byteorder" -version = "1.4.3" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cortex-m" @@ -113,9 +113,9 @@ dependencies = [ [[package]] name = "critical-section" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6548a0ad5d2549e111e1f6a11a6c2e2d00ce6a3dafe22948d67c2b443f775e52" +checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216" [[package]] name = "defmt" @@ -129,15 +129,15 @@ dependencies = [ [[package]] name = "defmt-macros" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4abc4821bd84d3d8f49945ddb24d029be9385ed9b77c99bf2f6296847a6a9f0" +checksum = "54f0216f6c5acb5ae1a47050a6645024e6edafc2ee32d421955eccfef12ef92e" dependencies = [ "defmt-parser", "proc-macro-error", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.39", ] [[package]] @@ -159,6 +159,15 @@ dependencies = [ "defmt", ] +[[package]] +name = "deranged" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f32d04922c60427da6f9fef14d042d9edddef64cb9d4ce0d64d0685fbeb1fd3" +dependencies = [ + "powerfmt", +] + [[package]] name = "embedded-dma" version = "0.2.0" @@ -217,14 +226,14 @@ checksum = "f95e2801cd355d4a1a3e3953ce6ee5ae9603a5c833455343a8bfe3f44d418246" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.39", ] [[package]] name = "fugit" -version = "0.3.6" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab17bb279def6720d058cb6c052249938e7f99260ab534879281a95367a87e5" +checksum = "17186ad64927d5ac8f02c1e77ccefa08ccd9eaa314d5a4772278aa204a22f7e7" dependencies = [ "gcd", ] @@ -285,9 +294,9 @@ dependencies = [ [[package]] name = "lock_api" -version = "0.4.9" +version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" dependencies = [ "autocfg", "scopeguard", @@ -318,6 +327,12 @@ dependencies = [ "defmt", ] +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -344,18 +359,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.56" +version = "1.0.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.27" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] @@ -405,14 +420,14 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" dependencies = [ - "semver 1.0.17", + "semver 1.0.20", ] [[package]] name = "scopeguard" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "semver" @@ -425,9 +440,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.17" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" +checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" [[package]] name = "semver-parser" @@ -515,9 +530,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.15" +version = "2.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" dependencies = [ "proc-macro2", "quote", @@ -539,49 +554,51 @@ dependencies = [ name = "tb6612fng" version = "0.1.0" dependencies = [ - "embedded-hal 0.2.7", + "embedded-hal 1.0.0-rc.1", ] [[package]] name = "thiserror" -version = "1.0.40" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" +checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.40" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" +checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.15", + "syn 2.0.39", ] [[package]] name = "time" -version = "0.3.21" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f3403384eaacbca9923fa06940178ac13e4edb725486d70e8e15881d0c836cc" +checksum = "c4a34ab300f2dee6e562c10a046fc05e358b29f9bf92277f30c3c8d82275f6f5" dependencies = [ + "deranged", + "powerfmt", "time-core", ] [[package]] name = "time-core" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "unicode-ident" -version = "1.0.8" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "vcell" @@ -603,9 +620,9 @@ checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" [[package]] name = "volatile-register" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ee8f19f9d74293faf70901bc20ad067dc1ad390d2cbf1e3f75f721ffee908b6" +checksum = "de437e2a6208b014ab52972a27e59b33fa2920d3e00fe05026167a1c509d19cc" dependencies = [ "vcell", ] diff --git a/examples/stm32f4-single-motor-example/src/main.rs b/examples/stm32f4-single-motor-example/src/main.rs index f24e0a4..06f0ea8 100644 --- a/examples/stm32f4-single-motor-example/src/main.rs +++ b/examples/stm32f4-single-motor-example/src/main.rs @@ -46,11 +46,12 @@ mod app { // set up the motor let motor_in1 = gpiob.pb5.into_push_pull_output(); let motor_in2 = gpiob.pb4.into_push_pull_output(); - let motor_pwm = ctx + let mut motor_pwm = ctx .device .TIM2 .pwm_hz(Channel3::new(gpiob.pb10), 100.kHz(), &clocks) .split(); + motor_pwm.enable(); let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm); motor.drive_backwards(0).expect(""); diff --git a/src/lib.rs b/src/lib.rs index 2818818..c37dbfa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,16 +14,16 @@ //! * `defmt`: you can enable the `defmt` feature to get a `defmt::Format` implementation for all structs & enums in this crate and a `defmt::debug` call for every speed change. #![forbid(unsafe_code)] -#![forbid(warnings)] +#![deny(warnings)] #![forbid(missing_docs)] #![forbid(missing_debug_implementations)] -#![forbid(unused)] +#![deny(unused)] #![no_std] #[cfg(feature = "defmt")] use defmt::Format; -use embedded_hal::digital::v2::OutputPin; -use embedded_hal::PwmPin; +use embedded_hal::digital::OutputPin; +use embedded_hal::pwm::SetDutyCycle; /// Defines errors which can happen while trying to set a speed. #[derive(PartialEq, Eq, Debug, Copy, Clone)] @@ -67,10 +67,10 @@ impl where MAIN1: OutputPin, MAIN2: OutputPin, - MAPWM: PwmPin, + MAPWM: SetDutyCycle, MBIN1: OutputPin, MBIN2: OutputPin, - MBPWM: PwmPin, + MBPWM: SetDutyCycle, STBY: OutputPin, { /// Instantiate a new [`Tb6612fng`] with the defined pins. @@ -79,17 +79,22 @@ where /// /// Usage example: /// ``` - /// # use embedded_hal_mock::pin::Mock as PinMock; - /// # use embedded_hal_mock::pin::Transaction as PinTransaction; + /// # use embedded_hal_mock::eh1::pin::Mock as PinMock; + /// # use embedded_hal_mock::eh1::pwm::Mock as PwmMock; /// # let motor_a_in1 = PinMock::new([]); + /// # let mut motor_a_in1_ = motor_a_in1.clone(); /// # let motor_a_in2 = PinMock::new([]); - /// # let motor_a_pwm_expectations = [PinTransaction::enable()]; - /// # let motor_a_pwm = PinMock::new(&motor_a_pwm_expectations); + /// # let mut motor_a_in2_ = motor_a_in2.clone(); + /// # let motor_a_pwm = PwmMock::new(&[]); + /// # let mut motor_a_pwm_ = motor_a_pwm.clone(); /// # let motor_b_in1 = PinMock::new([]); + /// # let mut motor_b_in1_ = motor_b_in1.clone(); /// # let motor_b_in2 = PinMock::new([]); - /// # let motor_b_pwm_expectations = [PinTransaction::enable()]; - /// # let motor_b_pwm = PinMock::new(&motor_a_pwm_expectations); + /// # let mut motor_b_in2_ = motor_b_in2.clone(); + /// # let motor_b_pwm = PwmMock::new(&[]); + /// # let mut motor_b_pwm_ = motor_b_pwm.clone(); /// # let standby = PinMock::new([]); + /// # let mut standby_ = standby.clone(); /// use tb6612fng::Tb6612fng; /// /// let controller = Tb6612fng::new( @@ -101,6 +106,14 @@ where /// motor_b_pwm, /// standby, /// ); + /// + /// # motor_a_in1_.done(); + /// # motor_a_in2_.done(); + /// # motor_a_pwm_.done(); + /// # motor_b_in1_.done(); + /// # motor_b_in2_.done(); + /// # motor_b_pwm_.done(); + /// # standby_.done(); /// ``` pub fn new( motor_a_in1: MAIN1, @@ -148,7 +161,7 @@ impl Motor where IN1: OutputPin, IN2: OutputPin, - PWM: PwmPin, + PWM: SetDutyCycle, { /// Instantiate a new [`Motor`] with the defined pins. /// This also automatically enables the PWM pin. @@ -156,12 +169,15 @@ where /// /// Usage example: /// ``` - /// # use embedded_hal_mock::pin::Mock as PinMock; - /// # use embedded_hal_mock::pin::Transaction as PinTransaction; + /// # use embedded_hal_mock::eh1::pin::Mock as PinMock; + /// # use embedded_hal_mock::eh1::pwm::Mock as PwmMock; + /// # use embedded_hal_mock::eh1::pin::Transaction as PinTransaction; /// # let motor_in1 = PinMock::new([]); + /// # let mut motor_in1_ = motor_in1.clone(); /// # let motor_in2 = PinMock::new([]); - /// # let motor_pwm_expectations = [PinTransaction::enable()]; - /// # let motor_pwm = PinMock::new(&motor_pwm_expectations); + /// # let mut motor_in2_ = motor_in2.clone(); + /// # let motor_pwm = PwmMock::new([]); + /// # let mut motor_pwm_ = motor_pwm.clone(); /// use tb6612fng::Motor; /// /// let motor = Motor::new( @@ -169,9 +185,12 @@ where /// motor_in2, /// motor_pwm, /// ); + /// + /// # motor_in1_.done(); + /// # motor_in2_.done(); + /// # motor_pwm_.done(); /// ``` - pub fn new(in1: IN1, in2: IN2, mut pwm: PWM) -> Motor { - pwm.enable(); + pub fn new(in1: IN1, in2: IN2, pwm: PWM) -> Motor { Motor { in1, in2, @@ -232,19 +251,12 @@ where } } - let max_duty = self.pwm.get_max_duty(); - - let duty = (speed as f32 * (max_duty as f32 / 100.0)) as u16; // speed given in percentage - #[cfg(feature = "defmt")] - defmt::debug!( - "driving {} with duty {} (max duty: {})", - drive_command, - duty, - max_duty - ); + defmt::debug!("driving {} with speed {}", drive_command, speed); - self.pwm.set_duty(duty); + self.pwm + .set_duty_cycle_percent(speed) + .map_err(|_| DriveError::InvalidSpeed)?; self.current_drive_command = drive_command; @@ -275,9 +287,11 @@ where #[cfg(test)] mod tests { use crate::{DriveCommand, DriveError, Motor}; - use embedded_hal_mock::pin::State::{High, Low}; - use embedded_hal_mock::pin::Transaction as PinTransaction; - use embedded_hal_mock::pin::{Mock as PinMock, PwmDuty}; + use embedded_hal_mock::eh1::pin::Mock as PinMock; + use embedded_hal_mock::eh1::pin::State::{High, Low}; + use embedded_hal_mock::eh1::pin::Transaction as PinTransaction; + use embedded_hal_mock::eh1::pwm::Mock as PwmMock; + use embedded_hal_mock::eh1::pwm::Transaction as PwmTransaction; #[test] fn test_motor_stop() { @@ -285,20 +299,23 @@ mod tests { let motor_in1_expectations = [PinTransaction::set(Low)]; let motor_in2_expectations = [PinTransaction::set(Low)]; let motor_pwm_expectations = [ - PinTransaction::enable(), - PinTransaction::get_max_duty(max_duty), - PinTransaction::set_duty(0), + PwmTransaction::get_max_duty_cycle(max_duty), + PwmTransaction::set_duty_cycle(0), ]; - let motor_in1 = PinMock::new(&motor_in1_expectations); - let motor_in2 = PinMock::new(&motor_in2_expectations); - let motor_pwm = PinMock::new(&motor_pwm_expectations); + let mut motor_in1 = PinMock::new(&motor_in1_expectations); + let mut motor_in2 = PinMock::new(&motor_in2_expectations); + let mut motor_pwm = PwmMock::new(&motor_pwm_expectations); - let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm); + let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone()); motor.stop(); assert_eq!(*motor.current_drive_command(), DriveCommand::Stop); assert_eq!(motor.current_speed(), 0); + + motor_in1.done(); + motor_in2.done(); + motor_pwm.done(); } #[test] @@ -307,20 +324,23 @@ mod tests { let motor_in1_expectations = [PinTransaction::set(High)]; let motor_in2_expectations = [PinTransaction::set(High)]; let motor_pwm_expectations = [ - PinTransaction::enable(), - PinTransaction::get_max_duty(max_duty), - PinTransaction::set_duty(0), + PwmTransaction::get_max_duty_cycle(max_duty), + PwmTransaction::set_duty_cycle(0), ]; - let motor_in1 = PinMock::new(&motor_in1_expectations); - let motor_in2 = PinMock::new(&motor_in2_expectations); - let motor_pwm = PinMock::new(&motor_pwm_expectations); + let mut motor_in1 = PinMock::new(&motor_in1_expectations); + let mut motor_in2 = PinMock::new(&motor_in2_expectations); + let mut motor_pwm = PwmMock::new(&motor_pwm_expectations); - let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm); + let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone()); motor.brake(); assert_eq!(*motor.current_drive_command(), DriveCommand::Brake); assert_eq!(motor.current_speed(), 0); + + motor_in1.done(); + motor_in2.done(); + motor_pwm.done(); } #[test] @@ -330,60 +350,61 @@ mod tests { let motor_in1_expectations = [PinTransaction::set(High)]; let motor_in2_expectations = [PinTransaction::set(Low)]; let motor_pwm_expectations = [ - PinTransaction::enable(), - PinTransaction::get_max_duty(max_duty), - PinTransaction::set_duty(speed as PwmDuty), + PwmTransaction::get_max_duty_cycle(max_duty), + PwmTransaction::set_duty_cycle(speed as u16), ]; - let motor_in1 = PinMock::new(&motor_in1_expectations); - let motor_in2 = PinMock::new(&motor_in2_expectations); - let motor_pwm = PinMock::new(&motor_pwm_expectations); + let mut motor_in1 = PinMock::new(&motor_in1_expectations); + let mut motor_in2 = PinMock::new(&motor_in2_expectations); + let mut motor_pwm = PwmMock::new(&motor_pwm_expectations); - let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm); + let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone()); motor.drive_forward(speed).expect("speed can be set"); assert_eq!(*motor.current_drive_command(), DriveCommand::Forward(100)); assert_eq!(motor.current_speed(), speed as i8); + + motor_in1.done(); + motor_in2.done(); + motor_pwm.done(); } #[test] fn test_motor_drive_backwards() { let max_duty = 100; - let speed: u8 = 100; + let speed = 100; let motor_in1_expectations = [PinTransaction::set(Low)]; let motor_in2_expectations = [PinTransaction::set(High)]; let motor_pwm_expectations = [ - PinTransaction::enable(), - PinTransaction::get_max_duty(max_duty), - PinTransaction::set_duty(speed as PwmDuty), + PwmTransaction::get_max_duty_cycle(max_duty), + PwmTransaction::set_duty_cycle(speed as u16), ]; - let motor_in1 = PinMock::new(&motor_in1_expectations); - let motor_in2 = PinMock::new(&motor_in2_expectations); - let motor_pwm = PinMock::new(&motor_pwm_expectations); + let mut motor_in1 = PinMock::new(&motor_in1_expectations); + let mut motor_in2 = PinMock::new(&motor_in2_expectations); + let mut motor_pwm = PwmMock::new(&motor_pwm_expectations); - let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm); + let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone()); motor.drive_backwards(speed).expect("speed can be set"); assert_eq!(*motor.current_drive_command(), DriveCommand::Backwards(100)); assert_eq!(motor.current_speed(), -(speed as i8)); + + motor_in1.done(); + motor_in2.done(); + motor_pwm.done(); } #[test] fn test_motor_drive_invalid_speed() { - let max_duty = 100; - let motor_in1_expectations = [PinTransaction::set(Low)]; - let motor_in2_expectations = [PinTransaction::set(High)]; - let motor_pwm_expectations = [ - PinTransaction::enable(), - PinTransaction::get_max_duty(max_duty), - PinTransaction::set_duty(100), - ]; - let motor_in1 = PinMock::new(&motor_in1_expectations); - let motor_in2 = PinMock::new(&motor_in2_expectations); - let motor_pwm = PinMock::new(&motor_pwm_expectations); + let motor_in1_expectations = []; + let motor_in2_expectations = []; + let motor_pwm_expectations = []; + let mut motor_in1 = PinMock::new(&motor_in1_expectations); + let mut motor_in2 = PinMock::new(&motor_in2_expectations); + let mut motor_pwm = PwmMock::new(&motor_pwm_expectations); - let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm); + let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone()); let current_drive_command = motor.current_drive_command().clone(); let current_speed = motor.current_speed(); @@ -398,5 +419,9 @@ mod tests { // this should still be what was set before the invalid command assert_eq!(*motor.current_drive_command(), current_drive_command); assert_eq!(motor.current_speed(), current_speed); + + motor_in1.done(); + motor_in2.done(); + motor_pwm.done(); } }