⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zk_api.c

📁 (绝对原创)Linux写的GB2312字库程序
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <string.h>#include "lcd.h"#include "ap_font.h"//#define ROW_NUM	8//#define COL_NUM 128//static lcd_dev lcd_dev0;//static unsigned char *frame_bufp;//static unsigned char *frame_buf_head;static unsigned char (*frame_buf)[ROW_NUM][COL_NUM];static unsigned char draw_mode;	//chars mode or bitmap modestatic struct lcd_dev *lcd_devp;static struct ap_font_desc *ap_font;int lcd_api_init(const struct ap_font_desc *ap_font_in, unsigned char mode){	int i,j,index;	//printf("lcd_api_int.\n");	frame_buf = (void *)malloc( sizeof(unsigned char) * ROW_NUM * COL_NUM);	if ( !frame_buf){		printf("malloc for lcd_frame_buf fail.\n");		return -1;	}	lcd_devp = malloc( sizeof(struct lcd_dev) );	if ( !lcd_devp ){		printf("malloc for lcd_dev fail.\n");		return -1;	}		/*	for ( i = 0; i < ROW_NUM; i++){		for ( j=0; j<COL_NUM; j++){			(*frame_buf)[i][j] = 0x00;		}	}*/	draw_mode = mode;	ap_font = ap_font_in;	lcd_devp->font_w = ap_font->width;	lcd_devp->font_h = ( (ap_font->high + DATA_BITS - 1) / 8 ) * 8;	return 0;} 	void lcd_api_close(void){	free(frame_buf);	frame_buf = NULL;	free(lcd_devp);	lcd_devp = NULL;		return;}int lcd_clear_scr(int dev){		int i,j;	for ( i = 0; i < ROW_NUM; i++){		for ( j=0; j<COL_NUM; j++){			(*frame_buf)[i][j] = 0x00;		}	}		if ( ioctl(dev, LCD_CLEAR, (unsigned long)0) ){		printf("call ioctl error.\n");		return -1;	}		return 0;}int lcd_full_scr(int dev){	int i,j;	for ( i = 0; i < ROW_NUM; i++){		for ( j=0; j<COL_NUM; j++){			(*frame_buf)[i][j] = 0xff;		}	}		if ( ioctl(dev, LCD_FULL, (unsigned long)0) ){		printf("call ioctl error.\n");		return -1;	}		return 0;}#define get_font_size()	( (unsigned int)(ap_font->width * ap_font->high) / DATA_BITS)static int get_font_data(unsigned char *str, unsigned char *buf){	unsigned char	high_byte, low_byte;	unsigned int 	font_size;	unsigned int 	num_offset;	unsigned long 	data_offset;	unsigned int	i;		high_byte = *str;	low_byte = *(str + 1);	if ( high_byte < 0xa1 || high_byte > 0xf7 || low_byte < 0xa1 || low_byte > 0xfe ){		printf("\n gb2312 code wrong!\n");		printf("high_byte = %d,low_byte=%d.\n",high_byte,low_byte);		return -1;	}	font_size = get_font_size();    	num_offset = ( 94 * (high_byte - 0xa1) + (low_byte - 0xa1) );    	data_offset = (unsigned long)(num_offset * font_size);    	memcpy(buf, (unsigned char *)((unsigned long)(ap_font->data)+data_offset), font_size);	return 0;}const static unsigned char mask_table0[8] = {0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};const static unsigned char mask_table1[8] = {0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01};//extern int testdev;static int wr_ch_to_frame_buf(int dev, unsigned char x0, unsigned char y0,		        unsigned char *str, unsigned char *font_buf){		unsigned int 	font_size;	unsigned int 	num_offset;	unsigned long 	data_offset;	unsigned char   bit_mask;	unsigned int	index;	unsigned char	row,col;	unsigned char	i;	int	row_cnt;	int 	offset;	//int debug;			bit_mask = y0 % DATA_BITS;	row = y0 / DATA_BITS;	get_font_data(str, font_buf);	//lcd_devp->x0 = x0;	//lcd_devp->y0 = row;	//lcd_devp->font_w = ap_font->width;	//lcd_devp->font_h = ap_font->high + DATA_BITS;	//debug = 1;	index = 0;			for ( i = 0, col = x0; i < ap_font->width; i++, col++){		/*if (debug){			printf("old: frame_buf[%d][%d] = %d.\n", row, col, (*frame_buf)[row][col] );		}*/		lcd_devp->buf[index++] = (*frame_buf)[row][col] = 				( (*frame_buf)[row][col] & mask_table0[bit_mask-1] )					| (font_buf[i] >> bit_mask);		/*if (debug){			printf("new: frame_buf[%d][%d] = %d.\n", row, col, (*frame_buf)[row][col] );			debug = 0;		}*/	}		row_cnt = ap_font->high / DATA_BITS;	offset = 0;	row++;	//debug = 1;	while ( row_cnt-- > 1 ){		for ( i = 0, col = x0; i < ap_font->width; i++, col++){			/*if (debug){				printf("old: frame_buf[%d][%d] = %d.\n", row, col, (*frame_buf)[row][col] );			}*/			lcd_devp->buf[index++] = (*frame_buf)[row][col] = 					( font_buf[offset+i] << (DATA_BITS - bit_mask) )					| ( font_buf[ap_font->width+offset+i] >> bit_mask );			/*if (debug){				printf("new: frame_buf[%d][%d] = %d.\n", row, col, (*frame_buf)[row][col] );				debug = 0;			}*/		}			row++;		offset += ap_font->width;	}	//debug = 1;	for ( i = 0, col = x0; i < ap_font->width; i++, col++){		/*if (debug){			printf("old: frame_buf[%d][%d] = %d.\n", row, col, (*frame_buf)[row][col] );		}*/		lcd_devp->buf[index++] = (*frame_buf)[row][col] = 				( (*frame_buf)[row][col] & mask_table1[bit_mask] )					| (font_buf[offset+i] << (DATA_BITS - bit_mask) );			/*if (debug){			printf("new: frame_buf[%d][%d] = %d.\n", row, col, (*frame_buf)[row][col] );			debug = 0;		}*/	}		lcd_devp->x0 = x0;	lcd_devp->y0 = y0 / DATA_BITS;	lcd_devp->font_w = ap_font->width;	lcd_devp->font_h = ap_font->high + DATA_BITS;	lcd_devp->len = 1;		if ( ioctl(dev, LCD_DRAW_CHARS, (unsigned long)lcd_devp) ){	        	printf("call ioctl error.\n");	}                                     }int lcd_write_chars(int dev, unsigned char y0, unsigned char x0,			unsigned char *str, unsigned char len){		int i, ret, len_tmp;	unsigned char 	*font_buf;        unsigned int	font_size;	unsigned char	x,y;	font_size = get_font_size();	font_buf = (unsigned char *)malloc( sizeof(unsigned char) * font_size );	if ( !font_buf ){		printf("malloc for font_buf fail.\n");		}	len_tmp = len;	x = x0;	y = y0;	if ( y > (ROW_NUM * DATA_BITS) ){		printf("row address over.\n");		return -1;	}	for (i = 0; i < len_tmp; i++){		if ( (x + ap_font->width) > COL_NUM ){			y  += ap_font->high; // if y > max_al			x = 0;		}		ret = wr_ch_to_frame_buf(dev, x, y, str, font_buf);			if ( y >= (ROW_NUM * DATA_BITS) ){			return -1;		}		str += 2;		x += ap_font->width;	}		free(font_buf);	//printf("update_frame_buf function return.\n");	//print("x1 = %d, y1 = %d.\n",*x1,*y1);	return 0;}/*int lcd_write_chars(int testdev, unsigned char y0, unsigned char x0,		    unsigned char *str, unsigned char len){	unsigned char row;	signed char ret;	unsigned int buf_index;	unsigned int i,j;	unsigned char x1,y1;	unsigned int font_size;	row = y0 / DATA_BITS;	//printf("lcd_write_chars.\n");		font_size = get_font_size();	printf("\nfont_size = %d.\n",font_size);		buf_index = 0;	for ( i = 0; i < len; i++){		ret = get_font_data(str, (unsigned char *)( (unsigned long)(lcd_devp->buf)+buf_index) );		if ( ret ){			break;		}				if ( i > 3){			printf("len = %d.\n",i);			for (j=0; j < font_size; j++){				printf("data[%d]=0x%x,", j, *(unsigned char *)((unsigned long)(lcd_devp->buf)+buf_index+j));				if ( j && !(j % 8) ){					printf("\n");				}			}		}		printf("\n");		str += 2;		buf_index += font_size;			}	printf("last len = %d.\n",i);	lcd_devp->x0 = x0;	lcd_devp->y0 = y0;	lcd_devp->len = i;				if ( ioctl(testdev, LCD_DRAW_CHARS, (unsigned long)lcd_devp) ){	               	printf("call ioctl error.\n");	}	                                              	return 0;}*/                                                                                                                                               

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -