Skip to content

Commit f163f88

Browse files
committed
Deprecate Toggle use Switch
1 parent 4fe0f9b commit f163f88

File tree

6 files changed

+62
-32
lines changed

6 files changed

+62
-32
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919
- Update `stm32f3` pac to v0.15.1 ([#335])
2020
- Update `bxcan` pac to v0.7.0 ([#335])
2121
- The MSRV was bumped to 1.59 ([#335])
22+
- Deprecate `Toggle` enum and use `Switch` instead for better naming purposes
23+
([#334])
24+
- Add `impl From<Toggle> for Switch` to reduce churn.
2225

2326
## [v0.9.1] - 2022-09-07
2427

@@ -571,6 +574,7 @@ let clocks = rcc
571574
[filter]: https://defmt.ferrous-systems.com/filtering.html
572575

573576
[#335]: https://github.com/stm32-rs/stm32f3xx-hal/pull/335
577+
[#334]: https://github.com/stm32-rs/stm32f3xx-hal/pull/334
574578
[#322]: https://github.com/stm32-rs/stm32f3xx-hal/pull/322
575579
[#318]: https://github.com/stm32-rs/stm32f3xx-hal/pull/318
576580
[#317]: https://github.com/stm32-rs/stm32f3xx-hal/pull/317

examples/serial_echo_rtic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod app {
1919
pac,
2020
prelude::*,
2121
serial::{Event, Serial},
22-
Toggle,
22+
Switch,
2323
};
2424
use systick_monotonic::*;
2525

@@ -81,7 +81,7 @@ mod app {
8181
pins.1.internal_pull_up(&mut gpioa.pupdr, true);
8282
let mut serial: SerialType =
8383
Serial::new(cx.device.USART1, pins, 19200.Bd(), clocks, &mut rcc.apb2);
84-
serial.configure_interrupt(Event::ReceiveDataRegisterNotEmpty, Toggle::On);
84+
serial.configure_interrupt(Event::ReceiveDataRegisterNotEmpty, Switch::On);
8585

8686
rprintln!("post init");
8787

@@ -100,12 +100,12 @@ mod app {
100100

101101
if serial.is_event_triggered(Event::ReceiveDataRegisterNotEmpty) {
102102
dir.set_high().unwrap();
103-
serial.configure_interrupt(Event::ReceiveDataRegisterNotEmpty, Toggle::Off);
103+
serial.configure_interrupt(Event::ReceiveDataRegisterNotEmpty, Switch::Off);
104104
match serial.read() {
105105
Ok(byte) => {
106106
serial.write(byte).unwrap();
107107
rprintln!("{:?}", char::from_u32(byte.into()).unwrap_or('?'));
108-
serial.configure_interrupt(Event::TransmissionComplete, Toggle::On);
108+
serial.configure_interrupt(Event::TransmissionComplete, Switch::On);
109109
}
110110
Err(_error) => rprintln!("irq error"),
111111
};

src/adc.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{
3030
pac::{self, adc1, Interrupt},
3131
rcc::{Clocks, Enable, AHB},
3232
time::{duration::Microseconds, fixed_point::FixedPoint, rate::Hertz},
33-
Toggle,
33+
Switch,
3434
};
3535

3636
use crate::pac::{adc1_2, adc1_2::ccr::CKMODE_A};
@@ -493,7 +493,7 @@ where
493493
// TODO(Sh3Rm4n): Would about concurrent calibrations between related ADCs?
494494
// int_ref: &mut VoltageInternalReference<ADC>,
495495
) {
496-
self.configure_voltage_regulator(Toggle::On, clocks);
496+
self.configure_voltage_regulator(Switch::On, clocks);
497497

498498
self.reg
499499
.cr
@@ -512,21 +512,21 @@ where
512512
asm::delay(cpu_cycles);
513513

514514
// When the internal voltage regulator is disabled, the internal analog calibration is kept
515-
self.configure_voltage_regulator(Toggle::Off, clocks);
515+
self.configure_voltage_regulator(Switch::Off, clocks);
516516
}
517517

518518
/// Enable the interal voltage generator Blocks until regulator is enabled.
519-
fn configure_voltage_regulator(&mut self, toggle: impl Into<Toggle>, clocks: &Clocks) {
519+
fn configure_voltage_regulator(&mut self, toggle: impl Into<Switch>, clocks: &Clocks) {
520520
let already_on = self.reg.cr.read().advregen().is_enabled();
521521
let toggle = toggle.into();
522522
self.reg.cr.modify(|_, w| w.advregen().intermediate());
523523
self.reg.cr.modify(|_, w| {
524524
w.advregen().variant(match toggle {
525-
Toggle::On => adc1::cr::ADVREGEN_A::Enabled,
526-
Toggle::Off => adc1::cr::ADVREGEN_A::Disabled,
525+
Switch::On => adc1::cr::ADVREGEN_A::Enabled,
526+
Switch::Off => adc1::cr::ADVREGEN_A::Disabled,
527527
})
528528
});
529-
if toggle == Toggle::On && !already_on {
529+
if toggle == Switch::On && !already_on {
530530
let wait = MAX_ADVREGEN_STARTUP.integer()
531531
* clocks.sysclk().integer()
532532
* <Microseconds as FixedPoint>::SCALING_FACTOR;
@@ -1103,20 +1103,20 @@ where
11031103
/// Enable the interrupt for the specified [`Event`].
11041104
#[inline]
11051105
pub fn enable_interrupt(&mut self, event: Event) {
1106-
self.configure_interrupt(event, Toggle::On);
1106+
self.configure_interrupt(event, Switch::On);
11071107
}
11081108

11091109
/// Disable the interrupt for the specified [`Event`].
11101110
#[inline]
11111111
pub fn disable_interrupt(&mut self, event: Event) {
1112-
self.configure_interrupt(event, Toggle::Off);
1112+
self.configure_interrupt(event, Switch::Off);
11131113
}
11141114

11151115
/// Enable or disable the interrupt for the specified [`Event`].
11161116
#[inline]
1117-
pub fn configure_interrupt(&mut self, event: Event, enable: impl Into<Toggle>) {
1118-
// Do a round way trip to be convert Into<Toggle> -> bool
1119-
let enable: Toggle = enable.into();
1117+
pub fn configure_interrupt(&mut self, event: Event, enable: impl Into<Switch>) {
1118+
// Do a round way trip to be convert Into<Switch> -> bool
1119+
let enable: Switch = enable.into();
11201120
let enable: bool = enable.into();
11211121
match event {
11221122
Event::AdcReady => self.reg.ier.modify(|_, w| w.adrdyie().bit(enable)),

src/gpio.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::{
6666
hal::digital::v2::OutputPin,
6767
pac::{Interrupt, EXTI},
6868
rcc::AHB,
69-
Toggle,
69+
Switch,
7070
};
7171

7272
use crate::hal::digital::v2::{toggleable, InputPin, StatefulOutputPin};
@@ -623,10 +623,10 @@ where
623623
///
624624
/// Remeber to also configure the interrupt pin on
625625
/// the SysCfg site, with [`crate::syscfg::SysCfg::select_exti_interrupt_source()`]
626-
pub fn configure_interrupt(&mut self, exti: &mut EXTI, enable: impl Into<Toggle>) {
626+
pub fn configure_interrupt(&mut self, exti: &mut EXTI, enable: impl Into<Switch>) {
627627
const BITWIDTH: u8 = 1;
628628

629-
let enable: Toggle = enable.into();
629+
let enable: Switch = enable.into();
630630
let enable: bool = enable.into();
631631

632632
let index = self.index.index();
@@ -642,12 +642,12 @@ where
642642
/// Remeber to also configure the interrupt pin on
643643
/// the SysCfg site, with [`crate::syscfg::SysCfg::select_exti_interrupt_source()`]
644644
pub fn enable_interrupt(&mut self, exti: &mut EXTI) {
645-
self.configure_interrupt(exti, Toggle::On)
645+
self.configure_interrupt(exti, Switch::On)
646646
}
647647

648648
/// Disable external interrupts from this pin
649649
pub fn disable_interrupt(&mut self, exti: &mut EXTI) {
650-
self.configure_interrupt(exti, Toggle::Off)
650+
self.configure_interrupt(exti, Switch::Off)
651651
}
652652

653653
/// Clear the interrupt pending bit for this pin

src/lib.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,24 +274,50 @@ cfg_if! {
274274
// TODO: Maybe move to some mod like "util"?
275275
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
276276
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
277+
#[deprecated(since = "0.10.0", note = "Use Switch instead")]
278+
#[allow(deprecated)]
277279
pub enum Toggle {
278280
/// Toggle something on / enable a thing.
279281
On,
280282
/// Toggle something off / disable a thing.
281283
Off,
282284
}
283285

284-
impl From<Toggle> for bool {
286+
/// Switch something on or off.
287+
///
288+
/// Convenience enum and wrapper around a bool, which more explicit about the intention to enable
289+
/// or disable something, in comparison to `true` or `false`.
290+
// TODO: Maybe move to some mod like "util"?
291+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
292+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
293+
pub enum Switch {
294+
/// Switch something on / enable a thing.
295+
On,
296+
/// Switch something off / disable a thing.
297+
Off,
298+
}
299+
300+
#[allow(deprecated)]
301+
impl From<Toggle> for Switch {
285302
fn from(toggle: Toggle) -> Self {
286-
matches!(toggle, Toggle::On)
303+
match toggle {
304+
Toggle::On => Switch::On,
305+
Toggle::Off => Switch::Off,
306+
}
307+
}
308+
}
309+
310+
impl From<Switch> for bool {
311+
fn from(switch: Switch) -> Self {
312+
matches!(switch, Switch::On)
287313
}
288314
}
289315

290-
impl From<bool> for Toggle {
316+
impl From<bool> for Switch {
291317
fn from(b: bool) -> Self {
292318
match b {
293-
true => Toggle::On,
294-
false => Toggle::Off,
319+
true => Switch::On,
320+
false => Switch::Off,
295321
}
296322
}
297323
}

src/serial.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
},
2525
rcc::{self, Clocks},
2626
time::rate::*,
27-
Toggle,
27+
Switch,
2828
};
2929

3030
#[allow(unused_imports)]
@@ -623,20 +623,20 @@ where
623623
/// Enable the interrupt for the specified [`Event`].
624624
#[inline]
625625
pub fn enable_interrupt(&mut self, event: Event) {
626-
self.configure_interrupt(event, Toggle::On);
626+
self.configure_interrupt(event, Switch::On);
627627
}
628628

629629
/// Disable the interrupt for the specified [`Event`].
630630
#[inline]
631631
pub fn disable_interrupt(&mut self, event: Event) {
632-
self.configure_interrupt(event, Toggle::Off);
632+
self.configure_interrupt(event, Switch::Off);
633633
}
634634

635635
/// Enable or disable the interrupt for the specified [`Event`].
636636
#[inline]
637-
pub fn configure_interrupt(&mut self, event: Event, enable: impl Into<Toggle>) {
638-
// Do a round way trip to be convert Into<Toggle> -> bool
639-
let enable: Toggle = enable.into();
637+
pub fn configure_interrupt(&mut self, event: Event, enable: impl Into<Switch>) {
638+
// Do a round way trip to be convert Into<Switch> -> bool
639+
let enable: Switch = enable.into();
640640
let enable: bool = enable.into();
641641
match event {
642642
Event::TransmitDataRegisterEmtpy => self.usart.cr1.modify(|_, w| w.txeie().bit(enable)),

0 commit comments

Comments
 (0)