Skip to content

Instantly share code, notes, and snippets.

@iwatake2222
Last active December 5, 2017 04:43
Show Gist options
  • Save iwatake2222/6ae77db26ba01e3fee33031c47871a05 to your computer and use it in GitHub Desktop.
Save iwatake2222/6ae77db26ba01e3fee33031c47871a05 to your computer and use it in GitHub Desktop.
ラズパイでフレームバッファ(/dev/fb0)を使用して、直接ディスプレイ画像を入出力する ref: https://qiita.com/take-iwiw/items/0a7a2fefec9d93cdf6db
cat /dev/fb0 > aaa.raw
fbset
mode "800x600"
geometry 800 600 800 600 32
timings 0 0 0 0 0 0 0
rgba 8/16,8/
cat /sys/class/graphics/fb0/bits_per_pixel
32
cat /sys/class/graphics/fb0/virtual_size
800,600
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <linux/omapfb.h>
void getFrameBufferSize(int* width, int* height, int* colorWidth)
{
int fd;
int n;
char str[64];
/* Get Bit Per Pixel (usually 32-bit(4Byte) */
fd = open("/sys/class/graphics/fb0/bits_per_pixel", O_RDONLY);
n = read(fd, str, sizeof(str));
str[n] = '\0';
*colorWidth = atoi(str);
close(fd);
/* Get Frame Buffer Size */
fd = open("/sys/class/graphics/fb0/virtual_size", O_RDONLY);
n = read(fd, str, sizeof(str));
str[n] = '\0';
/* divide "800,600" into "800" and "600" */
*width = atoi(strtok(str, ","));
*height = atoi(strtok(NULL, ","));
close(fd);
}
void getFrameBufferSize2(int* width, int* height, int* colorWidth)
{
struct fb_var_screeninfo var;
int fd;
fd = open("/dev/fb0", O_RDWR);
ioctl(fd, FBIOGET_VSCREENINFO, &var);
*colorWidth = var.bits_per_pixel;
*width = var.xres_virtual;
*height = var.yres_virtual;
close(fd);
}
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void getFrameBufferSize(int* width, int* height, int* colorWidth);
void getFrameBufferSize2(int* width, int* height, int* colorWidth);
void drawColor(int width, int height, int color);
void drawColor2(int width, int height, int color);
void readBuffer(int width, int height, void* dstBuffer);
void readBuffer2(int width, int height, void* dstBuffer);
void saveFileBinary(const char* filename, void* data, int size);
// g++ main.cpp getSizeFrameBuffer.cpp readFrameBuffer.cpp writeFrameBuffer.cpp
int main()
{
int colorWidth;
int width;
int height;
/* get frame buffer size */
getFrameBufferSize(&width, &height, &colorWidth);
printf("%d(W) x %d(H) x %d(Bit)\n", width, height, colorWidth);
/* screen capture */
uint32_t *buffer = new uint32_t[width * height];
readBuffer(width, height, buffer);
saveFileBinary("capture.raw", buffer, width * height * 4);
delete[] buffer;
/* Fill solid color */
drawColor(width, height, 0xFFFF0000); // RED
}
void saveFileBinary(const char* filename, void* data, int size)
{
FILE *fp;
fp = fopen(filename, "wb");
fwrite(data, 1, size, fp);
fclose(fp);
}
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <linux/omapfb.h>
void readBuffer(int width, int height, void* dstBuffer)
{
int fd;
fd = open("/dev/fb0", O_RDONLY);
read(fd, dstBuffer, width * height * 4);
close(fd);
}
void readBuffer2(int width, int height, void* dstBuffer)
{
int fd;
fd = open("/dev/fb0", O_RDONLY);
uint32_t *frameBuffer = (uint32_t *)mmap(NULL, width * height * 4, PROT_READ, MAP_SHARED, fd, 0);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
((uint32_t*)dstBuffer)[x + y * width] = frameBuffer[x + y * width];
}
}
munmap(frameBuffer, width * height * 4);
close(fd);
}
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <linux/omapfb.h>
// color = AARRGGBB
void drawColor(int width, int height, int color)
{
uint32_t *canvas = new uint32_t[width * height];
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
canvas[x + y * width] = color;
}
}
int fd;
fd = open("/dev/fb0", O_WRONLY);
write(fd, canvas, width * height * 4);
close(fd);
delete[] canvas;
}
// color = AARRGGBB
void drawColor2(int width, int height, int color)
{
int fd;
fd = open("/dev/fb0", O_RDWR);
uint32_t *frameBuffer = (uint32_t *)mmap(NULL, width * height * 4, PROT_WRITE, MAP_SHARED, fd, 0);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
frameBuffer[x + y * width] = color;
}
}
msync(frameBuffer, width * height * 4, 0);
munmap(frameBuffer, width * height * 4);
close(fd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment