Skip to content

Commit 4865a5d

Browse files
authored
Disallow setting fp brightness to less than 5 prcnt (grouzen#55)
* Disable setting the FP brightness level to less than 5% * Run tests in CI pipeline
1 parent 2c39922 commit 4865a5d

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

.github/workflows/ci.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ jobs:
3737
- name: Run Clippy
3838
run: cargo make lint
3939

40+
- uses: moonrepo/setup-rust@v1
41+
with:
42+
bins: cargo-make
43+
components: clippy
44+
- name: Run tests
45+
run: cargo make test
46+
4047
build:
4148
name: Build
4249
runs-on: ${{ matrix.os }}

Makefile.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ args = ["build"]
1111
command = "cargo"
1212
args = ["clippy", "--no-deps", "--all-features", "--all-targets", "--", "-D", "warnings"]
1313

14+
[tasks.test]
15+
command = "cargo"
16+
args = ["test"]
17+
1418
[tasks.check-all]
1519
dependencies = [
1620
"format",
1721
"lint",
22+
"test",
1823
"build",
1924
]

src/framework/fingerprint.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ impl Fingerprint {
3131
})
3232
}
3333

34+
pub fn percentage() -> Self {
35+
Self {
36+
led_brightness_capability: FpLedBrightnessCapability::Percentage,
37+
}
38+
}
39+
40+
pub fn level() -> Self {
41+
Self {
42+
led_brightness_capability: FpLedBrightnessCapability::Level,
43+
}
44+
}
45+
3446
pub fn adjust_led_brightness_by_delta(&self, current: u8, delta: i8) -> u8 {
3547
match self.led_brightness_capability {
3648
FpLedBrightnessCapability::Level => {
@@ -42,7 +54,12 @@ impl Fingerprint {
4254
FpLedBrightnessCapability::Percentage => {
4355
let new_value = current as i8 + delta;
4456

45-
new_value as u8
57+
// NOTE: disable setting the FP brightness to less than 5%
58+
if new_value < 5 {
59+
current
60+
} else {
61+
new_value as u8
62+
}
4663
}
4764
}
4865
}
@@ -106,3 +123,22 @@ fn adjust_led_brightness_level_by_delta(
106123
_ => level,
107124
}
108125
}
126+
127+
#[cfg(test)]
128+
mod tests {
129+
130+
use crate::framework::fingerprint::Fingerprint;
131+
132+
#[test]
133+
fn adjust_led_brightness_by_delta_for_percentage() {
134+
let fingerprint = Fingerprint::percentage();
135+
136+
let result_five = fingerprint.adjust_led_brightness_by_delta(10, -5);
137+
let result_six = fingerprint.adjust_led_brightness_by_delta(11, -5);
138+
let result_less_than_five = fingerprint.adjust_led_brightness_by_delta(9, -5);
139+
140+
assert!(result_five == 5);
141+
assert!(result_six == 6);
142+
assert!(result_less_than_five == 9);
143+
}
144+
}

src/tui.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,13 @@ impl Tui {
133133
mod tests {
134134
use std::sync::Arc;
135135

136-
use framework_lib::chromium_ec::CrosEc;
137136
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent};
138137

139138
use crate::{app::AppEvent, framework::fingerprint::Fingerprint, tui::Tui};
140139

141140
#[test]
142141
fn handle_input_internal_quit_event() {
143-
let ec = CrosEc::new();
144-
let fingerprint = Arc::new(Fingerprint::new(&ec).unwrap());
142+
let fingerprint = Arc::new(Fingerprint::percentage());
145143
let mut tui = Tui::new(fingerprint);
146144
let event = Event::Key(KeyEvent::from(KeyCode::Char('q')));
147145

0 commit comments

Comments
 (0)