Hi,
just saw this. I do not have a 100% reliable solution, but maybe some enhancements:
1) You can use "bounce_time" on our btn, which will do software debouncing.
2) I tried around with that topic for some time and ended up using a queue in which the events are written to and read again.
Check out my code below, I am trying to do basically the same stuff (with added horiztontal scrolling). Also take a look at the while loop - the way it is done it will close down properly and not hang on shutdown or so.
#!/usr/bin/env python
# In Libreelec, gpiozero is located as below, must be in path
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
# Import libraries
import xbmc
import Queue
# Try to import gpiozero
import gpiozero as gpz
#Create Queue
q = Queue.Queue()
#Monitor for checking for aborts
monitor = xbmc.Monitor()
#No buttons were held at start
gpz.Button.was_held = False
def rotary_dt_rising(): # rotary A event handler
if rotary_button.is_pressed:
if rotary_clk.is_active: # rotary A rising while A is active is a clockwise turn
q.put('Action(Right)')
else:
if rotary_clk.is_active:
q.put('Action(Down)')
def rotary_clk_rising(): # rotary B event handler
if rotary_button.is_pressed:
if rotary_dt.is_active:
q.put('Action(Left)')
else:
if rotary_dt.is_active:
q.put('Action(Up)') # rotary B rising while A is active is a clockwise turn
def rotary_button_held(rotary_button):
rotary_button.was_held = True
def rotary_button_release(rotary_button):
if not rotary_button.was_held:
q.put('Action(Select)')
rotary_button.was_held = False
def close_the_buttons(): # need to be closed so KODI can exit
rotary_dt.close()
rotary_clk.close()
rotary_button.close()
#Create elements for rotary switch
rotary_dt = gpz.DigitalInputDevice(27) # Rotary encoder rotary A connected to GPIO27
rotary_clk = gpz.DigitalInputDevice(22) # Rotary encoder rotary B connected to GPIO17
rotary_button = gpz.Button(17, bounce_time=0.01, hold_time=0.5) # Rotary button connected to GPIO22
#Display Start Notification
#xbmc.executebuiltin('Notification(Boombox Skript gestartet, abortRequested FALSE, 1000)')
while not monitor.abortRequested():
#Rotary
rotary_dt.when_activated = rotary_dt_rising # Register the event handler for rotary A
rotary_clk.when_activated = rotary_clk_rising # Register the event handler for rotary B
rotary_button.when_held = rotary_button_held
rotary_button.when_released = rotary_button_release
# Start queue loop
while not q.empty():
message = q.get()
xbmc.executebuiltin(message)
if monitor.waitForAbort(0.1):
break
close_the_buttons()
sys.exit()
Display More