|
最近想玩玩墨水屏,墨水屏是驱动了但是不显示中文就有点不完美了,本来看微雪的例程是利用汉字取模的方式,但是取模显示的文字太少了,所以直接买了一块高通的字库芯片,本来想着看着stm32的例程能直接改,但是移植过来发现没用。哪位大佬给我看看代码,这代码参考了论坛大佬的另一款字库芯片。
#include "gt30l.h"
#include <SPI.h>
// 申请一个字的字库缓存
char fontBuff[128];
char fontMatrix[128];
void GT30Linit(void)
{
pinMode(GT30L_CS_PIN , OUTPUT);
digitalWrite(GT30L_CS_PIN , HIGH);
SPI.begin();
}
/******************************************************************************
function: 清空单个字缓存
******************************************************************************/
void ClearMem(void)
{
unsigned char i;
for (i = 0; i < 128; i++)
{
fontMatrix[i] = 0;
}
}
/******************************************************************************
function: 汉字转换程序
parameter:
putdata: 原汉字点 (横置横排)
getdate: 目标汉字点,high 原汉字高度,width 原汉字宽度,
******************************************************************************/
void FontMap(char *getdate, char *putdata, uint8_t width, uint8_t high)
{
unsigned short i, j, wbyte; // word
uint8_t i_8;
wbyte = (width + 7) / 8;
for (i = 0; i < high; i++)
{
for (j = 0; j < width; j++)
{
i_8 = i / 8;
if ((*(putdata + wbyte * i + j / 8) & (0x80 >> (j % 8))) > 0)
getdate[i_8 * width + j] |= (0x01 << (i % 8));
else
getdate[i_8 * width + j] &= (~(0x01 << (i % 8)));
}
}
}
/******************************************************************************
function: 向字库写入命令
parameter:
dat : 要写入的命令
******************************************************************************/
void Font_command(uint8_t dat)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
digitalWrite(GT30L_SCLK_PIN, 1);
if (dat & 0x80)
{
digitalWrite(GT30L_MOSI_PIN, 1);
}
else
{
digitalWrite(GT30L_MOSI_PIN, 0);
}
digitalWrite(GT30L_SCLK_PIN, 1);
dat <<= 1;
}
}
/******************************************************************************
function: 从字库读取数据
******************************************************************************/
// uint8_t getOneDataFromROM(void)
// {
// uint8_t i;
// uint8_t ret_data = 0; //返回数据初始化
// for (i = 0; i < 8; i++)
// {
// //字库时钟拉低
// digitalWrite(GT30L_SCLK_PIN, 0);
// ret_data <<= 1;
// if (digitalRead(GT30L_MISO_PIN))
// {
// ret_data++;
// }
// digitalWrite(GT30L_SCLK_PIN, 1); //字库时钟拉高
// }
// return ret_data; //返回读出的一个字节
// }
byte revBit(byte input)
{
byte output = 0;
for (int i = 0; i < 8; i++)
{
if (input & (1 << i))
output |= 1 << (7 - i);
}
return output;
}
/******************************************************************************
function: 从字库读取N个数据
******************************************************************************/
void getNBytesDataFromROM(uint8_t AddrHigh, uint8_t AddrMid, uint8_t AddrLow, char *pBuff, uint8_t DataLen)
{
uint8_t i;
digitalWrite(GT30L_CS_PIN, 0); //字库片选
SPI.transfer(0x03);//移入命令字
SPI.transfer(AddrHigh);//移入地址次高位
SPI.transfer(AddrMid);//移入地址次低位
SPI.transfer(AddrLow);//移入地址最低位
for (i = 0; i < DataLen; i++)
{
byte buf = SPI.transfer(0x00);
*(pBuff + i) = revBit(buf); //读一个字节数据
}
digitalWrite(GT30L_CS_PIN, 1); //取消字库片选
}
void Paint_DrawString(UWORD Xstart, UWORD Ystart, const char *pString, UWORD font, UWORD Color_Foreground, UWORD Color_Background)
{
const char *p_text = pString;
int x = Xstart, y = Ystart;
uint8_t width, height; // 该字符的宽高
uint8_t AddrHigh, AddrMid, AddrLow; //字高、中、低地址
uint32_t FontAddr = 0; //字地址
uint32_t BaseAdd = 0; //字库基地址
uint8_t n, d; // n: 字节数,d, 文字间距
while (*p_text != 0)
{
// 根据字符类型取值
// ASCII < 126
if (*p_text <= 0x7F)
{
switch (font)
{
case 7:
BaseAdd = 0x1DDF80;
n = 8;
d = 6;
width = 8, height = 8;
break; // 5x7 ASCII
case 8:
BaseAdd = 0x1DE280;
n = 8;
d = 8;
width = 8, height = 8;
break; // 7x8 ASCII
case 12:
BaseAdd = 0x1DBE00;
n = 12;
d = 6;
width = 6, height = 12;
break; // 6x12 ASCII
case 16:
BaseAdd = 0x1DD780;
n = 16;
d = 8;
width = 8, height = 16;
break; // 8x16 ASCII
case 24:
BaseAdd = 0x1DFF00;
n = 48;
d = 12;
width = 12, height = 24;
break; // 12x24 ASCII
case 32:
BaseAdd = 0x1E5A50;
n = 64;
d = 16;
width = 16, height = 32;
break; // 16x32 ASCII
}
if ((*p_text >= 0x20) && (*p_text <= 0x7E))
{
FontAddr = *p_text - 0x20;
FontAddr = (unsigned long)((FontAddr * n) + BaseAdd);
AddrHigh = (FontAddr & 0xff0000) >> 16; /*地址的高8位,共24位*/
AddrMid = (FontAddr & 0xff00) >> 8; /*地址的中8位,共24位*/
AddrLow = FontAddr & 0xff; /*地址的低8位,共24位*/
getNBytesDataFromROM(AddrHigh, AddrMid, AddrLow, fontBuff, n); /*取一个汉字的数据,存到"FontBuf[]"*/
FontMap(fontMatrix, fontBuff, width, height);
// Paint_DrawChar(width, height, x, y, Color_Foreground, Color_Background);
}
// 下一个字的指针
p_text += 1;
// 下一个字的开始位置
x += d;
}
else
{ // Chinese
switch (font)
{
// n:字符所占字节数 d:字间距
case 12:
BaseAdd = 0x00;
n = 24;
d = 12;
break; // 12*12
case 16:
BaseAdd = 0x2C9D0;
n = 32;
d = 16;
break; // 15*16
case 24:
BaseAdd = 0x68190;
n = 72;
d = 24;
break; // 24*24
case 32:
BaseAdd = 0xEDF00;
n = 128;
d = 32;
break; // 32*32
}
if (((*p_text >= 0xA1) && (*p_text <= 0xA9)) && (*(p_text + 1) >= 0xA1))
{
//国标简体(GB2312)汉字在 字库IC中的地址由以下公式来计算://
// Address = ((MSB - 0xA1) * 94 + (LSB - 0xA1))*n+ BaseAdd; 分三部取地址///
FontAddr = (*p_text - 0xA1) * 94;
FontAddr += (*(p_text + 1) - 0xA1);
FontAddr = (unsigned long)((FontAddr * n) + BaseAdd);
AddrHigh = (FontAddr & 0xff0000) >> 16; //地址的高8位,共24位//
AddrMid = (FontAddr & 0xff00) >> 8; //地址的中8位,共24位//
AddrLow = FontAddr & 0xff; //地址的低8位,共24位//
getNBytesDataFromROM(AddrHigh, AddrMid, AddrLow, fontBuff, n); //取一个汉字的数据,存到"FontBuf[]"
FontMap(fontMatrix, fontBuff, d, d);
// Paint_DrawChar(width, height, x, y, Color_Foreground, Color_Background);
}
else if (((*p_text >= 0xB0) && (*p_text <= 0xF7)) && (*(p_text + 1) >= 0xA1))
{
//国标简体(GB2312) 字库IC中的地址由以下公式来计算://
// Address = ((MSB - 0xB0) * 94 + (LSB - 0xA1)+846)*n+ BaseAdd; 分三部取地址//
FontAddr = (*p_text - 0xB0) * 94;
FontAddr += (*(p_text + 1) - 0xA1) + 846;
FontAddr = (unsigned long)((FontAddr * n) + BaseAdd);
AddrHigh = (FontAddr & 0xff0000) >> 16; //地址的高8位,共24位//
AddrMid = (FontAddr & 0xff00) >> 8; //地址的中8位,共24位//
AddrLow = FontAddr & 0xff; //地址的低8位,共24位//
getNBytesDataFromROM(AddrHigh, AddrMid, AddrLow, fontBuff, n); //取一个汉字的数据,存到"FontBuf[ ]"
FontMap(fontMatrix, fontBuff, d, d);
// Paint_DrawChar(width, height, x, y, Color_Foreground, Color_Background);
}
p_text += 2;
x += width;
}
ClearMem();
}
}
|
|