diff --git a/wii-ext/src/async_impl/classic.rs b/wii-ext/src/async_impl/classic.rs index 91c37ca..b2d72d5 100644 --- a/wii-ext/src/async_impl/classic.rs +++ b/wii-ext/src/async_impl/classic.rs @@ -10,7 +10,7 @@ // // See `decode_classic_report` and `decode_classic_hd_report` for data format -use crate::async_impl::interface::{ClassicAsyncError, InterfaceAsync}; +use crate::async_impl::interface::{AsyncImplError, InterfaceAsync}; use crate::core::classic::*; use crate::core::{ControllerIdReport, ControllerType}; use embedded_hal_async; @@ -44,7 +44,7 @@ where // / // / Since each device will have different tolerances, we take a snapshot of some analog data // / to use as the "baseline" center. - pub async fn update_calibration(&mut self) -> Result<(), ClassicAsyncError> { + pub async fn update_calibration(&mut self) -> Result<(), AsyncImplError> { let data = self.read_report().await?; self.calibration = CalibrationData { joystick_left_x: data.joystick_left_x, @@ -60,7 +60,7 @@ where /// Send the init sequence to the Wii extension controller /// /// This could be a bit faster with DelayUs, but since you only init once we'll re-use delay_ms - pub async fn init(&mut self) -> Result<(), ClassicAsyncError> { + pub async fn init(&mut self) -> Result<(), AsyncImplError> { // Extension controllers by default will use encrypted communication, as that is what the Wii does. // We can disable this encryption by writing some magic values // This is described at https://wiibrew.org/wiki/Wiimote/Extension_Controllers#The_New_Way @@ -73,40 +73,38 @@ where } /// poll the controller for the latest data - async fn read_classic_report(&mut self) -> Result { + async fn read_classic_report(&mut self) -> Result { if self.hires { let buf = self.interface.read_hd_report().await?; - ClassicReading::from_data(&buf).ok_or(ClassicAsyncError::InvalidInputData) + ClassicReading::from_data(&buf).ok_or(AsyncImplError::InvalidInputData) } else { let buf = self.interface.read_ext_report().await?; - ClassicReading::from_data(&buf).ok_or(ClassicAsyncError::InvalidInputData) + ClassicReading::from_data(&buf).ok_or(AsyncImplError::InvalidInputData) } } /// Simple blocking read helper that will start a sample, wait 10ms, then read the value - async fn read_report(&mut self) -> Result { + async fn read_report(&mut self) -> Result { self.read_classic_report().await } /// Do a read, and report axis values relative to calibration - pub async fn read(&mut self) -> Result { + pub async fn read(&mut self) -> Result { Ok(ClassicReadingCalibrated::new( self.read_classic_report().await?, &self.calibration, )) } - pub async fn enable_hires(&mut self) -> Result<(), ClassicAsyncError> { + pub async fn enable_hires(&mut self) -> Result<(), AsyncImplError> { self.interface.enable_hires().await } - pub async fn read_id(&mut self) -> Result { + pub async fn read_id(&mut self) -> Result { self.interface.read_id().await } - pub async fn identify_controller( - &mut self, - ) -> Result, ClassicAsyncError> { + pub async fn identify_controller(&mut self) -> Result, AsyncImplError> { self.interface.identify_controller().await } } diff --git a/wii-ext/src/async_impl/interface.rs b/wii-ext/src/async_impl/interface.rs index f04f1c0..8a43e7c 100644 --- a/wii-ext/src/async_impl/interface.rs +++ b/wii-ext/src/async_impl/interface.rs @@ -21,7 +21,7 @@ use defmt; #[cfg_attr(feature = "defmt_print", derive(defmt::Format))] #[derive(Debug)] -pub enum ClassicAsyncError { +pub enum AsyncImplError { I2C, InvalidInputData, Error, @@ -53,33 +53,33 @@ where } /// Read the button/axis data from the classic controller - pub(super) async fn read_ext_report(&mut self) -> Result { + pub(super) async fn read_ext_report(&mut self) -> Result { self.start_sample().await?; self.delay_us(INTERMESSAGE_DELAY_MICROSEC_U32).await; let mut buffer: ExtReport = ExtReport::default(); self.i2cdev .read(EXT_I2C_ADDR as u8, &mut buffer) .await - .map_err(|_| ClassicAsyncError::I2C) + .map_err(|_| AsyncImplError::I2C) .and(Ok(buffer)) } /// Read a high-resolution version of the button/axis data from the classic controller - pub(super) async fn read_hd_report(&mut self) -> Result { + pub(super) async fn read_hd_report(&mut self) -> Result { self.start_sample().await?; self.delay_us(INTERMESSAGE_DELAY_MICROSEC_U32).await; let mut buffer: ExtHdReport = ExtHdReport::default(); self.i2cdev .read(EXT_I2C_ADDR as u8, &mut buffer) .await - .map_err(|_| ClassicAsyncError::I2C) + .map_err(|_| AsyncImplError::I2C) .and(Ok(buffer)) } /// Send the init sequence to the Wii extension controller /// /// This could be a bit faster with DelayUs, but since you only init once we'll re-use delay_ms - pub(super) async fn init(&mut self) -> Result<(), ClassicAsyncError> { + pub(super) async fn init(&mut self) -> Result<(), AsyncImplError> { // Extension controllers by default will use encrypted communication, as that is what the Wii does. // We can disable this encryption by writing some magic values // This is described at https://wiibrew.org/wiki/Wiimote/Extension_Controllers#The_New_Way @@ -99,7 +99,7 @@ where /// This enables the controllers high-resolution report data mode, which returns each /// analogue axis as a u8, rather than packing smaller integers in a structure. /// If your controllers supports this mode, you should use it. It is much better. - pub(super) async fn enable_hires(&mut self) -> Result<(), ClassicAsyncError> { + pub(super) async fn enable_hires(&mut self) -> Result<(), AsyncImplError> { self.set_register_with_delay(0xFE, 0x03).await?; self.delay_us(100_000).await; Ok(()) @@ -114,33 +114,29 @@ where pub(super) async fn set_read_register_address( &mut self, byte0: u8, - ) -> Result<(), ClassicAsyncError> { + ) -> Result<(), AsyncImplError> { self.i2cdev .write(EXT_I2C_ADDR as u8, &[byte0]) .await - .map_err(|_| ClassicAsyncError::I2C) + .map_err(|_| AsyncImplError::I2C) .and(Ok(())) } pub(super) async fn set_read_register_address_with_delay( &mut self, byte0: u8, - ) -> Result<(), ClassicAsyncError> { + ) -> Result<(), AsyncImplError> { self.delay_us(INTERMESSAGE_DELAY_MICROSEC_U32).await; let res = self.set_read_register_address(byte0); res.await } /// Set a single register at target address - pub(super) async fn set_register( - &mut self, - addr: u8, - byte1: u8, - ) -> Result<(), ClassicAsyncError> { + pub(super) async fn set_register(&mut self, addr: u8, byte1: u8) -> Result<(), AsyncImplError> { self.i2cdev .write(EXT_I2C_ADDR as u8, &[addr, byte1]) .await - .map_err(|_| ClassicAsyncError::I2C) + .map_err(|_| AsyncImplError::I2C) .and(Ok(())) } @@ -148,13 +144,13 @@ where &mut self, addr: u8, byte1: u8, - ) -> Result<(), ClassicAsyncError> { + ) -> Result<(), AsyncImplError> { self.delay_us(INTERMESSAGE_DELAY_MICROSEC_U32).await; let res = self.set_register(addr, byte1); res.await } - pub(super) async fn read_id(&mut self) -> Result { + pub(super) async fn read_id(&mut self) -> Result { self.set_read_register_address(0xfa).await?; let i2c_id = self.read_ext_report().await?; Ok(i2c_id) @@ -162,13 +158,13 @@ where pub(super) async fn identify_controller( &mut self, - ) -> Result, ClassicAsyncError> { + ) -> Result, AsyncImplError> { let i2c_id = self.read_id().await?; Ok(crate::core::identify_controller(i2c_id)) } /// tell the extension controller to prepare a sample by setting the read cursor to 0 - pub(super) async fn start_sample(&mut self) -> Result<(), ClassicAsyncError> { + pub(super) async fn start_sample(&mut self) -> Result<(), AsyncImplError> { self.set_read_register_address(0x00).await?; Ok(()) } diff --git a/wii-ext/src/async_impl/nunchuk.rs b/wii-ext/src/async_impl/nunchuk.rs index 6c10a50..5395b7a 100644 --- a/wii-ext/src/async_impl/nunchuk.rs +++ b/wii-ext/src/async_impl/nunchuk.rs @@ -6,7 +6,7 @@ // Nunchuk technically supports HD report, but the last two bytes will be zeroes // There's no benefit, so we're leaving that unimplemented -use crate::async_impl::interface::{ClassicAsyncError as AsyncImplError, InterfaceAsync}; +use crate::async_impl::interface::{AsyncImplError, InterfaceAsync}; use crate::core::nunchuk::*; use crate::core::{ControllerIdReport, ControllerType}; use embedded_hal_async; diff --git a/wii-ext/src/blocking_impl/classic.rs b/wii-ext/src/blocking_impl/classic.rs index 61af9ee..b6d19ad 100644 --- a/wii-ext/src/blocking_impl/classic.rs +++ b/wii-ext/src/blocking_impl/classic.rs @@ -10,7 +10,7 @@ // // See `decode_classic_report` and `decode_classic_hd_report` for data format -use crate::blocking_impl::interface::{Error, Interface}; +use crate::blocking_impl::interface::{BlockingImplError, Interface}; use crate::core::classic::{CalibrationData, ClassicReading, ClassicReadingCalibrated}; use crate::core::ControllerType; use embedded_hal::i2c::I2c; @@ -42,7 +42,7 @@ where /// This method will open the provide i2c device file and will /// send the required init sequence in order to read data in /// the future. - pub fn new(i2cdev: T, delay: DELAY) -> Result, Error> { + pub fn new(i2cdev: T, delay: DELAY) -> Result, BlockingImplError> { let interface = Interface::new(i2cdev, delay); let mut classic = Classic { interface, @@ -57,7 +57,7 @@ where /// /// Since each device will have different tolerances, we take a snapshot of some analog data /// to use as the "baseline" center. - pub fn update_calibration(&mut self) -> Result<(), Error> { + pub fn update_calibration(&mut self) -> Result<(), BlockingImplError> { let data = self.read_report_blocking()?; self.calibration = CalibrationData { @@ -74,7 +74,7 @@ where /// Send the init sequence to the Wii extension controller /// /// This could be a bit faster with DelayNs, but since you only init once we'll re-use delay_ms - pub fn init(&mut self) -> Result<(), Error> { + pub fn init(&mut self) -> Result<(), BlockingImplError> { // Extension controllers by default will use encrypted communication, as that is what the Wii does. // We can disable this encryption by writing some magic values // This is described at https://wiibrew.org/wiki/Wiimote/Extension_Controllers#The_New_Way @@ -92,7 +92,7 @@ where /// This enables the controllers high-resolution report data mode, which returns each /// analogue axis as a u8, rather than packing smaller integers in a structure. /// If your controllers supports this mode, you should use it. It is much better. - pub fn enable_hires(&mut self) -> Result<(), Error> { + pub fn enable_hires(&mut self) -> Result<(), BlockingImplError> { self.interface.enable_hires()?; self.hires = true; self.update_calibration()?; @@ -108,42 +108,42 @@ where /// This function does not work. /// TODO: work out why, make it public when it works #[allow(dead_code)] - fn disable_hires(&mut self) -> Result<(), Error> { + fn disable_hires(&mut self) -> Result<(), BlockingImplError> { self.interface.disable_hires()?; self.hires = false; self.update_calibration()?; Ok(()) } - pub fn identify_controller(&mut self) -> Result, Error> { + pub fn identify_controller(&mut self) -> Result, BlockingImplError> { self.interface.identify_controller() } /// poll the controller for the latest data - fn read_classic_report(&mut self) -> Result> { + fn read_classic_report(&mut self) -> Result> { if self.hires { let buf = self.interface.read_hd_report()?; - ClassicReading::from_data(&buf).ok_or(Error::InvalidInputData) + ClassicReading::from_data(&buf).ok_or(BlockingImplError::InvalidInputData) } else { let buf = self.interface.read_report()?; - ClassicReading::from_data(&buf).ok_or(Error::InvalidInputData) + ClassicReading::from_data(&buf).ok_or(BlockingImplError::InvalidInputData) } } /// Simple read helper helper with no delay. Works for testing, not on real hardware - pub fn read_classic_no_wait(&mut self) -> Result> { + pub fn read_classic_no_wait(&mut self) -> Result> { self.interface.start_sample()?; self.read_classic_report() } /// Simple blocking read helper that will start a sample, wait 10ms, then read the value - pub fn read_report_blocking(&mut self) -> Result> { + pub fn read_report_blocking(&mut self) -> Result> { self.interface.start_sample_and_wait()?; self.read_classic_report() } /// Do a read, and report axis values relative to calibration - pub fn read_blocking(&mut self) -> Result> { + pub fn read_blocking(&mut self) -> Result> { Ok(ClassicReadingCalibrated::new( self.read_report_blocking()?, &self.calibration, diff --git a/wii-ext/src/blocking_impl/interface.rs b/wii-ext/src/blocking_impl/interface.rs index 57fac46..a569622 100644 --- a/wii-ext/src/blocking_impl/interface.rs +++ b/wii-ext/src/blocking_impl/interface.rs @@ -12,7 +12,7 @@ pub struct Interface { #[cfg_attr(feature = "defmt_print", derive(defmt::Format))] /// Errors in this crate #[derive(Debug)] -pub enum Error { +pub enum BlockingImplError { /// I²C bus communication error I2C(E), /// Invalid input data provided @@ -29,7 +29,7 @@ where } /// Send the init sequence to the Wii extension controller - pub(super) fn init(&mut self) -> Result<(), Error> { + pub(super) fn init(&mut self) -> Result<(), BlockingImplError> { // Extension controllers by default will use encrypted communication, as that is what the Wii does. // We can disable this encryption by writing some magic values // This is described at https://wiibrew.org/wiki/Wiimote/Extension_Controllers#The_New_Way @@ -49,25 +49,27 @@ where Ok(()) } - pub(super) fn read_id(&mut self) -> Result> { + pub(super) fn read_id(&mut self) -> Result> { self.set_read_register_address(0xfa)?; let i2c_id = self.read_report()?; Ok(i2c_id) } - pub(super) fn identify_controller(&mut self) -> Result, Error> { + pub(super) fn identify_controller( + &mut self, + ) -> Result, BlockingImplError> { let i2c_id = self.read_id()?; Ok(crate::core::identify_controller(i2c_id)) } /// tell the extension controller to prepare a sample by setting the read cursor to 0 - pub(super) fn start_sample(&mut self) -> Result<(), Error> { + pub(super) fn start_sample(&mut self) -> Result<(), BlockingImplError> { self.set_read_register_address(0x00)?; Ok(()) } /// tell the extension controller to prepare a sample by setting the read cursor to 0 - pub(super) fn start_sample_and_wait(&mut self) -> Result<(), Error> { + pub(super) fn start_sample_and_wait(&mut self) -> Result<(), BlockingImplError> { self.set_read_register_address(0x00)?; self.delay.delay_us(INTERMESSAGE_DELAY_MICROSEC); Ok(()) @@ -79,38 +81,41 @@ where /// increments the register read postion on each read operation, and also on /// every write operation. /// This should be called before a read operation to ensure you get the correct data - pub(super) fn set_read_register_address(&mut self, byte0: u8) -> Result<(), Error> { + pub(super) fn set_read_register_address( + &mut self, + byte0: u8, + ) -> Result<(), BlockingImplError> { self.i2cdev .write(EXT_I2C_ADDR as u8, &[byte0]) - .map_err(Error::I2C) + .map_err(BlockingImplError::I2C) .and(Ok(())) } /// Set a single register at target address - pub(super) fn set_register(&mut self, addr: u8, byte1: u8) -> Result<(), Error> { + pub(super) fn set_register(&mut self, addr: u8, byte1: u8) -> Result<(), BlockingImplError> { self.i2cdev .write(EXT_I2C_ADDR as u8, &[addr, byte1]) - .map_err(Error::I2C) + .map_err(BlockingImplError::I2C) .and(Ok(())) } /// Read the button/axis data from the classic controller - pub(super) fn read_report(&mut self) -> Result> { + pub(super) fn read_report(&mut self) -> Result> { let mut buffer: ExtReport = ExtReport::default(); self.i2cdev .read(EXT_I2C_ADDR as u8, &mut buffer) - .map_err(Error::I2C) + .map_err(BlockingImplError::I2C) .and(Ok(buffer)) } - pub(super) fn enable_hires(&mut self) -> Result<(), Error> { + pub(super) fn enable_hires(&mut self) -> Result<(), BlockingImplError> { self.delay.delay_us(INTERMESSAGE_DELAY_MICROSEC * 2); self.set_register(0xFE, 0x03)?; self.delay.delay_us(INTERMESSAGE_DELAY_MICROSEC * 2); Ok(()) } - pub(super) fn disable_hires(&mut self) -> Result<(), Error> { + pub(super) fn disable_hires(&mut self) -> Result<(), BlockingImplError> { self.delay.delay_us(INTERMESSAGE_DELAY_MICROSEC * 2); self.set_register(0xFE, 0x01)?; self.delay.delay_us(INTERMESSAGE_DELAY_MICROSEC * 2); @@ -118,11 +123,11 @@ where } /// Read a high-resolution version of the button/axis data from the classic controller - pub(super) fn read_hd_report(&mut self) -> Result> { + pub(super) fn read_hd_report(&mut self) -> Result> { let mut buffer: ExtHdReport = ExtHdReport::default(); self.i2cdev .read(EXT_I2C_ADDR as u8, &mut buffer) - .map_err(Error::I2C) + .map_err(BlockingImplError::I2C) .and(Ok(buffer)) } } diff --git a/wii-ext/src/blocking_impl/nunchuk.rs b/wii-ext/src/blocking_impl/nunchuk.rs index caeed40..2204fe8 100644 --- a/wii-ext/src/blocking_impl/nunchuk.rs +++ b/wii-ext/src/blocking_impl/nunchuk.rs @@ -7,7 +7,7 @@ // TODO: nunchuk technically supports HD report, but the last two bytes will be zeroes // work out if it's worth supporting that -use crate::blocking_impl::interface::{Error, Interface}; +use crate::blocking_impl::interface::{BlockingImplError, Interface}; use crate::core::nunchuk::{CalibrationData, NunchukReading, NunchukReadingCalibrated}; use crate::core::ControllerType; use embedded_hal::i2c::{I2c, SevenBitAddress}; @@ -33,7 +33,7 @@ where /// This method will open the provide i2c device file and will /// send the required init sequence in order to read data in /// the future. - pub fn new(i2cdev: I2C, delay: DELAY) -> Result, Error> { + pub fn new(i2cdev: I2C, delay: DELAY) -> Result, BlockingImplError> { let interface = Interface::new(i2cdev, delay); let mut nunchuk = Nunchuk { interface, @@ -47,7 +47,7 @@ where /// /// Since each device will have different tolerances, we take a snapshot of some analog data /// to use as the "baseline" center. - pub fn update_calibration(&mut self) -> Result<(), Error> { + pub fn update_calibration(&mut self) -> Result<(), BlockingImplError> { let data = self.read_report_blocking()?; self.calibration = CalibrationData { @@ -58,7 +58,7 @@ where } /// Send the init sequence to the Wii extension controller - pub fn init(&mut self) -> Result<(), Error> { + pub fn init(&mut self) -> Result<(), BlockingImplError> { // These registers must be written to disable encryption.; the documentation is a bit // lacking but it appears this is some kind of handshake to // perform unencrypted data tranfers @@ -66,24 +66,26 @@ where self.update_calibration() } - pub fn identify_controller(&mut self) -> Result, Error> { + pub fn identify_controller( + &mut self, + ) -> Result, BlockingImplError> { self.interface.identify_controller() } /// Read the button/axis data from the nunchuk - fn read_nunchuk(&mut self) -> Result> { + fn read_nunchuk(&mut self) -> Result> { let buf = self.interface.read_report()?; - NunchukReading::from_data(&buf).ok_or(Error::InvalidInputData) + NunchukReading::from_data(&buf).ok_or(BlockingImplError::InvalidInputData) } /// Simple blocking read helper that will start a sample, wait `INTERMESSAGE_DELAY_MICROSEC`, then read the value - pub fn read_report_blocking(&mut self) -> Result> { + pub fn read_report_blocking(&mut self) -> Result> { self.interface.start_sample()?; self.read_nunchuk() } /// Do a read, and report axis values relative to calibration - pub fn read_blocking(&mut self) -> Result> { + pub fn read_blocking(&mut self) -> Result> { Ok(NunchukReadingCalibrated::new( self.read_report_blocking()?, &self.calibration,