Skip to content

Commit 0d014af

Browse files
committed
re-introduce usage of defmt
i had forgotten to completely remove the dependency in the now reverted commit, thus the optional dependency still created a feature which consuming crates could enable. removing it would be a breaking change. this leaves two options: * remove the `defmt` _dependency_ and add a dummy _feature_ of the same name, issuing a compile warning if it is enabled, eventually removing it in v2.0 if or when that will ever be released * accept the fact that we already have the optional dependency and thus the feature and make use of it this contains a few changes to the reverted commit: * the feature is now called `defmt` rather than `defmt-03` (as that's the name of the implicit feature for the optional dependency) * the `debug` output in `drive` is kept but downgraded to `trace` and slightly reworded (the speed is already present in the command) * the `cfg`-guarded `use` is removed to make the code cleaner This reverts commit d9da807. (with minor adaptations)
1 parent 7c3b229 commit 0d014af

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
fail-fast: false
1515
matrix:
1616
rust: [1.81.0, stable]
17-
features: ['']
17+
features: ['', '--all-features']
1818
runs-on: ubuntu-latest
1919
steps:
2020
- uses: actions/checkout@v4

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
<!-- next-header -->
88
## [Unreleased] - ReleaseDate
9+
10+
### Added
11+
12+
* Re-introduced usage of `defmt` (the dependency was still present, only its usage and the `defmt-03` feature were
13+
removed before the previous release)
14+
915
### Changed
1016

1117
* Updated to `defmt` 1.0 (non-breaking change, backwards compatible with 0.3 through semver trick)

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ license = "MIT OR Apache-2.0"
1212
authors = ["Ralph Ursprung <[email protected]>", "ripytide <[email protected]>"]
1313

1414
[features]
15+
defmt = ["dep:defmt", "embedded-hal/defmt-03"]
1516

1617
[dependencies]
1718
embedded-hal = "1.0"

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ See the documentation for usage examples.
2020
* You plan on using a single motor with the standby feature: use `Motor` and control the standby pin manually
2121
* You plan on using a single motor without the standby feature: use `Motor`
2222

23+
## Optional features
24+
* `defmt`: you can enable this feature to get a `defmt::Format` implementation for all structs & enums in this crate and a `defmt::trace` call for every speed change.
25+
2326
## Examples
2427
A simple example for the STM32F4 microcontrollers is [available](examples/stm32f4-single-motor-example/README.md).
2528

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
//! * You plan on using both motors without the standby feature: use two separate [`Motor`]s
1010
//! * You plan on using a single motor with the standby feature: use [`Motor`] and control the standby pin manually
1111
//! * You plan on using a single motor without the standby feature: use [`Motor`]
12+
//!
13+
//! ## Optional features
14+
//! * `defmt`: you can enable this feature to get a `defmt::Format` implementation for all structs & enums in this crate and a `defmt::trace` call for every speed change.
1215
1316
#![forbid(unsafe_code)]
1417
#![deny(warnings)]
@@ -24,6 +27,7 @@ use embedded_hal::pwm::SetDutyCycle;
2427

2528
/// Defines errors which can happen when calling [`Motor::drive()`].
2629
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
30+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2731
pub enum MotorError<IN1Error, IN2Error, PWMError> {
2832
/// An invalid speed has been defined. The speed must be given as a percentage value between 0 and 100 to be valid.
2933
InvalidSpeed,
@@ -68,6 +72,7 @@ impl<
6872

6973
/// Defines errors which can happen when calling [`Tb6612fng::new()`].
7074
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
75+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7176
pub enum Tb6612fngError<STBYError> {
7277
/// An error in setting the initial output of the standby pin
7378
Standby(STBYError),
@@ -93,6 +98,7 @@ impl<STBYError: Debug + Error + 'static> Error for Tb6612fngError<STBYError> {
9398

9499
/// Defines the possible drive commands.
95100
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
101+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
96102
pub enum DriveCommand {
97103
/// Drive forward with the defined speed (in percentage)
98104
Forward(u8),
@@ -109,6 +115,7 @@ pub enum DriveCommand {
109115
/// Use the [`Motor`] struct directly if you only have one motor.
110116
/// See the crate-level comment for further details on when to use what.
111117
#[derive(Debug)]
118+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
112119
pub struct Tb6612fng<MAIN1, MAIN2, MAPWM, MBIN1, MBIN2, MBPWM, STBY> {
113120
/// The first motor, labelled as 'A' on the chip
114121
pub motor_a: Motor<MAIN1, MAIN2, MAPWM>,
@@ -245,6 +252,7 @@ where
245252
/// This is unaware of the standby pin. If you plan on using both motors and the standby feature then use the [`Tb6612fng`] struct instead.
246253
/// See the crate-level comment for further details on when to use what.
247254
#[derive(Debug)]
255+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
248256
pub struct Motor<IN1, IN2, PWM> {
249257
in1: IN1,
250258
in2: IN2,
@@ -350,6 +358,9 @@ where
350358
}
351359
}
352360

361+
#[cfg(feature = "defmt")]
362+
defmt::trace!("driving: {}", drive_command);
363+
353364
self.pwm
354365
.set_duty_cycle_percent(speed)
355366
.map_err(MotorError::PwmError)?;

0 commit comments

Comments
 (0)