Skip to content

Commit 9b915be

Browse files
committed
kblight: Fix duty/percentage scaling
Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 731ec3f commit 9b915be

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

framework_lib/src/chromium_ec/commands.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ impl EcRequest<()> for EcRequestPwmSetFanDutyV1 {
246246

247247
pub const PWM_MAX_DUTY: u16 = 0xFFFF;
248248

249+
pub fn percent_to_duty(percent: u8) -> u16 {
250+
let duty = percent as u32 * PWM_MAX_DUTY as u32;
251+
(duty / 100) as u16
252+
}
253+
254+
pub fn duty_to_percent(duty: u16) -> u8 {
255+
let percent = duty as u32 * 100;
256+
(percent / PWM_MAX_DUTY as u32) as u8
257+
}
258+
249259
#[repr(C, packed)]
250260
pub struct EcRequestPwmSetDuty {
251261
/// Duty cycle, min 0, max 0xFFFF

framework_lib/src/chromium_ec/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ impl CrosEc {
770770
/// * `percent` - An integer from 0 to 100. 0 being off, 100 being full brightness
771771
pub fn set_keyboard_backlight(&self, percent: u8) {
772772
debug_assert!(percent <= 100);
773-
let duty = percent as u16 * (PWM_MAX_DUTY / 100);
773+
let duty = percent_to_duty(percent);
774774

775775
let res = EcRequestPwmSetDuty {
776776
duty,
@@ -816,7 +816,7 @@ impl CrosEc {
816816
other => other?,
817817
};
818818

819-
Ok((kblight.duty / (PWM_MAX_DUTY / 100)) as u8)
819+
Ok(duty_to_percent(kblight.duty))
820820
}
821821

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

0 commit comments

Comments
 (0)