use std::sync::Arc;
use hidpp::channel::HidppChannel;
use crate::route::DeviceRoute;
use crate::smartshift::SmartShiftMode;
use crate::smartshift::SmartShiftStatus;
use super::WriteError;
use super::dpi::{DpiInfo, get_dpi_info_on_channel, set_dpi_on_channel};
use super::fn_lock::set_fn_lock_on_channel;
use super::lighting::{LightingMethod, set_keyboard_color_with_on_channel};
use super::smartshift::{
get_smartshift_status_on_channel, 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 get_dpi_info_on(shared: &SharedChannel) -> Result<DpiInfo, WriteError> {
get_dpi_info_on_channel(&shared.channel, shared.route.device_index()).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 get_smartshift_status_on(
shared: &SharedChannel,
) -> Result<SmartShiftStatus, WriteError> {
get_smartshift_status_on_channel(&shared.channel, shared.route.device_index()).await
}
pub async fn set_fn_lock_on(shared: &SharedChannel, on: bool) -> Result<(), WriteError> {
set_fn_lock_on_channel(&shared.channel, shared.route.device_index(), on).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
}
pub async fn set_keyboard_color_on(
shared: &SharedChannel,
r: u8,
g: u8,
b: u8,
) -> Result<(), WriteError> {
set_keyboard_color_with_on(shared, LightingMethod::Auto, r, g, b).await
}
pub async fn set_keyboard_color_with_on(
shared: &SharedChannel,
method: LightingMethod,
r: u8,
g: u8,
b: u8,
) -> Result<(), WriteError> {
set_keyboard_color_with_on_channel(
&shared.channel,
shared.route.device_index(),
method,
r,
g,
b,
)
.await
}