#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <linux/fb.h>
#include <linux/kd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include "ascii.lib"
#define SYS_BLACK 0x00000000
#define SYS_WHITE 0xffffffff
file *hzkfile=null;
unsigned char * chsTxt = "青梅不爱竹马";
short x,y;
short i;
unsigned char bufferTxt[2];
typedef unsigned long ColorType;
ColorType color = SYS_WHITE;
void DrawCharCHS(int x, int y, unsigned char c[2], ColorType color);//打印汉字
void fb_Text_16x16(int x, int y, unsigned char * dotCodes, ColorType color);//处理16x16的汉字点阵
void fb_PutPixel(short x, short y, ColorType color/*, int xorm */);//打印相应的点
void main()
{
hzkFile = fopen("./hz16", "rb");
x = 10, y = 40;
for (i = 0; i < strlen(chsTxt); i += 2)
{
bufferTxt[0] = chsTxt[i];
bufferTxt[1] = chsTxt[i + 1];
DrawCharCHS(x, y, bufferTxt, color);
x += 16;
}
fclose(hzkFile);
}
void DrawCharCHS(int x, int y, unsigned char c[2], ColorType color)
{
unsigned char codes[32];
short i;
unsigned char ch, cl;
unsigned long offset;
if (hzkFile == NULL)
{
printf("No Chinese Character Library opened.\n");
exit(1);
}
ch = c[0];//内码
cl = c[1];//内码
offset = ((ch - 0xa1) * 94L + (cl - 0xa1)) * 32L;
fseek(hzkFile, offset, SEEK_SET);//通过区位号找到汉字
fread(codes, 32, 1, hzkFile);
fb_Text_16x16(x, y, codes, color);
}
void fb_Text_16x16(int x, int y, unsigned char * dotCodes, ColorType color)
{
int i, j, k;
if (x < 0 || x >= SCREEN_WIDTH ||
y < 0 || y >= SCREEN_HEIGHT)
{
#ifdef ERR_DEBUG
printf("DEBUG_INFO: Pixel out of screen range.\n");
printf("DEBUG_INFO: x = %d, y = %d\n", x, y);
#endif
return;
}
if (dotCodes == NULL)
{
#ifdef ERR_DEBUG
printf("DEBUG_INFO: Dot codes NULL.\n");
#endif
return;
}
for(i = 0; i < 16; i++)
{
for (j = 0; j < 2; j++)
{
x += 8 * ( j + 1);
for (k = 0; k < 8; k++)
{
x--;
if ((dotCodes[2 * i + j] >> k) & 0x1)
{
fb_PutPixel(x, y, color);
}
}
}
x -= 8;
++y;
}
return;
}
void fb_PutPixel(short x, short y, ColorType color/*, int xorm */)
{
void * currPoint;
//下面的SCREEN_WIDTH和SRCEEN_HEIGHT为调用ioctl(fb_con, FBIOGET_VSCREENINFO, &vinfo)得到其中vinfo为帧缓存中struct fb_var_screeninfo结构体中的信息。
if (x < 0 || x >= SCREEN_WIDTH ||
y < 0 || y >= SCREEN_HEIGHT)
{
#ifdef ERR_DEBUG
printf("DEBUG_INFO: Pixel out of screen range.\n");
printf("DEBUG_INFO: x = %d, y = %d\n", x, y);
#endif
return;
}
// Calculate address of specified point
currPoint = (ByteType *)frame_base + y * LINE_SIZE + x * PIXEL_SIZE;//写入指定的缓存区域,LINE_SIZE为一行的点的个数,PIXEL_SIZE为一个像素点的大小。
#ifdef DEBUG
printf("DEBUG_INFO: x = %d, y= %d, currPoint = 0x%x\n", x, y, currPoint);
#endif
switch (BPP)
{
case 8:
*((ByteType *)currPoint) = (color & 0xff);
break;
case 16:
*((WordType *)currPoint) = (color & 0xffff);
break;
}
return;
}