#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include "common.h"
#define FB_DEVICE "/dev/fb0"
#define COLOR_RED 0x00FF0000
int fb_draw_chessboard(int x, int y, int color);
int fb_draw_stones(int appro_x, int appro_y, int color);
int stone_save(int x, int y);
int stone_restore(int x, int y);
int save_cursor[SAVE_SIZE * SAVE_SIZE];
int main(void)
{
int i,j;
int x,y,r;
int fd,prot;
size_t length;
struct fb_var_screeninfo fb_var;
fd = open(FB_DEVICE, O_RDWR);
if (fd < 0)
{
fprintf(stderr, "Open %s failed: %s\n", FB_DEVICE, strerror(errno));
exit(EXIT_FAILURE);
}
if (ioctl(fd, FBIOGET_VSCREENINFO, &fb_var) < 0)
{
close(fd);
fprintf(stderr, "fb ioctl failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
fb_inf.w = fb_var.xres;
fb_inf.h = fb_var.yres;
fb_inf.bpp = fb_var.bits_per_pixel;
// printf("w:%d\th:%d\tbpp:%d\t\n",fb_inf.w,fb_inf.h,fb_inf.bpp);
length = fb_inf.w * fb_inf.h * fb_inf.bpp / 8;
prot = PROT_READ | PROT_WRITE;
fb_inf.fbmem = mmap(0, length, prot, MAP_SHARED, fd, 0);
if (fb_inf.fbmem == MAP_FAILED)
{
close(fd);
fprintf(stderr, "fb mmap failed:%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
close(fd);
// draw chess-board and chess
fb_draw_chessboard(X_START, Y_START, LINE_COLOR);
// fb_draw_stones(X_START+EACH_SQU, Y_START+EACH_SQU, WHITE);
// fb_draw_stones(X_START+3*EACH_SQU, Y_START+2*EACH_SQU, BLACK);
srand(time(NULL));
for(i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
{
x = X_START + j * EACH_SQU;
y = Y_START + i * EACH_SQU;
stone_save(x-PISE_SIZE, y-PISE_SIZE);
// r = rand() % 0x01000000;
r = rand() & 0x00FFFFFF;
// fb_draw_stones(x, y, COLOR_RED+r);
fb_draw_stones(x, y, BLACK+r);
sleep(2);
stone_restore(x-PISE_SIZE, y-PISE_SIZE);
}
}
exit(EXIT_SUCCESS);
}
// draw a color point
// return 0 : success
int fb_pixel(int x, int y, int color)
{
int offset;
offset = (fb_inf.w * y + x) * fb_inf.bpp / 8;
*(int *)(fb_inf.fbmem + offset) = color;
return 0;
}
// draw a color line, the slope is single
int fb_draw_row(int x, int y, int len, int color)
{
int i;
for(i=0; i<len; i++)
{
fb_pixel(x+i, y, color);
}
return 0;
}
int fb_draw_col(int x, int y, int len, int color)
{
int i;
for(i=0; i<len; i++)
{
fb_pixel(x, y+i, color);
}
return 0;
}
// draw a color squarearea
int fb_draw_squarearea(int x, int y, int x_len, int y_len, int color)
{
int i, j;
for(j=0; j<y_len; j++)
{
for(i=0; i<x_len; i++)
{
fb_pixel(x+i, y+j, color);
}
}
return 0;
}
int fb_draw_chessboard(int x, int y, int color)
{
int i=0;
int xx=x;
int yy=y;
int x_len,y_len;
x_len = EACH_SQU * (COL - 1) + 60;
y_len = EACH_SQU * (ROW - 1) + 60;
fb_draw_squarearea(x-30, y-30, x_len, y_len, BG_COLOR);
for(i=0; i<ROW; i++)
{
fb_draw_row(x, y, EACH_SQU*(COL-1), color);
y += EACH_SQU;
}
for(i=0; i<COL; i++)
{
fb_draw_col(xx, yy, EACH_SQU*(ROW - 1), color);
xx += EACH_SQU;
}
return 0;
}
// draw a chess stones
int fb_draw_stones(int appro_x, int appro_y, int color)
{
int i, j;
for(i=(appro_x-PISE_SIZE); i<=(appro_x + PISE_SIZE); i++)
{
for(j=(appro_y-PISE_SIZE); j<=(appro_y + PISE_SIZE); j++)
{
if ((i-appro_x)*(i-appro_x)+(j-appro_y)*(j-appro_y) <= PISE_SIZE*PISE_SIZE)
{
fb_pixel(i, j, color);
}
}
}
return 0;
}
int stone_save(int x, int y)
{
int i,j,offset;
for(j=0; j<SAVE_SIZE; j++)
{
for(i=0; i<SAVE_SIZE; i++)
{
offset = ((x + i) + (y + j) * fb_inf.w) * fb_inf.bpp / 8;
save_cursor[i+j*SAVE_SIZE] = *(int *)(fb_inf.fbmem + offset);
}
}
return 0;
}
int stone_restore(int x, int y)
{
int i,j,color;
for(j=0; j<SAVE_SIZE; j++)
{
for(i=0; i<SAVE_SIZE; i++)
{
color = save_cursor[i+j*SAVE_SIZE];
fb_pixel(x+i, y+j, color);
}
}
return 0;
}
fb.tar.gz_fb mmap_frambuffer_linux fb mmap
版权申诉
94 浏览量
2022-09-24
03:04:39
上传
评论
收藏 1KB GZ 举报
朱moyimi
- 粉丝: 61
- 资源: 1万+
最新资源
- WS2-32.lib,在编译程序中可以链接使用
- 秒懂傅里叶变换matlab程序实现过程
- ZEND解密dezender12
- sony 索尼IMX334摄像头模组电路板AD版硬件PCB图(6层板).zip
- 基于flask和echarts融合交易策略的bitfinex可视化微服务.zip
- 包含了wvp-assist.tar wvp-talk.tar zlmediakit.tar .
- 3r4efgh53wgrf43tw
- 2024新版Java基础从入门到精通全套视频+资料下载
- Spring AI大模型视频教程+ChatGPT视频教程+OpenAI大模型视频教程(资料+视频教程)
- ABB工业机器人教程PDF版本
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈