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

# 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);
    }
}
"""


 

你可能感兴趣的:(avr+ks0108屏显示中文解决方案)