Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions framework_lib/src/chromium_ec/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ impl EcRequest<()> for EcRequestPwmSetFanDutyV1 {

pub const PWM_MAX_DUTY: u16 = 0xFFFF;

pub fn percent_to_duty(percent: u8) -> u16 {
let duty = percent as u32 * PWM_MAX_DUTY as u32;
(duty / 100) as u16
}

pub fn duty_to_percent(duty: u16) -> u8 {
let percent = duty as u32 * 100;
(percent / PWM_MAX_DUTY as u32) as u8
}

#[repr(C, packed)]
pub struct EcRequestPwmSetDuty {
/// Duty cycle, min 0, max 0xFFFF
Expand Down
37 changes: 33 additions & 4 deletions framework_lib/src/chromium_ec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,24 +770,53 @@ impl CrosEc {
/// * `percent` - An integer from 0 to 100. 0 being off, 100 being full brightness
pub fn set_keyboard_backlight(&self, percent: u8) {
debug_assert!(percent <= 100);
let duty = percent_to_duty(percent);

let res = EcRequestPwmSetDuty {
duty: percent as u16 * (PWM_MAX_DUTY / 100),
duty,
pwm_type: PwmType::KbLight as u8,
index: 0,
}
.send_command(self);

// Early systems (hx20/hx30) don't enable CONFIG_PWM_KBLIGHT in their EC firmware;
// keyboard backlight is at generic PWM channel index 1.
let res = match res {
Err(EcError::Response(EcResponseStatus::InvalidParameter)) => {
EcRequestPwmSetDuty {
duty,
pwm_type: PwmType::Generic as u8,
index: 1,
}
.send_command(self)
}
other => other,
};
debug_assert!(res.is_ok());
}
Comment on lines +782 to 796
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res is moved into the if let Err(...) = res pattern match, but then it’s used again afterwards in debug_assert!(res.is_ok()). This won’t compile because EcResult<()> isn’t Copy. Match on &res (or restructure with match res { ... }) so you can both detect InvalidParameter and still use/inspect the original result.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it definitely does compile


/// Check the current brightness of the keyboard backlight
pub fn get_keyboard_backlight(&self) -> EcResult<u8> {
let kblight = EcRequestPwmGetDuty {
let res = EcRequestPwmGetDuty {
pwm_type: PwmType::KbLight as u8,
index: 0,
}
.send_command(self)?;
.send_command(self);

// Early systems (hx20/hx30) don't enable CONFIG_PWM_KBLIGHT in their EC firmware;
// keyboard backlight is at generic PWM channel index 1.
let kblight = match res {
Err(EcError::Response(EcResponseStatus::InvalidParameter)) => {
EcRequestPwmGetDuty {
pwm_type: PwmType::Generic as u8,
index: 1,
}
.send_command(self)?
}
other => other?,
};

Ok((kblight.duty / (PWM_MAX_DUTY / 100)) as u8)
Ok(duty_to_percent(kblight.duty))
}

pub fn ps2_emulation_enable(&self, enable: bool) -> EcResult<()> {
Expand Down
Loading