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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! Implements the `RgbEffects` feature (ID `0x8071`, version 4), the modern
//! per-cluster RGB effect engine (successor to
//! [`ColorLedEffects`](super::color_led_effects), `0x8070`).
//!
//! A device groups its LEDs into *clusters*, each supporting a set of *effects*.
//! [`get_device_info`](RgbEffectsFeature::get_device_info),
//! [`get_cluster_info`](RgbEffectsFeature::get_cluster_info) and
//! [`get_effect_info`](RgbEffectsFeature::get_effect_info) decode the three
//! general-info modes of the polymorphic `getInfo` function; effects are applied
//! with [`set_rgb_cluster_effect`](RgbEffectsFeature::set_rgb_cluster_effect).
//!
//! Software must first take control with
//! [`set_sw_control`](RgbEffectsFeature::set_sw_control) before applying effects
//! or power modes, or those calls return a "not allowed" error.
//!
//! 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::RgbEffectsEvent;
pub use types::{
ActivityEventType, CLUSTER_EFFECT_PARAM_COUNT, DisplayPersistencyCapabilities,
EventsNotificationFlags, LED_BIN_PARAM_COUNT, LedBinIndex, ONBOARD_INFO_PARAM_COUNT,
PowerModeTarget, RgbClusterInfo, RgbDeviceInfo, RgbEffectInfo, RgbExtCapabilities,
RgbNvCapabilities, RgbNvConfig, RgbPersistence, RgbPowerMode, RgbPowerModeConfig, RgbSwControl,
SlotInfoType, SwControlFlags,
};
use self::types::{ALL_CLUSTERS, ALL_EFFECTS, GetOrSet, be16};
use crate::{
feature::{EventSource, FeatureEndpoint},
protocol::v20::{self, Hidpp20Error},
};
/// `typeOfInfo` value selecting general info in `getInfo`.
const TYPE_GENERAL_INFO: u8 = 0x00;
/// `typeOfInfo` value selecting onboard-stored effect info in `getInfo`.
const TYPE_ONBOARD_EFFECT: u8 = 0x01;
/// `getOrSet` value requesting a backup read in `manageRgbLedBinInfo`.
const GET_BACKUP: u8 = 0x02;
/// Bit offset of the power-mode target in the `setRgbClusterEffect` flags byte.
const POWER_TARGET_SHIFT: u8 = 2;
/// Implements the `RgbEffects` / `0x8071` feature.
#[derive(Feature)]
#[creatable(id = 0x8071, version = 0)]
pub struct RgbEffectsFeature {
/// The endpoint this feature talks to.
endpoint: FeatureEndpoint,
/// Publishes decoded events to listeners.
events: EventSource<RgbEffectsEvent>,
}
impl RgbEffectsFeature {
/// Retrieves device-level RGB information (`getInfo` device mode).
pub async fn get_device_info(&self) -> Result<RgbDeviceInfo, Hidpp20Error> {
let payload = self
.endpoint
.call(0, [ALL_CLUSTERS, ALL_EFFECTS, TYPE_GENERAL_INFO])
.await?
.extend_payload();
Ok(RgbDeviceInfo::from_payload(&payload))
}
/// Retrieves cluster-level information for `cluster_index` (`getInfo` cluster
/// mode).
///
/// The reply must be long and must echo `cluster_index` in byte 0; a short
/// reply (which would zero-fill the fields) or the info of another cluster
/// is refused with [`Hidpp20Error::UnsupportedResponse`].
pub async fn get_cluster_info(
&self,
cluster_index: u8,
) -> Result<RgbClusterInfo, Hidpp20Error> {
let v20::Message::Long(_, payload) = self
.endpoint
.call(0, [cluster_index, ALL_EFFECTS, TYPE_GENERAL_INFO])
.await?
else {
return Err(Hidpp20Error::UnsupportedResponse);
};
let info = RgbClusterInfo::from_payload(&payload);
if info.cluster_index != cluster_index {
return Err(Hidpp20Error::UnsupportedResponse);
}
Ok(info)
}
/// Retrieves effect-level information for an effect of a cluster (`getInfo`
/// effect mode).
pub async fn get_effect_info(
&self,
cluster_index: u8,
cluster_effect_index: u8,
) -> Result<RgbEffectInfo, Hidpp20Error> {
let payload = self
.endpoint
.call(0, [cluster_index, cluster_effect_index, TYPE_GENERAL_INFO])
.await?
.extend_payload();
Ok(RgbEffectInfo::from_payload(&payload))
}
/// Retrieves raw information about an onboard-stored effect slot.
///
/// The returned parameters' meaning depends on `slot_info_type` (see the
/// feature spec): e.g. slot state, defaults, UUID bytes, or effect-name
/// characters.
pub async fn get_onboard_effect_info(
&self,
cluster_index: u8,
cluster_effect_index: u8,
slot: u8,
slot_info_type: SlotInfoType,
) -> Result<[u8; ONBOARD_INFO_PARAM_COUNT], Hidpp20Error> {
let mut args = [0; 16];
args[..5].copy_from_slice(&[
cluster_index,
cluster_effect_index,
TYPE_ONBOARD_EFFECT,
slot,
slot_info_type.into(),
]);
let payload = self.endpoint.call_long(0, args).await?.extend_payload();
let mut params = [0; ONBOARD_INFO_PARAM_COUNT];
params.copy_from_slice(&payload[3..3 + ONBOARD_INFO_PARAM_COUNT]);
Ok(params)
}
/// Applies effect `cluster_effect_index` to `cluster_index`.
///
/// `params` are effect-specific (discoverable via [`Self::get_effect_info`]).
/// `persistence` controls volatile/non-volatile storage and `power_mode`
/// selects which power mode the effect applies to. Requires software control
/// (see [`Self::set_sw_control`]).
///
/// Drive this future to completion even if its requester cancels: it waits
/// for native write completion before bounding the response wait. See
/// [`crate::channel::HidppChannel::send_write_through`].
pub async fn set_rgb_cluster_effect(
&self,
cluster_index: u8,
cluster_effect_index: u8,
params: [u8; CLUSTER_EFFECT_PARAM_COUNT],
persistence: RgbPersistence,
power_mode: PowerModeTarget,
) -> Result<(), Hidpp20Error> {
let mut args = [0; 16];
args[0] = cluster_index;
args[1] = cluster_effect_index;
args[2..2 + CLUSTER_EFFECT_PARAM_COUNT].copy_from_slice(¶ms);
args[12] = persistence.bits() | (u8::from(power_mode) << POWER_TARGET_SHIFT);
self.endpoint.call_long_write_through(1, args).await?;
Ok(())
}
/// Sets the multi-LED pattern of `cluster_index`.
pub async fn set_multi_led_cluster_pattern(
&self,
cluster_index: u8,
pattern: u8,
) -> Result<(), Hidpp20Error> {
self.endpoint.call(2, [cluster_index, pattern, 0]).await?;
Ok(())
}
/// Reads one non-volatile configuration `capability`.
///
/// The reply must be long: its six parameters are what
/// [`Self::set_nv_config`] writes back.
pub async fn get_nv_config(
&self,
capability: RgbNvCapabilities,
) -> Result<RgbNvConfig, Hidpp20Error> {
let [cap_hi, cap_lo] = capability.bits().to_be_bytes();
let v20::Message::Long(_, payload) = self
.endpoint
.call(3, [GetOrSet::Get.into(), cap_hi, cap_lo])
.await?
else {
return Err(Hidpp20Error::UnsupportedResponse);
};
let mut params = [0; 6];
params.copy_from_slice(&payload[4..10]);
Ok(RgbNvConfig {
capability: RgbNvCapabilities::from_bits_retain(be16(&payload, 1)),
state: payload[3],
params,
})
}
/// Writes one non-volatile configuration entry (to EEPROM).
///
/// Outside this crate `config` can only come from [`Self::get_nv_config`]:
/// edit the fields to change, and every other parameter is written back
/// as read.
pub async fn set_nv_config(&self, config: &RgbNvConfig) -> Result<(), Hidpp20Error> {
let [cap_hi, cap_lo] = config.capability.bits().to_be_bytes();
let mut args = [0; 16];
args[..4].copy_from_slice(&[GetOrSet::Set.into(), cap_hi, cap_lo, config.state]);
args[4..10].copy_from_slice(&config.params);
self.endpoint.call_long(3, args).await?;
Ok(())
}
/// Reads raw manufacturing LED bin parameters.
///
/// `backup` reads the backup copy instead of the active one.
pub async fn get_led_bin_info(
&self,
cluster_index: u8,
led_bin_index: LedBinIndex,
backup: bool,
) -> Result<[u8; LED_BIN_PARAM_COUNT], Hidpp20Error> {
let get_or_set = if backup {
GET_BACKUP
} else {
GetOrSet::Get.into()
};
let payload = self
.endpoint
.call(4, [get_or_set, cluster_index, led_bin_index.into()])
.await?
.extend_payload();
let mut params = [0; LED_BIN_PARAM_COUNT];
params.copy_from_slice(&payload[3..3 + LED_BIN_PARAM_COUNT]);
Ok(params)
}
/// Stores raw manufacturing LED bin parameters.
pub async fn set_led_bin_info(
&self,
cluster_index: u8,
led_bin_index: LedBinIndex,
params: [u8; LED_BIN_PARAM_COUNT],
) -> Result<(), Hidpp20Error> {
let mut args = [0; 16];
args[0] = GetOrSet::Set.into();
args[1] = cluster_index;
args[2] = led_bin_index.into();
args[3..3 + LED_BIN_PARAM_COUNT].copy_from_slice(¶ms);
self.endpoint.call_long(4, args).await?;
Ok(())
}
/// Retrieves the software-control and event-notification flags.
pub async fn get_sw_control(&self) -> Result<RgbSwControl, Hidpp20Error> {
let payload = self
.endpoint
.call(5, [GetOrSet::Get.into(), 0, 0])
.await?
.extend_payload();
Ok(RgbSwControl {
control: SwControlFlags::from_bits_retain(payload[1]),
events: EventsNotificationFlags::from_bits_retain(payload[2]),
})
}
/// Sets the software-control and event-notification flags.
///
/// Drive to completion, as for [`Self::set_rgb_cluster_effect`]. The
/// [v4 spec, pp. 26–28](https://drive.google.com/file/d/1lecrJQgAC7wnXlo8PEnwVSb3L0GioALG/view)
/// returns all three request fields; matching them keeps a late claim ACK
/// with different flags from satisfying a restore request.
pub async fn set_sw_control(
&self,
control: SwControlFlags,
events: EventsNotificationFlags,
) -> Result<(), Hidpp20Error> {
self.endpoint
.call_echoed_write_through(5, [GetOrSet::Set.into(), control.bits(), events.bits()])
.await?;
Ok(())
}
/// Applies an effect-sync `drift_value` (milliseconds) correction.
///
/// A `cluster_index` of `0xff` targets all clusters.
pub async fn set_effect_sync_correction(
&self,
cluster_index: u8,
drift_value: i16,
) -> Result<(), Hidpp20Error> {
let [drift_hi, drift_lo] = drift_value.to_be_bytes();
let mut args = [0; 16];
args[..4].copy_from_slice(&[cluster_index, 0, drift_hi, drift_lo]);
self.endpoint.call_long(6, args).await?;
Ok(())
}
/// Retrieves the RGB power-mode configuration.
pub async fn get_power_mode_config(&self) -> Result<RgbPowerModeConfig, Hidpp20Error> {
let payload = self
.endpoint
.call(7, [GetOrSet::Get.into(), 0, 0])
.await?
.extend_payload();
Ok(RgbPowerModeConfig::from_payload(&payload))
}
/// Writes the RGB power-mode configuration.
pub async fn set_power_mode_config(
&self,
config: RgbPowerModeConfig,
) -> Result<(), Hidpp20Error> {
let [flags_hi, flags_lo] = config.flags.to_be_bytes();
let [psave_hi, psave_lo] = config.no_activity_timeout_to_power_save.to_be_bytes();
let [off_hi, off_lo] = config.no_activity_timeout_to_off.to_be_bytes();
let mut args = [0; 16];
args[..7].copy_from_slice(&[
GetOrSet::Set.into(),
flags_hi,
flags_lo,
psave_hi,
psave_lo,
off_hi,
off_lo,
]);
self.endpoint.call_long(7, args).await?;
Ok(())
}
/// Retrieves the current RGB power mode.
pub async fn get_power_mode(&self) -> Result<RgbPowerMode, Hidpp20Error> {
let payload = self
.endpoint
.call(8, [GetOrSet::Get.into(), 0, 0])
.await?
.extend_payload();
RgbPowerMode::try_from(payload[1]).map_err(|_| Hidpp20Error::UnsupportedResponse)
}
/// Sets the RGB power mode. Requires software control of power modes (see
/// [`Self::set_sw_control`]).
pub async fn set_power_mode(&self, mode: RgbPowerMode) -> Result<(), Hidpp20Error> {
self.endpoint
.call(8, [GetOrSet::Set.into(), mode.into(), 0])
.await?;
Ok(())
}
/// Shuts down the RGB system.
///
/// Requires [`RgbExtCapabilities::SHUTDOWN`].
pub async fn shutdown(&self) -> Result<(), Hidpp20Error> {
self.endpoint.call(9, [0; 3]).await?;
Ok(())
}
}