avr+ks0108屏显示中文解决方案

本文介绍了一种利用Python生成汉字位图的方法,并将其嵌入到代码中以供进一步使用的技术。通过定义函数getbmp来创建指定大小的位图文件,再将这些位图整合到代码中形成字符集,可用于特定的显示需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# coding=gb2312

import Image, ImageDraw,ImageFont,sys, string,codecs

def getbmp(c,txtsize):
    im = Image.new("1", (txtsize,txtsize))
    
    draw = ImageDraw.Draw(im)
    font = ImageFont.FreeTypeFont('Zfull-GB.ttf', txtsize)
    #font =  ImageFont.load('wenquanyi_9pt.pil')
    
    #D:/project/jj
    draw.text((0,0),c, 1, font)
    output=[0]
    pos=0
    bit=0
    im.save("%c_%d.bmp"%(c,txtsize))
    for  scanline in xrange(0,txtsize,8):
        for i in xrange(0,txtsize):
            output.append(0)
            bit=0
            pos+=1
            for j in xrange(scanline,scanline+8):
                if j>=im.size[1]:break
                if im.getpixel((i,j))!=0:
                    output[pos]|= 1<<(bit);
                bit+=1
    return output

chars={}
hzbmp=[]

for line0 in file('showsentence.txt'):
    ln = line0.strip().split(',')
    size = 12
    if len(ln)>1: 
        size = string.atoi(ln[1])
    line=unicode(ln[0], "gb2312")
    for c in line.strip():
        if (c,size) not in chars: 
            chars[(c,size)] = [len(chars), len(hzbmp)]
            hzbmp.extend(getbmp(c, size)[:])
            
#add postfix
postfix=""
if len(sys.argv)>1:
    postfix=sys.argv[1]
#output index
i = 0
print "static uint16_t hzIndex%s[]={"%postfix
for line0 in file('showsentence.txt'):
    ln = line0.strip().split(',')
    line=unicode(ln[0], "gb2312")
    print "    %d,"%i,
    i+=len(line.strip())+1
print "%d "%i
print "};" 
print
#output sentence 
i = 0
print "static uint16_t hzSentences%s[]={"%postfix
for line0 in file('showsentence.txt'):
    ln = line0.strip().split(',')
    size = 12
    if len(ln)>1: 
        size = string.atoi(ln[1])
    line=unicode(ln[0], "gb2312")
    print ("    //%d:%s"%(i,line)).encode('gb2312')
    i+=1
    print "    %d,"%size,
    for c in line.strip():
        print "%d,"%chars[(c,size)][1],
    print
print "};" 
print
#output hzbmp    
print "static const unsigned char hzPixels%s[] PROGMEM = {"%postfix
i=0
for v in hzbmp:
    print "%3d,"%v,
    i+=1
    if(i>=16):
        i=0
        print
print "};" 


print 
print "static void showSentence%s(int ind)"%postfix
print "{"
print "    char size = hzSentences%s[hzIndex%s[ind]];"%(postfix,postfix)
print "    uint16_t i = 1;"
print "    for(; i<hzIndex%s[ind+1]-hzIndex%s[ind]; i++)"%(postfix,postfix)
print "    {"
print "         ks0108ShowHZ(hzSentences%s[hzIndex%s[ind]+i],hzPixels%s,size,size);"%(postfix,postfix,postfix)
print "    }"
print "}"
    
"""
/*
showsentence(1); //xxxxxxx
showsentence(2); //xxxxxxx
*/
sentence[]=
{
"size|pos0|pos1...",
}
hzBits[]=
{
}
static void showSentence(int ind)
{

    char size = hzSentences[hzIndex[ind]];
    int i = 1;
    for(; i<hzIndex[ind+1]-hzIndex[ind]; i++)
    {
     ks0108ShowHZ(hzSentences[hzIndex[ind]+i],hzPixels,size,size);
    }
}
"""


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值