use std::sync::Arc;
use hidpp::channel::HidppChannel;
use crate::route::DeviceRoute;
use crate::smartshift::SmartShiftMode;
use super::WriteError;
use super::dpi::set_dpi_on_channel;
use super::smartshift::{set_smartshift_on_channel, toggle_smartshift_on_channel};
#[derive(Clone)]
pub struct SharedChannel {
channel: Arc<HidppChannel>,
route: DeviceRoute,
}
impl SharedChannel {
#[must_use]
pub(crate) fn new(channel: Arc<HidppChannel>, route: DeviceRoute) -> Self {
Self { channel, route }
}
#[must_use]
pub fn matches(&self, route: &DeviceRoute) -> bool {
self.route == *route
}
pub(crate) fn channel(&self) -> &Arc<HidppChannel> {
&self.channel
}
pub(crate) fn device_index(&self) -> u8 {
self.route.device_index()
}
}
pub async fn set_dpi_on(shared: &SharedChannel, dpi: u16) -> Result<(), WriteError> {
set_dpi_on_channel(&shared.channel, shared.route.device_index(), dpi).await
}
pub async fn toggle_smartshift_on(shared: &SharedChannel) -> Result<SmartShiftMode, WriteError> {
toggle_smartshift_on_channel(&shared.channel, shared.route.device_index()).await
}
pub async fn set_smartshift_on(
shared: &SharedChannel,
mode: SmartShiftMode,
auto_disengage: u8,
tunable_torque: u8,
) -> Result<(), WriteError> {
set_smartshift_on_channel(
&shared.channel,
shared.route.device_index(),
mode,
auto_disengage,
tunable_torque,
)
.await
}