Skip to content

Commit

Permalink
migrate to embedded-hal=1.0.0.rc1
Browse files Browse the repository at this point in the history
as alpha.10 of embedded-hal has added `SetDutyCycle` as the equivalent /
successor of the old `PwmPin` support for it can now be added here.

since e-h 1.0.0 will be released at the end of this year it's safe to
just directly migrate to the RC1 instead of adding it as an optional
support (requiring another breaking change in a later step which would
then remove e-h 0.2 support).
  • Loading branch information
rursprung committed Nov 25, 2023
1 parent a152edb commit 7a4e5b3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 52 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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.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.

<!-- next-url -->
[Unreleased]: https://github.com/rursprung/tb6612fng-rs/compare/v0.1.1...HEAD
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.10.0-rc.2"
embedded-hal-mock = { version = "0.10.0-rc.2", features = ["eh1"] }
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/stm32f4-single-motor-example/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/stm32f4-single-motor-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("");

Expand Down
85 changes: 38 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

#[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)]
Expand Down Expand Up @@ -67,10 +67,10 @@ impl<MAIN1, MAIN2, MAPWM, MBIN1, MBIN2, MBPWM, STBY>
where
MAIN1: OutputPin,
MAIN2: OutputPin,
MAPWM: PwmPin<Duty = u16>,
MAPWM: SetDutyCycle,
MBIN1: OutputPin,
MBIN2: OutputPin,
MBPWM: PwmPin<Duty = u16>,
MBPWM: SetDutyCycle,
STBY: OutputPin,
{
/// Instantiate a new [`Tb6612fng`] with the defined pins.
Expand All @@ -79,19 +79,19 @@ where
///
/// Usage example:
/// ```
/// # use embedded_hal_mock::eh0::pin::Mock as PinMock;
/// # use embedded_hal_mock::eh0::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 mut motor_a_in2_ = motor_a_in2.clone();
/// # let motor_a_pwm = PinMock::new(&[PinTransaction::enable()]);
/// # 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 mut motor_b_in2_ = motor_b_in2.clone();
/// # let motor_b_pwm = PinMock::new(&[PinTransaction::enable()]);
/// # let motor_b_pwm = PwmMock::new(&[]);
/// # let mut motor_b_pwm_ = motor_b_pwm.clone();
/// # let standby = PinMock::new([]);
/// # let mut standby_ = standby.clone();
Expand Down Expand Up @@ -161,21 +161,22 @@ impl<IN1, IN2, PWM> Motor<IN1, IN2, PWM>
where
IN1: OutputPin,
IN2: OutputPin,
PWM: PwmPin<Duty = u16>,
PWM: SetDutyCycle,
{
/// Instantiate a new [`Motor`] with the defined pins.
/// This also automatically enables the PWM pin.
/// The initial state of the motor will be [stopped](DriveCommand::Stop).
///
/// Usage example:
/// ```
/// # use embedded_hal_mock::eh0::pin::Mock as PinMock;
/// # use embedded_hal_mock::eh0::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 mut motor_in2_ = motor_in2.clone();
/// # let motor_pwm = PinMock::new(&[PinTransaction::enable()]);
/// # let motor_pwm = PwmMock::new([]);
/// # let mut motor_pwm_ = motor_pwm.clone();
/// use tb6612fng::Motor;
///
Expand All @@ -189,8 +190,7 @@ where
/// # motor_in2_.done();
/// # motor_pwm_.done();
/// ```
pub fn new(in1: IN1, in2: IN2, mut pwm: PWM) -> Motor<IN1, IN2, PWM> {
pwm.enable();
pub fn new(in1: IN1, in2: IN2, pwm: PWM) -> Motor<IN1, IN2, PWM> {
Motor {
in1,
in2,
Expand Down Expand Up @@ -251,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;

Expand Down Expand Up @@ -294,23 +287,24 @@ where
#[cfg(test)]
mod tests {
use crate::{DriveCommand, DriveError, Motor};
use embedded_hal_mock::eh0::pin::State::{High, Low};
use embedded_hal_mock::eh0::pin::Transaction as PinTransaction;
use embedded_hal_mock::eh0::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() {
let max_duty = 100;
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 mut motor_in1 = PinMock::new(&motor_in1_expectations);
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);

let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());

Expand All @@ -330,13 +324,12 @@ 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 mut motor_in1 = PinMock::new(&motor_in1_expectations);
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);

let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());

Expand All @@ -357,13 +350,12 @@ 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 mut motor_in1 = PinMock::new(&motor_in1_expectations);
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);

let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());

Expand All @@ -380,17 +372,16 @@ mod tests {
#[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 mut motor_in1 = PinMock::new(&motor_in1_expectations);
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);

let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());

Expand All @@ -408,10 +399,10 @@ mod tests {
fn test_motor_drive_invalid_speed() {
let motor_in1_expectations = [];
let motor_in2_expectations = [];
let motor_pwm_expectations = [PinTransaction::enable()];
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 = PinMock::new(&motor_pwm_expectations);
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);

let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());

Expand Down

0 comments on commit 7a4e5b3

Please sign in to comment.