Skip to content

Commit 39f215e

Browse files
committed
Separate Capture, Pwm and Qei traits into modules
1 parent 88412b9 commit 39f215e

File tree

4 files changed

+304
-290
lines changed

4 files changed

+304
-290
lines changed

src/capture.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//! Input capture
2+
3+
use nb;
4+
5+
/// Input capture
6+
///
7+
/// # Examples
8+
///
9+
/// You can use this interface to measure the period of (quasi) periodic signals
10+
/// / events
11+
///
12+
/// ```
13+
/// extern crate embedded_hal as hal;
14+
/// #[macro_use(block)]
15+
/// extern crate nb;
16+
///
17+
/// use hal::prelude::*;
18+
///
19+
/// fn main() {
20+
/// let mut capture: Capture1 = {
21+
/// // ..
22+
/// # Capture1
23+
/// };
24+
///
25+
/// capture.try_set_resolution(1.ms()).unwrap();
26+
///
27+
/// let before = block!(capture.try_capture(Channel::_1)).unwrap();
28+
/// let after = block!(capture.try_capture(Channel::_1)).unwrap();
29+
///
30+
/// let period = after.wrapping_sub(before);
31+
///
32+
/// println!("Period: {} ms", period);
33+
/// }
34+
///
35+
/// # use core::convert::Infallible;
36+
/// # struct MilliSeconds(u32);
37+
/// # trait U32Ext { fn ms(self) -> MilliSeconds; }
38+
/// # impl U32Ext for u32 { fn ms(self) -> MilliSeconds { MilliSeconds(self) } }
39+
/// # struct Capture1;
40+
/// # enum Channel { _1 }
41+
/// # impl hal::Capture for Capture1 {
42+
/// # type Error = Infallible;
43+
/// # type Capture = u16;
44+
/// # type Channel = Channel;
45+
/// # type Time = MilliSeconds;
46+
/// # fn try_capture(&mut self, _: Channel) -> ::nb::Result<u16, Self::Error> { Ok(0) }
47+
/// # fn try_disable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
48+
/// # fn try_enable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
49+
/// # fn try_get_resolution(&self) -> Result<MilliSeconds, Self::Error> { unimplemented!() }
50+
/// # fn try_set_resolution<T>(&mut self, _: T) -> Result<(), Self::Error> where T: Into<MilliSeconds> { Ok(()) }
51+
/// # }
52+
/// ```
53+
// unproven reason: pre-singletons API. With singletons a `CapturePin` (cf. `PwmPin`) trait seems more
54+
// appropriate
55+
pub trait Capture {
56+
/// Enumeration of `Capture` errors
57+
///
58+
/// Possible errors:
59+
///
60+
/// - *overcapture*, the previous capture value was overwritten because it
61+
/// was not read in a timely manner
62+
type Error;
63+
64+
/// Enumeration of channels that can be used with this `Capture` interface
65+
///
66+
/// If your `Capture` interface has no channels you can use the type `()`
67+
/// here
68+
type Channel;
69+
70+
/// A time unit that can be converted into a human time unit (e.g. seconds)
71+
type Time;
72+
73+
/// The type of the value returned by `capture`
74+
type Capture;
75+
76+
/// "Waits" for a transition in the capture `channel` and returns the value
77+
/// of counter at that instant
78+
///
79+
/// NOTE that you must multiply the returned value by the *resolution* of
80+
/// this `Capture` interface to get a human time unit (e.g. seconds)
81+
fn try_capture(&mut self, channel: Self::Channel) -> nb::Result<Self::Capture, Self::Error>;
82+
83+
/// Disables a capture `channel`
84+
fn try_disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
85+
86+
/// Enables a capture `channel`
87+
fn try_enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
88+
89+
/// Returns the current resolution
90+
fn try_get_resolution(&self) -> Result<Self::Time, Self::Error>;
91+
92+
/// Sets the resolution of the capture timer
93+
fn try_set_resolution<R>(&mut self, resolution: R) -> Result<(), Self::Error>
94+
where
95+
R: Into<Self::Time>;
96+
}

src/lib.rs

Lines changed: 6 additions & 290 deletions
Original file line numberDiff line numberDiff line change
@@ -697,295 +697,11 @@ pub mod spi;
697697
pub mod timer;
698698
pub mod watchdog;
699699

700-
/// Input capture
701-
///
702-
/// # Examples
703-
///
704-
/// You can use this interface to measure the period of (quasi) periodic signals
705-
/// / events
706-
///
707-
/// ```
708-
/// extern crate embedded_hal as hal;
709-
/// #[macro_use(block)]
710-
/// extern crate nb;
711-
///
712-
/// use hal::prelude::*;
713-
///
714-
/// fn main() {
715-
/// let mut capture: Capture1 = {
716-
/// // ..
717-
/// # Capture1
718-
/// };
719-
///
720-
/// capture.try_set_resolution(1.ms()).unwrap();
721-
///
722-
/// let before = block!(capture.try_capture(Channel::_1)).unwrap();
723-
/// let after = block!(capture.try_capture(Channel::_1)).unwrap();
724-
///
725-
/// let period = after.wrapping_sub(before);
726-
///
727-
/// println!("Period: {} ms", period);
728-
/// }
729-
///
730-
/// # use core::convert::Infallible;
731-
/// # struct MilliSeconds(u32);
732-
/// # trait U32Ext { fn ms(self) -> MilliSeconds; }
733-
/// # impl U32Ext for u32 { fn ms(self) -> MilliSeconds { MilliSeconds(self) } }
734-
/// # struct Capture1;
735-
/// # enum Channel { _1 }
736-
/// # impl hal::Capture for Capture1 {
737-
/// # type Error = Infallible;
738-
/// # type Capture = u16;
739-
/// # type Channel = Channel;
740-
/// # type Time = MilliSeconds;
741-
/// # fn try_capture(&mut self, _: Channel) -> ::nb::Result<u16, Self::Error> { Ok(0) }
742-
/// # fn try_disable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
743-
/// # fn try_enable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
744-
/// # fn try_get_resolution(&self) -> Result<MilliSeconds, Self::Error> { unimplemented!() }
745-
/// # fn try_set_resolution<T>(&mut self, _: T) -> Result<(), Self::Error> where T: Into<MilliSeconds> { Ok(()) }
746-
/// # }
747-
/// ```
748-
// unproven reason: pre-singletons API. With singletons a `CapturePin` (cf. `PwmPin`) trait seems more
749-
// appropriate
750-
pub trait Capture {
751-
/// Enumeration of `Capture` errors
752-
///
753-
/// Possible errors:
754-
///
755-
/// - *overcapture*, the previous capture value was overwritten because it
756-
/// was not read in a timely manner
757-
type Error;
700+
mod capture;
701+
pub use capture::Capture;
758702

759-
/// Enumeration of channels that can be used with this `Capture` interface
760-
///
761-
/// If your `Capture` interface has no channels you can use the type `()`
762-
/// here
763-
type Channel;
703+
mod pwm;
704+
pub use pwm::{Pwm, PwmPin};
764705

765-
/// A time unit that can be converted into a human time unit (e.g. seconds)
766-
type Time;
767-
768-
/// The type of the value returned by `capture`
769-
type Capture;
770-
771-
/// "Waits" for a transition in the capture `channel` and returns the value
772-
/// of counter at that instant
773-
///
774-
/// NOTE that you must multiply the returned value by the *resolution* of
775-
/// this `Capture` interface to get a human time unit (e.g. seconds)
776-
fn try_capture(&mut self, channel: Self::Channel) -> nb::Result<Self::Capture, Self::Error>;
777-
778-
/// Disables a capture `channel`
779-
fn try_disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
780-
781-
/// Enables a capture `channel`
782-
fn try_enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
783-
784-
/// Returns the current resolution
785-
fn try_get_resolution(&self) -> Result<Self::Time, Self::Error>;
786-
787-
/// Sets the resolution of the capture timer
788-
fn try_set_resolution<R>(&mut self, resolution: R) -> Result<(), Self::Error>
789-
where
790-
R: Into<Self::Time>;
791-
}
792-
793-
/// Pulse Width Modulation
794-
///
795-
/// # Examples
796-
///
797-
/// Use this interface to control the power output of some actuator
798-
///
799-
/// ```
800-
/// extern crate embedded_hal as hal;
801-
///
802-
/// use hal::prelude::*;
803-
///
804-
/// fn main() {
805-
/// let mut pwm: Pwm1 = {
806-
/// // ..
807-
/// # Pwm1
808-
/// };
809-
///
810-
/// pwm.try_set_period(1.khz()).unwrap();
811-
///
812-
/// let max_duty = pwm.try_get_max_duty().unwrap();
813-
///
814-
/// // brightest LED
815-
/// pwm.try_set_duty(Channel::_1, max_duty).unwrap();
816-
///
817-
/// // dimmer LED
818-
/// pwm.try_set_duty(Channel::_2, max_duty / 4).unwrap();
819-
/// }
820-
///
821-
/// # use core::convert::Infallible;
822-
/// # struct KiloHertz(u32);
823-
/// # trait U32Ext { fn khz(self) -> KiloHertz; }
824-
/// # impl U32Ext for u32 { fn khz(self) -> KiloHertz { KiloHertz(self) } }
825-
/// # enum Channel { _1, _2 }
826-
/// # struct Pwm1;
827-
/// # impl hal::Pwm for Pwm1 {
828-
/// # type Error = Infallible;
829-
/// # type Channel = Channel;
830-
/// # type Time = KiloHertz;
831-
/// # type Duty = u16;
832-
/// # fn try_disable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
833-
/// # fn try_enable(&mut self, _: Channel) -> Result<(), Self::Error> { unimplemented!() }
834-
/// # fn try_get_duty(&self, _: Channel) -> Result<u16, Self::Error> { unimplemented!() }
835-
/// # fn try_get_max_duty(&self) -> Result<u16, Self::Error> { Ok(0) }
836-
/// # fn try_set_duty(&mut self, _: Channel, _: u16) -> Result<(), Self::Error> { Ok(()) }
837-
/// # fn try_get_period(&self) -> Result<KiloHertz, Self::Error> { unimplemented!() }
838-
/// # fn try_set_period<T>(&mut self, _: T) -> Result<(), Self::Error> where T: Into<KiloHertz> { Ok(()) }
839-
/// # }
840-
/// ```
841-
// unproven reason: pre-singletons API. The `PwmPin` trait seems more useful because it models independent
842-
// PWM channels. Here a certain number of channels are multiplexed in a single implementer.
843-
pub trait Pwm {
844-
/// Enumeration of `Pwm` errors
845-
type Error;
846-
847-
/// Enumeration of channels that can be used with this `Pwm` interface
848-
///
849-
/// If your `Pwm` interface has no channels you can use the type `()`
850-
/// here
851-
type Channel;
852-
853-
/// A time unit that can be converted into a human time unit (e.g. seconds)
854-
type Time;
855-
856-
/// Type for the `duty` methods
857-
///
858-
/// The implementer is free to choose a float / percentage representation
859-
/// (e.g. `0.0 .. 1.0`) or an integer representation (e.g. `0 .. 65535`)
860-
type Duty;
861-
862-
/// Disables a PWM `channel`
863-
fn try_disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
864-
865-
/// Enables a PWM `channel`
866-
fn try_enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
867-
868-
/// Returns the current PWM period
869-
fn try_get_period(&self) -> Result<Self::Time, Self::Error>;
870-
871-
/// Returns the current duty cycle
872-
fn try_get_duty(&self, channel: Self::Channel) -> Result<Self::Duty, Self::Error>;
873-
874-
/// Returns the maximum duty cycle value
875-
fn try_get_max_duty(&self) -> Result<Self::Duty, Self::Error>;
876-
877-
/// Sets a new duty cycle
878-
fn try_set_duty(&mut self, channel: Self::Channel, duty: Self::Duty)
879-
-> Result<(), Self::Error>;
880-
881-
/// Sets a new PWM period
882-
fn try_set_period<P>(&mut self, period: P) -> Result<(), Self::Error>
883-
where
884-
P: Into<Self::Time>;
885-
}
886-
887-
/// A single PWM channel / pin
888-
///
889-
/// See `Pwm` for details
890-
pub trait PwmPin {
891-
/// Enumeration of `PwmPin` errors
892-
type Error;
893-
894-
/// Type for the `duty` methods
895-
///
896-
/// The implementer is free to choose a float / percentage representation
897-
/// (e.g. `0.0 .. 1.0`) or an integer representation (e.g. `0 .. 65535`)
898-
type Duty;
899-
900-
/// Disables a PWM `channel`
901-
fn try_disable(&mut self) -> Result<(), Self::Error>;
902-
903-
/// Enables a PWM `channel`
904-
fn try_enable(&mut self) -> Result<(), Self::Error>;
905-
906-
/// Returns the current duty cycle
907-
fn try_get_duty(&self) -> Result<Self::Duty, Self::Error>;
908-
909-
/// Returns the maximum duty cycle value
910-
fn try_get_max_duty(&self) -> Result<Self::Duty, Self::Error>;
911-
912-
/// Sets a new duty cycle
913-
fn try_set_duty(&mut self, duty: Self::Duty) -> Result<(), Self::Error>;
914-
}
915-
916-
/// Quadrature encoder interface
917-
///
918-
/// # Examples
919-
///
920-
/// You can use this interface to measure the speed of a motor
921-
///
922-
/// ```
923-
/// extern crate embedded_hal as hal;
924-
/// #[macro_use(block)]
925-
/// extern crate nb;
926-
///
927-
/// use hal::prelude::*;
928-
///
929-
/// fn main() {
930-
/// let mut qei: Qei1 = {
931-
/// // ..
932-
/// # Qei1
933-
/// };
934-
/// let mut timer: Timer6 = {
935-
/// // ..
936-
/// # Timer6
937-
/// };
938-
///
939-
///
940-
/// let before = qei.try_count().unwrap();
941-
/// timer.try_start(1.s()).unwrap();
942-
/// block!(timer.try_wait());
943-
/// let after = qei.try_count().unwrap();
944-
///
945-
/// let speed = after.wrapping_sub(before);
946-
/// println!("Speed: {} pulses per second", speed);
947-
/// }
948-
///
949-
/// # use core::convert::Infallible;
950-
/// # struct Seconds(u32);
951-
/// # trait U32Ext { fn s(self) -> Seconds; }
952-
/// # impl U32Ext for u32 { fn s(self) -> Seconds { Seconds(self) } }
953-
/// # struct Qei1;
954-
/// # impl hal::Qei for Qei1 {
955-
/// # type Error = Infallible;
956-
/// # type Count = u16;
957-
/// # fn try_count(&self) -> Result<u16, Self::Error> { Ok(0) }
958-
/// # fn try_direction(&self) -> Result<::hal::Direction, Self::Error> { unimplemented!() }
959-
/// # }
960-
/// # struct Timer6;
961-
/// # impl hal::timer::CountDown for Timer6 {
962-
/// # type Error = Infallible;
963-
/// # type Time = Seconds;
964-
/// # fn try_start<T>(&mut self, _: T) -> Result<(), Infallible> where T: Into<Seconds> { Ok(()) }
965-
/// # fn try_wait(&mut self) -> ::nb::Result<(), Infallible> { Ok(()) }
966-
/// # }
967-
/// ```
968-
// unproven reason: needs to be re-evaluated in the new singletons world. At the very least this needs a
969-
// reference implementation
970-
pub trait Qei {
971-
/// Enumeration of `Qei` errors
972-
type Error;
973-
974-
/// The type of the value returned by `count`
975-
type Count;
976-
977-
/// Returns the current pulse count of the encoder
978-
fn try_count(&self) -> Result<Self::Count, Self::Error>;
979-
980-
/// Returns the count direction
981-
fn try_direction(&self) -> Result<Direction, Self::Error>;
982-
}
983-
984-
/// Count direction
985-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
986-
pub enum Direction {
987-
/// 3, 2, 1
988-
Downcounting,
989-
/// 1, 2, 3
990-
Upcounting,
991-
}
706+
mod qei;
707+
pub use qei::{Direction, Qei};

0 commit comments

Comments
 (0)