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
//! Implements the `Illumination` feature (ID `0x1990`) for devices with a
//! controllable illumination light (brightness in Lumens and color temperature
//! in Kelvin).
//!
//! Brightness and color temperature share the same control shape — an info
//! query, a value get/set, and a level-list get/set — exposed as two parallel
//! sets of methods. Feature version 1 adds the effective-maximum brightness
//! query and its events.
//!
//! All multi-byte fields in this feature are big-endian.
pub mod event;
pub mod types;
#[cfg(test)]
mod tests;
use openlogi_hidpp_derive::Feature;
pub use event::IlluminationEvent;
pub use types::{
BrightnessClampedSource, ControlCapabilities, ControlInfo, IlluminationState, LevelConfig,
SetLevels,
};
use self::types::{be16, illumination_state};
use crate::{
feature::{EventSource, FeatureEndpoint},
protocol::v20::{self, ErrorType, Hidpp20Error},
};
// Function ids. Color-temperature functions mirror the brightness ones offset by
// five, but they are spelled out for clarity.
const FN_GET_ILLUMINATION: u8 = 0;
const FN_SET_ILLUMINATION: u8 = 1;
const FN_GET_BRIGHTNESS_INFO: u8 = 2;
const FN_GET_BRIGHTNESS: u8 = 3;
const FN_SET_BRIGHTNESS: u8 = 4;
const FN_GET_BRIGHTNESS_LEVELS: u8 = 5;
const FN_SET_BRIGHTNESS_LEVELS: u8 = 6;
const FN_GET_COLOR_TEMPERATURE_INFO: u8 = 7;
const FN_GET_COLOR_TEMPERATURE: u8 = 8;
const FN_SET_COLOR_TEMPERATURE: u8 = 9;
const FN_GET_COLOR_TEMPERATURE_LEVELS: u8 = 10;
const FN_SET_COLOR_TEMPERATURE_LEVELS: u8 = 11;
const FN_GET_BRIGHTNESS_EFFECTIVE_MAX: u8 = 12;
/// Implements the `Illumination` / `0x1990` feature.
#[derive(Feature)]
#[creatable(id = 0x1990, version = 0)]
pub struct IlluminationFeature {
/// The endpoint this feature talks to.
endpoint: FeatureEndpoint,
/// Publishes decoded events to listeners.
events: EventSource<IlluminationEvent>,
}
impl IlluminationFeature {
/// Retrieves whether the illumination is on.
pub async fn get_illumination(&self) -> Result<IlluminationState, Hidpp20Error> {
let payload = self
.endpoint
.call(FN_GET_ILLUMINATION, [0; 3])
.await?
.extend_payload();
illumination_state(payload[0])
}
/// Turns the illumination on or off.
pub async fn set_illumination(&self, state: IlluminationState) -> Result<(), Hidpp20Error> {
self.endpoint
.call(FN_SET_ILLUMINATION, [u8::from(state), 0, 0])
.await?;
Ok(())
}
/// Retrieves the brightness capabilities and range (in Lumens).
pub async fn get_brightness_info(&self) -> Result<ControlInfo, Hidpp20Error> {
self.read_info(FN_GET_BRIGHTNESS_INFO).await
}
/// Retrieves the current brightness (in Lumens).
pub async fn get_brightness(&self) -> Result<u16, Hidpp20Error> {
self.read_value(FN_GET_BRIGHTNESS).await
}
/// Sets the brightness (in Lumens).
///
/// The value must be within `[min, max]` and on the resolution grid from
/// [`Self::get_brightness_info`]. On devices with a dynamic maximum a value
/// above the effective maximum is clamped (see
/// [`IlluminationEvent::BrightnessClamped`]).
pub async fn set_brightness(&self, brightness: u16) -> Result<(), Hidpp20Error> {
self.write_value(FN_SET_BRIGHTNESS, brightness).await
}
/// Retrieves the brightness level configuration starting at `start_index`
/// (ignored for linear levels).
pub async fn get_brightness_levels(
&self,
start_index: u8,
) -> Result<LevelConfig, Hidpp20Error> {
self.read_levels(FN_GET_BRIGHTNESS_LEVELS, start_index)
.await
}
/// Writes the brightness level configuration.
pub async fn set_brightness_levels(&self, levels: &SetLevels) -> Result<(), Hidpp20Error> {
self.write_levels(FN_SET_BRIGHTNESS_LEVELS, levels).await
}
/// Retrieves the current effective maximum brightness (in Lumens), or `0`
/// when none is in effect. Requires feature version 1.
pub async fn get_brightness_effective_max(&self) -> Result<u16, Hidpp20Error> {
self.read_value(FN_GET_BRIGHTNESS_EFFECTIVE_MAX).await
}
/// Retrieves the color-temperature capabilities and range (in Kelvin).
pub async fn get_color_temperature_info(&self) -> Result<ControlInfo, Hidpp20Error> {
self.read_info(FN_GET_COLOR_TEMPERATURE_INFO).await
}
/// Retrieves the current color temperature (in Kelvin).
pub async fn get_color_temperature(&self) -> Result<u16, Hidpp20Error> {
self.read_value(FN_GET_COLOR_TEMPERATURE).await
}
/// Sets the color temperature (in Kelvin).
///
/// The value must be within `[min, max]` and on the resolution grid from
/// [`Self::get_color_temperature_info`].
pub async fn set_color_temperature(&self, color_temperature: u16) -> Result<(), Hidpp20Error> {
self.write_value(FN_SET_COLOR_TEMPERATURE, color_temperature)
.await
}
/// Retrieves the color-temperature level configuration starting at
/// `start_index` (ignored for linear levels).
pub async fn get_color_temperature_levels(
&self,
start_index: u8,
) -> Result<LevelConfig, Hidpp20Error> {
self.read_levels(FN_GET_COLOR_TEMPERATURE_LEVELS, start_index)
.await
}
/// Writes the color-temperature level configuration.
pub async fn set_color_temperature_levels(
&self,
levels: &SetLevels,
) -> Result<(), Hidpp20Error> {
self.write_levels(FN_SET_COLOR_TEMPERATURE_LEVELS, levels)
.await
}
/// Shared `get<Control>Info` reader.
///
/// The fields fill eight bytes, more than a short reply holds, so the
/// reply must be long: a short one would zero-fill the range and the
/// level count. Logi Options+ also refuses fewer than eight bytes
/// (reverse-engineered from its `getControlInfo`).
async fn read_info(&self, function: u8) -> Result<ControlInfo, Hidpp20Error> {
let v20::Message::Long(_, payload) = self.endpoint.call(function, [0; 3]).await? else {
return Err(Hidpp20Error::UnsupportedResponse);
};
Ok(ControlInfo::from_payload(&payload))
}
/// Shared `get<Control>` / effective-max reader for a big-endian `u16`.
async fn read_value(&self, function: u8) -> Result<u16, Hidpp20Error> {
let payload = self.endpoint.call(function, [0; 3]).await?.extend_payload();
Ok(be16(&payload, 0))
}
/// Shared `set<Control>` writer for a big-endian `u16`.
async fn write_value(&self, function: u8, value: u16) -> Result<(), Hidpp20Error> {
let [hi, lo] = value.to_be_bytes();
self.endpoint.call(function, [hi, lo, 0]).await?;
Ok(())
}
/// Shared `get<Control>Levels` reader.
///
/// A level page is the whole long payload, so a short reply is refused
/// rather than read as an empty or linear table. Logi Options+ refuses
/// fewer than sixteen bytes (reverse-engineered from its
/// `getControlLevels`).
async fn read_levels(
&self,
function: u8,
start_index: u8,
) -> Result<LevelConfig, Hidpp20Error> {
if start_index > 0x0f {
return Err(Hidpp20Error::Feature(ErrorType::InvalidArgument));
}
// The request carries the start index in the high nibble of byte 0.
let v20::Message::Long(_, payload) = self
.endpoint
.call(function, [start_index << 4, 0, 0])
.await?
else {
return Err(Hidpp20Error::UnsupportedResponse);
};
Ok(LevelConfig::from_payload(&payload))
}
/// Shared `set<Control>Levels` writer.
///
/// A non-linear table longer than one request goes out page by page. The
/// first rejected page stops the write, leaving the earlier pages applied.
async fn write_levels(&self, function: u8, levels: &SetLevels) -> Result<(), Hidpp20Error> {
for payload in levels.to_payloads()? {
self.endpoint.call_long(function, payload).await?;
}
Ok(())
}
}