练习1,数码管全关全断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
from gpiozero import LEDBoard from time import sleep from signal import pause leds = LEDBoard(2, 3, 4, 14, 17,27,22, active_high=False) M7G= [ (0, 0, 0, 0, 0, 0, 0),#M7G[0],全灭 (1, 1, 1, 1, 1, 1, 1),#M7G[1],全亮 (0, 1, 0, 1, 0, 1, 0),#M7G[2] (1, 0, 1, 0, 1, 0, 1),#M7G[3] ] while True: leds.value = M7G[0] sleep(1) leds.value = M7G[1] sleep(1) leds.value = M7G[2] sleep(1) leds.value = M7G[3] sleep(1) leds.value = M7G[1] sleep(1) |
测试2:数码管灰度显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from gpiozero import LEDBoard from time import sleep from signal import pause leds = LEDBoard(2, 3, 4, 14, 17,27,22,pwm=True, active_high=False) M7G= [ (0, 0, 0, 0, 0, 0, 0), (1, 1, 1, 1, 1, 1, 1), (0.5, 0.5, 0.5, 0.5,0.5,0.5,0.5), (0.1, 0.3, 0.5, 0.7,0.9, 0,1) ] while True: leds.value = M7G[1] sleep(1) leds.value = M7G[0] sleep(1) leds.value = M7G[2] sleep(1) leds.value = M7G[0] sleep(1) leds.value = M7G[3] sleep(1) leds.value = M7G[0] sleep(1) |
测试3:数码管逐笔段点亮
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from gpiozero import LEDBoard from time import sleep from signal import pause leds = LEDBoard(2, 3, 4, 14, 17,27,22,active_high=False) #leds.blink() while True : for n in range(0,7) : leds[n].on() sleep(1) #leds[n].off() leds.off() sleep(1) |
另一种循环方法for led in leds :
1 2 3 4 5 6 7 8 9 10 11 |
from gpiozero import LEDBoard from time import sleep from signal import pause leds = LEDBoard(2, 3, 4, 14, 17,27,22,active_high=False) while True : leds.on() sleep(1) for led in leds : led.off() sleep(1) sleep(1) |
测试4:数码管按键
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
from gpiozero import LEDBoard,Button from time import sleep from signal import pause leds = LEDBoard(2, 3, 4, 14, 17,27,22,active_high=False) button = Button(21) val = 0 def zheng(): for n in range(0,7) : leds[n].on() sleep(1) leds[n].off() leds.off() sleep(1) def fan(): for n in range(0,7) : leds[6 - n].on() sleep(1) leds[6 - n].off() leds.off() sleep(1) while True: button.wait_for_press() zheng() button.wait_for_release() fan() pause() |
测试5:数码管按键2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
from gpiozero import LEDBoard,Button from time import sleep from signal import pause leds = LEDBoard(2, 3, 4, 14, 17,27,22,active_high=False) button = Button(21) def zheng(): for led in leds : led.on() sleep(1) led.off() leds.off() sleep(1) def fan(): for n in range(0,7) : leds[6 - n].on() sleep(1) leds[6 - n].off() leds.off() sleep(1) while True: button.wait_for_press() while(button.value): zheng() button.wait_for_release() while(not button.value): fan() pause() |
测试2:数码管tk按键
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
from gpiozero import LEDBoard from time import sleep from signal import pause from tkinter import * leds = LEDBoard(2, 3, 4, 14, 17,27,22,active_high=False) M7G= [ (0, 0, 0, 0, 0,0, 0), (1, 1, 1, 1,1, 1,0 ), (0, 1, 1, 0, 0,0, 0), (1, 1, 0, 1, 1,0, 1), (1, 1, 1, 1,0, 0,1), (0, 1, 1, 0,0, 1,1), (1, 0, 1, 1,0, 1,1), (1, 0, 1, 1,1, 1,1), (1, 1, 1, 0,0, 0,0), (1, 1, 1, 1,1, 1,1), (1, 1, 1, 1,0, 1,1), ] val=0 leds.value = M7G[val] #定义Button的回调函数 def helloButton(): global val if val>=10: val=0 else : val+=1 leds.value = M7G[val] root = Tk() #通过command属性来指定Button的回调函数 Button(root,text = 'LED Button',command = helloButton).pack() root.mainloop()#TK的主程序循环 |
测试7:数码管TK滚动条
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
from gpiozero import LEDBoard from time import sleep from signal import pause from tkinter import * leds = LEDBoard(2, 3, 4, 14, 17,27,22,active_high=False) M7G= [ (0, 0, 0, 0, 0,0, 0), (1, 1, 1, 1,1, 1,0 ), (0, 1, 1, 0, 0,0, 0), (1, 1, 0, 1, 1,0, 1), (1, 1, 1, 1,0, 0,1), (0, 1, 1, 0,0, 1,1), (1, 0, 1, 1,0, 1,1), (1, 0, 1, 1,1, 1,1), (1, 1, 1, 0,0, 0,0), (1, 1, 1, 1,1, 1,1), (1, 1, 1, 1,0, 1,1), ] leds.value = M7G[0] def ledset(text): leds.value = M7G[int(text)+1] root = Tk() Scale(root, from_ = 0, #设置最大值 to = 9, #设置最小值 resolution = 1, #设置步距值 orient = HORIZONTAL, #设置水平方向 command = ledset #设置回调函数 ).pack() root.mainloop() |
转载请注明:徐自远的乱七八糟小站 » 20170904 (第5课)树莓派PYPI- 数码管