From 45c6a7f7e2e776878d5dd6da9296c5d6026942c2 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sun, 3 Mar 2019 14:58:41 +1300 Subject: Allow brightness set --- clock-disp.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/clock-disp.c b/clock-disp.c index c8d8c74..8aa4fc3 100644 --- a/clock-disp.c +++ b/clock-disp.c @@ -12,6 +12,13 @@ enum ALIGN_MODE { ALIGN_MODE_RIGHT, }; +//#define DEBUG +#ifdef DEBUG +# define DEBUG_P(x...) printf("DEBUG: "x) +#else +# define DEBUG_P(x...) +#endif + #define WINDOW_TITLE "Clock Display" #define SCREEN_WIDTH 800 #define SCREEN_HEIGHT 480 @@ -87,11 +94,32 @@ void get_clock_data(struct clock_data *l) } } +int calculate_brightness(int midday_mins, int now_mins) +{ +#define MIN_BRIGHTNESS 11 +#define MAX_BRIGHTNESS 255 +#define MIN_BRIGHT_VIRTUAL (-150) +#define MAX_BRIGHT_VIRTUAL 400 + int ret = 0; + + DEBUG_P("midday %d, now %d minutes\n", midday_mins, now_mins); + + ret = MAX_BRIGHT_VIRTUAL - ((MAX_BRIGHT_VIRTUAL - MIN_BRIGHT_VIRTUAL) * abs(now_mins - midday_mins)) / midday_mins; + + if (ret < MIN_BRIGHTNESS) + ret = MIN_BRIGHTNESS; + + if (ret > MAX_BRIGHTNESS) + ret = MAX_BRIGHTNESS; + + return ret; +} + /** * Read clock's statistics from files and push them into shared data area for * display thread to pick up */ -void *read_stats(void* unused) +void *read_stats(void* brightness_f_p) { char buffer[32]; char *next = buffer; @@ -99,6 +127,8 @@ void *read_stats(void* unused) struct clock_data l; memset(&l, 0, sizeof(l)); + FILE* brightness_f = brightness_f_p; + while (1) { /* Read sunrise, sunset */ if (f = fopen("/tmp/stats-sun", "r")) { @@ -151,6 +181,38 @@ void *read_stats(void* unused) } fclose(f); } + + /* FIXME move decls to top */ + int sunrise_hours = 0; + int sunrise_minutes = 0; + int sunset_hours = 0; + int sunset_minutes = 0; + if ( sscanf(l.sunrise_time, "%d:%d", &sunrise_hours, &sunrise_minutes) == 2 + && sscanf(l.sunset_time, "%d:%d", &sunset_hours, &sunset_minutes) == 2) { + sunrise_minutes += 60 * sunrise_hours; + sunset_minutes += 60 * sunset_hours; + int local_midday = (sunrise_minutes + sunset_minutes) / 2; + + time_t t; + struct tm *time_s; + t = time(NULL); + if ((time_s = localtime(&t)) == NULL) { + /* FIXME error */ + continue; + } + + int brightness = calculate_brightness(local_midday, time_s->tm_hour*60 + time_s->tm_min); + DEBUG_P("Calculated brightness %d\n", brightness); + if (!brightness_f) { + DEBUG_P("Brightness file not open, not writing to it\n"); + } else { + snprintf(buffer, sizeof(buffer), "%d\n", brightness); + if (fwrite(buffer, strlen(buffer), 1, brightness_f) != strlen(buffer)) { + perror("fwrite"); + } + } + } + sem_wait(&data_lock); publish_clock_data(&l); sem_post(&data_lock); @@ -275,9 +337,23 @@ int main(void) return 1; } + FILE* brightness_f = fopen("/sys/class/backlight/rpi_backlight/brightness", "w+"); + if (!brightness_f) { + perror("fopen"); + } + + if (setgid(getgid()) < 0) { + perror("setgid"); + return 1; + } + if (setuid(getuid()) < 0) { + perror("setuid"); + return 1; + } + /* launch pthread */ pthread_t thread; - if (pthread_create(&thread, NULL, read_stats, NULL) != 0) { + if (pthread_create(&thread, NULL, read_stats, brightness_f) != 0) { perror("pthread_create"); return 1; } -- cgit v1.1