Skip to content

Commit

Permalink
use timestamp instead of counting down ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotd committed Mar 7, 2020
1 parent b25a22e commit 4cb0931
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tp-auto-kbbl"
version = "0.1.1"
version = "0.1.2"
authors = ["Tobias Dühr <[email protected]>"]
edition = "2018"

Expand Down
22 changes: 9 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ fn main() {
);

let mut key_event = false;
let mut timeout = 0;
let mut brightness = 0;
let mut current_brightness = -1;
let mut last_event_ts = time::SystemTime::now();
let timeout: u64 = config.timeout as u64;

loop {
// Wait 100ms in each loop to limit CPU usage
Expand All @@ -83,20 +84,15 @@ fn main() {
debug!("e: {:?}, b: {:?}, t: {:?}", key_event, brightness, timeout);
if key_event {
brightness = config.brightness;
timeout = config.timeout * 10;
last_event_ts = time::SystemTime::now();
key_event = false;
continue;
}
if timeout > 0 {
// Dim light
if config.dim && config.brightness > 1 && timeout < config.timeout * 10 / 2 {
brightness = 1;
}
// Count down
timeout -= 1;
} else {
// Timeout is zero? Switch lights off
brightness = 0;
let es = last_event_ts.elapsed().unwrap().as_secs();
if es >= timeout {
brightness = 0
} else if config.dim && config.brightness > 1 && es >= timeout / 2 {
brightness = 1
}
}
if brightness != current_brightness {
println!("Setting brightness to {}", brightness);
Expand Down

0 comments on commit 4cb0931

Please sign in to comment.