TFT_eSPI库的字符显示分析
在User_Setup.h文件内,定义要使用的系统自带字体,不用的直接省略掉
#define LOAD_GLCD // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
#define LOAD_FONT2 // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
#define LOAD_FONT4 // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
//#define LOAD_FONT6 // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm
#define LOAD_FONT7 // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:-.
//#define LOAD_FONT8 // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.
//#define LOAD_FONT8N // Font 8. Alternative to Font 8 above, slightly narrower, so 3 digits fit a 160 pixel TFT
//#define LOAD_GFXFF // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts
以7段数字显示为例,看TFT_eSPI.h文件的定义
// Create a null set in case some fonts not used (to prevent crash)
const uint8_t widtbl_null[1] = {0};
PROGMEM const uint8_t chr_null[1] = {0};
PROGMEM const uint8_t* const chrtbl_null[1] = {chr_null};
typedef struct {
const uint8_t *chartbl;
const uint8_t *widthtbl;
uint8_t height;
uint8_t baseline;
} fontinfo;
// Now fill the structure
const PROGMEM fontinfo fontdata [] = {
...
#ifdef LOAD_FONT7 //通过预定义的字体号直接定义字体
{ (const uint8_t *)chrtbl_f7s, widtbl_f7s, chr_hgt_f7s, baseline_f7s},
#else
{ (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
#endif
...
}
再看Font7srle.h文件
#include
#define nr_chrs_f7s 96 //字符总数,ASCII内可显示96个字符
#define chr_hgt_f7s 48 //字符高度
#define baseline_f7s 47 //基准线?目前还没搞懂什么意思
#define data_size_f7s 8 //一个字节
#define firstchr_f7s 32 //各个英文font都是从32(空格)开始
extern const unsigned char widtbl_f7s[96]; //font内96个字符各自的宽度数组
extern const unsigned char* const chrtbl_f7s[96]; //96个字符索引
再看Font7srle.c文件
PROGMEM const unsigned char widtbl_f7s[96] = // character width table
{
12, 12, 12, 12, 12, 12, 12, 12, // char 32 - 39
12, 12, 12, 12, 12, 32, 12, 12, // char 40 - 47
32, 32, 32, 32, 32, 32, 32, 32, // char 48 - 55 48-57是10个数字,都是32位的像素宽度
32, 32, 12, 12, 12, 12, 12, 12, // char 56 - 63
12, 12, 12, 12, 12, 12, 12, 12, // char 64 - 71
12, 12, 12, 12, 12, 12, 12, 12, // char 72 - 79
12, 12, 12, 12, 12, 12, 12, 12, // char 80 - 87
12, 12, 12, 12, 12, 12, 12, 12, // char 88 - 95
12, 12, 12, 12, 12, 12, 12, 12, // char 96 - 103
12, 12, 12, 12, 12, 12, 12, 12, // char 104 - 111
12, 12, 12, 12, 12, 12, 12, 12, // char 112 - 119
12, 12, 12, 12, 12, 12, 12, 12 // char 120 - 127
};
PROGMEM const unsigned char* const chrtbl_f7s[96] = // 96个字符按顺序对应的点阵数组名称
{
chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20,
chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_2D, chr_f7s_2E, chr_f7s_20,
chr_f7s_30, chr_f7s_31, chr_f7s_32, chr_f7s_33, chr_f7s_34, chr_f7s_35, chr_f7s_36, chr_f7s_37,
chr_f7s_38, chr_f7s_39, chr_f7s_3A, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20,
chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20,
chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20,
chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20,
chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20,
chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20,
chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20,
chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20,
chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20, chr_f7s_20
};
显示汉字
取模略,使用的软件是PCtoLCD2002
建自己的汉字库
HanZi16.h,16X16大小,用的宋体,相当于12号字
#include
PROGMEM const unsigned char hz16_20[] =
{
0x04, 0x40, 0x0E, 0x50, 0x78, 0x48, 0x08, 0x48, 0x08, 0x40, 0xFF, 0xFE, 0x08, 0x40, 0x08, 0x44,
0x0A, 0x44, 0x0C, 0x48, 0x18, 0x30, 0x68, 0x22, 0x08, 0x52, 0x08, 0x8A, 0x2B, 0x06, 0x10, 0x02
};
PROGMEM const unsigned char hz16_21[] =
{
0x00, 0x08, 0x01, 0xFC, 0x7E, 0x10, 0x22, 0x10, 0x11, 0x20, 0x7F, 0xFE, 0x42, 0x02, 0x82, 0x04,
0x7F, 0xF8, 0x04, 0x00, 0x07, 0xF0, 0x0A, 0x10, 0x11, 0x20, 0x20, 0xC0, 0x43, 0x30, 0x1C, 0x0E
};
PROGMEM const unsigned char hz16_22[] =
{
0x08, 0x80, 0x08, 0x80, 0x08, 0x80, 0x11, 0xFE, 0x11, 0x02, 0x32, 0x04, 0x34, 0x20, 0x50, 0x20,
0x91, 0x28, 0x11, 0x24, 0x12, 0x24, 0x12, 0x22, 0x14, 0x22, 0x10, 0x20, 0x10, 0xA0, 0x10, 0x40
};
PROGMEM const unsigned char hz16_23[] =
{
0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x3F, 0xF8, 0x21, 0x08, 0x21, 0x08, 0x21, 0x08,
0x21, 0x08, 0x21, 0x08, 0x3F, 0xF8, 0x21, 0x08, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00
};
PROGMEM const unsigned char hz16_24[] =
{
0x00, 0x00, 0x7F, 0xFC, 0x40, 0x04, 0x40, 0x04, 0x5F, 0xF4, 0x41, 0x04, 0x41, 0x04, 0x4F, 0xE4,
0x41, 0x04, 0x41, 0x44, 0x41, 0x24, 0x5F, 0xF4, 0x40, 0x04, 0x40, 0x04, 0x7F, 0xFC, 0x40, 0x04
};
struct FNT_HZ16 // 汉字字模数据结构
{
char Index[4]; // 汉字内码索引,存放内码,如"中",在UTF-8编码下,每个汉字占3个字节,第四个是结束符0
const unsigned char* hz16_Id; // 点阵码数据 存放内码后对应的 点阵序列 每个字需要32个字节的点阵序列
unsigned char hz_width;
};
PROGMEM const FNT_HZ16 hanzi16[] =
{
{"我", hz16_20,16}, {"爱", hz16_21,16}, {"你", hz16_22,16}, {"中", hz16_23,16}, {"国", hz16_24,16}
};
主程序
#include // ST7735 or ST7735S驱动
#include
#include "HanZi16.h"
TFT_eSPI tft = TFT_eSPI();
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
tft.init();
tft.setRotation(0);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_YELLOW, TFT_BLACK); // 新字体不画背景色 Note: the new fonts do not draw the background colour
drawHanzi(32, 8, "我", TFT_YELLOW);
drawHanzi(32, 28, "爱", TFT_YELLOW);
drawHanzi(32, 48, "你", TFT_YELLOW);
drawHanzi(32, 68, "中", TFT_YELLOW);
drawHanzi(32, 88, "国", TFT_YELLOW);
char str[] = "中国我爱你";
drawHanziS(52, 8, str, TFT_YELLOW);
}
void loop() {
// put your main code here, to run repeatedly:
}
void drawHanzi(int32_t x, int32_t y, const char c[3], uint32_t color) { //显示单一汉字
for (int k = 0; k < 5; k++)
if (hanzi16[k].Index[0] == c[0] && hanzi16[k].Index[1] == c[1] && hanzi16[k].Index[2] == c[2])
{ tft.drawBitmap(x, y, hanzi16[k].hz16_Id, hanzi16[k].hz_width, 16, color);
}
}
void drawHanziS(int32_t x, int32_t y, const char str[], uint32_t color) { //显示整句汉字,字库目前有限,比较简单,没有换行功能,上下输出,左右输出是在函数内实现
int y0 = y;
for (int i = 0; i < strlen(str); i += 3) {
drawHanzi(x, y0, str+i, color);
y0 += 20;
}
}
汉字显示效果.jpg