1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use crate::{
controller::Controller,
error::MouseError,
flags::{MouseMovementFlags, MouseStatusFlags},
COMMAND_ACKNOWLEDGED, RESEND, SELF_TEST_FAILED, SELF_TEST_PASSED,
};
pub use self::mouse_type::MouseType;
mod mouse_type;
const VALID_RESOLUTIONS: [u8; 4] = [0, 1, 2, 3];
const VALID_SAMPLE_RATES: [u8; 7] = [10, 20, 40, 60, 80, 100, 200];
type Result<T> = core::result::Result<T, MouseError>;
#[repr(u8)]
enum Command {
SetScaling1To1 = 0xe6,
SetScaling2To1 = 0xe7,
SetResolution = 0xe8,
StatusRequest = 0xe9,
SetStreamMode = 0xea,
ReadData = 0xeb,
ResetWrapMode = 0xec,
SetWrapMode = 0xee,
SetRemoteMode = 0xf0,
GetDeviceID = 0xf2,
SetSampleRate = 0xf3,
EnableDataReporting = 0xf4,
DisableDataReporting = 0xf5,
SetDefaults = 0xf6,
ResendLastPacket = 0xfe,
ResetAndSelfTest = 0xff,
}
#[derive(Debug)]
pub struct Mouse<'c> {
controller: &'c mut Controller,
}
impl<'c> Mouse<'c> {
pub(crate) const fn new(controller: &'c mut Controller) -> Self {
Self { controller }
}
fn check_response(&mut self) -> Result<()> {
match self.controller.read_data()? {
COMMAND_ACKNOWLEDGED => Ok(()),
RESEND => Err(MouseError::Resend),
other => Err(MouseError::InvalidResponse(other)),
}
}
fn write_command(&mut self, command: Command, data: Option<u8>) -> Result<()> {
self.controller.write_mouse(command as u8)?;
self.check_response()?;
if let Some(data) = data {
self.controller.write_data(data as u8)?;
self.check_response()?;
}
Ok(())
}
pub fn set_scaling_one_to_one(&mut self) -> Result<()> {
self.write_command(Command::SetScaling1To1, None)
}
pub fn set_scaling_two_to_one(&mut self) -> Result<()> {
self.write_command(Command::SetScaling2To1, None)
}
pub fn set_resolution(&mut self, resolution: u8) -> Result<()> {
if !VALID_RESOLUTIONS.contains(&resolution) {
return Err(MouseError::InvalidResolution(resolution));
}
let resolution_index = VALID_RESOLUTIONS
.iter()
.position(|&n| n == resolution)
.unwrap() as u8;
self.write_command(Command::SetResolution, Some(resolution_index))
}
pub fn get_status_packet(&mut self) -> Result<(MouseStatusFlags, u8, u8)> {
self.write_command(Command::StatusRequest, None)?;
let status = MouseStatusFlags::from_bits_truncate(self.controller.read_data()?);
let resolution = self.controller.read_data()?;
let sample_rate = self.controller.read_data()?;
if !VALID_RESOLUTIONS.contains(&resolution) {
return Err(MouseError::InvalidResolution(resolution));
}
if !VALID_SAMPLE_RATES.contains(&sample_rate) {
return Err(MouseError::InvalidSampleRate(sample_rate));
}
Ok((status, resolution, sample_rate))
}
pub fn set_stream_mode(&mut self) -> Result<()> {
self.write_command(Command::SetStreamMode, None)
}
pub fn request_data_packet(&mut self) -> Result<(MouseMovementFlags, i16, i16)> {
self.write_command(Command::ReadData, None)?;
self.read_data_packet()
}
pub fn read_data_packet(&mut self) -> Result<(MouseMovementFlags, i16, i16)> {
let movement_flags = MouseMovementFlags::from_bits_truncate(self.controller.read_data()?);
let mut x_movement = self.controller.read_data()? as u16;
let mut y_movement = self.controller.read_data()? as u16;
if movement_flags.contains(MouseMovementFlags::X_SIGN_BIT) {
x_movement |= 0xff00;
}
if movement_flags.contains(MouseMovementFlags::Y_SIGN_BIT) {
y_movement |= 0xff00;
}
Ok((movement_flags, x_movement as i16, y_movement as i16))
}
pub fn reset_wrap_mode(&mut self) -> Result<()> {
self.write_command(Command::ResetWrapMode, None)
}
pub fn set_wrap_mode(&mut self) -> Result<()> {
self.write_command(Command::SetWrapMode, None)
}
pub fn set_remote_mode(&mut self) -> Result<()> {
self.write_command(Command::SetRemoteMode, None)
}
pub fn get_mouse_type(&mut self) -> Result<MouseType> {
self.write_command(Command::GetDeviceID, None)?;
Ok(MouseType::from(self.controller.read_data()?))
}
pub fn set_sample_rate(&mut self, sample_rate: u8) -> Result<()> {
if !VALID_SAMPLE_RATES.contains(&sample_rate) {
return Err(MouseError::InvalidSampleRate(sample_rate));
}
self.write_command(Command::SetSampleRate, Some(sample_rate))
}
pub fn enable_data_reporting(&mut self) -> Result<()> {
self.write_command(Command::EnableDataReporting, None)
}
pub fn disable_data_reporting(&mut self) -> Result<()> {
self.write_command(Command::DisableDataReporting, None)
}
pub fn set_defaults(&mut self) -> Result<()> {
self.write_command(Command::SetDefaults, None)
}
pub fn resend_last_packet(&mut self) -> Result<()> {
Ok(self
.controller
.write_mouse(Command::ResendLastPacket as u8)?)
}
pub fn reset_and_self_test(&mut self) -> Result<()> {
self.write_command(Command::ResetAndSelfTest, None)?;
let result = match self.controller.read_data()? {
SELF_TEST_PASSED => Ok(()),
SELF_TEST_FAILED => Err(MouseError::SelfTestFailed),
RESEND => Err(MouseError::Resend),
other => Err(MouseError::InvalidResponse(other)),
};
let _device_id = self.controller.read_data()?;
result
}
}