有关于电子书代码的问题,求大神指导!!!
小弟打算写个读取电子书的代码,现在只能翻下一页,请问怎么才能“翻上一页”。求指导代码如下
程序代码:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <linux/fb.h> #include <sys/mman.h> #include <sys/time.h> #include <sys/wait.h> #include <sys/ipc.h> #include "font_8x16.h" #define Width 320 //LCD屏的宽度 #define Heigth 240 //LCD屏的高度 #define Start_x 8 //屏-左上角的起始坐标 x y #define Start_y 8 #define End_x (Width - 8) //屏-左上角的起始坐标 x y #define End_y (Heigth - 8) int fk; //按键的文件描述符 char button_status[6]; int fp=0; //LCD屏的文件描述符 struct fb_var_screeninfo vinfo; //LCD可变参数结构体 struct fb_fix_screeninfo finfo; //LCD固定参数结构体 char *fbp = 0; long screensize=0; //初始化屏幕大小为0 int s_x = 0, s_y = 0,id = 0; //屏幕的x y 坐标 //清屏 void clear_screen() { memset(fbp, 0xf8, screensize); } //绘制单个字符 void Draw_8X16(int x, int y, const unsigned char ch[]) { int i = 0, j = 0; unsigned char mask, buffer; long location_tt; location_tt = x * vinfo.bits_per_pixel / 8 + y *finfo.line_length; for (i = 0; i < 16; i++) { mask = 0x80; buffer = ch[i]; location_tt += finfo.line_length; for (j = 0; j < 8; j ++) { if ((buffer & mask)) { *(fbp +location_tt + j * vinfo.bits_per_pixel / 8) = 0; *(fbp +location_tt + j * vinfo.bits_per_pixel / 8 + 1) = 0; } mask = mask >> 1; } } s_x += 8; } //绘制单个汉字 16 * 16 void Draw_16X16(int x, int y, const unsigned char ch[]) { int i = 0, j = 0; unsigned char mask, buffer; long location_tt; location_tt = x * vinfo.bits_per_pixel / 8 + y * finfo.line_length; for (i = 0; i < 16; i++) { mask = 0x80; buffer = ch[i * 2]; location_tt += finfo.line_length; for (j = 0; j < 8; j++) { if ((buffer & mask)) { *(fbp +location_tt + j * vinfo.bits_per_pixel / 8) = 0; *(fbp +location_tt + j * vinfo.bits_per_pixel / 8 + 1) = 0; } mask = mask >> 1; } mask = 0x80; buffer = ch[i * 2 + 1]; for (j = 0; j < 8; j++) { if ((buffer & mask)) { *(fbp +location_tt + (8 + j) * vinfo.bits_per_pixel / 8) = 0; *(fbp +location_tt + (8 + j) * vinfo.bits_per_pixel / 8 + 1) = 0; } mask = mask >> 1; } } s_x += 16; } //判断是否换行 x坐标 int Is_next_line(int x) { if (x >= End_x) return 1; return 0; } //跳到下一行 int Next_line(int y) { //sleep(1); y = y + 16; s_y = y; s_x = Start_x; return y; } //判断是否换屏 y坐标 int Is_next_screen(int y) { if (y >= End_y - 8) return 1; return 0; } //清屏,跳到下一屏 void Next_screen() { int ret; int i; while (1) { ret = read(fk, button_status, 6); if (ret < 0) { printf("read button status err\n"); } //按键控制翻到下一页 if (button_status[0] == '1') { //清屏 clear_screen(); //s_x = Start_x; s_y = Start_y; break; } if (button_status[1] == '1'){ for(i=0;i<255;i++) ; if (button_status[1] == '1'){ clear_screen(); printf("%d\n",id); id=id-532; s_y = Start_y; break;} } } // sleep(2); } //清屏,跳到上一屏 void UP_screen(unsigned int zf_index) { int ret; //int i=0; // unsigned char *buffer; //unsigned char buf[16]; while (1) { ret = read(fk, button_status, 6); if (ret < 0) { printf("read button status err\n"); } //按键控制翻到上一页 if (button_status[1] == '1') { //清屏 clear_screen(); //s_x = Start_x; //zf_index = buffer[i] * 16; // memcpy(buf, (const void *)&Assic[zf_index], 16); s_y=zf_index; break; } } // sleep(2); } //绘制一行字符 void Put_text_ch(int x, int y, int size, const unsigned char ch[]) { int i = 0, j = 0; unsigned char buffer[16]; for (i = 0; i < size; i++) { for (j = 0; j < 16; j++) { buffer[j] = ch[i * 16 + j]; } if (i != 0) x += 8; if(Is_next_line(x)) { y = Next_line(y); x = s_x; if (Is_next_screen(y)) { Next_screen(); y = s_y; } } Draw_8X16(x, y, buffer); } } //绘制一行汉字 void Put_text_hz(int x, int y, int size, const unsigned char ch[]) { int i = 0, j = 0; unsigned char buffer[32]; for (i = 0; i < size; i++) { for (j = 0; j < 32; j++) { buffer[j] = ch[i * 32 + j]; } if (i != 0) x += 16; if(Is_next_line(x)) { y = Next_line(y); x = s_x; if (Is_next_screen(y)) { Next_screen(); y = s_y; } } Draw_16X16(x, y, buffer); } } //打开LCD设备 void open_lcd() { //以可读可写的方式打开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"); //获取屏幕相关参数 if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo)){ printf("Error reading fixed information\n"); exit(2); } if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo)){ printf("Error reading variable information\n"); exit(3); } //计算屏幕缓冲区大小 screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; //单帧画面空间 printf("可见屏幕一行有多少个像素点:xres= %d\n", vinfo.xres); printf("可见屏幕一列有多少个像素点:yres = %d\n", vinfo.yres); printf("每个相似的位数即BPP: bits_per_pixel = %d\n", vinfo.bits_per_pixel); /*这就是把fp所指的文件中从开始到screensize大小的内容给映射出来,得到一个指向这块空间的指针*/ 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); } printf("The Framebuffer device was mapped to memory successfully!\n"); } //关闭LCD设备 void close_lcd() { munmap (fbp, screensize); /*解除映射*/ close (fp); /*关闭文件*/ } //打开文本文件 int open_txt() { int fd_t; fd_t = open("./1.txt",0); if (fd_t < 0) { printf("txt can't open!\n"); return (-1); } return fd_t; } //关闭文本文件 void close_txt(int fd_t) { close(fd_t); } //计算文件总字节数 off_t cal_filesize(fd_t) { struct stat txt_stat; off_t file_size_t = 0; fstat(fd_t, &txt_stat); file_size_t = txt_stat.st_size; printf("file_size = %d\n", file_size_t); return file_size_t; } int open_buttons() { int fk_t; fk_t = open("/dev/buttons", O_RDWR); if (fk_t < 0) { printf("can't open /dev/buttons\n"); return -1; } return fk_t; } void close_buttons(int fk_t) { close(fk_t); } int main(void) { int fd; unsigned char *buffer; off_t file_size = 0; int k = 0; unsigned int zf_index; unsigned char buf[16]; fk = open_buttons(); open_lcd(); clear_screen(); s_x = Start_x; s_y = Start_y; //传过去的应该是文件路径 fd = open_txt(); if (fd < 0) { printf("txt can't open!\n"); return (-1); } //计算文件总字节数 file_size = cal_filesize(fd); buffer = (unsigned char *)malloc(file_size); memset(buffer, 0, file_size); read(fd, buffer, file_size); //printf("%s\n", buffer); for (id = 0; id < file_size - 1; id++) { //判断文本中的当前字是不是换行符 if (buffer[id] == '\n') { //换行 Next_line(s_y); //判断一下是不是又刚好是换屏 if (Is_next_screen(s_y)) Next_screen(); continue; } zf_index = buffer[id] * 16; memcpy(buf, (const void *)&Assic[zf_index], 16); /*for (k = 0; k < 16; k++) printf("0x%x\t",buf[k]); printf("\n"); */ Put_text_ch(s_x, s_y, 1, buf); } close_txt(fd); close_lcd(); close_buttons(fk); return 0; }: