From 810a7e29b867ef615db6294f874361db3e15338a Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Thu, 10 Oct 2024 23:07:29 +0200 Subject: [PATCH] completed c implementation --- Makefile | 4 ++++ README.md | 2 +- ambientd.c | 11 ++++++----- ambient.py => ambientd.py | 0 sv/ambientd/run | 2 +- 5 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 Makefile rename ambient.py => ambientd.py (100%) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8099614 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +CC = gcc +CFLAGS = -Wall -Wextra -pedantic -std=c99 + +ambientd: ambientd.c diff --git a/README.md b/README.md index 9eacbdf..055ffdd 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ give a number and not an error ## Install -Copy ambient.py to a directory in PATH: `sudo cp ambient.py /usr/local/bin` +Copy ambient.py to a directory in PATH: `sudo cp ambientd.py /usr/local/bin/ambientd` ## User Service diff --git a/ambientd.c b/ambientd.c index 4826e08..bcc78af 100644 --- a/ambientd.c +++ b/ambientd.c @@ -26,13 +26,13 @@ int max_luminance = 400; // minimum display brightness in percent float min_brightness_percent = 0.05; -// retturns the luminance value in percentage between 0 and 1 +// returns the luminance value in percentage between 0 and 1 int get_luminance_raw (void) { const char *x = fgets(STR_BUFFER, sizeof(STR_BUFFER), luminance_fp); if (x == NULL) { fprintf(stderr, "Could not read luminance value: %s\n", strerror(errno)); - return 1.0; + return -1; } int luminance = strtol(STR_BUFFER, NULL, 10); rewind(luminance_fp); @@ -109,7 +109,8 @@ int main (void) static float luminance; static float luminance_percent; - luminance_samples[idx] = get_luminance_raw(); + x = get_luminance_raw(); + if (x >= 0) luminance_samples[idx] = x; idx = (idx + 1) % MEDIAN_SAMPLES; luminance = 0; @@ -147,7 +148,7 @@ int main (void) actual_brightness = read_int(actual_brightness_fp); if (actual_brightness < 0) { fprintf(stderr, "error reading actual brightness: %s\n", strerror(errno)); - actual_brightness = brightness_target; + actual_brightness = brightness_adjusted; } off = actual_brightness - brightness_target; @@ -155,7 +156,7 @@ int main (void) user_offset = off; } - printf("brightness target: %d; user offset: %d; brightness adjusted: %d\n\n", brightness_target, user_offset, brightness_adjusted); + printf("brightness target: %d; user offset: %d; brightness adjusted: %d\n", brightness_target, user_offset, brightness_adjusted); } diff --git a/ambient.py b/ambientd.py similarity index 100% rename from ambient.py rename to ambientd.py diff --git a/sv/ambientd/run b/sv/ambientd/run index 39a535f..358991e 100755 --- a/sv/ambientd/run +++ b/sv/ambientd/run @@ -1,3 +1,3 @@ #!/bin/sh -exec ambient.py +exec ambientd