From 1d4759d8c04f18dc2e4f3ce45c4656f85f7301a7 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 3 Mar 2022 09:04:58 +0000 Subject: [PATCH] Add main loop with framebuffer color change --- src/main.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index e08dc19..e3e8b77 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,43 @@ #include +#include +#include +#include +#include +#include +#include +#include int main(void) { - printf("hello world\n"); + // Open frambuffer and get the size. + int fb = open("/dev/fb0", O_RDWR); + if (fb <= 0) { + fprintf(stderr, "couldn't open the framebuffer\n"); + exit(EXIT_FAILURE); + } + struct fb_var_screeninfo info; + if (ioctl(fb, FBIOGET_VSCREENINFO, &info) != 0) { + fprintf(stderr, "couldn't get the framebuffer size\n"); + exit(EXIT_FAILURE); + } + + // Mmap the framebuffer to a buffer object. + size_t width = info.xres; + size_t height = info.yres; + size_t len = 4 * width * height; + uint32_t *buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0); + if (buf == MAP_FAILED) { + fprintf(stderr, "couldn't mmap the framebuffer\n"); + exit(EXIT_FAILURE); + } + uint8_t shade = 0; + while (true) { + for (size_t j = 0; j < height; j++) { + for (size_t i = 0; i < width; i++) { + buf[j * width + i] = shade; + } + } + shade++; + } return 0; }