Created
July 1, 2014 15:00
-
-
Save akoskovacs/9ca900d0979fcc12580d to your computer and use it in GitHub Desktop.
Raspberry Pi direct (memory-mapped) framebuffer drawing PoC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <inttypes.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#include <linux/fb.h> | |
#include <sys/mman.h> | |
#define RGB565(r, g, b) (uint16_t)(b|(g<<5)|(r<<11)) | |
int main(int argc, char *argv[]) | |
{ | |
int fbfd = 0; | |
struct fb_var_screeninfo var_info; | |
struct fb_fix_screeninfo fix_info; | |
long int screensize = 0; | |
long int i; | |
uint16_t *fbp = 0; | |
fbfd = open("/dev/fb0", O_RDWR); | |
if (fbfd == -1) { | |
fprintf(stderr, "Cannot open framebuffer!\n"); | |
return 1; | |
} | |
printf("Framebuffer opened\n"); | |
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &var_info)) { | |
fprintf(stderr, "Error reading screen info.\n"); | |
return 1; | |
} | |
printf("Display info: %dx %dy, %d bpp\n" | |
, var_info.xres, var_info.yres | |
, var_info.bits_per_pixel); | |
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &fix_info)) { | |
fprintf(stderr, "Error reading fix screen info.\n"); | |
return 1; | |
} | |
screensize = fix_info.smem_len; | |
fbp = (uint16_t *)mmap(0, screensize, PROT_READ|PROT_WRITE | |
, MAP_SHARED, fbfd, 0); | |
screensize /= 2; | |
if (fbp == MMAP_FAILED) { | |
fprintf(stderr, "Cannot map buffer!\n"); | |
return 1; | |
} | |
// Hungarian tricolor | |
for (i = 0; i < screensize/3; i++) { | |
fbp[i] = RGB565(240, 0, 0); | |
} | |
for (; i < 2*(screensize/3); i++) { | |
fbp[i] = RGB565(255, 255, 255); | |
} | |
for (; i < screensize; i++) { | |
fbp[i] = RGB565(0, 210, 0); | |
} | |
sleep(3); | |
munmap(fbp, screensize); | |
close(fbfd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment