用户工具

站点工具


microbit:趣味程序:micropython:在16x16点阵屏显示汉字和图案

使用microbit控制16×16点阵屏显示汉字和图案。汉字和图案的数据是通过microbit_toolbox软件生成,考虑到microbit内存较少,使用了单色方式,但是可以在程序中加入变色效果。

程序:

from microbit import *
from neopixel import NeoPixel
 
class neo16x16:
    def __init__(self, pin):
        self.np = NeoPixel(pin, 256)
        self.color = (0,0,8)
 
    def clear(self):
        self.np.clear()
 
    def set(self, n, color=''):
        if color!='':
            self.np[n] = color
        else:
            self.np[n] = self.color
        self.np.show()
 
    def setcolor(self, color):
        self.color = color
 
    def show(self, dat, pos=0, clear = True, color=''):
        if color != '':
            self.color = color
        if clear:
            for i in range(256):
                self.np[i]=(0,0,0)
        for x in range(16):
            for y in range(16):
                if (x+pos)>=len(dat):
                    self.np[x*16+y]=(0,0,0)
                else:
                    if (1<<y)&dat[x+pos]:
                        if pos%2==0:
                            self.np[x*16 + y] = self.color
                        else:
                            self.np[x*16 +15 - y] = self.color
        self.np.show()
 
def _delay(t):
    while t>0:
        t = t - 1
 
npdat=[
0x3F80,0x01FC,0x0080,0x01FC,0x3F80,0x0180,0x3F80,0x01FC,
0x0000,0x0000,0x3F98,0x19FC,0x3F98,0x0000,0x0000,0x0000,
0x0F00,0x01F8,0x3F80,0x010C,0x3080,0x019C,0x1980,0x0090,
0x0000,0x01FC,0x3F80,0x01FC,0x0180,0x0100,0x0080,0x0000,
0x0F00,0x01F8,0x3F80,0x010C,0x3080,0x01FC,0x1F80,0x00F0,
0x0000,0x0000,0x3080,0x010C,0x3080,0x0000,0x0000,0x0000,
0x3FF8,0x1FFC,0x3FF8,0x010C,0x3080,0x01FC,0x1F80,0x00F0,
0x0000,0x0000,0x3F98,0x19FC,0x3F98,0x0000,0x0000,0x0000,
0x0080,0x0100,0x1FF0,0x0FFC,0x3FF0,0x0104,0x3080,0x000C,
]
 
ne = neo16x16(pin1)
ne.setcolor((16,16,31))
n = 0
while True:
    ne.show(npdat, n)
    n = (n+1)%63
    _delay(100)


purge    随机主题   
microbit/趣味程序/micropython/在16x16点阵屏显示汉字和图案.txt · 最后更改: 2021/04/01 20:33 由 shaoziyang · 查看次数: 5719