So I'm working on controlling the menu with a ky-040 rotary controller and a few buttons, new to python I somehow hacked this code together that allows me to scroll up and down and click select.
The code is brute forced together and I know it could be optimized, it also has a bug where if you turn the knob too quickly after clicking it fires the select event twice. Being new to Python I'm struggling to understand how "print" the os.system "ssh function thingy" into the "while True:" part, ha I have alot to learn about python. But I think that may make it run smoother. So far its interesting and this encoder working is pretty sweet, but im not sure how optimized this is and how it will effect the pi's cpu etc. Next I need to figure out how to make different events fire on long presses and convert this to a volume control, but I'm making progress, any help on how to fix the double firing of events on rapid wheel changes after click or how to improve this would be appreciated.
The only real part of this code I had to figure out was this
os.system("kodi-send --action='Up'")
But I still dont really understand the whole import kodi stuff or where that comes from...
#!/usr/bin/python
import sys
sys.path.append ( "/storage/.kodi/addons/virtual.rpi-tools/lib" )
import RPi.GPIO as GPIO
import os
import time
RoAPin = 17 # pin11
RoBPin = 18 # pin12
BtnPin = 13 # Button Pin
globalCounter = 0
flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(RoAPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(RoBPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=btnISR, bouncetime=10)
def rotaryDeal():
global flag
global Last_RoB_Status
global Current_RoB_Status
global globalCounter
Last_RoB_Status = GPIO.input(RoBPin)
while(not GPIO.input(RoAPin)):
Current_RoB_Status = GPIO.input(RoBPin)
flag = 1
if flag == 1:
flag = 0
if (Last_RoB_Status == 0) and (Current_RoB_Status == 1):
globalCounter = globalCounter - 1
os.system("kodi-send --action='Up'")
if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
globalCounter = globalCounter + 1
os.system("kodi-send --action='Down'")
def btnISR(channel):
os.system("kodi-send --action='Select'")
global globalCounter
globalCounter = 0
def loop():
global globalCounter
tmp = 0 # Rotary Temperary
while True:
time.sleep(.01)
rotaryDeal()
if tmp != globalCounter:
print 'globalCounter = %d' % globalCounter
tmp = globalCounter
def destroy():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
Display More