Skip to content

Commit 844f01c

Browse files
committed
migrate to embedded-hal=1.0.0.rc1
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).
1 parent 735c10c commit 844f01c

File tree

6 files changed

+47
-52
lines changed

6 files changed

+47
-52
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased] - ReleaseDate
99
### Changed
1010
* 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.
11+
* Breaking: the API was migrated from `embedded-hal:0.2` to `embedded-hal:1.0.0-rc1`.
12+
If your HAL does not yet implement this, then please use the previous release of the library.
1113

1214
<!-- next-url -->
1315
[Unreleased]: https://github.com/rursprung/tb6612fng-rs/compare/v0.1.1...HEAD

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ keywords = ["tb6612fng", "driver", "motor", "controller", "embedded-hal-driver"]
1111
license = "MIT OR Apache-2.0"
1212

1313
[dependencies]
14-
embedded-hal = "0.2"
14+
embedded-hal = "1.0.0-rc.1"
1515

1616
defmt = { version = "0.3", optional = true }
1717

1818
[dev-dependencies]
19-
embedded-hal-mock = "0.10.0-rc.2"
19+
embedded-hal-mock = { version = "0.10.0-rc.2", features = ["eh1"] }

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ A simple example for the STM32F4 microcontrollers is [available](examples/stm32f
3030
For the changelog please see the dedicated [CHANGELOG.md](CHANGELOG.md).
3131

3232
## Roadmap to v1.0.0
33-
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).
33+
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/),
34+
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).
3435
See [the tracking issue](https://github.com/rursprung/tb6612fng-rs/issues/4) for the roadmap to v1.0.0.
3536

3637
## Minimum Supported Rust Version (MSRV)

examples/stm32f4-single-motor-example/Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/stm32f4-single-motor-example/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ mod app {
4646
// set up the motor
4747
let motor_in1 = gpiob.pb5.into_push_pull_output();
4848
let motor_in2 = gpiob.pb4.into_push_pull_output();
49-
let motor_pwm = ctx
49+
let mut motor_pwm = ctx
5050
.device
5151
.TIM2
5252
.pwm_hz(Channel3::new(gpiob.pb10), 100.kHz(), &clocks)
5353
.split();
54+
motor_pwm.enable();
5455
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
5556
motor.drive_backwards(0).expect("");
5657

src/lib.rs

+38-47
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
#[cfg(feature = "defmt")]
2424
use defmt::Format;
25-
use embedded_hal::digital::v2::OutputPin;
26-
use embedded_hal::PwmPin;
25+
use embedded_hal::digital::OutputPin;
26+
use embedded_hal::pwm::SetDutyCycle;
2727

2828
/// Defines errors which can happen while trying to set a speed.
2929
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
@@ -67,10 +67,10 @@ impl<MAIN1, MAIN2, MAPWM, MBIN1, MBIN2, MBPWM, STBY>
6767
where
6868
MAIN1: OutputPin,
6969
MAIN2: OutputPin,
70-
MAPWM: PwmPin<Duty = u16>,
70+
MAPWM: SetDutyCycle,
7171
MBIN1: OutputPin,
7272
MBIN2: OutputPin,
73-
MBPWM: PwmPin<Duty = u16>,
73+
MBPWM: SetDutyCycle,
7474
STBY: OutputPin,
7575
{
7676
/// Instantiate a new [`Tb6612fng`] with the defined pins.
@@ -79,19 +79,19 @@ where
7979
///
8080
/// Usage example:
8181
/// ```
82-
/// # use embedded_hal_mock::eh0::pin::Mock as PinMock;
83-
/// # use embedded_hal_mock::eh0::pin::Transaction as PinTransaction;
82+
/// # use embedded_hal_mock::eh1::pin::Mock as PinMock;
83+
/// # use embedded_hal_mock::eh1::pwm::Mock as PwmMock;
8484
/// # let motor_a_in1 = PinMock::new([]);
8585
/// # let mut motor_a_in1_ = motor_a_in1.clone();
8686
/// # let motor_a_in2 = PinMock::new([]);
8787
/// # let mut motor_a_in2_ = motor_a_in2.clone();
88-
/// # let motor_a_pwm = PinMock::new(&[PinTransaction::enable()]);
88+
/// # let motor_a_pwm = PwmMock::new(&[]);
8989
/// # let mut motor_a_pwm_ = motor_a_pwm.clone();
9090
/// # let motor_b_in1 = PinMock::new([]);
9191
/// # let mut motor_b_in1_ = motor_b_in1.clone();
9292
/// # let motor_b_in2 = PinMock::new([]);
9393
/// # let mut motor_b_in2_ = motor_b_in2.clone();
94-
/// # let motor_b_pwm = PinMock::new(&[PinTransaction::enable()]);
94+
/// # let motor_b_pwm = PwmMock::new(&[]);
9595
/// # let mut motor_b_pwm_ = motor_b_pwm.clone();
9696
/// # let standby = PinMock::new([]);
9797
/// # let mut standby_ = standby.clone();
@@ -161,21 +161,22 @@ impl<IN1, IN2, PWM> Motor<IN1, IN2, PWM>
161161
where
162162
IN1: OutputPin,
163163
IN2: OutputPin,
164-
PWM: PwmPin<Duty = u16>,
164+
PWM: SetDutyCycle,
165165
{
166166
/// Instantiate a new [`Motor`] with the defined pins.
167167
/// This also automatically enables the PWM pin.
168168
/// The initial state of the motor will be [stopped](DriveCommand::Stop).
169169
///
170170
/// Usage example:
171171
/// ```
172-
/// # use embedded_hal_mock::eh0::pin::Mock as PinMock;
173-
/// # use embedded_hal_mock::eh0::pin::Transaction as PinTransaction;
172+
/// # use embedded_hal_mock::eh1::pin::Mock as PinMock;
173+
/// # use embedded_hal_mock::eh1::pwm::Mock as PwmMock;
174+
/// # use embedded_hal_mock::eh1::pin::Transaction as PinTransaction;
174175
/// # let motor_in1 = PinMock::new([]);
175176
/// # let mut motor_in1_ = motor_in1.clone();
176177
/// # let motor_in2 = PinMock::new([]);
177178
/// # let mut motor_in2_ = motor_in2.clone();
178-
/// # let motor_pwm = PinMock::new(&[PinTransaction::enable()]);
179+
/// # let motor_pwm = PwmMock::new([]);
179180
/// # let mut motor_pwm_ = motor_pwm.clone();
180181
/// use tb6612fng::Motor;
181182
///
@@ -189,8 +190,7 @@ where
189190
/// # motor_in2_.done();
190191
/// # motor_pwm_.done();
191192
/// ```
192-
pub fn new(in1: IN1, in2: IN2, mut pwm: PWM) -> Motor<IN1, IN2, PWM> {
193-
pwm.enable();
193+
pub fn new(in1: IN1, in2: IN2, pwm: PWM) -> Motor<IN1, IN2, PWM> {
194194
Motor {
195195
in1,
196196
in2,
@@ -251,19 +251,12 @@ where
251251
}
252252
}
253253

254-
let max_duty = self.pwm.get_max_duty();
255-
256-
let duty = (speed as f32 * (max_duty as f32 / 100.0)) as u16; // speed given in percentage
257-
258254
#[cfg(feature = "defmt")]
259-
defmt::debug!(
260-
"driving {} with duty {} (max duty: {})",
261-
drive_command,
262-
duty,
263-
max_duty
264-
);
255+
defmt::debug!("driving {} with speed {}", drive_command, speed);
265256

266-
self.pwm.set_duty(duty);
257+
self.pwm
258+
.set_duty_cycle_percent(speed)
259+
.map_err(|_| DriveError::InvalidSpeed)?;
267260

268261
self.current_drive_command = drive_command;
269262

@@ -294,23 +287,24 @@ where
294287
#[cfg(test)]
295288
mod tests {
296289
use crate::{DriveCommand, DriveError, Motor};
297-
use embedded_hal_mock::eh0::pin::State::{High, Low};
298-
use embedded_hal_mock::eh0::pin::Transaction as PinTransaction;
299-
use embedded_hal_mock::eh0::pin::{Mock as PinMock, PwmDuty};
290+
use embedded_hal_mock::eh1::pin::Mock as PinMock;
291+
use embedded_hal_mock::eh1::pin::State::{High, Low};
292+
use embedded_hal_mock::eh1::pin::Transaction as PinTransaction;
293+
use embedded_hal_mock::eh1::pwm::Mock as PwmMock;
294+
use embedded_hal_mock::eh1::pwm::Transaction as PwmTransaction;
300295

301296
#[test]
302297
fn test_motor_stop() {
303298
let max_duty = 100;
304299
let motor_in1_expectations = [PinTransaction::set(Low)];
305300
let motor_in2_expectations = [PinTransaction::set(Low)];
306301
let motor_pwm_expectations = [
307-
PinTransaction::enable(),
308-
PinTransaction::get_max_duty(max_duty),
309-
PinTransaction::set_duty(0),
302+
PwmTransaction::get_max_duty_cycle(max_duty),
303+
PwmTransaction::set_duty_cycle(0),
310304
];
311305
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
312306
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
313-
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
307+
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
314308

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

@@ -330,13 +324,12 @@ mod tests {
330324
let motor_in1_expectations = [PinTransaction::set(High)];
331325
let motor_in2_expectations = [PinTransaction::set(High)];
332326
let motor_pwm_expectations = [
333-
PinTransaction::enable(),
334-
PinTransaction::get_max_duty(max_duty),
335-
PinTransaction::set_duty(0),
327+
PwmTransaction::get_max_duty_cycle(max_duty),
328+
PwmTransaction::set_duty_cycle(0),
336329
];
337330
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
338331
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
339-
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
332+
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
340333

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

@@ -357,13 +350,12 @@ mod tests {
357350
let motor_in1_expectations = [PinTransaction::set(High)];
358351
let motor_in2_expectations = [PinTransaction::set(Low)];
359352
let motor_pwm_expectations = [
360-
PinTransaction::enable(),
361-
PinTransaction::get_max_duty(max_duty),
362-
PinTransaction::set_duty(speed as PwmDuty),
353+
PwmTransaction::get_max_duty_cycle(max_duty),
354+
PwmTransaction::set_duty_cycle(speed as u16),
363355
];
364356
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
365357
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
366-
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
358+
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
367359

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

@@ -380,17 +372,16 @@ mod tests {
380372
#[test]
381373
fn test_motor_drive_backwards() {
382374
let max_duty = 100;
383-
let speed: u8 = 100;
375+
let speed = 100;
384376
let motor_in1_expectations = [PinTransaction::set(Low)];
385377
let motor_in2_expectations = [PinTransaction::set(High)];
386378
let motor_pwm_expectations = [
387-
PinTransaction::enable(),
388-
PinTransaction::get_max_duty(max_duty),
389-
PinTransaction::set_duty(speed as PwmDuty),
379+
PwmTransaction::get_max_duty_cycle(max_duty),
380+
PwmTransaction::set_duty_cycle(speed as u16),
390381
];
391382
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
392383
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
393-
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
384+
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
394385

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

@@ -408,10 +399,10 @@ mod tests {
408399
fn test_motor_drive_invalid_speed() {
409400
let motor_in1_expectations = [];
410401
let motor_in2_expectations = [];
411-
let motor_pwm_expectations = [PinTransaction::enable()];
402+
let motor_pwm_expectations = [];
412403
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
413404
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
414-
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
405+
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
415406

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

0 commit comments

Comments
 (0)