openlogi_hid/write/
shared.rs1use std::sync::Arc;
2
3use hidpp::channel::HidppChannel;
4
5use crate::route::DeviceRoute;
6use crate::smartshift::SmartShiftMode;
7use crate::smartshift::SmartShiftStatus;
8
9use super::WriteError;
10use super::dpi::{DpiInfo, get_dpi_info_on_channel, set_dpi_on_channel};
11use super::fn_lock::set_fn_lock_on_channel;
12use super::lighting::{LightingMethod, set_keyboard_color_with_on_channel};
13use super::smartshift::{
14 get_smartshift_status_on_channel, set_smartshift_on_channel, toggle_smartshift_on_channel,
15};
16
17#[derive(Clone)]
24pub struct SharedChannel {
25 channel: Arc<HidppChannel>,
26 route: DeviceRoute,
27}
28
29impl SharedChannel {
30 #[must_use]
32 pub(crate) fn new(channel: Arc<HidppChannel>, route: DeviceRoute) -> Self {
33 Self { channel, route }
34 }
35
36 #[must_use]
39 pub fn matches(&self, route: &DeviceRoute) -> bool {
40 self.route == *route
41 }
42
43 pub(crate) fn channel(&self) -> &Arc<HidppChannel> {
44 &self.channel
45 }
46
47 pub(crate) fn device_index(&self) -> u8 {
48 self.route.device_index()
49 }
50}
51
52pub async fn set_dpi_on(shared: &SharedChannel, dpi: u16) -> Result<(), WriteError> {
55 set_dpi_on_channel(&shared.channel, shared.route.device_index(), dpi).await
56}
57
58pub async fn get_dpi_info_on(shared: &SharedChannel) -> Result<DpiInfo, WriteError> {
60 get_dpi_info_on_channel(&shared.channel, shared.route.device_index()).await
61}
62
63pub async fn toggle_smartshift_on(shared: &SharedChannel) -> Result<SmartShiftMode, WriteError> {
65 toggle_smartshift_on_channel(&shared.channel, shared.route.device_index()).await
66}
67
68pub async fn get_smartshift_status_on(
70 shared: &SharedChannel,
71) -> Result<SmartShiftStatus, WriteError> {
72 get_smartshift_status_on_channel(&shared.channel, shared.route.device_index()).await
73}
74
75pub async fn set_fn_lock_on(shared: &SharedChannel, on: bool) -> Result<(), WriteError> {
78 set_fn_lock_on_channel(&shared.channel, shared.route.device_index(), on).await
79}
80
81pub async fn set_smartshift_on(
84 shared: &SharedChannel,
85 mode: SmartShiftMode,
86 auto_disengage: u8,
87 tunable_torque: u8,
88) -> Result<(), WriteError> {
89 set_smartshift_on_channel(
90 &shared.channel,
91 shared.route.device_index(),
92 mode,
93 auto_disengage,
94 tunable_torque,
95 )
96 .await
97}
98
99pub async fn set_keyboard_color_on(
102 shared: &SharedChannel,
103 r: u8,
104 g: u8,
105 b: u8,
106) -> Result<(), WriteError> {
107 set_keyboard_color_with_on(shared, LightingMethod::Auto, r, g, b).await
108}
109
110pub async fn set_keyboard_color_with_on(
113 shared: &SharedChannel,
114 method: LightingMethod,
115 r: u8,
116 g: u8,
117 b: u8,
118) -> Result<(), WriteError> {
119 set_keyboard_color_with_on_channel(
120 &shared.channel,
121 shared.route.device_index(),
122 method,
123 r,
124 g,
125 b,
126 )
127 .await
128}