From 3dd423da77b431dfb153359e85f302f2892992cb Mon Sep 17 00:00:00 2001 From: jixishi Date: Fri, 5 Jul 2024 23:09:54 +0800 Subject: [PATCH] =?UTF-8?q?UP=20=E6=94=AF=E6=8C=81=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/oled/test.c | 5 +- lib/inc/font/font.h | 4 +- lib/inc/font/lvgl.h | 2 +- lib/inc/oled/oled.h | 1 + lib/inc/oled/oled_font_chuc.h | 152 +++++++++++++++++++++++++++------- lib/src/font/font.cpp | 37 +++------ lib/src/oled/oled.cpp | 35 +++++++- tools/unicode_ch_gen.py | 16 ++-- 8 files changed, 180 insertions(+), 72 deletions(-) diff --git a/demo/oled/test.c b/demo/oled/test.c index 501cfca..018ca13 100644 --- a/demo/oled/test.c +++ b/demo/oled/test.c @@ -121,11 +121,12 @@ void Test_OLED() { uint8_t buft[13] = {0x6C, 0x14, 0x6C, 0xE1, 0x5F, 0x0F, 0x6D, 0xB2, 0x4F, 0x4D, 0x8B, 0xA1, 0x00}; - OLED_ShowCHString(&oled, 16, 1, buft); +// OLED_ShowCHString(&oled, 16, 1, buft); uint8_t sbuf[30] = {0}; uint8_t bufx[8] = {0x5F, 0x53, 0x52, 0x4D, 0x6D, 0xB2, 0x4F, 0x4D}; sprintf(sbuf, "%s: 13.4271m\0", bufx); - OLED_ShowCHString(&oled, 1, 24, sbuf); +// OLED_ShowCHString(&oled, 1, 24, sbuf); + OLED_ShowCHString(&oled, 1, 24, "姓名学号班级"); OLED_DrawRect(&oled, 0, 0, 127, 63); // };// diff --git a/lib/inc/font/font.h b/lib/inc/font/font.h index dd6fe03..5d5ebc1 100644 --- a/lib/inc/font/font.h +++ b/lib/inc/font/font.h @@ -33,9 +33,9 @@ struct Font { #endif }; -char Font_utf8_to_unicode(uint8_t *pInput, uint32_t *unicode_letter); +uint8_t Font_utf8_to_unicode(uint8_t *pInput, uint32_t *unicode_letter); -char Font_draw_letter(const lv_font_t *font, Font_f_t *fd, uint32_t letter, int16_t pos_x, int16_t pos_y); +uint8_t Font_draw_letter(const lv_font_t *font, Font_f_t *fd, uint32_t letter, int16_t pos_x, int16_t pos_y); #ifdef __cplusplus } diff --git a/lib/inc/font/lvgl.h b/lib/inc/font/lvgl.h index 848b991..5c4fc5c 100644 --- a/lib/inc/font/lvgl.h +++ b/lib/inc/font/lvgl.h @@ -175,7 +175,7 @@ typedef struct _lv_font_struct { } lv_font_t; -#define NULL 0 /* see */ +//#define NULL 0 /* see */ /** Bitmap formats*/ typedef enum { diff --git a/lib/inc/oled/oled.h b/lib/inc/oled/oled.h index 46a6e9a..ef4e3c8 100644 --- a/lib/inc/oled/oled.h +++ b/lib/inc/oled/oled.h @@ -10,6 +10,7 @@ extern "C" { #define REFRESH_CALL_ENABLE 1 //使用DMA或者整体刷新函数 #define HZK_FONT +#define UTF8_TO_UNICODE //#define LVGL_FONT /** diff --git a/lib/inc/oled/oled_font_chuc.h b/lib/inc/oled/oled_font_chuc.h index 013631d..3e407f2 100644 --- a/lib/inc/oled/oled_font_chuc.h +++ b/lib/inc/oled/oled_font_chuc.h @@ -12,70 +12,158 @@ uint8_t Hzk_size=16; Chinese_t Hzk[] = { { - // Original: 气 - {0x6C, 0x14}, + // Original: 姓 + { 0x59, 0xD3 }, { - 0x10, 0x00, 0x10, 0x08, 0x1F, 0xFC, 0x20, 0x00, 0x2F, 0xF0, 0x40, 0x00, 0xBF, 0xE0, 0x00, 0x20, - 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x22, 0x00, 0x12, 0x00, 0x0A, 0x00, 0x04, + 0x10, 0x20, 0x10, 0x20, 0x11, 0x20, 0x11, 0x24, 0xFD, 0xFE, 0x25, 0x20, 0x26, 0x20, 0x24, 0x20, + 0x24, 0x28, 0x45, 0xFC, 0x28, 0x20, 0x10, 0x20, 0x28, 0x20, 0x44, 0x24, 0x87, 0xFE, 0x00, 0x00, } }, { - // Original: 泡 - {0x6C, 0xE1}, + // Original: 名 + { 0x54, 0x0D }, { - 0x01, 0x00, 0x41, 0x04, 0x31, 0xFE, 0x12, 0x04, 0x82, 0x24, 0x67, 0xF4, 0x2A, 0x24, 0x02, 0x24, - 0x0A, 0x24, 0x13, 0xE4, 0xE2, 0x14, 0x22, 0x08, 0x22, 0x02, 0x22, 0x02, 0x21, 0xFE, 0x20, 0x00, + 0x02, 0x00, 0x02, 0x00, 0x07, 0xF0, 0x04, 0x10, 0x0C, 0x20, 0x12, 0x40, 0x01, 0x80, 0x06, 0x00, + 0x18, 0x08, 0xEF, 0xFC, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0F, 0xF8, 0x08, 0x08, } }, { - // Original: 式 - {0x5F, 0x0F}, + // Original: 学 + { 0x5B, 0x66 }, { - 0x00, 0x80, 0x00, 0xA0, 0x00, 0x90, 0x00, 0x84, 0xFF, 0xFE, 0x00, 0x80, 0x00, 0x80, 0x3E, 0x80, - 0x08, 0x80, 0x08, 0x40, 0x08, 0x40, 0x09, 0x20, 0x0E, 0x22, 0x70, 0x12, 0x20, 0x0A, 0x00, 0x04, + 0x22, 0x08, 0x11, 0x08, 0x11, 0x10, 0x00, 0x20, 0x7F, 0xFE, 0x40, 0x02, 0x80, 0x04, 0x1F, 0xE0, + 0x00, 0x40, 0x01, 0x84, 0xFF, 0xFE, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x05, 0x00, 0x02, 0x00, } }, { - // Original: 液 - {0x6D, 0xB2}, + // Original: 号 + { 0x53, 0xF7 }, { - 0x00, 0x80, 0x40, 0x44, 0x37, 0xFE, 0x10, 0x20, 0x81, 0x20, 0x61, 0x3C, 0x22, 0x44, 0x0A, 0x64, - 0x16, 0x98, 0x2B, 0x48, 0xE2, 0x50, 0x22, 0x20, 0x22, 0x50, 0x22, 0x8E, 0x23, 0x04, 0x22, 0x00, + 0x00, 0x10, 0x1F, 0xF8, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1F, 0xF0, 0x00, 0x04, 0xFF, 0xFE, + 0x04, 0x00, 0x08, 0x10, 0x1F, 0xF8, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0xA0, 0x00, 0x40, } }, { - // Original: 位 - {0x4F, 0x4D}, + // Original: 班 + { 0x73, 0xED }, { - 0x08, 0x80, 0x08, 0x40, 0x08, 0x40, 0x10, 0x08, 0x17, 0xFC, 0x30, 0x00, 0x52, 0x08, 0x92, 0x08, - 0x11, 0x10, 0x11, 0x10, 0x10, 0x90, 0x10, 0xA0, 0x10, 0x24, 0x1F, 0xFE, 0x10, 0x00, 0x10, 0x00, + 0x01, 0x00, 0x09, 0x04, 0xFD, 0x7E, 0x21, 0x10, 0x25, 0x10, 0x25, 0x10, 0x25, 0x10, 0xF5, 0x7C, + 0x25, 0x10, 0x29, 0x10, 0x21, 0x10, 0x22, 0x10, 0x3A, 0x10, 0xE2, 0x14, 0x44, 0xFE, 0x08, 0x00, } }, { - // Original: 计 - {0x8B, 0xA1}, + // Original: 级 + { 0x7E, 0xA7 }, { - 0x00, 0x40, 0x20, 0x40, 0x10, 0x40, 0x10, 0x40, 0x00, 0x40, 0x00, 0x44, 0xF7, 0xFE, 0x10, 0x40, - 0x10, 0x40, 0x10, 0x40, 0x10, 0x40, 0x12, 0x40, 0x14, 0x40, 0x18, 0x40, 0x10, 0x40, 0x00, 0x40, + 0x10, 0x08, 0x17, 0xFC, 0x21, 0x08, 0x21, 0x08, 0x49, 0x10, 0xF9, 0x10, 0x11, 0x3C, 0x21, 0x84, + 0x41, 0x88, 0xF9, 0x48, 0x02, 0x50, 0x02, 0x20, 0x1A, 0x50, 0xE4, 0x88, 0x49, 0x0E, 0x02, 0x04, } }, { - // Original: 当 - {0x5F, 0x53}, + // Original: 信 + { 0x4F, 0xE1 }, { - 0x01, 0x00, 0x21, 0x08, 0x19, 0x18, 0x09, 0x20, 0x01, 0x00, 0x01, 0x08, 0x7F, 0xFC, 0x00, 0x08, - 0x00, 0x08, 0x00, 0x08, 0x3F, 0xF8, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x7F, 0xF8, 0x00, 0x08, + 0x08, 0x80, 0x08, 0x44, 0x0F, 0xFE, 0x10, 0x00, 0x10, 0x08, 0x37, 0xFC, 0x50, 0x00, 0x90, 0x08, + 0x17, 0xFC, 0x10, 0x00, 0x13, 0xF8, 0x12, 0x08, 0x12, 0x08, 0x12, 0x08, 0x13, 0xF8, 0x12, 0x08, } }, { - // Original: 前 - {0x52, 0x4D}, + // Original: 息 + { 0x60, 0x6F }, { - 0x10, 0x10, 0x0C, 0x30, 0x04, 0x44, 0xFF, 0xFE, 0x00, 0x00, 0x3E, 0x08, 0x22, 0x48, 0x22, 0x48, - 0x3E, 0x48, 0x22, 0x48, 0x22, 0x48, 0x3E, 0x48, 0x22, 0x08, 0x22, 0x08, 0x2A, 0x28, 0x24, 0x10, + 0x02, 0x00, 0x04, 0x10, 0x1F, 0xF8, 0x10, 0x10, 0x10, 0x10, 0x1F, 0xF0, 0x10, 0x10, 0x1F, 0xF0, + 0x10, 0x10, 0x10, 0x10, 0x1F, 0xF0, 0x02, 0x00, 0x51, 0x84, 0x50, 0x92, 0x90, 0x12, 0x0F, 0xF0, + } + }, + { + // Original: 显 + { 0x66, 0x3E }, + { + 0x00, 0x10, 0x1F, 0xF8, 0x10, 0x10, 0x10, 0x10, 0x1F, 0xF0, 0x10, 0x10, 0x10, 0x10, 0x1F, 0xF0, + 0x14, 0x50, 0x44, 0x44, 0x34, 0x4C, 0x14, 0x50, 0x04, 0x40, 0x04, 0x44, 0xFF, 0xFE, 0x00, 0x00, + } + }, + { + // Original: 示 + { 0x79, 0x3A }, + { + 0x00, 0x10, 0x3F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xFF, 0xFE, 0x01, 0x00, + 0x01, 0x00, 0x09, 0x20, 0x19, 0x18, 0x21, 0x0C, 0x41, 0x04, 0x01, 0x00, 0x05, 0x00, 0x02, 0x00, + } + }, + { + // Original: 图 + { 0x56, 0xFE }, + { + 0x00, 0x04, 0x7F, 0xFE, 0x44, 0x04, 0x47, 0xE4, 0x4C, 0x44, 0x52, 0x84, 0x41, 0x04, 0x42, 0x84, + 0x46, 0x44, 0x49, 0x3C, 0x70, 0x94, 0x46, 0x04, 0x41, 0x04, 0x40, 0x84, 0x7F, 0xFC, 0x40, 0x04, + } + }, + { + // Original: 片 + { 0x72, 0x47 }, + { + 0x00, 0x80, 0x20, 0x80, 0x20, 0x80, 0x20, 0x80, 0x20, 0x84, 0x3F, 0xFE, 0x20, 0x00, 0x20, 0x00, + 0x3F, 0xC0, 0x20, 0x40, 0x20, 0x40, 0x20, 0x40, 0x20, 0x40, 0x20, 0x40, 0x40, 0x40, 0x80, 0x40, + } + }, + { + // Original: 视 + { 0x89, 0xC6 }, + { + 0x20, 0x08, 0x13, 0xFC, 0x12, 0x08, 0x02, 0x48, 0xFE, 0x48, 0x0A, 0x48, 0x12, 0x48, 0x32, 0x48, + 0x5A, 0x48, 0x96, 0x68, 0x12, 0xA8, 0x10, 0xA0, 0x11, 0x22, 0x11, 0x22, 0x12, 0x1E, 0x14, 0x00, + } + }, + { + // Original: 频 + { 0x98, 0x91 }, + { + 0x10, 0x04, 0x13, 0xFE, 0x54, 0x20, 0x5E, 0x44, 0x51, 0xFE, 0x55, 0x04, 0xFF, 0x24, 0x11, 0x24, + 0x55, 0x24, 0x55, 0x24, 0x55, 0x24, 0x95, 0x24, 0x08, 0x20, 0x10, 0x50, 0x20, 0x8C, 0x43, 0x04, + } + }, + { + // Original: 专 + { 0x4E, 0x13 }, + { + 0x01, 0x00, 0x01, 0x00, 0x01, 0x10, 0x3F, 0xF8, 0x02, 0x00, 0x02, 0x04, 0xFF, 0xFE, 0x04, 0x00, + 0x04, 0x00, 0x0F, 0xF8, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x06, 0x80, 0x01, 0x80, 0x00, 0x40, + } + }, + { + // Original: 业 + { 0x4E, 0x1A }, + { + 0x04, 0x40, 0x04, 0x40, 0x04, 0x40, 0x04, 0x40, 0x44, 0x44, 0x24, 0x44, 0x24, 0x48, 0x14, 0x48, + 0x14, 0x50, 0x14, 0x50, 0x14, 0x60, 0x04, 0x40, 0x04, 0x40, 0x04, 0x44, 0xFF, 0xFE, 0x00, 0x00, + } + }, + { + // Original: 电 + { 0x75, 0x35 }, + { + 0x02, 0x00, 0x02, 0x00, 0x02, 0x10, 0x7F, 0xF8, 0x42, 0x10, 0x42, 0x10, 0x7F, 0xF0, 0x42, 0x10, + 0x42, 0x10, 0x7F, 0xF0, 0x42, 0x10, 0x02, 0x00, 0x02, 0x04, 0x02, 0x04, 0x01, 0xFC, 0x00, 0x00, + } + }, + { + // Original: 子 + { 0x5B, 0x50 }, + { + 0x00, 0x00, 0x3F, 0xF0, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x01, 0x80, 0x01, 0x04, 0xFF, 0xFE, + 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x05, 0x00, 0x02, 0x00, } }, }; +Chinese_t* find_chinese_data(uint8_t unicode_high, uint8_t unicode_low) { + for (int i = 0; i < sizeof(Hzk) / sizeof(Chinese_t); ++i) { + if (Hzk[i].unicode[0] == unicode_high && Hzk[i].unicode[1] == unicode_low) { + return &Hzk[i]; + } + } + return NULL; +} #endif //HW_LIB_OLED_FONT_CHUC_H \ No newline at end of file diff --git a/lib/src/font/font.cpp b/lib/src/font/font.cpp index 34c4a4d..c71be5e 100644 --- a/lib/src/font/font.cpp +++ b/lib/src/font/font.cpp @@ -2,59 +2,44 @@ #include "lvgl.h" #include "font.h" -char Font_utf8_to_unicode(uint8_t *pInput, uint32_t *unicode_letter) { - char outputSize = 0; - char ppOutput[8] = {0}; - - char *tmp = ppOutput; //临时变量,用于遍历输出字符串 - +uint8_t Font_utf8_to_unicode(uint8_t *pInput, uint32_t *unicode_letter) { + uint8_t outputSize = 0; + uint8_t * tmp= ( uint8_t *) unicode_letter; if (*pInput > 0x00 && *pInput <= 0x7F) //处理单字节UTF8字符(英文字母、数字) { - *tmp = *pInput; tmp++; *tmp = 0; //小端法表示,在高地址填补0 outputSize = 1; } else if (((*pInput) & 0xE0) == 0xC0) //处理双字节UTF8字符 { - - char high = *pInput; - + uint8_t high = *pInput; pInput++; - - char low = *pInput; + uint8_t low = *pInput; if ((low & 0xC0) != 0x80) //检查是否为合法的UTF8字符表示 return 0; //如果不是则报错 - *tmp = (high << 6) + (low & 0x3F); tmp++; *tmp = (high >> 2) & 0x07; outputSize = 2; } else if (((*pInput) & 0xF0) == 0xE0) //处理三字节UTF8字符 { - - char high = *pInput; + uint8_t high = *pInput; pInput++; - char middle = *pInput; + uint8_t middle = *pInput; pInput++; - char low = *pInput; - + uint8_t low = *pInput; if (((middle & 0xC0) != 0x80) || ((low & 0xC0) != 0x80)) return 0; - - *tmp = (middle << 6) + (low & 0x7F); + *tmp = (middle << 6) | (low & 0x3F); tmp++; - *tmp = (high << 4) + ((middle >> 2) & 0x0F); + *tmp = (high << 4) | ((middle >> 2) & 0x0F); outputSize = 3; } else //对于其他字节数的UTF8字符不进行处理 { return 0; } - unicode_letter[0] = ppOutput[1]; - unicode_letter[0] <<= 8; - unicode_letter[0] |= ppOutput[0]; - return outputSize; } @@ -176,7 +161,7 @@ void draw_letter_normal(Font_f_t *fd, uint16_t pos_x, uint16_t pos_y, lv_font_gl #endif } -char Font_draw_letter(const lv_font_t *font, Font_f_t *fd, uint32_t letter, int16_t pos_x, int16_t pos_y) { +uint8_t Font_draw_letter(const lv_font_t *font, Font_f_t *fd, uint32_t letter, int16_t pos_x, int16_t pos_y) { const uint8_t *map_p; lv_font_glyph_dsc_t g; diff --git a/lib/src/oled/oled.cpp b/lib/src/oled/oled.cpp index c101c6f..4b2c9ea 100644 --- a/lib/src/oled/oled.cpp +++ b/lib/src/oled/oled.cpp @@ -257,6 +257,39 @@ void OLED_ShowCHChr(OLED_T *dev, uint8_t x, uint8_t y, Chinese_t *hzk) { } } +#ifdef UTF8_TO_UNICODE + +#include "font.h" + +#define GET_LOW_BYTE0(x) ((x >> 0) & 0x000000ff) /* 获取第0个字节 */ +#define GET_LOW_BYTE1(x) ((x >> 8) & 0x000000ff) /* 获取第1个字节 */ + +void OLED_ShowCHString(OLED_T *dev, uint8_t x, uint8_t y, uint8_t *str) { + uint32_t unicode_letter; + uint8_t step; + while (*str != '\0') { + bool found = false; + step = Font_utf8_to_unicode(str, &unicode_letter); + if (step != 0) { + Chinese_t *hzk = find_chinese_data(GET_LOW_BYTE1(unicode_letter), GET_LOW_BYTE0(unicode_letter)); + OLED_ShowCHChr(dev, x, y, hzk); + x += Hzk_size; + str += step; + found = true; + } + if (!found) { + OLED_ShowChar(dev, x, y, *str, Hzk_size); + x += Hzk_size / 2; + str++; + } + if (x > dev->width - Hzk_size) { + x = 0; + y += Hzk_size; + } + } +} + +#else void OLED_ShowCHString(OLED_T *dev, uint8_t x, uint8_t y, uint8_t *str) { uint16_t index; while (*str != '\0') { @@ -283,7 +316,7 @@ void OLED_ShowCHString(OLED_T *dev, uint8_t x, uint8_t y, uint8_t *str) { } } } - +#endif #endif #ifdef LGVL_FONT #include "font.h" diff --git a/tools/unicode_ch_gen.py b/tools/unicode_ch_gen.py index 5cd10a2..2e7ae39 100644 --- a/tools/unicode_ch_gen.py +++ b/tools/unicode_ch_gen.py @@ -111,14 +111,14 @@ def format_chinese_array_as_text(chinese_array, size): text_output += '\n '.join(bytes_lines) text_output += ",\n }\n },\n" text_output += "};\n\n" - # text_output += "Chinese_t* find_chinese_data(uint8_t unicode_high, uint8_t unicode_low) {\n" - # text_output += " for (int i = 0; i < sizeof(Hzk) / sizeof(Chinese_t); ++i) {\n" - # text_output += " if (Hzk[i].unicode[0] == unicode_high && Hzk[i].unicode[1] == unicode_low) {\n" - # text_output += " return &Hzk[i];\n" - # text_output += " }\n" - # text_output += " }\n" - # text_output += " return NULL;\n" - # text_output += "}\n" + text_output += "Chinese_t* find_chinese_data(uint8_t unicode_high, uint8_t unicode_low) {\n" + text_output += " for (int i = 0; i < sizeof(Hzk) / sizeof(Chinese_t); ++i) {\n" + text_output += " if (Hzk[i].unicode[0] == unicode_high && Hzk[i].unicode[1] == unicode_low) {\n" + text_output += " return &Hzk[i];\n" + text_output += " }\n" + text_output += " }\n" + text_output += " return NULL;\n" + text_output += "}\n" text_output += "\n#endif //HW_LIB_OLED_FONT_CHUC_H" return text_output