Skip to content

Commit 7612d79

Browse files
committed
refactor: update API to propagate embedded_hal errors
Also: - renamed error types to their struct names - renamed DriveCommand::Backwards to DriveCommand::Backward to match DriveCommand::Forward - removed the `drive_forward`, `drive_backward`, `stop` and `brake` functions as they are duplicates to the `drive` function with the different enum variants and make the API surface larger - cleaned up the changelog
1 parent 0dcfa8a commit 7612d79

File tree

3 files changed

+131
-77
lines changed

3 files changed

+131
-77
lines changed

CHANGELOG.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
<!-- next-header -->
88
## [Unreleased] - ReleaseDate
99
### Changed
10+
1011
* `Motor::new()` and `Driver::new()` methods now set the outputs upon their
1112
initialisation to the documented defaults.
13+
* `Motor::new()` and `Driver::new()` methods now also return errors if they fail
14+
to set their outputs upon initialisation.
1215
* Breaking: update to `embedded-hal` 1.0
16+
* Renamed error types to their struct names
17+
* Renamed `DriveCommand::Backwards` to `DriveCommand::Backward` to match
18+
`DriveCommand::Forward`
19+
20+
### Removed
21+
22+
* Removed the `drive_forward`, `drive_backward`, `stop` and `brake`
23+
functions as they are duplicates to the `drive` function with the
24+
different enum variants and make the API surface larger
1325

1426
## [0.2.0] - 2023-11-28
27+
1528
### Changed
16-
* 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.
29+
30+
* Due to dependency updates the MSRV has been updated from 1.60 to 1.63. This
31+
should only be relevant if you use the `defmt` feature, but we now only test
32+
with 1.63 and not older releases, so it's not guaranteed to work otherwise.
1733
* Breaking: the API was migrated from `embedded-hal:0.2` to `embedded-hal:1.0.0-rc1`.
1834
If your HAL does not yet implement this, then please use the previous release of the library.
1935

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ mod app {
5252
.pwm_hz(Channel3::new(gpiob.pb10), 100.kHz(), &clocks)
5353
.split();
5454
motor_pwm.enable();
55-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
56-
motor.drive_backwards(0).expect("");
55+
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm).unwrap();
56+
motor.drive(DriveCommand::Backward(0)).unwrap();
5757

5858
// set up the button
5959
let mut button = gpioc.pc13.into_pull_down_input();
@@ -103,20 +103,20 @@ mod app {
103103
}
104104
0 => {
105105
*motor_ramp_direction = 1;
106-
DriveCommand::Backwards(1)
106+
DriveCommand::Backward(1)
107107
}
108108
_ => DriveCommand::Forward((*speed as i8 + *motor_ramp_direction) as u8),
109109
},
110-
DriveCommand::Backwards(speed) => match speed {
110+
DriveCommand::Backward(speed) => match speed {
111111
100 => {
112112
*motor_ramp_direction = -1;
113-
DriveCommand::Backwards(99)
113+
DriveCommand::Backward(99)
114114
}
115115
0 => {
116116
*motor_ramp_direction = 1;
117117
DriveCommand::Forward(1)
118118
}
119-
_ => DriveCommand::Backwards((*speed as i8 + *motor_ramp_direction) as u8),
119+
_ => DriveCommand::Backward((*speed as i8 + *motor_ramp_direction) as u8),
120120
},
121121
DriveCommand::Stop | DriveCommand::Brake => {
122122
return;
@@ -140,16 +140,16 @@ mod app {
140140
.lock(|motor| match motor.current_drive_command() {
141141
DriveCommand::Stop => {
142142
defmt::info!("motor stopped => applying brake");
143-
motor.brake();
143+
motor.drive(DriveCommand::Brake).unwrap();
144144
}
145145
DriveCommand::Brake => {
146146
defmt::info!("brake was on => starting the motor again");
147-
motor.drive_backwards(0).expect("");
147+
motor.drive(DriveCommand::Backward(0)).unwrap();
148148
update_motor_speed::spawn_after(100.millis()).ok();
149149
}
150150
_ => {
151151
defmt::info!("was driving so far => stopping the motor");
152-
motor.stop();
152+
motor.drive(DriveCommand::Stop).unwrap();
153153
}
154154
});
155155
}

0 commit comments

Comments
 (0)