目录
1.准备
1.显示字体头文件.h
2.静态库.a
3.显示时间.c
(源码获取请看5.资料)
效果如下
注意:获取时间是当前系统的时间,并不是通过网络获取时间,所以需要自己进行微调
2.显示字体头文件
#ifndef __font_h__
#define __font_h__
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#define color u32
#define getColor(a, b, c, d) (a|b<<8|c<<16|d<<24)
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef char s8;
typedef short s16;
typedef int s32;
typedef long long s64;
typedef struct stbtt_fontinfo
{
void * userdata;
unsigned char * data; // pointer to .ttf file
int fontstart; // offset of start of font
int numGlyphs; // number of glyphs, needed for range checking
int loca,head,glyf,hhea,hmtx,kern; // table locations as offset from start of .ttf
int index_map; // a cmap mapping for our chosen character encoding
int indexToLocFormat; // format needed to map from glyph index to glyph
} stbtt_fontinfo;
typedef struct{
u32 height;
u32 width;
u32 byteperpixel;
u8 *map;
}bitmap;
typedef struct{
stbtt_fontinfo *info;
u8 *buffer;
float scale;
}font;
//lcd设备结构体
struct LcdDevice
{
int fd;
unsigned int *mp; //保存映射首地址
};
//1.初始化字库
font *fontLoad(char *fontPath);
//2.设置字体的大小
void fontSetSize(font *f, s32 pixels);
//3.设置字体输出框的大小
bitmap *createBitmap(u32 width, u32 height, u32 byteperpixel);
//可以指定输出框的颜色
bitmap *createBitmapWithInit(u32 width, u32 height, u32 byteperpixel, color c);
//4.把字体输出到输出框中
void fontPrint(font *f, bitmap *screen, s32 x, s32 y, char *text, color c, s32 maxWidth);
//5.把输出框的所有信息显示到LCD屏幕中
void show_font_to_lcd(unsigned int *p,int px,int py,bitmap *bm);
// 关闭字体库
void fontUnload(font *f);
// 关闭bitmap
void destroyBitmap(bitmap *bm);
#endif
3.显示时间代码
#include "../lib/font.h"
#include <unistd.h>
#include <stdlib.h>
#include <linux/input.h>
#include <time.h>
#include <pthread.h>
#include <string.h>
#define LCD_DEV_PATH "/dev/fb0"
#define FONT_PATH "/usr/share/fonts/DroidSansFallback.ttf"
#define LCD_DEV_TOUCH_PATH "/dev/input/event0"
#define LCD_W 800
#define LCD_H 480
void show_font(struct LcdDevice *lcd, char *buf_time)
{
// 打开字体
font *f = fontLoad(FONT_PATH);
// 字体大小的设置,30表示字体大小,数字越大字体越大
fontSetSize(f, 30);
// 创建一个画板(点阵图)宽是200个像素,高是50个像素,getColor后面三个十进制数表示颜色BGR
bitmap *bm_time = createBitmapWithInit(220, 50, 4, getColor(0, 0, 0, 255));
// 将字体写到点阵图上 0,0表示汉字在画板的左上角显示
fontPrint(f, bm_time, 0, 0, buf_time, getColor(0, 255, 255, 255), 0);
// 把字体框输出到LCD屏幕上 参数0,0表示画板显示的坐标位置
show_font_to_lcd(lcd->mp, 0, 0, bm_time);
// 关闭字体,关闭画板
fontUnload(f);
destroyBitmap(bm_time);
}
// 初始化Lcd
struct LcdDevice *init_lcd(const char *device)
{
// 申请空间
struct LcdDevice *lcd = malloc(sizeof(struct LcdDevice));
if (lcd == NULL)
{
return NULL;
}
// 1打开设备
lcd->fd = open(device, O_RDWR);
if (lcd->fd < 0)
{
perror("open lcd fail");
free(lcd);
return NULL;
}
// 映射
lcd->mp = mmap(NULL, LCD_H * LCD_W * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd->fd, 0);
return lcd;
}
// 获取时间
char *get_time()
{
time_t rawtime;
struct tm *timeinfo;
char *buffer = (char *)malloc(sizeof(char) * 80);
time(&rawtime);
timeinfo = localtime(&rawtime);
// 格式化时间到 buffer 字符串
snprintf(buffer, 80, "%04d-%02d-%02d %02d:%02d:%02d",
timeinfo->tm_year + 1900, // 年份是从1900年开始计算的,所以要加1900
timeinfo->tm_mon + 1, // 月份是从0开始的,所以要加1
timeinfo->tm_mday, // 日期
timeinfo->tm_hour, // 小时
timeinfo->tm_min, // 分钟
timeinfo->tm_sec); // 秒
return buffer;
}
int main()
{
// 初始化Lcd
struct LcdDevice *lcd = init_lcd(LCD_DEV_PATH);
if (lcd == (struct LcdDevice *)NULL)
{
printf("初始化LCD失败!\n");
return -1;
}
while (1)
{
char *buf_time = get_time();
show_font(lcd, buf_time);
}
return 0;
}
4.编译命令
arm-linux-gcc test.c libfont.a -o test -lm
5.资料
链接:https://pan.baidu.com/s/1BXn7Ui1gFfGHdjrOpxiBIg
提取码:b4fz