Skip to content

Commit 0b8763e

Browse files
committed
fp-brightness: Add support for V1 host command
V0: - Set level (ultra-low, low, medium, high, auto) - Get percentage V1: - Set percentage - Get level (ultra-low, low, medium, high, auto) Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 3c7bfea commit 0b8763e

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

framework_lib/src/chromium_ec/commands.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ pub enum FpLedBrightnessLevel {
849849
}
850850

851851
#[repr(C, packed)]
852-
pub struct EcRequestFpLedLevelControl {
852+
pub struct EcRequestFpLedLevelControlV0 {
853853
/// See enum FpLedBrightnessLevel
854854
pub set_level: u8,
855855
/// Boolean. >1 to get the level
@@ -858,12 +858,42 @@ pub struct EcRequestFpLedLevelControl {
858858

859859
#[repr(C, packed)]
860860
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
861-
pub struct EcResponseFpLedLevelControl {
861+
pub struct EcResponseFpLedLevelControlV0 {
862+
/// Current brightness, 1-100%
863+
pub percentage: u8,
864+
}
865+
866+
impl EcRequest<EcResponseFpLedLevelControlV0> for EcRequestFpLedLevelControlV0 {
867+
fn command_id() -> EcCommands {
868+
EcCommands::FpLedLevelControl
869+
}
870+
fn command_version() -> u8 {
871+
0
872+
}
873+
}
874+
875+
#[repr(C, packed)]
876+
pub struct EcRequestFpLedLevelControlV1 {
877+
/// Percentage 1-100
878+
pub set_percentage: u8,
879+
/// Boolean. >1 to get the level
880+
pub get_level: u8,
881+
}
882+
883+
#[repr(C, packed)]
884+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
885+
pub struct EcResponseFpLedLevelControlV1 {
886+
/// Current brightness, 1-100%
887+
pub percentage: u8,
888+
/// Requested level. See enum FpLedBrightnessLevel
862889
pub level: u8,
863890
}
864891

865-
impl EcRequest<EcResponseFpLedLevelControl> for EcRequestFpLedLevelControl {
892+
impl EcRequest<EcResponseFpLedLevelControlV1> for EcRequestFpLedLevelControlV1 {
866893
fn command_id() -> EcCommands {
867894
EcCommands::FpLedLevelControl
868895
}
896+
fn command_version() -> u8 {
897+
1
898+
}
869899
}

framework_lib/src/chromium_ec/mod.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,17 @@ impl CrosEc {
313313
Ok((limits.min_percentage, limits.max_percentage))
314314
}
315315

316+
pub fn set_fp_led_percentage(&self, percentage: u8) -> EcResult<()> {
317+
// Sending bytes manually because the Set command, as opposed to the Get command,
318+
// does not return any data
319+
let limits = &[percentage, 0x00];
320+
let data = self.send_command(EcCommands::FpLedLevelControl as u16, 1, limits)?;
321+
322+
util::assert_win_len(data.len(), 0);
323+
324+
Ok(())
325+
}
326+
316327
pub fn set_fp_led_level(&self, level: FpLedBrightnessLevel) -> EcResult<()> {
317328
// Sending bytes manually because the Set command, as opposed to the Get command,
318329
// does not return any data
@@ -325,16 +336,33 @@ impl CrosEc {
325336
}
326337

327338
/// Get fingerprint led brightness level
328-
pub fn get_fp_led_level(&self) -> EcResult<u8> {
329-
let res = EcRequestFpLedLevelControl {
330-
set_level: 0xFF,
339+
pub fn get_fp_led_level(&self) -> EcResult<(u8, Option<FpLedBrightnessLevel>)> {
340+
let res = EcRequestFpLedLevelControlV1 {
341+
set_percentage: 0xFF,
331342
get_level: 0xFF,
332343
}
333-
.send_command(self)?;
344+
.send_command(self);
345+
346+
// If V1 does not exist, fall back
347+
if let Err(EcError::Response(EcResponseStatus::InvalidVersion)) = res {
348+
let res = EcRequestFpLedLevelControlV0 {
349+
set_level: 0xFF,
350+
get_level: 0xFF,
351+
}
352+
.send_command(self)?;
353+
debug!("Current Brightness: {}%", res.percentage);
354+
return Ok((res.percentage, None));
355+
}
356+
357+
let res = res?;
334358

335-
debug!("Level Raw: {}", res.level);
359+
debug!("Current Brightness: {}%", res.percentage);
360+
debug!("Level Raw: {}", res.level);
336361

337-
Ok(res.level)
362+
// TODO: can turn this into None and log
363+
let level = FromPrimitive::from_u8(res.level)
364+
.ok_or(EcError::DeviceError(format!("Invalid level {}", res.level)))?;
365+
Ok((res.percentage, Some(level)))
338366
}
339367

340368
/// Get the intrusion switch status (whether the chassis is open or not)

framework_lib/src/commandline/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,8 +1364,13 @@ fn handle_fp_brightness(ec: &CrosEc, maybe_brightness: Option<FpBrightnessArg>)
13641364
ec.set_fp_led_level(brightness.into())?;
13651365
}
13661366

1367-
let level = ec.get_fp_led_level()?;
1368-
println!("Fingerprint LED Brightness: {:?}%", level);
1367+
let (brightness, level) = ec.get_fp_led_level()?;
1368+
// TODO: Rename to power button
1369+
println!("Fingerprint LED Brightness");
1370+
if let Some(level) = level {
1371+
println!(" Requested: {:?}", level);
1372+
}
1373+
println!(" Brightness: {}%", brightness);
13691374

13701375
Ok(())
13711376
}

0 commit comments

Comments
 (0)