17
17
#![ deny( unused) ]
18
18
#![ no_std]
19
19
20
+ use core:: error:: Error ;
21
+ use core:: fmt:: { Debug , Formatter } ;
20
22
use embedded_hal:: digital:: { OutputPin , StatefulOutputPin } ;
21
23
use embedded_hal:: pwm:: SetDutyCycle ;
22
24
@@ -33,13 +35,62 @@ pub enum MotorError<IN1Error, IN2Error, PWMError> {
33
35
PwmError ( PWMError ) ,
34
36
}
35
37
38
+ impl < IN1Error : Debug , IN2Error : Debug , PWMError : Debug > core:: fmt:: Display
39
+ for MotorError < IN1Error , IN2Error , PWMError >
40
+ {
41
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> core:: fmt:: Result {
42
+ use MotorError :: * ;
43
+ match self {
44
+ InvalidSpeed => write ! ( f, "an invalid speed has been specified" ) ,
45
+ In1Error ( _) => write ! ( f, "failed to set the output of the IN1 pin" ) ,
46
+ In2Error ( _) => write ! ( f, "failed to set the output of the IN2 pin" ) ,
47
+ PwmError ( _) => write ! ( f, "failed to set the output of the PWM pin" ) ,
48
+ }
49
+ }
50
+ }
51
+
52
+ impl <
53
+ IN1Error : Debug + Error + ' static ,
54
+ IN2Error : Debug + Error + ' static ,
55
+ PWMError : Debug + Error + ' static ,
56
+ > Error for MotorError < IN1Error , IN2Error , PWMError >
57
+ {
58
+ fn source ( & self ) -> Option < & ( dyn Error + ' static ) > {
59
+ use MotorError :: * ;
60
+ match self {
61
+ InvalidSpeed => None ,
62
+ In1Error ( e) => Some ( e) ,
63
+ In2Error ( e) => Some ( e) ,
64
+ PwmError ( e) => Some ( e) ,
65
+ }
66
+ }
67
+ }
68
+
36
69
/// Defines errors which can happen when calling [`Tb6612fng::new()`].
37
70
#[ derive( PartialEq , Eq , Debug , Copy , Clone ) ]
38
71
pub enum Tb6612fngError < STBYError > {
39
72
/// An error in setting the initial output of the standby pin
40
73
Standby ( STBYError ) ,
41
74
}
42
75
76
+ impl < STBYError : Debug > core:: fmt:: Display for Tb6612fngError < STBYError > {
77
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> core:: fmt:: Result {
78
+ use Tb6612fngError :: * ;
79
+ match self {
80
+ Standby ( _) => write ! ( f, "failed to set the output of the standby pin" ) ,
81
+ }
82
+ }
83
+ }
84
+
85
+ impl < STBYError : Debug + Error + ' static > Error for Tb6612fngError < STBYError > {
86
+ fn source ( & self ) -> Option < & ( dyn Error + ' static ) > {
87
+ use Tb6612fngError :: * ;
88
+ match self {
89
+ Standby ( e) => Some ( e) ,
90
+ }
91
+ }
92
+ }
93
+
43
94
/// Defines the possible drive commands.
44
95
#[ derive( PartialEq , Eq , Debug , Copy , Clone ) ]
45
96
pub enum DriveCommand {
89
140
///
90
141
/// # Usage example
91
142
/// ```
143
+ ///
144
+ /// # fn main() -> Result<(), Box<dyn core::error::Error>> {
92
145
/// # use embedded_hal_mock::eh1::digital::Mock as PinMock;
93
146
/// # use embedded_hal_mock::eh1::pwm::Mock as PwmMock;
94
147
/// # use embedded_hal_mock::eh1::pwm::Transaction as PwmTransaction;
@@ -113,10 +166,9 @@ where
113
166
/// # let mut standby_ = standby.clone();
114
167
///
115
168
/// use tb6612fng::{Motor, Tb6612fng};
116
- ///
117
169
/// let controller = Tb6612fng::new(
118
- /// Motor::new(motor_a_in1, motor_a_in2, motor_a_pwm).unwrap() ,
119
- /// Motor::new(motor_b_in1, motor_b_in2, motor_b_pwm).unwrap() ,
170
+ /// Motor::new(motor_a_in1, motor_a_in2, motor_a_pwm)? ,
171
+ /// Motor::new(motor_b_in1, motor_b_in2, motor_b_pwm)? ,
120
172
/// standby,
121
173
/// );
122
174
///
@@ -127,6 +179,8 @@ where
127
179
/// # motor_b_in2_.done();
128
180
/// # motor_b_pwm_.done();
129
181
/// # standby_.done();
182
+ /// # Ok(())
183
+ /// # }
130
184
/// ```
131
185
#[ allow( clippy:: type_complexity) ]
132
186
pub fn new (
0 commit comments