本文整理汇总了Python中gpiozero.Button.when_pressed方法的典型用法代码示例。如果您正苦于以下问题:Python Button.when_pressed方法的具体用法?Python Button.when_pressed怎么用?Python Button.when_pressed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gpiozero.Button
的用法示例。
在下文中一共展示了Button.when_pressed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: time
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
global beep_start
global pause_start
global pause_stop
beep_start = time()
pause_stop = time()
pause_length = pause_stop - pause_start
print("Start: %.1f" % (beep_start))
print("Pause length: %.3f" % (pause_length))
buzzer.on()
def stop_buzzing():
global beep_start
global pause_start
beep_stop = time()
pause_start = time()
beep_length = beep_stop - beep_start
print("Length: %.1f" % (beep_length))
if beep_length > 0 and beep_length < 0.3:
print(".")
#urllib2.urlopen("http://morsecode.local/send.php?symbol=dot").read()
elif beep_length >= 0.3:
print("-")
#urllib2.urlopen("http://morsecode.local/send.php?symbol=dash").read()
buzzer.off()
while True:
button.when_pressed = start_buzzing
button.when_released = stop_buzzing
示例2: str
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
wiringpi.wiringPiSetup()
wiringpi.pinMode(led, 2)
wiringpi.pwmWrite(led, comboStates['brightness'])
os.system("amixer sset -q 'PCM' " + str(comboStates['volume']) + "%")
if comboStates['wifi'] == 1:
os.system("sudo rfkill unblock wifi")
else:
os.system("sudo rfkill block wifi")
if comboStates['bluetooth'] == 1:
os.system("sudo rfkill unblock bluetooth")
else:
os.system("sudo rfkill block bluetooth")
except:
writeData(statePath)
wiringpi.wiringPiSetup()
wiringpi.pinMode(led, 2)
wiringpi.pwmWrite(led, comboStates['brightness'])
os.system("amixer sset -q 'PCM' " + str(comboStates['volume']) + "%")
if comboStates['wifi'] == 1:
os.system("sudo rfkill unblock wifi")
else:
os.system("sudo rfkill block wifi")
if comboStates['bluetooth'] == 1:
os.system("sudo rfkill unblock bluetooth")
else:
os.system("sudo rfkill block bluetooth")
# Interrupt
functionBtn.when_pressed = checkFunction
pause()
示例3: Button
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
import os
from gpiozero import Button
from signal import pause
button = Button(4,pull_up=False)
def pressed(button):
print("Pin %s pressed. The system is going to try shutdown now!" % button.pin)
os.system("shutdown now -h")
button.when_pressed = pressed
pause()
示例4: MotionSensor
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
import pygame.mixer
from pygame.mixer import Sound
import time
from gpiozero import MotionSensor
from gpiozero import Button
from signal import pause
pygame.mixer.init()
pir = MotionSensor(4)
button = Button(2)
drum = Sound("test/ka_ru_na_2.wav")
while True:
button.when_pressed = drum.play
time.sleep(4)
if pir.motion_detected:
drum.play()
print("Motion Detect!: !")
time.sleep(4)
示例5: LED
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
from gpiozero import LED, Button
from time import sleep
from random import uniform
import sys
led = LED(4)
right_button = Button(15)
left_button = Button(14)
left_name = input('left player name is ')
right_name = input('right player name is ')
led.on()
sleep(uniform(1,2))
led.off()
def pressed(button):
if button.pin.number == 14:
print(left_name + ' won the game')
else:
print(right_name + ' won the game')
sys.exit()
right_button.when_pressed=pressed
left_button.when_pressed=pressed
示例6: check_call
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
omx11 = udp_client.UDPClient("192.168.1.211", 9998)
omx12 = udp_client.UDPClient("192.168.1.212", 9998)
msg2 = osc_message_builder.OscMessageBuilder(address="/omxplayer")
msg2.add_arg(1)
msg2.add_arg(3)
msg2 = msg2.build()
omx1.send(msg2)
omx2.send(msg2)
omx3.send(msg2)
omx4.send(msg2)
omx5.send(msg2)
omx6.send(msg2)
omx7.send(msg2)
omx8.send(msg2)
omx9.send(msg2)
omx10.send(msg2)
omx11.send(msg2)
omx12.send(msg2)
check_call(['sudo', 'reboot'])
def btn_pressed():
_thread.start_new_thread(reboot_process, (1, ""))
reboot_btn = Button(27, hold_time=3)
reboot_btn.when_held = poweroff
reboot_btn.when_pressed = btn_pressed
pause()
示例7: PiGPIOFactory
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
from gpiozero import Button, Robot
from gpiozero.pins.pigpio import PiGPIOFactory
from signal import pause
factory = PiGPIOFactory(host='192.168.1.17')
robot = Robot(left=(4, 14), right=(17, 18), pin_factory=factory) # remote pins
# local buttons
left = Button(26)
right = Button(16)
fw = Button(21)
bw = Button(20)
fw.when_pressed = robot.forward
fw.when_released = robot.stop
left.when_pressed = robot.left
left.when_released = robot.stop
right.when_pressed = robot.right
right.when_released = robot.stop
bw.when_pressed = robot.backward
bw.when_released = robot.stop
pause()
示例8: LED
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
from gpiozero import LED, Button
from time import sleep
from random import uniform
led = LED(4)
blue_button = Button(23)
red_button = Button(24)
game_count = input('How many games do you want to play?')
blue_score = 0
red_score = 0
def pressed(button):
if button.pin.number == 23:
global blue_score
blue_score = blue_score + 1
print('Blue Button Won The Game ' + str(blue_score) + ' Times!')
else:
global red_score
red_score = red_score + 1
print('Red Button Won The Game ' + str(red_score) + ' Times!')
for x in xrange(game_count):
led.on()
sleep(uniform(2,5))
led.off()
blue_button.when_pressed = pressed
red_button.when_pressed = pressed
sleep(2)
示例9: Color
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
shutdown = False
### Styling
## Font
camera.annotate_background = Color('black')
camera.annotate_text_size = 16
camera.vflip = True
start = time.time()
#camera.start_preview()
filename = video_dir+'dashcam_'+datetime.now().strftime('%Y-%m-%d_%H.%M.%S')+'.h264'
videos = deque([filename])
camera.start_recording(filename,format='h264',resize=(vid_width,vid_height))
rec_button = Button(4)
stop_button = Button(25)
rec_button.when_pressed = recbuttonpress
stop_button.when_pressed = stopbuttonpress
led = RGBLED(27,8,17)
while not camera.closed:
led.color = (1,0,1)
if not shutdown:
end = time.time()
seconds = end - start
camera.annotate_text = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if save == True:
save = False
print "Saving video"
filename = video_dir+'dashcam_'+datetime.now().strftime('%Y-%m-%d_%H.%M.%S')+'.h264'
camera.split_recording(filename)
示例10: set_volume
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
note_additor = new_additor
# Select the first sound font
set_volume()
set_instrument()
set_note_additor()
# Play a tone so we know that we've started
startup()
# Set-up main loop watchers
main_loop_stopped = 0
stop_main_loop = 0
print("Initialising rear buttons")
button_reset.when_pressed = reset
button_shutdown.when_pressed = shutdown
# Assign actions to when_pressed for each button
thumb_bottom.when_pressed = thumb_bottom_start
thumb_bottom.when_released = thumb_bottom_stop
thumb_right.when_pressed = thumb_right_start
thumb_right.when_released = thumb_right_stop
thumb_top.when_pressed = thumb_top_start
thumb_top.when_released = thumb_top_stop
index_finger.when_pressed = index_finger_start
index_finger.when_released = index_finger_stop
middle_finger.when_pressed = middle_finger_start
middle_finger.when_released = middle_finger_stop
ring_finger.when_pressed = ring_finger_start
ring_finger.when_released = ring_finger_stop
示例11: PiCamera
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
from picamera import PiCamera
from datetime import datetime
from time import sleep
from gpiozero import Button
camera = PiCamera()
button = Button(14)
now = datetime.now()
filename = ''
def take_photo():
global filename
filename = "{0:%Y}-{0:%m}-{0:%d}-{0:%H}-{0:%M}-{0:%S}.png".format(now)
camera.start_preview(alpha=190)
sleep(1)
camera.capture("/home/pi/{0}".format(filename))
camera.stop_preview()
button.when_pressed = take_photo
示例12: int
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
day = int(time.strftime("%j")) #get the day of the year [0,366]
playlist = ( int(day) + offset ) % 2
if playlist == 0:
if day > 330: #kiss me baby, it's christmas time
maxvol = 80
system("mpg321 --random " + mediapath + "christmas/* &")
else:
maxvol = 90
system("mpg321 --random " + mediapath + "music/* &")
elif playlist == 1:
maxvol = 100
system("mpg321 --random " + mediapath + "waves/* &")
else:
pass
rampvol(0.25,0,maxvol)
sleep(2700)
rampvol(10,maxvol,0,-1)
sys.exit("Finished playing naptime music")
def rampvol(speed = 0.25, start = 0, stop = 100, step = 1):
vol = start
while vol != stop:
vol = vol + step
system("amixer -q sset PCM,0 " + str(vol) + "%")
sleep(speed)
#main program execution
try:
button.when_pressed = playnap() #what to do when/if the button is pressed
pause(10800) #have a three hour window during which the button could be pressed
except:
pass
示例13: LED
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
from signal import pause
led = LED(17)
button = Button(2)
#button.when_pressed = led.on
#button.when_released = led.off
p_exit = 0;
def led_exit():
global p_exit
p_exit = 1
button.when_pressed = led_exit
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN)
acoustic_on = 0
def acoustic_detect():
global acoustic_on
global p_exit
while True:
if (p_exit):
break
acoustic_on = 0
if (GPIO.input(18) == 0):
acoustic_on = 1
示例14: len
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
i += 1
effect = effects[i % len(effects)]
camera.image_effect = effect
def capture():
global last_capture
dt = datetime.now().isoformat()
filename = '/home/pi/photobooth/{}.jpg'.format(dt)
camera.capture(filename)
last_capture = filename
def tweet(message, img):
with open(img, 'rb') as photo:
twitter.update_status_with_media(status=message, media=photo)
left.when_pressed = change_effect
while True:
camera.start_preview()
camera.annotate_text = "Press left button to change the effect"
sleep(2)
camera.annotate_text = "Press right button to take a picture"
sleep(2)
camera.annotate_text = None
right.wait_for_press()
capture()
camera.annotate_text = "Press right button to tweet the photo"
right.wait_for_press()
camera.stop_preview()
handle = input("Enter your Twitter handle: @")
message = "Welcome to @Raspberry_Pi Towers, @{}".format(handle)
示例15: Sound
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_pressed [as 别名]
from pygame.mixer import Sound
from gpiozero import Button, LED
from signal import pause
from time import sleep
pygame.mixer.init()
good = Sound("/home/pi/crackerjoke/goodjoke.wav")
bad = Sound("/home/pi/crackerjoke/badjoke.wav")
goodbutton = Button(21)
badbutton = Button(24)
red = LED(8)
green = LED(7)
while True:
red.on()
green.on()
goodbutton.when_pressed = good.play
badbutton.when_pressed = bad.play
pause()
#################################################################
#If you’d like the code to run on reboot, allowing you to detach yourself from the monitor, keyboard, and mouse, open a terminal window and type:
nano ~/.config/lxsession/LXDE-pi/autostart
#At the bottom of the file, add:
@python /home/pi/crackerjoke/crackerjoke.py
#Save and reboot.