Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename error types for blocking/async impls #15

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions wii-ext/src/async_impl/classic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -73,40 +73,38 @@ where
}

/// poll the controller for the latest data
async fn read_classic_report(&mut self) -> Result<ClassicReading, ClassicAsyncError> {
async fn read_classic_report(&mut self) -> Result<ClassicReading, AsyncImplError> {
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<ClassicReading, ClassicAsyncError> {
async fn read_report(&mut self) -> Result<ClassicReading, AsyncImplError> {
self.read_classic_report().await
}

/// Do a read, and report axis values relative to calibration
pub async fn read(&mut self) -> Result<ClassicReadingCalibrated, ClassicAsyncError> {
pub async fn read(&mut self) -> Result<ClassicReadingCalibrated, AsyncImplError> {
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<ControllerIdReport, ClassicAsyncError> {
pub async fn read_id(&mut self) -> Result<ControllerIdReport, AsyncImplError> {
self.interface.read_id().await
}

pub async fn identify_controller(
&mut self,
) -> Result<Option<ControllerType>, ClassicAsyncError> {
pub async fn identify_controller(&mut self) -> Result<Option<ControllerType>, AsyncImplError> {
self.interface.identify_controller().await
}
}
36 changes: 16 additions & 20 deletions wii-ext/src/async_impl/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -53,33 +53,33 @@ where
}

/// Read the button/axis data from the classic controller
pub(super) async fn read_ext_report(&mut self) -> Result<ExtReport, ClassicAsyncError> {
pub(super) async fn read_ext_report(&mut self) -> Result<ExtReport, AsyncImplError> {
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<ExtHdReport, ClassicAsyncError> {
pub(super) async fn read_hd_report(&mut self) -> Result<ExtHdReport, AsyncImplError> {
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
Expand All @@ -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(())
Expand All @@ -114,61 +114,57 @@ 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(()))
}

pub(super) async fn set_register_with_delay(
&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<ControllerIdReport, ClassicAsyncError> {
pub(super) async fn read_id(&mut self) -> Result<ControllerIdReport, AsyncImplError> {
self.set_read_register_address(0xfa).await?;
let i2c_id = self.read_ext_report().await?;
Ok(i2c_id)
}

pub(super) async fn identify_controller(
&mut self,
) -> Result<Option<ControllerType>, ClassicAsyncError> {
) -> Result<Option<ControllerType>, 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(())
}
Expand Down
2 changes: 1 addition & 1 deletion wii-ext/src/async_impl/nunchuk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 13 additions & 13 deletions wii-ext/src/blocking_impl/classic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Classic<T, DELAY>, Error<E>> {
pub fn new(i2cdev: T, delay: DELAY) -> Result<Classic<T, DELAY>, BlockingImplError<E>> {
let interface = Interface::new(i2cdev, delay);
let mut classic = Classic {
interface,
Expand All @@ -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<E>> {
pub fn update_calibration(&mut self) -> Result<(), BlockingImplError<E>> {
let data = self.read_report_blocking()?;

self.calibration = CalibrationData {
Expand All @@ -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<E>> {
pub fn init(&mut self) -> Result<(), BlockingImplError<E>> {
// 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
Expand All @@ -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<E>> {
pub fn enable_hires(&mut self) -> Result<(), BlockingImplError<E>> {
self.interface.enable_hires()?;
self.hires = true;
self.update_calibration()?;
Expand All @@ -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<E>> {
fn disable_hires(&mut self) -> Result<(), BlockingImplError<E>> {
self.interface.disable_hires()?;
self.hires = false;
self.update_calibration()?;
Ok(())
}

pub fn identify_controller(&mut self) -> Result<Option<ControllerType>, Error<E>> {
pub fn identify_controller(&mut self) -> Result<Option<ControllerType>, BlockingImplError<E>> {
self.interface.identify_controller()
}

/// poll the controller for the latest data
fn read_classic_report(&mut self) -> Result<ClassicReading, Error<E>> {
fn read_classic_report(&mut self) -> Result<ClassicReading, BlockingImplError<E>> {
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<ClassicReading, Error<E>> {
pub fn read_classic_no_wait(&mut self) -> Result<ClassicReading, BlockingImplError<E>> {
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<ClassicReading, Error<E>> {
pub fn read_report_blocking(&mut self) -> Result<ClassicReading, BlockingImplError<E>> {
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<ClassicReadingCalibrated, Error<E>> {
pub fn read_blocking(&mut self) -> Result<ClassicReadingCalibrated, BlockingImplError<E>> {
Ok(ClassicReadingCalibrated::new(
self.read_report_blocking()?,
&self.calibration,
Expand Down
Loading