#include <linux/fb.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include "colorIdx.h"
#define FB_DEV "/dev/fb0"
struct fb_var_screeninfo var;
char *fb_dev_base;
int fb_dev_pitch;
int fb_dev_size;
static void setcolor(int fd)
{
struct fb_cmap cmap;
unsigned short r_table[256];
unsigned short g_table[256];
unsigned short b_table[256];
unsigned short trans_table[256];
int i;
cmap.start = 0;
cmap.len = COLOR_IDX_MAX;
for (i = 0; i < cmap.len - cmap.start; i++) {
r_table[i] = (colorIdx[i] & 0xff0000)>>16;
g_table[i] = (colorIdx[i] & 0x00ff00)>>8;
b_table[i] = (colorIdx[i] & 0x0000ff);
if (0==i)
trans_table[i] = 0;
else
trans_table[i] =255;
}
cmap.red = r_table;
cmap.green = g_table;
cmap.blue = b_table;
cmap.transp = trans_table;
if (ioctl(fd, FBIOPUTCMAP, &cmap) < 0) {
printf("Unable to put cmap!");
return;
}
}
void DrawRect(int x,int y,int width,int height,unsigned char color)
{
int i,j;
int location=0;
for(i=y;i<y+height;++i)
{
for(j=x;j<x+width;++j)
{
location=var.xres*(i-1)+j;
*(fb_dev_base + location) = color;
}
}
}
int main()
{
int fd;
fd=open(FB_DEV,O_RDWR,0);
if (fd > 0) printf("open %s success.\n", FB_DEV);
else {
perror(FB_DEV);
return -1;
}
if (ioctl(fd, FBIOGET_VSCREENINFO, &var) < 0)
{
perror("FBIOGET_VSCREENINFO");
goto Error1;
}
printf("xres: %d, yres: %d, bpp: %d\n", var.xres, var.yres, var.bits_per_pixel);
fb_dev_pitch = var.xres * (var.bits_per_pixel >> 3);
fb_dev_size = fb_dev_pitch * var.yres;
fb_dev_base = mmap(NULL, fb_dev_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (fb_dev_base == MAP_FAILED)
{
perror("mmap");
goto Error1;
}
setcolor(fd);
memset(fb_dev_base,0,fb_dev_size);
DrawRect(100,100,640,480,100);
Error2:
if (munmap(fb_dev_base, fb_dev_size) < 0)
{
perror("munmap");
}
Error1:
close(fd);
return 0;
}
// ====== .h
#ifndef __COLOR_INDEX__H
#define __COLOR_INDEX__H
#ifdef __cplusplus
extern "C"
{
#endif
#define COLOR_IDX_MAX 256
unsigned int colorIdx[COLOR_IDX_MAX] = {
0x0,
0x800000,
0x8000,
0x808000,
0x80,
0x800080,
0x8080,
0xc0c0c0,
0xc0dcc0,
0xa6caf0,
0x402000,
0x602000,
0x802000,
0xa02000,
0xc02000,
0xe02000,
0x4000,
0x204000,
0x404000,
0x604000,
0x804000,
0xa04000,
0xc04000,
0xe04000,
0x6000,
0x206000,
0x406000,
0x606000,
0x806000,
0xa06000,
0xc06000,
0xe06000,
0x8000,
0x208000,
0x408000,
0x608000,
0x808000,
0xa08000,
0xc08000,
0xe08000,
0xa000,
0x20a000,
0x40a000,
0x60a000,
0x80a000,
0xa0a000,
0xc0a000,
0xe0a000,
0xc000,
0x20c000,
0x40c000,
0x60c000,
0x80c000,
0xa0c000,
0xc0c000,
0xe0c000,
0xe000,
0x20e000,
0x40e000,
0x60e000,
0x80e000,
0xa0e000,
0xc0e000,
0xe0e000,
0x40,
0x200040,
0x400040,
0x600040,
0x800040,
0xa00040,
0xc00040,
0xe00040,
0x2040,
0x202040,
0x402040,
0x602040,
0x802040,
0xa02040,
0xc02040,
0xe02040,
0x4040,
0x204040,
0x404040,
0x604040,
0x804040,
0xa04040,
0xc04040,
0xe04040,
0x6040,
0x206040,
0x406040,
0x606040,
0x806040,
0xa06040,
0xc06040,
0xe06040,
0x8040,
0x208040,
0x408040,
0x608040,
0x808040,
0xa08040,
0xc08040,
0xe08040,
0xa040,
0x20a040,
0x40a040,
0x60a040,
0x80a040,
0xa0a040,
0xc0a040,
0xe0a040,
0xc040,
0x20c040,
0x40c040,
0x60c040,
0x80c040,
0xa0c040,
0xc0c040,
0xe0c040,
0xe040,
0x20e040,
0x40e040,
0x60e040,
0x80e040,
0xa0e040,
0xc0e040,
0xe0e040,
0x80,
0x200080,
0x400080,
0x600080,
0x800080,
0xa00080,
0xc00080,
0xe00080,
0x2080,
0x202080,
0x402080,
0x602080,
0x802080,
0xa02080,
0xc02080,
0xe02080,
0x4080,
0x204080,
0x404080,
0x604080,
0x804080,
0xa04080,
0xc04080,
0xe04080,
0x6080,
0x206080,
0x406080,
0x606080,
0x806080,
0xa06080,
0xc06080,
0xe06080,
0x8080,
0x208080,
0x408080,
0x608080,
0x808080,
0xa08080,
0xc08080,
0xe08080,
0xa080,
0x20a080,
0x40a080,
0x60a080,
0x80a080,
0xa0a080,
0xc0a080,
0xe0a080,
0xc080,
0x20c080,
0x40c080,
0x60c080,
0x80c080,
0xa0c080,
0xc0c080,
0xe0c080,
0xe080,
0x20e080,
0x40e080,
0x60e080,
0x80e080,
0xa0e080,
0xc0e080,
0xe0e080,
0xc0,
0x2000c0,
0x4000c0,
0x6000c0,
0x8000c0,
0xa000c0,
0xc000c0,
0xe000c0,
0x20c0,
0x2020c0,
0x4020c0,
0x6020c0,
0x8020c0,
0xa020c0,
0xc020c0,
0xe020c0,
0x40c0,
0x2040c0,
0x4040c0,
0x6040c0,
0x8040c0,
0xa040c0,
0xc040c0,
0xe040c0,
0x60c0,
0x2060c0,
0x4060c0,
0x6060c0,
0x8060c0,
0xa060c0,
0xc060c0,
0xe060c0,
0x80c0,
0x2080c0,
0x4080c0,
0x6080c0,
0x8080c0,
0xa080c0,
0xc080c0,
0xe080c0,
0xa0c0,
0x20a0c0,
0x40a0c0,
0x60a0c0,
0x80a0c0,
0xa0a0c0,
0xc0a0c0,
0xe0a0c0,
0xc0c0,
0x20c0c0,
0x40c0c0,
0x60c0c0,
0x80c0c0,
0xa0c0c0,
0xfffbf0,
0xa0a0a4,
0x808080,
0xff0000,
0xff00,
0xffff00,
0xff,
0xff00ff,
0xffff,
0xffffff,
};
#ifdef __cplusplus
}
#endif
#endif
================================================================================
此外:还可采用RGB 332
#define RGB(r,g,b) ((U16)(((b & 0xc0) >> 6) | ((g & 0xe0) >> 3) | (r & 0xe0)))
#define GetRValue(rgb) ((U8)((rgb & 0xe0)))
#define GetGValue(rgb) ((U8)((rgb & 0x1c)))
#define GetBValue(rgb) ((U8)((rgb & 0x03)))