Hye all,我正在使用覆盆子pi,一些按钮和一些python来构建照片亭。我不是程序员,但是到目前为止我已经糊里糊涂了。
现在,我的脚本如下所示。我正在尝试使用连接到Raspberry Pi的GPIO的按钮来触发按键来控制照片幻灯片。
目前,我有按钮工作(带去抖),当他们只打印“按钮1”等。但是当我为每个按钮的功能添加XTE线时,去抖动停止工作,我得到3-4次点击按下每个按钮。
任何人都知道如何让这个工作?或者,如果有不同的/更好的方法将GPIO信号转换为按键? (我尝试了一段时间的Uinput,但那不起作用)
提前致谢。
#!/usr/bin/env python
### IMPORTS ###
import os # Allows us to run console commands and programs
from time import sleep # Allows us to call the sleep function to slow down our loop
import datetime # Allows us to look at the time
import RPi.GPIO as GPIO # Allows us to call our GPIO pins and names it just GPIO
GPIO.setmode(GPIO.BCM) # Set's GPIO pins to BCM GPIO numbering
b1count = 1
b2count = 1
b3count = 1
### GPIO SETUP ###
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set input pin to use the internal pull down resistor
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set input pin to use the internal pull down resistor
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set input pin to use the internal pull down resistor
### BUTTON PRESS FUNCTIONS ###
def Input_1(channel):
os.system('xte "key q"') # Xte converting button Presses into Keystrokes.
global b1count # Imports global variable set above
now = datetime.datetime.now() # What time is it now?
print ' - Button 1 - %r - %r' % (b1count, now.strftime("%H:%M:%S.%f")) # Prints which button was pressed, how many times, and timestamps it
b1count = b1count+1 # Increments button count
def Input_2(channel):
os.system('xte "key h"') # Xte converting button Presses into Keystrokes.
global b2count # Imports global variable set above
now = datetime.datetime.now() # What time is it now?
print ' - Button 2 - %r - %r' % (b2count, now.strftime("%H:%M:%S.%f")) # Prints which button was pressed, how many times, and timestamps it
b2count = b2count+1 # Increments button count
def Input_3(channel):
os.system('xte "key v"') # Xte converting button Presses into Keystrokes.
global b3count # Imports global variable set above
now = datetime.datetime.now() # What time is it now?
print ' - Button 3 - %r - %r' % (b3count, now.strftime("%H:%M:%S.%f")) # Prints which button was pressed, how many times, and timestamps it
b3count = b3count+1 # Increments button count
### BUTTON PRESS DETECTION ###
### Does a Callback to the appropriate Input function. Also debounces to prevent clicking the button multiple times a second.
GPIO.add_event_detect(23, GPIO.FALLING, callback=Input_1, bouncetime=1000) # Waiting for Button 1 to be pressed.
GPIO.add_event_detect(24, GPIO.FALLING, callback=Input_2, bouncetime=1000) # Waiting for Button 2 to be pressed.
GPIO.add_event_detect(25, GPIO.FALLING, callback=Input_3, bouncetime=1000) # Waiting for Button 3 to be pressed.
### FEH -D - Slideshow -Z - Zooms -Y Hides Pointer -F Fullscreen -r loads all images in the following directory###
os.system('feh -D 0.25 -Z -Y -F -r /home/pi/Desktop/Pics/') # Runs Feh Image Viewer with some command line arguments.
### Starts a neverending loop otherwise the script will just quit.
while True:
print "Waiting for input." # Insert Random Loop Junk
sleep(60); # Sleeps for a minute to save CPU cycles. Any interrupt will break this.
GPIO.cleanup()