Skip to content

Commit

Permalink
Update to embedded-hal-mock 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
9names committed Apr 9, 2024
1 parent 25cef25 commit 117873b
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 224 deletions.
2 changes: 1 addition & 1 deletion wii-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ embedded-hal-async = { version = "1"}
defmt = { version = "0.3.0", optional = true }

[dev-dependencies]
embedded-hal-mock = "0.8.0"
embedded-hal-mock = "0.10.0"
paste = "1.0.6"

[features]
Expand Down
47 changes: 28 additions & 19 deletions wii-ext/src/nunchuk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ where
mod tests {
use super::*;
use crate::test_data;
use embedded_hal_mock::i2c::{self, Transaction};
use embedded_hal_mock::eh1::i2c::{self, Transaction};
/// There's a certain amount of slop around the center position.
/// Allow up to this range without it being an error
const ZERO_SLOP: i8 = 5;
Expand All @@ -265,14 +265,15 @@ mod tests {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::NUNCHUCK_IDLE.to_vec()),
];
let mock = i2c::Mock::new(&expectations);
let mut mock = i2c::Mock::new(&expectations);
let mut nc = Nunchuk {
i2cdev: mock,
i2cdev: mock.clone(),
calibration: CalibrationData::default(),
};
let report = nc.read_no_wait().unwrap();
assert!(!report.button_c);
assert!(!report.button_z);
mock.done();
}

#[test]
Expand All @@ -281,10 +282,10 @@ mod tests {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::NUNCHUCK_IDLE.to_vec()),
];
let mock = i2c::Mock::new(&expectations);
let mut mock = i2c::Mock::new(&expectations);
let idle = NunchukReading::from_data(&test_data::NUNCHUCK_IDLE).unwrap();
let mut nc = Nunchuk {
i2cdev: mock,
i2cdev: mock.clone(),
calibration: CalibrationData {
joystick_x: idle.joystick_x,
joystick_y: idle.joystick_y,
Expand All @@ -295,6 +296,7 @@ mod tests {
assert!(!report.button_z);
assert_eq!(report.joystick_x, 0);
assert_eq!(report.joystick_y, 0);
mock.done();
}

#[test]
Expand All @@ -303,10 +305,10 @@ mod tests {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::NUNCHUCK_JOY_L.to_vec()),
];
let mock = i2c::Mock::new(&expectations);
let mut mock = i2c::Mock::new(&expectations);
let idle = NunchukReading::from_data(&test_data::NUNCHUCK_IDLE).unwrap();
let mut nc = Nunchuk {
i2cdev: mock,
i2cdev: mock.clone(),
calibration: CalibrationData {
joystick_x: idle.joystick_x,
joystick_y: idle.joystick_y,
Expand All @@ -318,6 +320,7 @@ mod tests {
assert!(report.joystick_x < -AXIS_MAX, "x = {}", report.joystick_x);
assert!(report.joystick_y > -ZERO_SLOP, "y = {}", report.joystick_y);
assert!(report.joystick_y < ZERO_SLOP, "y = {}", report.joystick_y);
mock.done();
}

#[test]
Expand All @@ -326,10 +329,10 @@ mod tests {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::NUNCHUCK_JOY_R.to_vec()),
];
let mock = i2c::Mock::new(&expectations);
let mut mock = i2c::Mock::new(&expectations);
let idle = NunchukReading::from_data(&test_data::NUNCHUCK_IDLE).unwrap();
let mut nc = Nunchuk {
i2cdev: mock,
i2cdev: mock.clone(),
calibration: CalibrationData {
joystick_x: idle.joystick_x,
joystick_y: idle.joystick_y,
Expand All @@ -341,6 +344,7 @@ mod tests {
assert!(report.joystick_x > AXIS_MAX, "x = {}", report.joystick_x);
assert!(report.joystick_y > -ZERO_SLOP, "y = {}", report.joystick_y);
assert!(report.joystick_y < ZERO_SLOP, "y = {}", report.joystick_y);
mock.done();
}

#[test]
Expand All @@ -349,10 +353,10 @@ mod tests {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::NUNCHUCK_JOY_U.to_vec()),
];
let mock = i2c::Mock::new(&expectations);
let mut mock = i2c::Mock::new(&expectations);
let idle = NunchukReading::from_data(&test_data::NUNCHUCK_IDLE).unwrap();
let mut nc = Nunchuk {
i2cdev: mock,
i2cdev: mock.clone(),
calibration: CalibrationData {
joystick_x: idle.joystick_x,
joystick_y: idle.joystick_y,
Expand All @@ -364,6 +368,7 @@ mod tests {
assert!(report.joystick_y > AXIS_MAX, "y = {}", report.joystick_y);
assert!(report.joystick_x > -ZERO_SLOP, "x = {}", report.joystick_x);
assert!(report.joystick_x < ZERO_SLOP, "x = {}", report.joystick_x);
mock.done();
}

#[test]
Expand All @@ -372,10 +377,10 @@ mod tests {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::NUNCHUCK_JOY_D.to_vec()),
];
let mock = i2c::Mock::new(&expectations);
let mut mock = i2c::Mock::new(&expectations);
let idle = NunchukReading::from_data(&test_data::NUNCHUCK_IDLE).unwrap();
let mut nc = Nunchuk {
i2cdev: mock,
i2cdev: mock.clone(),
calibration: CalibrationData {
joystick_x: idle.joystick_x,
joystick_y: idle.joystick_y,
Expand All @@ -387,6 +392,7 @@ mod tests {
assert!(report.joystick_y < -AXIS_MAX, "y = {}", report.joystick_y);
assert!(report.joystick_x > -ZERO_SLOP, "x = {}", report.joystick_x);
assert!(report.joystick_x < ZERO_SLOP, "x = {}", report.joystick_x);
mock.done();
}

#[test]
Expand All @@ -397,9 +403,9 @@ mod tests {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::NUNCHUCK_IDLE.to_vec()),
];
let mock = i2c::Mock::new(&expectations);
let mut mock = i2c::Mock::new(&expectations);
let mut nc = Nunchuk {
i2cdev: mock,
i2cdev: mock.clone(),
calibration: CalibrationData::default(),
};
let report = nc.read_no_wait().unwrap();
Expand All @@ -408,6 +414,7 @@ mod tests {
let report = nc.read_no_wait().unwrap();
assert!(!report.button_c);
assert!(!report.button_z);
mock.done();
}

#[test]
Expand All @@ -416,14 +423,15 @@ mod tests {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::NUNCHUCK_BTN_C.to_vec()),
];
let mock = i2c::Mock::new(&expectations);
let mut mock = i2c::Mock::new(&expectations);
let mut nc = Nunchuk {
i2cdev: mock,
i2cdev: mock.clone(),
calibration: CalibrationData::default(),
};
let report = nc.read_no_wait().unwrap();
assert!(report.button_c);
assert!(!report.button_z);
mock.done();
}

#[test]
Expand All @@ -432,13 +440,14 @@ mod tests {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::NUNCHUCK_BTN_Z.to_vec()),
];
let mock = i2c::Mock::new(&expectations);
let mut mock = i2c::Mock::new(&expectations);
let mut nc = Nunchuk {
i2cdev: mock,
i2cdev: mock.clone(),
calibration: CalibrationData::default(),
};
let report = nc.read_no_wait().unwrap();
assert!(!report.button_c);
assert!(report.button_z);
mock.done();
}
}
11 changes: 6 additions & 5 deletions wii-ext/tests/classic_hd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use embedded_hal_mock::delay::MockNoop;
use embedded_hal_mock::i2c::{self, Transaction};
use embedded_hal_mock::eh1::delay::NoopDelay;
use embedded_hal_mock::eh1::i2c::{self, Transaction};
use paste::paste;
use wii_ext::classic_sync::*;
use wii_ext::*;
Expand Down Expand Up @@ -45,9 +45,9 @@ macro_rules! assert_joystick_hd {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::$y.to_vec()),
];
let i2c = i2c::Mock::new(&expectations);
let mut delay = MockNoop::new();
let mut classic = Classic::new(i2c, &mut delay).unwrap();
let mut i2c = i2c::Mock::new(&expectations);
let mut delay = NoopDelay::new();
let mut classic = Classic::new(i2c.clone(), &mut delay).unwrap();
classic.enable_hires(&mut delay).unwrap();
let input = classic.read_blocking(&mut delay).unwrap();

Expand Down Expand Up @@ -93,6 +93,7 @@ macro_rules! assert_joystick_hd {
$rtl,
$rth
);
i2c.done();
}
}
};
Expand Down
39 changes: 22 additions & 17 deletions wii-ext/tests/classic_pdp_clone.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use embedded_hal_mock::delay::MockNoop;
use embedded_hal_mock::i2c::{self, Transaction};
use embedded_hal_mock::eh1::delay::NoopDelay;
use embedded_hal_mock::eh1::i2c::{self, Transaction};
use paste::paste;
use wii_ext::classic_sync::*;
use wii_ext::common::*;
Expand Down Expand Up @@ -50,11 +50,12 @@ fn classic_idle() {
Transaction::read(EXT_I2C_ADDR as u8, test_data::PDP_LINK_IDLE.to_vec()),
];

let i2c = i2c::Mock::new(&expectations);
let mut delay = MockNoop::new();
let mut classic = Classic::new(i2c, &mut delay).unwrap();
let mut i2c = i2c::Mock::new(&expectations);
let mut delay = NoopDelay::new();
let mut classic = Classic::new(i2c.clone(), &mut delay).unwrap();
let report = classic.read_report_blocking(&mut delay).unwrap();
assert_digital_eq(report, ClassicReading::default());
i2c.done();
}

// We don't want to write all that out for every digital button, so let's write a macro instead.
Expand Down Expand Up @@ -97,14 +98,15 @@ macro_rules! assert_button_fn {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, $y.to_vec()),
];
let i2c = i2c::Mock::new(&expectations);
let mut delay = MockNoop::new();
let mut classic = Classic::new(i2c, &mut delay).unwrap();
let mut i2c = i2c::Mock::new(&expectations);
let mut delay = NoopDelay::new();
let mut classic = Classic::new(i2c.clone(), &mut delay).unwrap();
let input = classic.read_report_blocking(&mut delay).unwrap();
assert_digital_eq(input, ClassicReading {
$x: true,
..Default::default()
});
i2c.done();
}
}
};
Expand Down Expand Up @@ -143,14 +145,15 @@ fn classic_calibrated_idle() {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::PDP_LINK_IDLE.to_vec()),
];
let i2c = i2c::Mock::new(&expectations);
let mut delay = MockNoop::new();
let mut classic = Classic::new(i2c, &mut delay).unwrap();
let mut i2c = i2c::Mock::new(&expectations);
let mut delay = NoopDelay::new();
let mut classic = Classic::new(i2c.clone(), &mut delay).unwrap();
let input = classic.read_blocking(&mut delay).unwrap();
assert_eq!(input.joystick_left_x, 0);
assert_eq!(input.joystick_left_y, 0);
assert_eq!(input.joystick_right_x, 0);
assert_eq!(input.joystick_right_y, 0);
i2c.done();
}

/// Test that no buttons are pressed when the controller is idle
Expand All @@ -169,9 +172,9 @@ fn classic_calibrated_joy_left() {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::PDP_LINK_LJOY_L.to_vec()),
];
let i2c = i2c::Mock::new(&expectations);
let mut delay = MockNoop::new();
let mut classic = Classic::new(i2c, &mut delay).unwrap();
let mut i2c = i2c::Mock::new(&expectations);
let mut delay = NoopDelay::new();
let mut classic = Classic::new(i2c.clone(), &mut delay).unwrap();
let input = classic.read_blocking(&mut delay).unwrap();

assert!(
Expand Down Expand Up @@ -204,6 +207,7 @@ fn classic_calibrated_joy_left() {
"trigger_right = {}",
input.trigger_right
);
i2c.done();
}

macro_rules! assert_joysticks {
Expand Down Expand Up @@ -231,9 +235,9 @@ macro_rules! assert_joysticks {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::$y.to_vec()),
];
let i2c = i2c::Mock::new(&expectations);
let mut delay = MockNoop::new();
let mut classic = Classic::new(i2c, &mut delay).unwrap();
let mut i2c = i2c::Mock::new(&expectations);
let mut delay = NoopDelay::new();
let mut classic = Classic::new(i2c.clone(), &mut delay).unwrap();
let input = classic.read_blocking(&mut delay).unwrap();

assert!(
Expand Down Expand Up @@ -278,6 +282,7 @@ macro_rules! assert_joysticks {
$rtl,
$rth
);
i2c.done();
}
}
};
Expand Down
11 changes: 6 additions & 5 deletions wii-ext/tests/classic_pdp_clone_hd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use embedded_hal_mock::delay::MockNoop;
use embedded_hal_mock::i2c::{self, Transaction};
use embedded_hal_mock::eh1::delay::NoopDelay;
use embedded_hal_mock::eh1::i2c::{self, Transaction};
use paste::paste;
use wii_ext::classic_sync::*;
use wii_ext::*;
Expand Down Expand Up @@ -45,9 +45,9 @@ macro_rules! assert_joystick_hd {
Transaction::write(EXT_I2C_ADDR as u8, vec![0]),
Transaction::read(EXT_I2C_ADDR as u8, test_data::$y.to_vec()),
];
let i2c = i2c::Mock::new(&expectations);
let mut delay = MockNoop::new();
let mut classic = Classic::new(i2c, &mut delay).unwrap();
let mut i2c = i2c::Mock::new(&expectations);
let mut delay = NoopDelay::new();
let mut classic = Classic::new(i2c.clone(), &mut delay).unwrap();
classic.enable_hires(&mut delay).unwrap();
let input = classic.read_blocking(&mut delay).unwrap();

Expand Down Expand Up @@ -93,6 +93,7 @@ macro_rules! assert_joystick_hd {
$rtl,
$rth
);
i2c.done();
}
}
};
Expand Down
Loading

0 comments on commit 117873b

Please sign in to comment.