文件结构
Makefile:
1 CROSSCOMPILE := arm-linux-
2 CFLAGS := -Wall -O2 -c
3 LDFLAGS := -lm -lfreetype
4 CC := $(CROSSCOMPILE)gcc
5 LD := $(CROSSCOMPILE)ld
6
7 OBJS := main.o
8 font/font_manager.o
9 font/font_gbk.o
10 font/font_asc.o
11 display/display_manager.o
12 display/lcd_fb.o
13 draw/draw.o
14
15 all: $(OBJS)
16 $(CC) $(LDFLAGS) -o txt $^
17
18 clean:
19 rm -f txt
20 rm -f $(OBJS)
21
22 %.o:%.c
23 $(CC) $(CFLAGS) -o $@ $<
main.c
1 #include
2 #include "font/font_manager.h"
3 #include "display/display_manager.h"
4 #include "draw/draw.h"
5
6 int main(int argc,char **argv)
7 {
8 //命令操作
9 char cmd;
10 //第一个参数 要显示的TXT 文件
11 char *file = argv[1];
12
13 if(-1 == font_init())
14 {
15 printf("font manager init error rn");
16 return -1;
17 }
18 printf("font init ok rn");
19
20 if(-1 == display_init())
21 {
22 printf("display manager init error rn");
23 return -1;
24 }
25 printf("display init ok rn");
26
27 if(-1 == open_txt(file))
28 {
29 printf("cat't open %s txt rn", file);
30 return -1;
31 }
32 printf("read file : %s ok rn", file);
33 show_page();
34
35 while(1)
36 {
37 cmd = getchar();
38 if('n' == cmd)
39 {
40 show_page_next();
41 }
42 if('p' == cmd)
43 {
44 show_page_pre();
45 }
46 if('q' == cmd)
47 {
48 return 0;
49 }
50 }
51 return 0;
52 }
HZK16 这个就不发了,网上自己找吧。
display/display_manager.h
1 #ifndef __DISPLAY_MANAGER__
2 #define __DISPLAY_MANAGER__
3
4 #include
5 #include
6
7 typedef struct display{
8 char *name;
9 int width;
10 int height;
11 int (*display_init)(void);
12 int (*display_px)(int color, int x, int y);
13 struct display *next;
14 } T_display, *PT_display;
15
16 int display_init(void);
17 int display_register(PT_display new_display);
18 PT_display display_select(char *name);
19
20 int init_lcd_fb();
21
22 #endif
display/display_manager.c
1 #include "display_manager.h"
2
3 static PT_display head_pt_display;
4
5 PT_display display_select(char *name)
6 {
7 PT_display temp_pt_display = head_pt_display;
8 while(temp_pt_display)
9 {
10 if(0 == strcmp(name, temp_pt_display->name))
11 {
12 temp_pt_display->display_init();
13 return temp_pt_display;
14 }
15 else
16 {
17 temp_pt_display = temp_pt_display->next;
18 }
19 }
20 return NULL;
21 }
22
23 int display_init(void)
24 {
25 if(-1 == init_lcd_fb())
26 {
27 printf("lcd fb init error n");
28 return -1;
29 }
30 return 0;
31 }
32
33 int display_register(PT_display new_display)
34 {
35 PT_display temp_pt_display = head_pt_display;
36 new_display->next = NULL;
37 if(! head_pt_display)
38 {
39 head_pt_display = new_display;
40 }
41 else
42 {
43 while(temp_pt_display->next)
44 {
45 temp_pt_display = temp_pt_display->next;
46 }
47 temp_pt_display = new_display;
48 }
49 return 0;
50 }
display/lcd_fb.c
1 #include "display_manager.h"
2
3 #include
4 #include
5 #include
6
7 #include
8
9 #include
10
11 #include
12
13 #include
14
15 #include
16
17 static int lcd_fb_display_init(void);
18 static int lcd_fb_display_px(int color, int x, int y);
19
20 static int fb_fd;
21 static unsigned char *fb_mem;
22 static int px_width;
23 static int line_width;
24 static int screen_width;
25 static struct fb_var_screeninfo var;
26
27 static T_display lcd_disp = {
28 .name = "lcd_fb",
29 .display_init = lcd_fb_display_init,
30 .display_px = lcd_fb_display_px,
31 };
32
33 int init_lcd_fb()
34 {
35 return display_register(&lcd_disp);
36 }
37
38 static int lcd_fb_display_init(void)
39 {
40 //如果使用 mmap 打开方式 必须是 读定方式
41 fb_fd = open("/dev/fb0", O_RDWR);
42 if(-1 == fb_fd)
43 {
44 printf("cat't open /dev/fb0 n");
45 return -1;
46 }
47 //获取屏幕参数
48 if(-1 == ioctl(fb_fd, FBIOGET_VSCREENINFO, &var))
49 {
50 close(fb_fd);
51 printf("cat't ioctl /dev/fb0 n");
52 return -1;
53 }
54 //计算参数
55 px_width = var.bits_per_pixel / 8;
56 line_width = var.xres * px_width;
57 screen_width = var.yres * line_width;
58
59 fb_mem = (unsigned char *)mmap(NULL, screen_width, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0);
60 if(fb_mem == (void *)-1)
61 {
62 close(fb_fd);
63 printf("cat't mmap /dev/fb0 n");
64 return -1;
65 }
66 //清屏
67 memset(fb_mem, 0 , screen_width);
68
69 //设置参数
70 lcd_disp.width = var.xres;
71 lcd_disp.height = var.yres;
72
73 return 0;
74 }
75
76 static int lcd_fb_display_px(int color, int x, int y)
77 {
78 unsigned char *pen8;
79 unsigned short *pen16;
80
81 unsigned char r,g,b;
82
83 pen8 = (unsigned char *)(fb_mem + y*line_width + x*px_width);
84 pen16 = (unsigned short *)pen8;
85
86 //合并为 565 格式 16bbp
87 r = (color>>16) & 0xff;
88 g = (color>>8) & 0xff;
89 b = (color>>0) & 0xff;
90 *pen16 = (r>>3)<<11 | (g>>2)<<5 | (b>>3);
91
92 return 0;
93 }
draw/draw.h
1 #include
2 #include
3 #include
4 #include
5 #include
6
7 #include
8
9 #include "../display/display_manager.h"
10 #include "../font/font_manager.h"
11
12 //定义一个用来实现 上下 翻页的链表
13 typedef struct t_draw_page{
14 int p;
15 int pos; //相对于 基地址的 位置
16 struct t_draw_page *pre;
17 struct t_draw_page *next;
18 } T_draw_page, *PT_draw_page;
19
20 int open_txt(char *file);
21 int show_page_pre();
22 int show_page_next();
23 int show_page();
24 int draw_page(unsigned char **pos);
25
26 int draw_font(int color, PT_fontMap pt_font);