diff options
author | joe <rbo@gmx.us> | 2025-09-26 00:41:53 +0200 |
---|---|---|
committer | joe <rbo@gmx.us> | 2025-09-26 00:41:53 +0200 |
commit | 8fd7803ae361855515ee20e2580e7b44257e08f8 (patch) | |
tree | 76de6d30fa115cb8341ce9034c016ebc2253aaeb | |
parent | rules (diff) | |
download | halfcab-8fd7803ae361855515ee20e2580e7b44257e08f8.tar.gz halfcab-8fd7803ae361855515ee20e2580e7b44257e08f8.tar.bz2 halfcab-8fd7803ae361855515ee20e2580e7b44257e08f8.tar.xz halfcab-8fd7803ae361855515ee20e2580e7b44257e08f8.tar.zst halfcab-8fd7803ae361855515ee20e2580e7b44257e08f8.zip |
it works
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | conf/52-esp.rules (renamed from 52-esp.rules) | 0 | ||||
-rw-r--r-- | src/c_defines.h | 3 | ||||
-rw-r--r-- | src/c_halfcab.c | 98 | ||||
-rw-r--r-- | src/c_screen.c | 115 | ||||
-rw-r--r-- | src/c_screen.h | 2 |
6 files changed, 146 insertions, 74 deletions
@@ -16,7 +16,7 @@ # # GNU Makefile -default: run +default: debug SHELL := /bin/sh OS = $(shell /usr/bin/uname) diff --git a/52-esp.rules b/conf/52-esp.rules index ff65585..ff65585 100644 --- a/52-esp.rules +++ b/conf/52-esp.rules diff --git a/src/c_defines.h b/src/c_defines.h index 7cc1779..898f5a4 100644 --- a/src/c_defines.h +++ b/src/c_defines.h @@ -66,8 +66,7 @@ #define BLUE CRGB(0, 0, 255) #define GRUV CRGB(255, 80, 0) -#define VERT_PIXEL_GAP 0 -#define HORZ_PIXEL_GAP 5 +#define RANDOM_PX_COUNT 7 enum bool_e { FALSE, diff --git a/src/c_halfcab.c b/src/c_halfcab.c index 83b8243..78a23e5 100644 --- a/src/c_halfcab.c +++ b/src/c_halfcab.c @@ -56,6 +56,7 @@ #include <stdlib.h> #include <string.h> #include <termios.h> +#include <time.h> #include <unistd.h> #include <X11/Xlib.h> @@ -64,7 +65,7 @@ #include "c_screen.h" static char -param_esp_com +c_param_esp_com (int fd, const char prog_name[]) { @@ -108,7 +109,7 @@ param_esp_com } static int -open_esp(const char prog_name[]) +c_open_esp(const char prog_name[]) { int fd; @@ -135,62 +136,83 @@ handle_sigterm(int signum) end = TRUE; } +static int +c_screen_colors(int fd) +{ + struct sigaction act; + Display* disp; + unsigned char com[(NUM_LEDS * 3) + 1]; + unsigned int t; + int i; + + bzero(com, (NUM_LEDS * 3) * sizeof(unsigned char)); + com[0] = 0xff; + disp = XOpenDisplay(NULL); + if (disp == NULL) { + i = 1; + while (i < (NUM_LEDS * 3) + 1) { + com[i] = 0xff; + i += 3; + } + write(fd, &com, ((NUM_LEDS * 3) + 1) * sizeof(unsigned char)); + return (1); + } + bzero(&act, sizeof(struct sigaction)); + t = time(NULL); + act.sa_handler = handle_sigterm; + sigaction(SIGTERM, &act, NULL); + sigaction(SIGINT, &act, NULL); + while (end == FALSE) { + c_get_screen_colors(com + 1, disp, t); + write(fd, &com, ((NUM_LEDS * 3) + 1) * sizeof(unsigned char)); + } + XCloseDisplay(disp); + return (0); +} + +static void +c_single_color +(int fd, + const char* argv[]) +{ + unsigned char com[4]; + int i; + + com[0] = 0xfe; + i = 1; + while (i < 4) { + com[i] = atoi(argv[i]); + i++; + } + write(fd, &com, 4 * sizeof(unsigned char)); +} + int main (int argc, const char* argv[]) { const char* prog_name = argv[0]; - struct sigaction act; - Display* disp; int fd; - int i; int ret; - unsigned char com[4]; - unsigned char leds[NUM_LEDS * 3]; - fd = open_esp(prog_name); + fd = c_open_esp(prog_name); if (fd < 0) { return (EXIT_FAILURE); } - if (param_esp_com(fd, prog_name) != 0) { + if (c_param_esp_com(fd, prog_name) != 0) { close(fd); return (EXIT_FAILURE); } if (argc < 4) { - com[0] = 0xff; - bzero(leds, (NUM_LEDS * 3) * sizeof(unsigned char)); - disp = XOpenDisplay(NULL); - if (disp == NULL) { - i = 0; - while (i < NUM_LEDS * 3) { - leds[i] = 0xff; - i += 3; - } - ret = write(fd, &com, 1 * sizeof(unsigned char)); - ret = write(fd, &leds, (NUM_LEDS * 3) * sizeof(unsigned char)); + ret = c_screen_colors(fd); + if (ret != 0) { + close(fd); return (EXIT_FAILURE); } - bzero(&act, sizeof(struct sigaction)); - act.sa_handler = handle_sigterm; - sigaction(SIGTERM, &act, NULL); - sigaction(SIGINT, &act, NULL); - while (end == FALSE) { - c_get_screen_colors(leds, disp); - ret = write(fd, &com, 1 * sizeof(unsigned char)); - ret = write(fd, &leds, (NUM_LEDS * 3) * sizeof(unsigned char)); - } - XCloseDisplay(disp); } else { - com[0] = 0xfe; - i = 1; - while (i < 4) { - com[i] = atoi(argv[i]); - i++; - } - ret = write(fd, &com, 4 * sizeof(unsigned char)); + c_single_color(fd, argv); } close(fd); - (void)ret; return (EXIT_SUCCESS); } diff --git a/src/c_screen.c b/src/c_screen.c index 5a67f9b..66f3653 100644 --- a/src/c_screen.c +++ b/src/c_screen.c @@ -47,6 +47,9 @@ * joe <rbo@gmx.us> */ +#include <stdlib.h> +#include <string.h> + #include <X11/X.h> #include <X11/Xlib.h> #include <X11/Xutil.h> @@ -54,42 +57,65 @@ #include "c_defines.h" #include "c_screen.h" +static int +c_rand +(short min, + short max) +{ + return min + (rand() % (max - min)); +} + static void -fill_led -(unsigned char leds[], - const int i, - Display* disp, - XImage* img, - const int min_x, - const int max_x, - const int min_y, - const int max_y) +c_fill_led +(unsigned char leds[], + const int i, + Display* disp, + XImage* img, + const short x_min, + const short x_max, + const short y_min, + const short y_max) { XColor c; + unsigned int total[3]; + short j; + short x; + short y; - (void)min_x; - (void)max_x; - (void)min_y; - (void)max_y; - c.pixel = XGetPixel(img, 0, 0); - XQueryColor(disp, DefaultColormap(disp, DefaultScreen(disp)), &c); - leds[i + 0] = c.red; - leds[i + 1] = c.green; - leds[i + 2] = c.blue; + bzero(&total, 3 * sizeof(unsigned int)); + j = 0; + while (j < RANDOM_PX_COUNT) { + x = c_rand(x_min, x_max); + y = c_rand(y_min, y_max); + j++; + c.pixel = XGetPixel(img, x, y); + XQueryColor(disp, DefaultColormap(disp, DefaultScreen(disp)), &c); + total[0] += c.red; + total[1] += c.green; + total[2] += c.blue; + } + j = 0; + while (j < 3) { + leds[i + j] = (total[j] / 256) / RANDOM_PX_COUNT; + j++; + } } void c_get_screen_colors (unsigned char leds[], - Display* disp) + Display* disp, + unsigned int t) { XImage* img; - const int top_leds = NUM_LEDS / 2; - const int side_leds = NUM_LEDS / 4; - const int top_px_per_led = SCREEN_W / top_leds; - const int side_px_per_led = SCREEN_H / side_leds; - int i; + static const short top_leds = NUM_LEDS / 2; + static const short side_leds = NUM_LEDS / 4; + static const short top_px_per_led = SCREEN_W / top_leds; + static const short side_px_per_led = SCREEN_H / side_leds; + short i; + short j; + srand(t); img = XGetImage( disp, RootWindow(disp, DefaultScreen(disp)), 1920, 0, @@ -100,13 +126,38 @@ c_get_screen_colors return; } i = 0; - fill_led( - leds, i, - disp, img, - 0, side_px_per_led, - VERT_PIXEL_GAP + (0 * side_px_per_led), - VERT_PIXEL_GAP + ((0 + 1) * side_px_per_led) - ); - (void)top_px_per_led; + j = 0; + while (j < side_leds) { + i = side_leds - j - 1; + c_fill_led( + leds, i * 3, + disp, img, + 0, side_px_per_led, + j * side_px_per_led, + (j + 1) * side_px_per_led + ); + j++; + } + i = 0; + while (i < top_leds) { + c_fill_led( + leds, (side_leds * 3) + (i * 3), + disp, img, + i * top_px_per_led, + (i + 1) * top_px_per_led, + 0, top_px_per_led + ); + i++; + } + i = 0; + while (i < side_leds) { + c_fill_led( + leds, (side_leds + top_leds + i) * 3, + disp, img, + SCREEN_W - side_px_per_led, SCREEN_W - 1, + i * side_px_per_led, (i + 1) * side_px_per_led + ); + i++; + } XDestroyImage(img); } diff --git a/src/c_screen.h b/src/c_screen.h index 6c93cf4..7515878 100644 --- a/src/c_screen.h +++ b/src/c_screen.h @@ -52,6 +52,6 @@ #include <X11/Xlib.h> -void c_get_screen_colors(unsigned char[], Display*); +void c_get_screen_colors(unsigned char[], Display*, unsigned int); #endif /* __C_SCREEN_H__ */ |