use crate::async_impl::interface::{AsyncImplError, InterfaceAsync};
use crate::core::classic::*;
use crate::core::ControllerType;
use embedded_hal_async;
#[derive(Debug, Default)]
pub struct Classic<I2C, Delay> {
interface: InterfaceAsync<I2C, Delay>,
hires: bool,
calibration: CalibrationData,
}
impl<I2C, Delay> Classic<I2C, Delay>
where
I2C: embedded_hal_async::i2c::I2c,
Delay: embedded_hal_async::delay::DelayNs,
{
pub fn new(i2cdev: I2C, delay: Delay) -> Self {
let interface = InterfaceAsync::new(i2cdev, delay);
Self {
interface,
hires: false,
calibration: CalibrationData::default(),
}
}
pub fn destroy(self) -> (I2C, Delay) {
self.interface.destroy()
}
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,
joystick_left_y: data.joystick_left_y,
joystick_right_x: data.joystick_right_x,
joystick_right_y: data.joystick_right_y,
trigger_left: data.trigger_left,
trigger_right: data.trigger_left,
};
Ok(())
}
pub async fn init(&mut self) -> Result<(), AsyncImplError> {
self.interface.init().await?;
self.update_calibration().await?;
Ok(())
}
async fn read_report(&mut self) -> Result<ClassicReading, AsyncImplError> {
if self.hires {
let buf = self.interface.read_hd_report().await?;
ClassicReading::from_data(&buf).ok_or(AsyncImplError::InvalidInputData)
} else {
let buf = self.interface.read_ext_report().await?;
ClassicReading::from_data(&buf).ok_or(AsyncImplError::InvalidInputData)
}
}
pub async fn read(&mut self) -> Result<ClassicReadingCalibrated, AsyncImplError> {
Ok(ClassicReadingCalibrated::new(
self.read_report().await?,
&self.calibration,
))
}
pub async fn enable_hires(&mut self) -> Result<(), AsyncImplError> {
self.interface.enable_hires().await
}
pub async fn identify_controller(&mut self) -> Result<Option<ControllerType>, AsyncImplError> {
self.interface.identify_controller().await
}
}