Skip to content

Commit

Permalink
Move data/functions from common into core
Browse files Browse the repository at this point in the history
  • Loading branch information
9names committed Apr 15, 2024
1 parent 592f556 commit cf22d2b
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 70 deletions.
2 changes: 1 addition & 1 deletion wii-ext/src/classic_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// See `decode_classic_report` and `decode_classic_hd_report` for data format

use crate::core::classic::*;
use crate::core::{ControllerIdReport, ControllerType};
use crate::interface_async::InterfaceAsync;
use crate::{ControllerIdReport, ControllerType};
use embedded_hal_async;

// use core::future::Future;
Expand Down
2 changes: 1 addition & 1 deletion wii-ext/src/classic_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// See `decode_classic_report` and `decode_classic_hd_report` for data format

use crate::core::classic::*;
use crate::core::ControllerType;
use crate::interface::Interface;
use crate::ControllerType;
use embedded_hal::i2c::I2c;

#[cfg(feature = "defmt_print")]
Expand Down
41 changes: 0 additions & 41 deletions wii-ext/src/common.rs

This file was deleted.

42 changes: 42 additions & 0 deletions wii-ext/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
pub mod classic;
pub mod nunchuk;

/// Standard input report
pub type ExtReport = [u8; 6];
/// HD input report
pub type ExtHdReport = [u8; 8];
/// Controller ID report
pub type ControllerIdReport = [u8; 6];

#[cfg_attr(feature = "defmt_print", derive(defmt::Format))]
#[derive(Debug)]
pub enum ControllerType {
Nunchuk,
Classic,
ClassicPro,
}

/// All Wii extension controllers use i2c address 52
pub const EXT_I2C_ADDR: u16 = 0x52;

/// There needs to be some time between i2c messages or the
/// wii ext device will abort the i2c transaction
/// 200 microseconds works in my tests - need to test with more devices
pub const INTERMESSAGE_DELAY_MICROSEC_U32: u32 = 200;

pub fn identify_controller(id: ControllerIdReport) -> Option<ControllerType> {
if id[2] != 0xA4 || id[3] != 0x20 {
// Not an extension controller
None
} else if id[0] == 0 && id[1] == 0 && id[4] == 0 && id[5] == 0 {
// It's a nunchuck
Some(ControllerType::Nunchuk)
} else if id[0] == 0 && id[1] == 0 && id[4] == 3 && id[5] == 1 {
// It's a wii classic controller
Some(ControllerType::Classic)
} else if id[0] == 1 && id[1] == 0 && id[4] == 1 && id[5] == 1 {
// It's a wii classic pro (or compatible) controller
// This is most wii classic extension controllers (NES/SNES/Clones)
Some(ControllerType::ClassicPro)
} else {
None
}
}
14 changes: 7 additions & 7 deletions wii-ext/src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::common::ControllerIdReport;
use crate::common::ExtHdReport;
use crate::common::ExtReport;
use crate::ControllerType;
use crate::EXT_I2C_ADDR;
use crate::INTERMESSAGE_DELAY_MICROSEC_U32 as INTERMESSAGE_DELAY_MICROSEC;
use crate::core::ControllerIdReport;
use crate::core::ControllerType;
use crate::core::ExtHdReport;
use crate::core::ExtReport;
use crate::core::EXT_I2C_ADDR;
use crate::core::INTERMESSAGE_DELAY_MICROSEC_U32 as INTERMESSAGE_DELAY_MICROSEC;
use embedded_hal::i2c::I2c;
use embedded_hal::i2c::SevenBitAddress;

Expand Down Expand Up @@ -60,7 +60,7 @@ where

pub(super) fn identify_controller(&mut self) -> Result<Option<ControllerType>, Error<E>> {
let i2c_id = self.read_id()?;
Ok(crate::common::identify_controller(i2c_id))
Ok(crate::core::identify_controller(i2c_id))
}

/// tell the extension controller to prepare a sample by setting the read cursor to 0
Expand Down
14 changes: 7 additions & 7 deletions wii-ext/src/interface_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
//
// See `decode_classic_report` and `decode_classic_hd_report` for data format

use crate::ControllerIdReport;
use crate::ControllerType;
use crate::ExtHdReport;
use crate::ExtReport;
use crate::EXT_I2C_ADDR;
use crate::INTERMESSAGE_DELAY_MICROSEC_U32;
use crate::core::ControllerIdReport;
use crate::core::ControllerType;
use crate::core::ExtHdReport;
use crate::core::ExtReport;
use crate::core::EXT_I2C_ADDR;
use crate::core::INTERMESSAGE_DELAY_MICROSEC_U32;
use embedded_hal_async;

#[cfg(feature = "defmt_print")]
Expand Down Expand Up @@ -166,7 +166,7 @@ where
&mut self,
) -> Result<Option<ControllerType>, ClassicAsyncError> {
let i2c_id = self.read_id().await?;
Ok(crate::common::identify_controller(i2c_id))
Ok(crate::core::identify_controller(i2c_id))
}

/// tell the extension controller to prepare a sample by setting the read cursor to 0
Expand Down
6 changes: 0 additions & 6 deletions wii-ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ pub mod classic_async;
/// Types + data decoding
pub mod core;

/// Anything common between nunchuk + classic
pub mod common;

/// i2c interface code
pub mod interface;
/// async i2c interface code
pub mod interface_async;

pub mod nunchuk;

// Expose all common types at the crate level
pub use common::*;
2 changes: 1 addition & 1 deletion wii-ext/src/nunchuk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// work out if it's worth supporting that

use crate::core::nunchuk::{CalibrationData, NunchukReading, NunchukReadingCalibrated};
use crate::core::ControllerType;
use crate::interface::Interface;
use crate::ControllerType;
use embedded_hal::i2c::{I2c, SevenBitAddress};

#[derive(Debug)]
Expand Down
1 change: 1 addition & 0 deletions wii-ext/tests/classic_hd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use paste::paste;
use wii_ext::classic_sync::*;
use wii_ext::*;
mod common;
use crate::core::EXT_I2C_ADDR;
use common::test_data;

/// There's a certain amount of slop around the center position.
Expand Down
3 changes: 1 addition & 2 deletions wii-ext/tests/classic_pdp_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ 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::*;
use wii_ext::core::classic::ClassicReading;
use wii_ext::*;
mod common;
use common::test_data;
use common::test_data::*;
use wii_ext::core::EXT_I2C_ADDR;

/// There's a certain amount of slop around the center position.
/// Allow up to this range without it being an error
Expand Down
1 change: 1 addition & 0 deletions wii-ext/tests/classic_pdp_clone_hd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use paste::paste;
use wii_ext::classic_sync::*;
use wii_ext::*;
mod common;
use crate::core::EXT_I2C_ADDR;
use common::test_data;

/// There's a certain amount of slop around the center position.
Expand Down
2 changes: 1 addition & 1 deletion wii-ext/tests/classic_pro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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::*;
use wii_ext::core::classic::ClassicReading;
use wii_ext::core::EXT_I2C_ADDR;
mod common;
use common::test_data;
use common::test_data::*;
Expand Down
2 changes: 1 addition & 1 deletion wii-ext/tests/classic_pro_hd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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::*;
use wii_ext::core::EXT_I2C_ADDR;
mod common;
use common::test_data;

Expand Down
2 changes: 1 addition & 1 deletion wii-ext/tests/classic_regular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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::*;
use wii_ext::core::classic::ClassicReading;
use wii_ext::core::EXT_I2C_ADDR;
mod common;
use common::test_data;
use common::test_data::*;
Expand Down
3 changes: 2 additions & 1 deletion wii-ext/tests/nunchuk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use embedded_hal_mock::eh1::{
delay::NoopDelay,
i2c::{self, Transaction},
};
use wii_ext::{nunchuk::Nunchuk, EXT_I2C_ADDR};
use wii_ext::core::EXT_I2C_ADDR;
use wii_ext::nunchuk::Nunchuk;
mod common;
use common::test_data;

Expand Down

0 comments on commit cf22d2b

Please sign in to comment.