I would like to introduce some small project for study, which are list below.
iolib- :OOD in C++, easy and explicit structure
fb-graphics : linux Frambuffer
libserial : OOD in C++
The following is the source code of fb- graphics .
--------------app-----------
#include "graphics.h"
int main()
{
int i;
InitDevice();
for (i=0;i<=1024;i++)
{
DrawPoint(i,-300*sin(0.015*i)+400);
usleep(300);
}
SaveScreen();
scanf("%d", &i);
RestoreScreen();
if(i == 1)
DrawRect(0,100,400,600);
else
DrawRect(200,200,600,400);
ClearWorld();
exit(0);
}
---------------lib code---------
#include "graphics.h"
struct FbInfo device_info;
struct Color front_color;
struct Color back_color;
int fp;
int InitDevice(void)
{
char *dev_id;
/*鍓嶇棰滆壊榛樿涓虹櫧鑹?/
front_color.blue |= 0xFF;
front_color.green |= 0xFF;
front_color.red |= 0xFF;
/*鍚庣棰滆壊榛樿涓洪粦鑹?/
back_color.blue &= 0;
back_color.green &= 0;
back_color.red &= 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
if ((dev_id = getenv("FB_DEVICE")) == NULL)
{
dev_id = "/dev/fb0"; /*娌℃湁鎵惧埌鐜鍙橀噺锛岀寽娴嬩竴涓澶囥€?/
}
if((fp = open (dev_id,O_RDWR)) < 0)
GoError("Can't open the file!");
if ((ioctl(fp,FBIOGET_FSCREENINFO,&finfo)) < 0)
GoError("Can't get the fixed info!");
if ((ioctl(fp,FBIOGET_VSCREENINFO,&vinfo)) < 0)
GoError("Can't get variable info");
device_info.bits_per_pixel = vinfo.bits_per_pixel;
device_info.line_len_bytes = finfo.line_length;
device_info.screen_len = vinfo.xres * vinfo.yres * (device_info.bits_per_pixel / 8);
device_info.x_res = vinfo.xres;
device_info.y_res = vinfo.yres;
if ((device_info.mem_pointer = (char *) mmap (0,
device_info.screen_len,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fp, 0)) < 0)
GoError("Can't map device into mem!");
if ((memset((void*) device_info.mem_pointer,0,device_info.screen_len)) < 0)
GoError("Can't memset.");
device_info.backup_pointer = malloc(device_info.screen_len);
if(device_info.backup_pointer == NULL)
GoError("Can't malloc.");
return 0;
}
void SaveScreen(void)
{
memcpy(device_info.backup_pointer,device_info.mem_pointer,device_info.screen_len);
}
void RestoreScreen(void)
{
memcpy(device_info.mem_pointer, device_info.backup_pointer, device_info.screen_len);
}
void DrawPoint(int32_t x,int32_t y)
{
int32_t location;
if(x > device_info.x_res || y > device_info.y_res )
return;
location = x * (device_info.bits_per_pixel / 8)
+ (y * device_info.line_len_bytes);
*(device_info.mem_pointer + location) = front_color.blue;
*(device_info.mem_pointer + location + 1) = front_color.green;
*(device_info.mem_pointer + location + 2) = front_color.red;
*(device_info.mem_pointer + location + 3) = 0;
}
void DrawLine(int32_t x1 , int32_t y1,int32_t x2,int32_t y2)
{
int i;
if (x1 != x2)
{
for (i = MIN(x1,x2) ; i <=MAX(x1,x2) ; i++)
{
DrawPoint(i , ((y2 - y1) / (x2 - x1)) * (i - x1) + y1);
}
}
else if (x1 == x2)
{
for (i = MIN(y1 , y2) ; i <= MAX(y1,y2) ; i++)
{
DrawPoint(x1,i);
}
}
}
/*
* (x1,y1)鏄乏涓婅鐨勭偣銆?x2,y2)鏄彸涓嬭鐨勭偣銆?
*
*/
void DrawRect(int32_t x1 , int32_t y1 , int32_t x2 , int32_t y2)
{
DrawLine(x1,y1,x2,y1);
DrawLine(x2,y1,x2,y2);
DrawLine(x2,y2,x1,y2);
DrawLine(x1,y2,x1,y1);
}
void FillRect(int32_t x1 , int32_t y1 , int32_t x2 , int32_t y2)
{
while (x1 - x2 < 1 || y1 - y2 < 1)
{
DrawRect(x1++ , y1++ , x2-- , y2--);
}
}
void DrawArc(int32_t x0 , int32_t y0, int32_t arc_start , int32_t arc_end,int32_t r)
{
int32_t end = MAX(arc_start,arc_end);
int32_t start = MIN(arc_start,arc_end);
float i;
for (i = start ; i <= end ; i++)
{
DrawPoint(r * cos(GETDEGREE(i)) + x0 , -r * sin(GETDEGREE(i)) +y0);
}
}
void DrawCirc(int32_t x0,int32_t y0,int32_t r)
{
DrawArc(x0,y0,0,360,r);
}
/*
void FillArc(int32_t x0 , int32_t y0, int32_t arc_start , int32_t arc_end,int32_t r)
{
int32_t end = MAX(arc_start,arc_end);
int32_t start = MIN(arc_start,arc_end);
float i;
for (i = start ; i <= end ; i++)
{
DrawPoint(r * cos(GETDEGREE(i)) + x0 , -r * sin(GETDEGREE(i)) +y0);
DrawLine(x0,y0,r * cos(GETDEGREE(i)) + x0 , -r * sin(GETDEGREE(i)) +y0);
}
}
*/
void FillCirc(int32_t x0,int32_t y0,int32_t r)
{
int i,j;
for (i = x0 - r; i <= x0 + r;i++)
for(j= y0 - r; j <= y0 + r; j++)
{
if(POWER(i-x0)+POWER(j-y0) <= POWER(r))
DrawPoint(i,j);
}
}
/*
void DrawCirc2(int32_t x0,int32_t y0,int32_t r)
{
int i,j;
for (i = x0 - r; i <= x0 + r;i++)
for(j= y0 - r; j <= y0 + r; j++)
{
if(POWER(i-x0)+POWER(j-y0)-POWER(r) <= 500 && POWER(i-x0)+POWER(j-y0)-POWER(r) >= 0)
{
DrawPoint(i,j);
}
}
}
*/
int ClearWorld(void)
{
munmap(device_info.mem_pointer,device_info.screen_len);
if(device_info.backup_pointer != NULL)
{
free(device_info.backup_pointer);
device_info.backup_pointer = NULL;
}
close(fp);
return 0;
}
void SetFrontColor(char red,char green,char blue)
{
front_color.red = red;
front_color.blue = blue;
front_color.green = green;
}
void SetBackColor(char red,char green,char blue)
{
int32_t location,x,y;
for (x = 0;x<device_info.x_res;x++)
for(y = 0;y<device_info.y_res;y++)
{
location = x * (device_info.bits_per_pixel / 8)
+ (y * device_info.line_len_bytes);
*(device_info.mem_pointer + location) = blue;
*(device_info.mem_pointer + location + 1) = green;
*(device_info.mem_pointer + location + 2) = red;
*(device_info.mem_pointer + location + 3) = 0;
}
}
void PrintInfo(void)
{
printf("The mem is :%d/n",device_info.screen_len);
printf("The line_length is :%d/n",device_info.line_len_bytes);
printf("The xres is :%d/n",device_info.x_res);
printf("The yres is :%d/n",device_info.y_res);
printf("bytes_per_pixel is :%d/n",(device_info.bits_per_pixel/8));
}
int GoError(char *msg)
{
fprintf(stderr,"%s/n",msg);
exit(1);
}
------------------------
The following phase has just occured to me. :-)
"屁股决定脑袋"
“ 一般来说,人都是个人利益高于公司利益,如果个人利益都没有得到满足,谁会去考虑公司利益啊”
“ 对搞技术的人说, 不是光为了钱 才就给你干活的~”