- 论坛徽章:
- 0
|
10可用积分
这个代码怎么弄成可以翻上一页或者可以显示中文- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <sys/time.h>
- #include <sys/wait.h>
- #include <sys/ipc.h>
- #include <linux/fb.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include "font_8x16.h"
-
- //画好显示区域,x方向左右空一个字,y方向上下空一个字节
- #define Start_x 16
- #define Start_y 8
- #define End_x (320 - 16)
- #define End_y (240 - 8)
- struct fb_var_screeninfo vinfo;
- struct fb_fix_screeninfo finfo;
- char *fbp = 0;
- long screensize = 0;
- long location = 0;
- int r = 0;
- unsigned int number;
- int fk;
- char buttom_status[6];
- int next_page = 0;
- unsigned int page[1000];
- unsigned int count = 0;
- int page_n;
- //清屏
- void Clean_screen()
- {
- memset(fbp, 0xf8, screensize);
- }
- void Draw_text16X16(long location_t,unsigned short color, const unsigned char ch[])
- {
- int i, j;
- unsigned char mask, buffer;
- for (i = 0; i < 16; i++)
- {
- mask = 0x80;
- buffer = ch[i * 2];
- location_t += finfo.line_length;
- for (j = 0; j < 8; j++) {
- if ((buffer & mask))
- {
- *(fbp +location_t + j * vinfo.bits_per_pixel / 8) = color;
- *(fbp +location_t + j * vinfo.bits_per_pixel / 8 + 1) = color>>8;
- }
- mask = mask >> 1;
- }
- mask = 0x80;
- buffer = ch[i * 2 + 1];
- for (j = 0; j < 8; j++)
- {
- if ((buffer & mask))
- {
- *(fbp +location_t + (8 + j) * vinfo.bits_per_pixel / 8) = color;
- *(fbp +location_t + (8 + j) * vinfo.bits_per_pixel / 8 + 1) = color>>8;
- }
- mask = mask >> 1;
- }
- }
- }
- void Draw_text16X8(long location_t,unsigned short color, const unsigned char ch[])
- {
- int i, j;
- unsigned char mask, buffer;
- for (i = 0; i < 16; i++)
- {
- mask = 0x80;
- buffer = ch[i];
- location_t += finfo.line_length;
- for (j = 0; j < 8; j++)
- {
- if ((buffer & mask))
- {
- *(fbp +location_t + j * vinfo.bits_per_pixel / 8) = color;
- *(fbp +location_t + j * vinfo.bits_per_pixel / 8 + 1) = color>>8;
- }
- mask = mask >> 1;
- }
- }
- }
- void Next_screen(long location_t, unsigned short color, unsigned char ch[], int Flag)
- {
- int ret;
- while (1)
- {
- ret = read(fk, buttom_status, 6);
- if (ret < 0)
- {
- printf("read buttom status err\n");
- }
-
- if (buttom_status[0] == '1')
- {
- next_page = 1;
- }
- if (next_page == 1)
- {
- next_page = 0;
- //清屏
- Clean_screen();
- //重新定一屏的起始位置
- location = location_t;
- r = 0;
- page[page_n] = count;
- page_n += 1;
- count = 0;
- //打印汉字
- if (Flag == 1)
- Draw_text16X16(location, color, ch);
- else if (Flag == 0)
- Draw_text16X8(location, color, ch);
- break;
- }else
- {
- continue;
- }
- }
- }
-
- void Next_line(long location_t,int r_t)
- {
- location = location_t + 16 * r_t * finfo.line_length;
- }
- void Put_lcd_Hz(long location_t, unsigned short color, unsigned char ch[])
- {
- int Flag_Hz = 0; //汉字标志位,默认为字符非汉字
- //计算字最右边的点有没有越界,判断位置需不需要换行
- location += 16 * (vinfo.bits_per_pixel / 8);
- //这个针对320x240的屏,最后一个字的点去除刚好整除为0,可以考虑用下面的方法——通用一些
- //if ((location % (vinfo.xres * vinfo.bits_per_pixel / 8)) < (16 * vinfo.bits_per_pixel /8))
- if((location - (Start_y + 16 * r) * finfo.line_length) > End_x * (vinfo.bits_per_pixel / 8))
- {
- //计算字最下边的点有没有越界,判断需不需要换屏
- if ((location + 16 * finfo.line_length) > (End_x * (vinfo.bits_per_pixel / 8) + End_y * finfo.line_length))
- {
- Flag_Hz = 1;
- Next_screen(location_t, color, ch, Flag_Hz);
- }else
- {
- r = r + 1;
- Next_line(location_t,r);
- }
- }
- //字最右边的点没有越界,位置要记得复位
- else
- location -= 16 * (vinfo.bits_per_pixel / 8);
- //打印16X16字
- Draw_text16X16(location, color, ch);
- //指向下一个字或字符的起始位置
- location += 16 * (vinfo.bits_per_pixel / 8);
- }
- void Put_lcd_CH(long location_t, unsigned short color, unsigned char ch[])
- {
- int Flag_Hz = 0; //汉字标志位,默认为字符非汉字
- //计算字最右边的点有没有越界,判断位置需不需要换行
- location += 8 * (vinfo.bits_per_pixel / 8);
- //判断位置需不需要换行
- if ((location - (Start_y + 16 * r) * finfo.line_length) > End_x * (vinfo.bits_per_pixel / 8))
- {
- //计算字最下边的点有没有越界,判断需不需要换屏
- if ((location + 16 * finfo.line_length) > (End_x * (vinfo.bits_per_pixel / 8) + End_y * finfo.line_length))
- {
- Next_screen(location_t, color, ch, Flag_Hz);
- }else
- {
- r = r + 1;
- Next_line(location_t,r);
- }
- }
- else
- location -= 8 * (vinfo.bits_per_pixel / 8);
-
- //打印16X8字
- Draw_text16X8(location, color, ch);
- location += 8 * (vinfo.bits_per_pixel / 8);
- }
- //换行符处理函数
- void do_line_break(long location_t)
- {
- //换行有可能是刚刚好也是换屏的时候
- if ((location + 16 * finfo.line_length) > (End_x * (vinfo.bits_per_pixel / 8) + (End_y - 1) * finfo.line_length))
- {
- Next_screen(location_t, 0, "next_screen", 2);
- }else
- {
- r = r + 1;
- Next_line(location_t, r);
- }
- }
-
- int main(void)
- {
- int fd, fp; //fp----LCD fd------.txt
- FILE *Hz_fp; //Hz_fp-----HZK16
- struct stat txt_stat; //文件状态,可获得文件的字节数
- off_t file_size; //文件字节数
- unsigned int hz_index, assic_index; //汉字在汉字库中的偏移量
- unsigned char *buffer; //存放文本文件内容的缓存
- unsigned char hz_buf[32], assic_buf[16]; //存放字模的数组
- long location_t; //保存一屏的起始位置
- //打开LCD设备
- fp = open("/dev/fb0", O_RDWR);
- if(fp < 0)
- {
- printf("Error : Can not open framebuffer device\n");
- exit(1);
- }
- puts("hello world.\n");
- printf("open framebuffer device\n");
-
-
- //初始化lcd
- if (ioctl(fp, FBIOGET_FSCREENINFO, &finfo))
- {
- printf("Error reading fixed information\n");
- exit(2);
- }
- if (ioctl(fp, FBIOGET_VSCREENINFO, &vinfo))
- {
- printf("Error reading fixed information\n");
- exit(3);
- }
- screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
-
- fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fp, 0);
- if ((int)fbp == -1)
- {
- printf("Error: failed to map framebuffer device to memory.\n");
- exit(4);
- }
- //清屏
- Clean_screen();
- //一屏的起始位置location_t
- location_t = Start_x * (vinfo.bits_per_pixel / 8) + Start_y * finfo.line_length;
- //location做一个全局变量,用于确定每个汉字或字符的输出位置
- location = location_t;
- fk = open("/dev/buttons", O_RDWR);
- if (fk < 0)
- {
- printf("can't open /dev/buttons\n");
- return -1;
- }
- //打开16x16的汉字库
- Hz_fp = fopen("HZK16", "rb");
- if (Hz_fp == NULL)
- {
- printf("HZK16 can't open!\n");
- return (-1);
- }
- //打开txt文本
- fd = open("/home/1.txt", O_RDONLY);
- if (fd < 0)
- {
- printf("txt can't open!\n");
- return -1;
- }
- //通过struct stat结构体求得文本的字节数
- fstat(fd, &txt_stat);
- file_size = txt_stat.st_size;
- //开辟与文本字节数一样大小的缓存
- buffer = (unsigned char *)malloc(file_size);
- //将申请到的缓存清零
- memset(buffer, 0, file_size);
- //一次性将文本的内容读到缓存中
- read(fd,buffer, file_size);
- while(*buffer != '\0')
- {
- //文本换行符
- if (*buffer == '\r' || *buffer == '\n')
- {
- do_line_break(location_t); //换行符处理函数
- buffer += 1;
- continue;
- }
- //一个tab用四个字节代替
- if (*buffer == '\t')
- {
- location += 32 * (vinfo.bits_per_pixel / 8);
- *buffer++;
- count += 1;
- continue;
- }
-
- if (*buffer == ' ' || *buffer == 0xa1)
- {
- location += 8 * (vinfo.bits_per_pixel / 8);
- *buffer++;
- count += 1;
- continue;
- }
- //判断汉字还是英文
- if (*buffer > 0xa1)
- {
- hz_index = ((*buffer - 0xa1) * 94 + (*(buffer + 1) - 0xa1)) * 32;
- fseek(Hz_fp, hz_index, SEEK_SET);
- fread(hz_buf, 32, 1, Hz_fp);
- Put_lcd_Hz(location_t, 0xf800, hz_buf);
- //sleep(1);
- buffer += 2;
- count += 2;
- }
- else
- {
- assic_index = *buffer * 16;
- memcpy(assic_buf, &Assic[assic_index], 16);
- Put_lcd_CH(location_t, 0x07e0, assic_buf);
- count++;
- buffer++;
- }
- }
-
- munmap(fbp, screensize);
- close(fp);
- fclose(Hz_fp);
- close(fk);
- close(fd);
- return 0;
- }
复制代码 |
|