smooth brightness changes

This commit is contained in:
Alessandro Mauri 2024-10-29 13:22:11 +01:00
parent 3a3879f6cd
commit 9ba6ed6136

View file

@ -18,7 +18,9 @@ const int MAX_LUMINANCE = 700;
// minimum display brightness in percent
const float MIN_BRIGHTNESS_PERCENT = 0.05;
const int SLEEP_DELAY = 3000;
const int SLEEP_DELAY = 2500;
const int SMOOTH_DURATION = 500;
const int SMOOTH_SAMPLES = 20;
macro abs(x) { return x < 0 ? -x : x; }
macro clamp(x, l, h) { return max(min(x, h), l); }
@ -154,11 +156,25 @@ fn int main()
if (brightness_delta > 0.1) {
user_offset = (int)(user_offset * clamp(1-(int)(brightness_delta*1.2), 0, 1));
}
brightness_adjusted = clamp(brightness_target + user_offset, min_brightness, max_brightness);
if (catch excuse = write_int(BRIGHTNESS_DEV, brightness_adjusted)) {
io::eprintfn("Error writing brightness: %s", excuse);
}
// animate the transition to the brightness target
{|
int brightness_now = read_int(ACTUAL_BRIGHTNESS_DEV) ?? brightness_adjusted;
if (brightness_now == brightness_adjusted) { return; }
int step = (brightness_adjusted-brightness_now)/(SMOOTH_SAMPLES-1);
for (int i; i < SMOOTH_SAMPLES-1; i++) {
anyfault e = @catch(write_int(BRIGHTNESS_DEV, brightness_now + step*i));
if (e) {
io::eprintfn("Error writing brightness: %s", e);
}
thread::sleep_ms(SMOOTH_DURATION/SMOOTH_SAMPLES);
}
anyfault e = @catch(write_int(BRIGHTNESS_DEV, brightness_adjusted));
if (e) {
io::eprintfn("Error writing brightness: %s", e);
}
|};
thread::sleep_ms(SLEEP_DELAY);
@ -182,6 +198,6 @@ fn int main()
}
// TODO: option to turn off logging
io::printfn("brightness {target=%d, adjusted=%d}, offset=%d, luminance=%d (%.5f%%)", brightness_target, brightness_adjusted, user_offset, luminance_median, luminance_percent);
io::printfn("brightness {target=%d, adjusted=%d}, offset=%d, luminance=%d (%.5f%%)", brightness_target, brightness_adjusted, user_offset, luminance_median, luminance_percent*100);
}
}