本文整理汇总了Python中gpiozero.Button.when_held方法的典型用法代码示例。如果您正苦于以下问题:Python Button.when_held方法的具体用法?Python Button.when_held怎么用?Python Button.when_held使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gpiozero.Button
的用法示例。
在下文中一共展示了Button.when_held方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: shutdown
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_held [as 别名]
from gpiozero import Button
from subprocess import check_call
from signal import pause
def shutdown():
check_call(['sudo', 'poweroff'])
shutdown_btn = Button(17, hold_time=2)
shutdown_btn.when_held = shutdown
pause()
示例2: check_call
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_held [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()
示例3: main
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_held [as 别名]
def main():
global page
global sd_image_version
global r
# Initialise some redis variables
r.set("gsm:active", 0)
r.set("wlan:active", 0)
r.set("eth:active", 0)
# First set up logging
atexit.register(logging.shutdown)
if not uselogfile:
loghandler = logging.StreamHandler()
else:
logfile = "/var/log/emonpilcd/emonpilcd.log"
print("emonPiLCD logging to:", logfile)
loghandler = logging.handlers.RotatingFileHandler(logfile,
mode='a',
maxBytes=1000 * 1024,
backupCount=1,
)
loghandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
logger.addHandler(loghandler)
logger.setLevel(logging.INFO)
logger.info("Starting emonPiLCD V" + version)
# Now check the LCD and initialise the object
global lcd
lcd = LCD(logger)
lcd.backlight = 1
# ------------------------------------------------------------------------------------
# Discover & display emonPi SD card image version
# ------------------------------------------------------------------------------------
sd_card_image = subprocess.call("ls /boot | grep emonSD", shell=True)
if not sd_card_image: # if emonSD file exists
sd_image_version = subprocess.check_output("ls /boot | grep emonSD", shell=True)
else:
sd_card_image = subprocess.call("ls /boot | grep emonpi", shell=True)
if not sd_card_image:
sd_image_version = subprocess.check_output("ls /boot | grep emonpi", shell=True)
else:
sd_image_version = "N/A "
sd_image_version = sd_image_version.rstrip()
lcd[0] = "emonPi Build:"
lcd[1] = sd_image_version
logger.info("SD card image build version: " + sd_image_version)
# Set up the buttons and install handlers
# emonPi LCD push button Pin 16 GPIO 23
# Uses gpiozero library to handle short and long press https://gpiozero.readthedocs.io/en/stable/api_input.html?highlight=button
# push_btn = Button(23, pull_up=False, hold_time=5, bounce_time=0.1)
# No bounce time increases response time but may result in switch bouncing...
logger.info("Attaching push button interrupt...")
try:
push_btn = Button(23, pull_up=False, hold_time=5)
push_btn.when_pressed = buttonPress
push_btn.when_held = buttonPressLong
except:
logger.error("Failed to attach LCD push button interrupt...")
logger.info("Attaching shutdown button interrupt...")
try:
shut_btn = Button(17, pull_up=False, hold_time=5)
shut_btn.when_pressed = preShutdown
shut_btn.when_held = shutdown
except:
logger.error("Failed to attach shutdown button interrupt...")
logger.info("Connecting to redis server...")
# We wait here until redis has successfully started up
while True:
try:
r.client_list()
break
except redis.ConnectionError:
logger.error("waiting for redis-server to start...")
time.sleep(1.0)
logger.info("Connected to redis")
logger.info("Connecting to MQTT Server: " + mqtt_host + " on port: " + str(mqtt_port) + " with user: " + mqtt_user)
def on_message(client, userdata, msg):
if mqtt_feed1_topic in msg.topic:
r.set("feed1", msg.payload)
if mqtt_feed2_topic in msg.topic:
r.set("feed2", msg.payload)
if mqtt_emonpi_topic in msg.topic:
r.set("basedata", msg.payload)
def on_connect(client, userdata, flags, rc):
#.........这里部分代码省略.........
示例4: btn_held
# 需要导入模块: from gpiozero import Button [as 别名]
# 或者: from gpiozero.Button import when_held [as 别名]
## simple script to monitor a gpio pin with attached button
## and shutdown pi when pressed for > 2 seconds
## must be run as root or via sudo
# imports
from gpiozero import Button
from os import system
from time import sleep
# config
PIN = 17
PULL_UP = True
HOLD_TIME = 2
# functions
def btn_held():
# respond to long press on button
system("poweroff")
# do stuff
if __name__ == '__main__':
pbutton = Button(pin=PIN,
pull_up=PULL_UP,
hold_time=HOLD_TIME,
hold_repeat=False)
pbutton.when_held = btn_held
while True:
sleep(1)