lcd framebuffer program
2016-07-12 22:42
288 查看
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#define cpu_get_pc() \
({ \
register long pg asm("r0"); \
__asm__("mov %0, pc"
\
: "=r" (pg) : : "cc");
\
(unsigned int *)(pg);
\
})
int main()
{
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
char *fbp = 0;
int x = 0, y = 0;
long int location = 0;
// Open the file for reading and writing
fbfd = open("/dev/fb0", O_RDWR);
if( fbfd == -1 ){
perror("Error: cannot open framebuffer device");
exit(1);
}
printf( "fbfd = %d.\n", fbfd );
printf( "cpu_get_pc() = %x.\n", cpu_get_pc() );
printf("The framebuffer device was opened successfully.\n");
// Get fixed screen information
if( ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1 ){
perror("Error reading fixed information");
exit(2);
}
printf( "&finfo = 0x%x.\n", &finfo );
// Get variable screen information
if( ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1 ){
perror("Error reading variable information");
exit(3);
}
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
// Figure out the size of the screen in bytes
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
// Map the device to memory
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
//do_mmap
if( (int)fbp == -1 ){
perror("Error: failed to map framebuffer device to memory");
exit(4);
}
printf( "fbp = 0x%x.\n", fbp );
printf( "screensize = 0x%x.\n", screensize );
printf("The framebuffer device was mapped to memory successfully.\n");
if( ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1 ){
perror("Error reading fixed information");
exit(2);
}
x = 100; y = 100;
// Where we are going to put the pixel
// Figure out where in memory to put the pixel
printf( "vinfo.xoffset = %d.\n", vinfo.xoffset );
printf( "vinfo.yoffset = %d.\n", vinfo.yoffset );
printf( "vinfo.bits_per_pixel = %d.\n", vinfo.bits_per_pixel );
printf( "finfo.line_length = %d.\n", finfo.line_length );
for (y = 0; y < 40; y++)
for (x = 0; x < 120; x++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
(y+vinfo.yoffset) * finfo.line_length;
if (vinfo.bits_per_pixel == 32) {
*(fbp + location) = 100; // Some blue
*(fbp + location + 1) = 15+(x-100)/2; // A little green
*(fbp + location + 2) = 200-(y-100)/5; // A lot of red
*(fbp + location + 3) = 0; // No transparency
} else {
//assume 16bpp
int b = 0;
//int g = (x-100)/6;
// A little green
int g = 0;
//int r = 31-(y-100)/16;
// A lot of red
int r = 10;
unsigned short int t = r<<11 | g << 5 | b;
*((unsigned short int*)(fbp + location)) = t;
//printf( "t = %x.\n", t );
}
}
munmap(fbp, screensize);
close(fbfd);
return 0;
}
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#define cpu_get_pc() \
({ \
register long pg asm("r0"); \
__asm__("mov %0, pc"
\
: "=r" (pg) : : "cc");
\
(unsigned int *)(pg);
\
})
int main()
{
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
char *fbp = 0;
int x = 0, y = 0;
long int location = 0;
// Open the file for reading and writing
fbfd = open("/dev/fb0", O_RDWR);
if( fbfd == -1 ){
perror("Error: cannot open framebuffer device");
exit(1);
}
printf( "fbfd = %d.\n", fbfd );
printf( "cpu_get_pc() = %x.\n", cpu_get_pc() );
printf("The framebuffer device was opened successfully.\n");
// Get fixed screen information
if( ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1 ){
perror("Error reading fixed information");
exit(2);
}
printf( "&finfo = 0x%x.\n", &finfo );
// Get variable screen information
if( ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1 ){
perror("Error reading variable information");
exit(3);
}
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
// Figure out the size of the screen in bytes
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
// Map the device to memory
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
//do_mmap
if( (int)fbp == -1 ){
perror("Error: failed to map framebuffer device to memory");
exit(4);
}
printf( "fbp = 0x%x.\n", fbp );
printf( "screensize = 0x%x.\n", screensize );
printf("The framebuffer device was mapped to memory successfully.\n");
if( ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1 ){
perror("Error reading fixed information");
exit(2);
}
x = 100; y = 100;
// Where we are going to put the pixel
// Figure out where in memory to put the pixel
printf( "vinfo.xoffset = %d.\n", vinfo.xoffset );
printf( "vinfo.yoffset = %d.\n", vinfo.yoffset );
printf( "vinfo.bits_per_pixel = %d.\n", vinfo.bits_per_pixel );
printf( "finfo.line_length = %d.\n", finfo.line_length );
for (y = 0; y < 40; y++)
for (x = 0; x < 120; x++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
(y+vinfo.yoffset) * finfo.line_length;
if (vinfo.bits_per_pixel == 32) {
*(fbp + location) = 100; // Some blue
*(fbp + location + 1) = 15+(x-100)/2; // A little green
*(fbp + location + 2) = 200-(y-100)/5; // A lot of red
*(fbp + location + 3) = 0; // No transparency
} else {
//assume 16bpp
int b = 0;
//int g = (x-100)/6;
// A little green
int g = 0;
//int r = 31-(y-100)/16;
// A lot of red
int r = 10;
unsigned short int t = r<<11 | g << 5 | b;
*((unsigned short int*)(fbp + location)) = t;
//printf( "t = %x.\n", t );
}
}
munmap(fbp, screensize);
close(fbfd);
return 0;
}
相关文章推荐
- lcd framebuffer
- lcd framebuffer 编程
- JS精确到小数点两位
- Js获取下拉框的值和文本select
- JQueryMobile学习
- JQ语法
- JS获取活动区域高和宽
- js判断浏览器
- 实现异步加载js文件及加载完成后回调
- 【OpenLayer3】Feature设置不同颜色技巧
- Blob file download in Angular.js using $resource
- 剑指offer系列-T20顺时针打印矩阵
- JS中的对象联姻:call方法
- jquery $.extend()【2】
- css折叠样式(3)――常用样式属性
- 【一天一道LeetCode】#237. Delete Node in a Linked List
- 【一天一道LeetCode】#237. Delete Node in a Linked List
- 关于ButterKnife注解不能获取到实例的几种可能
- backbonejs
- jQuery效果(一)——隐藏/显示