为更新而更新,为保持更新状态而更新。
给学生讲解用gpiozero库控制八段管和8x8共阳极LED点阵。已经讲解了单个LED的控制,RGB彩色灯珠的控制,在讲解八段管就很容易理解,多个八段管的讲解稍微麻烦一点,然后LED点阵为了便于理解,绘制了演示程序,可以演示行扫描图案变化过程,以及设置阳极高低引脚图案的变化。8短数码管的很简单,LED点阵的略复杂。
模拟共阴极八段数码管
image.png
from turtle import *
speed(0)
sega = Turtle()
sega.pu()
sega.goto(0, 300)
w = 150
h = 20
def rect(t,flag=True, fill=False):
for i in range(2):
if fill:
t.begin_fill()
if flag:
t.fd(w)
t.rt(90)
t.fd(h)
t.rt(90)
else:
t.fd(h)
t.rt(90)
t.fd(w)
t.rt(90)
if fill:
t.end_fill()
sega.pd()
rect(sega)
segg = Turtle()
segg.pu()
segg.goto(0, 120)
segg.pd()
rect(segg)
segd = Turtle()
segd.pu()
segd.goto(0, -65)
segd.pd()
rect(segd)
segb = Turtle()
segb.pu()
segb.goto(130, 275)
segb.pd()
rect(segb, False)
segc = Turtle()
segc.pu()
segc.goto(130, 90)
segc.pd()
rect(segc, False)
sege = Turtle()
sege.pu()
sege.goto(0, 90)
sege.pd()
rect(sege, False)
segf = Turtle()
segf.pu()
segf.goto(0, 275)
segf.pd()
rect(segf, False)
segh = Turtle()
segh.pu()
segh.goto(180, 30)
segh.pd()
rect(segh, False)
zero = [1, 1, 1, 1, 1, 1, 0, 0]
one = [0, 1, 1, 0, 0, 0, 0, 0]
two = [1, 1, 0, 1, 1, 0, 1, 0]
three = [1, 1, 1, 1, 0, 0, 1, 0]
four = [0, 1, 1, 0, 0, 1, 1, 0]
five = [1, 0, 1, 1, 0, 1, 1, 0]
segs = [sega, segb, segc, segd, sege, segf, segg, segh]
dirt = [True, False, False, True, False, False, True, False]
for seg in segs:
seg.speed(0)
for i in range(8):
if five[i] == 1:
rect(segs[i], flag=dirt[i], fill=True)
else:
rect(segs[i], flag=dirt[i], fill=False)
image.png
模拟共阳极8x8led点阵
image.png
from turtle import *
tracer(40, 2)
class LED():
def __init__(self, x, y, r=15):
self.r = r
self.neg = 0
self.pos = 0
self.t = Turtle()
self.t.pu()
self.t.goto(x, y)
self.t.speed(0)
self.t.ht()
self.setNeg(self.neg)
self.display()
def setNeg(self, value):
self.neg = value
self.display()
def display(self):
if self.neg == 1:
self.t.color((0.4, 0.4, 0.4), (1, 0, 0))
else:
self.t.color((0.4, 0.4, 0.4), 'white')
self.t.begin_fill()
self.t.pd()
self.t.circle(self.r)
self.t.pu()
self.t.end_fill()
offx = -200
offy = 200
leds = []
gap = 50
color((0.4, 0.4, 0.4))
pu()
goto(offx - gap/2, offy + 3*gap/4)
pd()
begin_fill()
for i in range(4):
fd(8 * gap)
rt(90)
end_fill()
##for row in range(8):
## led_row = []
## for col in range(8):
## led = LED(offx + gap * col, offy - gap * row)
## # led.display()
## led_row.append(LED(offx + 20 * col, offy - 20 * row))
## leds.append(led_row)
for j in range(8):
row = []
for i in range(8):
led = LED(offx + gap * i, offy - gap * j)
row.append(led)
leds.append(row)
from time import sleep
heart =[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
]
heart_sm =[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0],
]
while True:
for row in range(8):
for col in range(8):
leds[row][col].setNeg(heart[row][col])
leds[row][col].display()
for row in range(8):
for col in range(8):
leds[row][col].setNeg(heart_sm[row][col])
leds[row][col].display()
image.png
image.png
image.png