use std::time::Duration;
use nusb::Interface;
use nusb::transfer::{ControlOut, ControlType, Recipient};
use smol::Timer;
use crate::devices::abort;
const RESET_CONTROLLER: ControlOut = ControlOut {
control_type: ControlType::Vendor,
recipient: Recipient::Device,
request: 0x00,
value: 0x0000,
index: 0,
data: &[],
};
const BAUD_RATE: ControlOut = ControlOut {
control_type: ControlType::Vendor,
recipient: Recipient::Device,
request: 0x03,
value: 0x001A, index: 0,
data: &[],
};
const EIGHT_DATA_ONE_STOP_NO_PARITY: ControlOut = ControlOut {
control_type: ControlType::Vendor,
recipient: Recipient::Device,
request: 0x04,
value: 0x0008, index: 0,
data: &[],
};
const PURGE_RX: ControlOut = ControlOut {
control_type: ControlType::Vendor,
recipient: Recipient::Device,
request: 0x00,
value: 0x0001, index: 0,
data: &[],
};
const PURGE_TX: ControlOut = ControlOut {
control_type: ControlType::Vendor,
recipient: Recipient::Device,
request: 0x00,
value: 0x0002, index: 0,
data: &[],
};
const FLOW_CONTROL: ControlOut = ControlOut {
control_type: ControlType::Vendor,
recipient: Recipient::Device,
request: 0x02,
value: 0x0200, index: 0,
data: &[],
};
const RTS: ControlOut = ControlOut {
control_type: ControlType::Vendor,
recipient: Recipient::Device,
request: 0x01,
value: 0x0202, index: 0,
data: &[],
};
pub(super) async fn init(interface: &Interface) {
let control_out = async |control_out: ControlOut| {
interface
.control_out(control_out)
.await
.status
.unwrap_or_else(|e| abort(format!("Control transfer failed : {}", e)))
};
control_out(RESET_CONTROLLER).await;
control_out(BAUD_RATE).await;
control_out(EIGHT_DATA_ONE_STOP_NO_PARITY).await;
Timer::after(Duration::from_millis(50)).await; control_out(PURGE_RX).await;
control_out(PURGE_TX).await;
Timer::after(Duration::from_millis(50)).await; control_out(FLOW_CONTROL).await;
control_out(RTS).await;
}