动手学树莓派第7章:你牙牙学语的声音真好听

SAKSHAT提供的蜂鸣器使用方法

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

Buzzer
有源蜂鸣器类,控制蜂鸣器对象发出声音。 方法

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

初始化对象,设置蜂鸣器引脚和触发电平(高电平或低电平触发)。

is_on(self)

返回当前蜂鸣器工作状态。

on(self)

设置蜂鸣器开始工作。

off(self)

设置蜂鸣器停止工作。

beep(self, seconds)

设置蜂鸣器鸣叫 seconds 的时间。seconds 为毫秒时间。
beepAction(self, secs, sleepsecs, times)[/python]

设置蜂鸣器鸣叫 seconds 的时间,并间隔 sleepsecs 的时间再次鸣叫,如此周而复始 times 次。seconds 为毫秒时间。sleepsecs 为毫秒时间。times 为次数。

#让蜂鸣器一直响
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS

if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
        
        #让蜂鸣器一直响
        SAKS.buzzer.on()
        
    except KeyboardInterrupt:
        print("任务被终止了")
#让蜂鸣器停止响
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS

if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
        
        #让蜂鸣器一直响
        SAKS.buzzer.off()
        
    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()
        
        #让蜂鸣器一直响
        SAKS.buzzer.on()
        
        #获取蜂鸣器状态
        buzzer_status = SAKS.buzzer.is_on
        
        print("蜂鸣器响时,状态为:" + str(buzzer_status))
        
        time.sleep(5)
        
        #让蜂鸣器停止响
        SAKS.buzzer.off()
        
        #获取蜂鸣器状态
        buzzer_status = SAKS.buzzer.is_on
        
        print("蜂鸣器不响时,状态为:" + str(buzzer_status))
        
        time.sleep(5)
        
    except KeyboardInterrupt:
        print("任务被终止了")
#让蜂鸣器只响5s
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS

if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
        
        #让蜂鸣器只响5s
        SAKS.buzzer.beep(5)
        
    except KeyboardInterrupt:
        print("任务被终止了")
#播放出报警声音
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS

if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
        
        #让蜂鸣器一次响1s,两次两次间隔2s,共响5次
        SAKS.buzzer.beepAction(1, 2, 5)
        
    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:
            #让蜂鸣器一直响
            SAKS.buzzer.beepAction(0.01, 0.3, 2)
            time.sleep(1)
        
    except KeyboardInterrupt:
        print("任务被终止了")

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

返回课程目录

课程 gitee 地址:https://gitee.com/shirf_taste_raspi/shirf_serial_share