completed c implementation

This commit is contained in:
Alessandro Mauri 2024-10-10 23:07:29 +02:00
parent d06794b06e
commit 810a7e29b8
5 changed files with 12 additions and 7 deletions

4
Makefile Normal file
View file

@ -0,0 +1,4 @@
CC = gcc
CFLAGS = -Wall -Wextra -pedantic -std=c99
ambientd: ambientd.c

View file

@ -13,7 +13,7 @@ give a number and not an error
## Install ## 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 ## User Service

View file

@ -26,13 +26,13 @@ int max_luminance = 400;
// minimum display brightness in percent // minimum display brightness in percent
float min_brightness_percent = 0.05; 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) int get_luminance_raw (void)
{ {
const char *x = fgets(STR_BUFFER, sizeof(STR_BUFFER), luminance_fp); const char *x = fgets(STR_BUFFER, sizeof(STR_BUFFER), luminance_fp);
if (x == NULL) { if (x == NULL) {
fprintf(stderr, "Could not read luminance value: %s\n", strerror(errno)); fprintf(stderr, "Could not read luminance value: %s\n", strerror(errno));
return 1.0; return -1;
} }
int luminance = strtol(STR_BUFFER, NULL, 10); int luminance = strtol(STR_BUFFER, NULL, 10);
rewind(luminance_fp); rewind(luminance_fp);
@ -109,7 +109,8 @@ int main (void)
static float luminance; static float luminance;
static float luminance_percent; 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; idx = (idx + 1) % MEDIAN_SAMPLES;
luminance = 0; luminance = 0;
@ -147,7 +148,7 @@ int main (void)
actual_brightness = read_int(actual_brightness_fp); actual_brightness = read_int(actual_brightness_fp);
if (actual_brightness < 0) { if (actual_brightness < 0) {
fprintf(stderr, "error reading actual brightness: %s\n", strerror(errno)); fprintf(stderr, "error reading actual brightness: %s\n", strerror(errno));
actual_brightness = brightness_target; actual_brightness = brightness_adjusted;
} }
off = actual_brightness - brightness_target; off = actual_brightness - brightness_target;
@ -155,7 +156,7 @@ int main (void)
user_offset = off; 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);
} }

View file

@ -1,3 +1,3 @@
#!/bin/sh #!/bin/sh
exec ambient.py exec ambientd