Skip to content
Merged
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
33 changes: 29 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,49 @@ 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());
}

/// 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
7 changes: 5 additions & 2 deletions framework_lib/src/commandline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,8 +1430,11 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
} else if let Some(maybe_brightness) = &args.fp_brightness {
print_err(handle_fp_brightness(&ec, *maybe_brightness));
} else if let Some(Some(kblight)) = args.kblight {
assert!(kblight <= 100);
ec.set_keyboard_backlight(kblight);
if kblight > 100 {
error!("--kblight must be percentage 0-100");
} else {
ec.set_keyboard_backlight(kblight);
}
} else if let Some(None) = args.kblight {
print!("Keyboard backlight: ");
if let Some(percentage) = print_err(ec.get_keyboard_backlight()) {
Expand Down