Skip to content

Commit 0917d18

Browse files
committed
explicitly set outputs to documented state when calling new() functions
1 parent 82c7c81 commit 0917d18

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

src/lib.rs

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,34 @@ where
7575
{
7676
/// Instantiate a new [`Tb6612fng`] with the defined pins.
7777
/// This also automatically enables the two PWM pins.
78-
/// The initial state of the motors will be [stopped](DriveCommand::Stop).
78+
/// The initial state of the motors will be set to [stopped](DriveCommand::Stop).
79+
/// The initial state of standby will be *disabled*.
7980
///
8081
/// Usage example:
8182
/// ```
8283
/// # use embedded_hal_mock::eh1::pin::Mock as PinMock;
8384
/// # use embedded_hal_mock::eh1::pwm::Mock as PwmMock;
84-
/// # let motor_a_in1 = PinMock::new([]);
85+
/// # use embedded_hal_mock::eh1::pwm::Transaction as PwmTransaction;
86+
/// # use embedded_hal_mock::eh1::pin::Transaction as PinTransaction;
87+
/// # use embedded_hal_mock::eh1::pin::State::{High, Low};
88+
///
89+
/// # let motor_a_in1 = PinMock::new(&[PinTransaction::set(Low)]);
8590
/// # let mut motor_a_in1_ = motor_a_in1.clone();
86-
/// # let motor_a_in2 = PinMock::new([]);
91+
/// # let motor_a_in2 = PinMock::new(&[PinTransaction::set(Low)]);
8792
/// # let mut motor_a_in2_ = motor_a_in2.clone();
88-
/// # let motor_a_pwm = PwmMock::new(&[]);
93+
/// # let motor_a_pwm = PwmMock::new(&[PwmTransaction::max_duty_cycle(100), PwmTransaction::set_duty_cycle(0)]);
8994
/// # let mut motor_a_pwm_ = motor_a_pwm.clone();
90-
/// # let motor_b_in1 = PinMock::new([]);
95+
///
96+
/// # let motor_b_in1 = PinMock::new(&[PinTransaction::set(Low)]);
9197
/// # let mut motor_b_in1_ = motor_b_in1.clone();
92-
/// # let motor_b_in2 = PinMock::new([]);
98+
/// # let motor_b_in2 = PinMock::new(&[PinTransaction::set(Low)]);
9399
/// # let mut motor_b_in2_ = motor_b_in2.clone();
94-
/// # let motor_b_pwm = PwmMock::new(&[]);
100+
/// # let motor_b_pwm = PwmMock::new(&[PwmTransaction::max_duty_cycle(100), PwmTransaction::set_duty_cycle(0)]);
95101
/// # let mut motor_b_pwm_ = motor_b_pwm.clone();
96-
/// # let standby = PinMock::new([]);
102+
///
103+
/// # let standby = PinMock::new(&[PinTransaction::set(High)]);
97104
/// # let mut standby_ = standby.clone();
105+
///
98106
/// use tb6612fng::Tb6612fng;
99107
///
100108
/// let controller = Tb6612fng::new(
@@ -124,11 +132,15 @@ where
124132
motor_b_pwm: MBPWM,
125133
standby: STBY,
126134
) -> Tb6612fng<MAIN1, MAIN2, MAPWM, MBIN1, MBIN2, MBPWM, STBY> {
127-
Tb6612fng {
135+
let mut controller = Tb6612fng {
128136
motor_a: Motor::new(motor_a_in1, motor_a_in2, motor_a_pwm),
129137
motor_b: Motor::new(motor_b_in1, motor_b_in2, motor_b_pwm),
130138
standby,
131-
}
139+
};
140+
141+
controller.disable_standby();
142+
143+
controller
132144
}
133145

134146
/// Enable standby. This ignores any other setting currently done on the motors and puts them into standby.
@@ -165,18 +177,20 @@ where
165177
{
166178
/// Instantiate a new [`Motor`] with the defined pins.
167179
/// This also automatically enables the PWM pin.
168-
/// The initial state of the motor will be [stopped](DriveCommand::Stop).
180+
/// The initial state of the motor will be set to [stopped](DriveCommand::Stop).
169181
///
170182
/// Usage example:
171183
/// ```
172184
/// # use embedded_hal_mock::eh1::pin::Mock as PinMock;
173185
/// # use embedded_hal_mock::eh1::pwm::Mock as PwmMock;
186+
/// # use embedded_hal_mock::eh1::pwm::Transaction as PwmTransaction;
174187
/// # use embedded_hal_mock::eh1::pin::Transaction as PinTransaction;
175-
/// # let motor_in1 = PinMock::new([]);
188+
/// # use embedded_hal_mock::eh1::pin::State::{Low};
189+
/// # let motor_in1 = PinMock::new(&[PinTransaction::set(Low)]);
176190
/// # let mut motor_in1_ = motor_in1.clone();
177-
/// # let motor_in2 = PinMock::new([]);
191+
/// # let motor_in2 = PinMock::new(&[PinTransaction::set(Low)]);
178192
/// # let mut motor_in2_ = motor_in2.clone();
179-
/// # let motor_pwm = PwmMock::new([]);
193+
/// # let motor_pwm = PwmMock::new(&[PwmTransaction::max_duty_cycle(100), PwmTransaction::set_duty_cycle(0)]);
180194
/// # let mut motor_pwm_ = motor_pwm.clone();
181195
/// use tb6612fng::Motor;
182196
///
@@ -191,12 +205,16 @@ where
191205
/// # motor_pwm_.done();
192206
/// ```
193207
pub fn new(in1: IN1, in2: IN2, pwm: PWM) -> Motor<IN1, IN2, PWM> {
194-
Motor {
208+
let mut motor = Motor {
195209
in1,
196210
in2,
197211
pwm,
198212
current_drive_command: DriveCommand::Stop,
199-
}
213+
};
214+
215+
motor.drive(motor.current_drive_command).unwrap();
216+
217+
motor
200218
}
201219

202220
/// Drive forward with the defined speed. Note that the speed is a percentage between 0 and 100!
@@ -296,11 +314,13 @@ mod tests {
296314
#[test]
297315
fn test_motor_stop() {
298316
let max_duty = 100;
299-
let motor_in1_expectations = [PinTransaction::set(Low)];
300-
let motor_in2_expectations = [PinTransaction::set(Low)];
317+
let motor_in1_expectations = [PinTransaction::set(Low), PinTransaction::set(Low)];
318+
let motor_in2_expectations = [PinTransaction::set(Low), PinTransaction::set(Low)];
301319
let motor_pwm_expectations = [
302320
PwmTransaction::max_duty_cycle(max_duty),
303321
PwmTransaction::set_duty_cycle(0),
322+
PwmTransaction::max_duty_cycle(max_duty),
323+
PwmTransaction::set_duty_cycle(0),
304324
];
305325
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
306326
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
@@ -321,11 +341,13 @@ mod tests {
321341
#[test]
322342
fn test_motor_brake() {
323343
let max_duty = 100;
324-
let motor_in1_expectations = [PinTransaction::set(High)];
325-
let motor_in2_expectations = [PinTransaction::set(High)];
344+
let motor_in1_expectations = [PinTransaction::set(Low), PinTransaction::set(High)];
345+
let motor_in2_expectations = [PinTransaction::set(Low), PinTransaction::set(High)];
326346
let motor_pwm_expectations = [
327347
PwmTransaction::max_duty_cycle(max_duty),
328348
PwmTransaction::set_duty_cycle(0),
349+
PwmTransaction::max_duty_cycle(max_duty),
350+
PwmTransaction::set_duty_cycle(0),
329351
];
330352
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
331353
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
@@ -347,9 +369,11 @@ mod tests {
347369
fn test_motor_drive_forward() {
348370
let max_duty = 100;
349371
let speed: u8 = 100;
350-
let motor_in1_expectations = [PinTransaction::set(High)];
351-
let motor_in2_expectations = [PinTransaction::set(Low)];
372+
let motor_in1_expectations = [PinTransaction::set(Low), PinTransaction::set(High)];
373+
let motor_in2_expectations = [PinTransaction::set(Low), PinTransaction::set(Low)];
352374
let motor_pwm_expectations = [
375+
PwmTransaction::max_duty_cycle(max_duty),
376+
PwmTransaction::set_duty_cycle(0),
353377
PwmTransaction::max_duty_cycle(max_duty),
354378
PwmTransaction::set_duty_cycle(speed as u16),
355379
];
@@ -373,9 +397,11 @@ mod tests {
373397
fn test_motor_drive_backwards() {
374398
let max_duty = 100;
375399
let speed = 100;
376-
let motor_in1_expectations = [PinTransaction::set(Low)];
377-
let motor_in2_expectations = [PinTransaction::set(High)];
400+
let motor_in1_expectations = [PinTransaction::set(Low), PinTransaction::set(Low)];
401+
let motor_in2_expectations = [PinTransaction::set(Low), PinTransaction::set(High)];
378402
let motor_pwm_expectations = [
403+
PwmTransaction::max_duty_cycle(max_duty),
404+
PwmTransaction::set_duty_cycle(0),
379405
PwmTransaction::max_duty_cycle(max_duty),
380406
PwmTransaction::set_duty_cycle(speed as u16),
381407
];
@@ -397,16 +423,20 @@ mod tests {
397423

398424
#[test]
399425
fn test_motor_drive_invalid_speed() {
400-
let motor_in1_expectations = [];
401-
let motor_in2_expectations = [];
402-
let motor_pwm_expectations = [];
426+
let max_duty = 100;
427+
let motor_in1_expectations = [PinTransaction::set(Low)];
428+
let motor_in2_expectations = [PinTransaction::set(Low)];
429+
let motor_pwm_expectations = [
430+
PwmTransaction::max_duty_cycle(max_duty),
431+
PwmTransaction::set_duty_cycle(0),
432+
];
403433
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
404434
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
405435
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
406436

407437
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
408438

409-
let current_drive_command = motor.current_drive_command().clone();
439+
let current_drive_command = *motor.current_drive_command();
410440
let current_speed = motor.current_speed();
411441

412442
assert_eq!(

0 commit comments

Comments
 (0)