第7章:LED灯原来是这样点亮的

SAKSHAT提供的LED使用方法

当前SAKSHAT为我们提供的方法参考:http://wiki.nxez.com/saks:sdk:libraries:led74hc595

这里就直接摘录了,以下是创百科的内容:
74HC595 芯片驱动下的 LED 灯组类,控制一组 LED 工作。 方法

init(self, pins, real_true=GPIO.HIGH)

初始化对象,设置引脚和触发电平(高电平或低电平触发)。pins 为 IO 引脚数组。

ic(self)

返回当前驱动数码管的IC对象。

is_on(self, index)

返回当前 LED 的工作状态。true 或 false。index 为 LED 编号,从 0 开始。

row_status(self)

返回当前 LED 灯组的工作状态的数组。

on(self)

设置 LED 灯组为全部点亮状态。

off(self)

设置 LED 灯组为全部熄灭状态。

on_for_index(self, index)

设置当前 LED 灯组的第 index+1 个 LED 的状态为点亮。index 为 LED 编号,从 0 开始。

off_for_index(self, index)

设置当前 LED 灯组的第 index+1 个 LED 的状态为熄灭。index 为 LED 编号,从 0 开始。

set_row(self, status)

设置当前 LED 灯组的点亮状态。status 为布尔型数组。status 中的元素也可以为 None,表示不改变该元素位置的 LED 状态。


#点亮所有LED灯
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        #点亮所有LED灯
        SAKS.ledrow.on()
         
    except KeyboardInterrupt:
        print("任务被终止了")
#熄灭所有LED灯
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        #熄灭所有LED灯
        SAKS.ledrow.off()
         
    except KeyboardInterrupt:
        print("任务被终止了")
#返回当前LED灯组状态。
import time
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
led_status = []
 
if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        #点亮所有灯并输出所有灯状态
        for i in range(0, 8):
            SAKS.ledrow.on_for_index(i)
        led_status = SAKS.ledrow.row_status
        print("点亮所有灯状态" + str(led_status))
         
        time.sleep(5)
         
        #关闭所有灯并输出等状态
        for i in range(0, 8):
            SAKS.ledrow.off_for_index(i)
        led_status = SAKS.ledrow.row_status
        print("关闭所有灯状态" + str(led_status))
         
        time.sleep(5)
         
        #熄灭所有LED灯
        SAKS.ledrow.off()
         
        #间隔点亮LED灯模式1
        for i in range(0, 8, 2):
            SAKS.ledrow.on_for_index(i)
        led_status = SAKS.ledrow.row_status
        print("间隔点亮LED灯模式1" + str(led_status))
         
        time.sleep(5)
         
        #熄灭所有LED灯
        SAKS.ledrow.off()
         
        #间隔点亮LED灯模式2
        for i in range(1, 8, 2):
            SAKS.ledrow.on_for_index(i)
        led_status = SAKS.ledrow.row_status
        print("间隔点亮LED灯模式2" + str(led_status))
         
    except KeyboardInterrupt:
        print("任务被终止了")
#返回当前LED单个灯状态。
import time
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
led_status = []
 
if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        #熄灭所有LED灯
        SAKS.ledrow.off()
         
        #关闭所有灯并输出等状态
        for i in range(0, 8, 2):
            SAKS.ledrow.on_for_index(i)
        led_status = SAKS.ledrow.row_status
        print("关闭所有灯状态" + str(led_status))
         
        for i in range(0, 8):
            led_one_status = SAKS.ledrow.is_on(i)
            print("第" + str(i) + "个LED灯状态为" + str(led_one_status))
               
    except KeyboardInterrupt:
        print("任务被终止了")
#逐一点亮LED灯,然后逐一熄灭LED灯
import time
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        for i in range(0, 8):
            SAKS.ledrow.on_for_index(i)
            time.sleep(1)
             
        for i in range(0, 8):
            SAKS.ledrow.off_for_index(i)
            time.sleep(1)
         
    except KeyboardInterrupt:
        print("任务被终止了")
#设置LED灯族状态
import time
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        SAKS.ledrow.set_row([True, False, True, False, True, False, True, False])
 
        time.sleep(5)
 
        SAKS.ledrow.set_row([None, True, None, True, None, None, None, None])
 
        time.sleep(5)
         
        SAKS.ledrow.set_row([None, None, None, None, None, True, None, True])
         
        time.sleep(5)
         
        SAKS.ledrow.set_row([False, None, False, None, False, None, False, None])
         
        time.sleep(5)
         
        SAKS.ledrow.set_row([False, False, False, False, False, False, False, False])
         
    except KeyboardInterrupt:
        print("任务被终止了")
#流水灯走起
import time
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        while True:         
            for i in range(0, 8):
                SAKS.ledrow.on_for_index(i)
                time.sleep(0.1)
                SAKS.ledrow.off_for_index(i)
    except KeyboardInterrupt:
        print("任务被终止了")


课程 bilibili 视频地址:https://www.bilibili.com/video/av71878718/?p=18

 


评论:
相关文章
树莓派Build HAT

Raspberry Pi Build HAT 是一个扩展板,可连接到 Raspberry Pi 的 40 针 GPIO 接头,该接头是与 LEGO® Education 合作设计的,旨在使用 Raspberry Pi 计算机轻松控制 LEGO® Technic™ 电机和传感器。


树莓派超算和科学计算教程(全)

我们将用 Raspberry Pi 开始我们探索超级计算和科学编程科学领域的激动人心的旅程。


全球首款桌面级树莓派双轮足机器人问世!哈工大创业团队出品

近日,全球规模最大、最具影响力的众筹平台Kickstarter就发售了这样一种全新形态的桌宠机器人——全球首款桌面级树莓派双轮足机器人XGO-Rider。


树莓派如何设计3D打印外壳

这篇文章将为大家讲解有关树莓派如何设计3D打印外壳。


树莓派 5 发布,新特性细节公布

树莓派5 具有一些新特性,速度是树莓派 4B 的两倍多,并且是英国剑桥第一台自主设计芯片的树莓派计算机。


香橙派推出对标树莓派3B、CM4和Zero2W开发板

最近香橙派推出了它们的香橙派3B、香橙派CM4、香橙派Zero2W,代号和树莓派一模一样。分别对标树莓派的树莓派3B、树莓派CM4、以及树莓派Zero2W。


用树莓派实现网络打印服务器

本文详细介绍了如何设置Raspberry Pi的网络打印机,改造USB打印机为无线打印机,搭建一个中心打印服务器。


RaspberryPi HAT树莓派电机驱动板

这款电机驱动板足够强大,可以驱动4个直流电机或2个步进电机,并且额外提供4路全速PWM控制,可以控制4路舵机。


只需一枚树莓派,轻松造个游戏机

你怀恋小时候玩过的小霸王和街机游戏吗?使用RetroPie软件、树莓派和USB游戏控制器,不到一个小时,你就能重新捡起小时候玩过的游戏了:那些小时候没有打通的关,现在还有兴趣吗?


Emo:基于树莓派 4B DIY 能笑会动的桌面机器人

Emo 是一款个人伴侣机器人,集时尚与创新于一身。他的诞生离不开最新的树莓派 4 技术和先进的设计。他不仅仅是一款机器人,更是一个活生生的存在。

动手学树莓派 Python 篇

作者:大飞品树莓   共13讲

本课程使用树莓派配合 NXEZ 树莓派瑞士军刀扩展板卡,进行动手学树莓派。教程和教程示例源码均托管在 git 上,同时正在不断完善和更新,欢迎各位一线教师在自己的授课中参考。