From 38d25483657e82eb546ecb8566214f830efba461 Mon Sep 17 00:00:00 2001 From: spv Date: Sun, 21 Sep 2025 00:07:50 -0400 Subject: refactoring ftw (now w/ README updates!) --- src/main.c | 126 +++---------------------------------------------------------- 1 file changed, 5 insertions(+), 121 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 70424d3..4c09563 100644 --- a/src/main.c +++ b/src/main.c @@ -3,131 +3,15 @@ #include #include #include -#include -#include #include -#include -#define MS * 1000 -#define SEC * 1000000 +#include "consts.h" +#include "config.h" -#define DEBOUNCE (10 MS) -#define THREAD_WAIT (100 MS) -#define BACKLIGHT_TIME (10 SEC) -#define BACKLIGHT_FADE_FRAMETIME (50 MS) -#define BACKLIGHT_FADE_TIME (1000 MS) -#define BACKLIGHT_FADE_STEPS (BACKLIGHT_FADE_TIME / BACKLIGHT_FADE_FRAMETIME) +extern int light_thread(void); +extern int keypress_thread(void); -#define KEYPRESS_SIZE 72 -#define KEYBOARD_FILE "/dev/input/event1" -#define BACKLIGHT_FILE "/sys/class/leds/keyboard_light/brightness" - -//#define BACKLIGHT_FILE stdout - -#define MAX_BRIGHTNESS 255 - -static struct timeval* last_press; -static uint32_t curr_brightness; -static FILE* backlight_fp; - -void write_to_backlight(int n) { - fprintf(backlight_fp, "%d", n); - fflush(backlight_fp); - - curr_brightness = n; -} - -#if 0 -void backlight_fade(void) { - short brightness_step = MAX_BRIGHTNESS / BACKLIGHT_FADE_STEPS; - short* brightness = malloc(sizeof(short)); - - *brightness = MAX_BRIGHTNESS; - - fprintf(stderr, "ha, gay\n"); - - do { - fprintf(stderr, "%d %d %d\n", *brightness, brightness_step, 0); - - write_to_backlight(*brightness); - *brightness -= brightness_step; - - usleep(BACKLIGHT_FADE_FRAMETIME); - } while (*brightness >= brightness_step); -} -#endif - -void backlight_off(void) { - if (curr_brightness == 0) return; - write_to_backlight(0); - -// backlight_fade(); -} - -void backlight_on(void) { - curr_brightness = MAX_BRIGHTNESS; - - write_to_backlight(MAX_BRIGHTNESS); - usleep(BACKLIGHT_TIME); -} - -uint64_t time_since_press(void) { - struct timeval now; - - uint64_t now_usec; - uint64_t press_usec; - uint64_t diff; - - gettimeofday(&now, NULL); - - now_usec = now.tv_sec * 1000000L; - now_usec += now.tv_usec; - - press_usec = last_press->tv_sec * 1000000L; - press_usec += last_press->tv_usec; - - diff = now_usec - press_usec; - - return diff; -} - -void on_press(void) { - gettimeofday(last_press, NULL); -} - -int light_thread(void) { - // check last_press vs time, do the thing - backlight_fp = fopen(BACKLIGHT_FILE, "w"); - -// FILE* backlight_fp = BACKLIGHT_FILE; - - while (1) { - if (time_since_press() > BACKLIGHT_TIME) backlight_off(); - else backlight_on(); - - usleep(THREAD_WAIT); - } - - return 0; -} - -int keypress_thread(void) { - FILE* fp = fopen(KEYBOARD_FILE, "r"); - fseek(fp, 0, SEEK_END); - - void* discard = malloc(KEYPRESS_SIZE); - - while (1) { - fseek(fp, 0, SEEK_END); - fread(discard, 1, KEYPRESS_SIZE, fp); - - on_press(); - - usleep(DEBOUNCE); - } - - return 0; -} +struct timeval* last_press; void daemonize(void) { daemon(0, 0); -- cgit v1.2.3