1.背景
有一个产品,客户需要屏幕展示一些内容,要带一些中文,实现了OLED12864的驱动,但是它不带字库,现在要实现OLED全字库的显示
2.制作原始字库
下载软件pctolcd2002
设置
制作字库
打开原始文件
用软件自带的,或者自己去找字
生成字库文件
生成的内容如下
特别注意,保存的文件编码要是UTF-8的,不然后面会有点麻烦
3.转换
自行调整内容,字符"啊"之前的可以不要
目标格式应该是这样的
4.实现OLED驱动接口
oled.c
#include "oled.h"
/* ssd1306所对应的I2C控制器的设备节点 */
#define SSD1306_DEVICE "/dev/i2c-0"
/* ssd1306的I2C设备地址 */
#define SSD1306_ADDR 0x3C
uint8_t OLED_Display[128][8];
uint8_t OLED_Display_Cache[128][8];
static int oled_fd;
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char command)
{
unsigned char buf[2];
buf[0] = 0x00; // 控制字节,0x00表示接下来的字节是命令
buf[1] = command;
write(oled_fd, buf, 2);
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char data)
{
unsigned char buf[2];
buf[0] = 0x40; // 控制字节,0x40表示接下来的字节是数据
buf[1] = data;
write(oled_fd, buf, 2);
}
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
if(cmd)
{
Write_IIC_Data(dat);
}
else {
Write_IIC_Command(dat);
}
}
/* 设置OLED屏的显示坐标
* X : 表示OLED的水平坐标(0—127)
* Y : 表示OLED的页(0—7)
*/
void OLED_Set_Pos(unsigned char x, unsigned char y)
{ OLED_WR_Byte(0xb0+(y & 0x0f),OLED_CMD);
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f),OLED_CMD);
}
//开启OLED显示
void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
}
//关闭OLED显示
void OLED_Display_Off(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}
//清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!
void OLED_Clear(void)
{
uint8_t i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte (0x00,OLED_CMD); //设置显示位置—列低地址
OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
} //更新显示
}
/*
函数功能:OLED打点函数
输入参数:点所在坐标 x=0~127 y = 0~63
输出参数:无
用到的全局变量:无
*/
void OLED_Dot(uint8_t x,uint8_t y,uint8_t state)
{
if(state)
OLED_Display_Cache[x][y>>3] |= 1 << (y%8);
else
OLED_Display_Cache[x][y>>3]&= ~(1 << (y%8));
}
/*
函数功能:OLED刷屏任务
输入参数:OLED显示缓存数组
输出参数:无
用到的全局变量:无
*/
void OLED_Process()
{
unsigned char x,y;
for(y=0;y<8;y++)
{
for(x=0;x<128;x++)
{
if(OLED_Display[x][y] != OLED_Display_Cache[x][y])
{
OLED_Display[x] [y]= OLED_Display_Cache[x][y];
OLED_Set_Pos(x,y);
OLED_WR_Byte(OLED_Display[x] [y],OLED_DATA);
}
}
}
}
void OLED_Print(uint8_t x, uint8_t y, char *s)
{
uint16_t targetIndex = sizeof(CN16_Msk)/35; // 目标汉字的内码
printf("targetIndex:%d\r\n",targetIndex);
fflush(stdout);
uint16_t length = strlen(s);//取字符串总长
printf("length:%d\r\n",length);
fflush(stdout);
uint16_t offset = x;
for(uint16_t i=0;i<length;i++)
{
uint8_t first = *(s+i);
if(first <= 127){//小于128是ASCII符号
for(uint8_t n=0;n<8;n++)
{
OLED_Display_Cache[x+offset][y] = F8X16[first][n];
OLED_Display_Cache[x+offset][y+1] = F8X16[first][8+n+3]; offset++;
}
}else if(first > 127){//大于127,为汉字
uint8_t utf[4];
utf[0] = first;i++;
utf[1] = *(s+i);i++;
utf[2] = *(s+i);
for(uint16_t j=0;j<targetIndex;j++)
{
if(utf[0] == CN16_Msk[j][0] && utf[1] == CN16_Msk[j][1] && utf[2] == CN16_Msk[j][2])
{
for(uint8_t n=0;n<16;n++)
{
OLED_Display_Cache[x+offset][y] = CN16_Msk[j][n];
OLED_Display_Cache[x+offset][y+1] = CN16_Msk[j][16+n+3]; offset++;
}
}
}
}
}
}
//初始化SSD1306
void OLED_Init(void)
{
/* 打开ssd1306对应的I2C控制器文件 */
oled_fd =open(SSD1306_DEVICE, O_RDWR);
if (oled_fd< 0)
{
printf("open 0x%s failed \n",SSD1306_DEVICE);
}
/*设置ssd1306的I2C设备地址*/
if (ioctl(oled_fd,I2C_SLAVE_FORCE, SSD1306_ADDR) < 0)
{
printf("set slave address failed \n");
}
usleep(50000);
OLED_WR_Byte(0xAE,OLED_CMD);//--display off
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address
OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
OLED_WR_Byte(0x81,OLED_CMD); // contract control
OLED_WR_Byte(0xFF,OLED_CMD);//--128
OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
OLED_WR_Byte(0x00,OLED_CMD);//
OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
OLED_WR_Byte(0x80,OLED_CMD);//
OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
OLED_WR_Byte(0x05,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
OLED_WR_Byte(0xF1,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
OLED_WR_Byte(0x30,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
OLED_WR_Byte(0x14,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
OLED_Clear();
}
int main()
{
OLED_Init();
OLED_Print(8,2,"123和你啊");
OLED_Process();
while(1)
{
#if 0 //测试按点刷屏
for(uint8_t i=0;i<128;i++)
{
for(uint8_t j=0;j<64;j++)
{
OLED_Dot( i,j,1);
OLED_Process();
}
}
#endif
}
return 0;
}
oled.h
#ifndef __OLED_H
#define __OLED_H
//#include "sys.h"
#include "stdlib.h"
#include <stdint.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <string.h>
#include "oledfont.h"
#define OLED_MODE 0
#define SIZE 8
#define XLevelL 0x00
#define XLevelH 0x10
#define Max_Column 128
#define Max_Row 64
#define Brightness 0xFF
#define X_WIDTH 128
#define Y_WIDTH 64
//-----------------OLED IIC端口定义----------------
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
//OLED控制用函数
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Init(void);
void OLED_Clear(void);
#endif
5.字库调整
利用python把ascii编码改为utf8编码
with open('input.txt', 'r', encoding='utf-8') as input_file:
with open('output.txt', 'w', encoding='utf-8') as output_file:
for line in input_file:
char = line.strip()
utf8_byte = char.encode("utf-8")
hex_str = ", ".join([f"0x{x:02x}" for x in utf8_byte])
output_file.write(hex_str + "\n")
输入文件是
输出信息是
截取字库片段
#ifndef __OLEDFONT_H
#define __OLEDFONT_H
/****************************************8*16的点阵************************************/
const unsigned char F8X16[][16]=
{
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
{0xF0,0x08,0xF0,0x80,0x60,0x18,0x00,0x00,0x00,0x31,0x0C,0x03,0x1E,0x21,0x1E,0x00},/*"%",37*/
{0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x2C,0x19,0x27,0x21,0x10},/*"&",38*/
{0x00,0x12,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",39*/
{0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00},/*"(",40*/
{0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00},/*")",41*/
{0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00},/*"*",42*/
{0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x0F,0x01,0x01,0x01},/*"+",43*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x90,0x70,0x00,0x00,0x00,0x00,0x00},/*",",44*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00},/*"-",45*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00},/*".",46*/
{0x00,0x00,0x00,0x00,0xC0,0x38,0x04,0x00,0x00,0x60,0x18,0x07,0x00,0x00,0x00,0x00},/*"/",47*/
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},/*"0",48*/
{0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00},/*"1",49*/
{0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},/*"2",50*/
{0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00},/*"3",51*/
{0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24},/*"4",52*/
{0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00},/*"5",53*/
};
// typedef struct {
// // unsigned char Index[3]; // 汉字UTF-8内码
// // unsigned char Msk[32]; // 字模
// unsigned char Msk[35];
// } __attribute__((packed)) GB2312_GB16_t;
const unsigned char CN16_Msk[][35]={
{0xe5, 0x95, 0x8a,0xFC,0x04,0xFC,0x00,0xFE,0x42,0xBE,0x00,0xF2,0x12,0xF2,0x02,0xFE,0x02,0x00,0x00,0x0F,0x04,0x0F,0x00,0xFF,0x10,0x0F,0x00,0x0F,0x04,0x4F,0x80,0x7F,0x00,0x00,0x00},/*'啊',1410*/
{0xe9, 0x98, 0xbf,0x00,0xFE,0x02,0x22,0xDA,0x06,0x00,0xF2,0x12,0x12,0xF2,0x02,0xFE,0x02,0x02,0x00,0x00,0xFF,0x08,0x10,0x08,0x07,0x00,0x0F,0x04,0x04,0x4F,0x80,0x7F,0x00,0x00,0x00},/*'阿',1411*/
{0xe5, 0x9f, 0x83,0x20,0x20,0xFF,0x20,0x20,0x10,0x98,0xF4,0x92,0x91,0x90,0x94,0x98,0x30,0x00,0x00,0x10,0x30,0x1F,0x08,0x88,0x85,0x44,0x24,0x14,0x0F,0x14,0x24,0x44,0x84,0x84,0x00},/*'埃',1412*/
{0xe6, 0x8c, 0xa8,0x10,0x10,0xFF,0x10,0x00,0x10,0x98,0xF4,0x92,0x91,0x90,0x94,0x98,0x30,0x00,0x00,0x42,0x82,0x7F,0x01,0x84,0x85,0x44,0x24,0x14,0x0F,0x14,0x24,0x44,0x84,0x84,0x00},/*'挨',1413*/
{0xe5, 0x93, 0x8e,0x00,0xFC,0x04,0x04,0xFC,0x00,0xC4,0x1F,0x04,0x04,0x04,0x1F,0xC4,0x04,0x00,0x00,0x00,0x0F,0x04,0x04,0x8F,0x80,0x40,0x23,0x14,0x08,0x14,0x23,0x40,0x80,0x80,0x00},/*'哎',1414*/
{0xe5, 0x94, 0x89,0xFC,0x04,0x04,0xFC,0x00,0x10,0x98,0xF4,0x92,0x91,0x90,0x94,0x98,0x30,0x00,0x00,0x1F,0x08,0x08,0x1F,0x84,0x85,0x44,0x24,0x14,0x0F,0x14,0x24,0x44,0x84,0x84,0x00},/*'唉',1415*/
};
#endif
6.显示效果
7.优化
上面的代码,存在一个问题是,Y轴的步进必须是8个点,需要优化为任意位置的显示
void OLED_Dot_Line(uint8_t x, uint8_t y, uint8_t temp)
{
for(uint8_t i=0;i<8;i++)
{
if(temp&0x01)
{
OLED_Dot(x,y,1);
}
else{
OLED_Dot(x,y,0);
}
temp = temp>>1;
y++;
if(y>=64)return; //超区域了
}
}
void OLED_Print(uint8_t x, uint8_t y, char *s)
{
uint16_t targetIndex = sizeof(CN16_Msk)/35; // 目标汉字的内码
uint16_t length = strlen(s);//取字符串总长
// printf("length:%d\r\n",length);
// fflush(stdout);
uint16_t offset = x;
for(uint16_t i=0;i<length;i++)
{
uint8_t first = *(s+i);
if(first <= 127){//小于128是ASCII符号
for(uint8_t n=0;n<8;n++)
{
OLED_Dot_Line(x+offset,y,F8X16[first][n]);
OLED_Dot_Line(x+offset,y+8,F8X16[first][n+8]);
offset++;
}
}else if(first > 127){//大于127,为汉字
uint8_t utf[4];
utf[0] = first;i++;
utf[1] = *(s+i);i++;
utf[2] = *(s+i);
for(uint16_t j=0;j<targetIndex;j++)
{
if(utf[0] == CN16_Msk[j][0] && utf[1] == CN16_Msk[j][1] && utf[2] == CN16_Msk[j][2])
{
for(uint8_t n=0;n<16;n++)
{
OLED_Dot_Line(x+offset,y,CN16_Msk[j][n+3]);
OLED_Dot_Line(x+offset,y+8,CN16_Msk[j][n+16+3]);
offset++;
}
}
}
}
}
}