Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Tested on RPi 3B+ with the official 7" touch-screen
- # Confirmed running on LibreELEC 2.8.5 and can be added to .config/autostart.sh
- #
- import subprocess, time, sys, signal
- sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
- try:
- import RPi.GPIO as GPIO
- except ImportError:
- subprocess.call(['/usr/bin/kodi-send', '--action=InstallAddon(virtual.rpi-tools)'])
- sleep(10)
- import RPi.GPIO as GPIO
- # Define GPIO pins in use:
- # Button 1 (white cable) - Right most button on panel
- BUTTON1 = 7
- # Button 2 (yellow cable)
- BUTTON2 = 11
- # Button 3 (green cable)
- BUTTON3 = 12
- # Button 4 (blue cable)
- BUTTON4 = 13
- # Button 5 (brown cable) - Left most button on panel
- BUTTON5 = 15
- # Full path for LCD Brightness Control:
- LCDPATH = "/sys/devices/platform/rpi_backlight/backlight/rpi_backlight/brightness"
- class GpioButtons(object):
- """ Not so cleanly written class object to handle tactile buttons on RPi GPIO header """
- def __init__(self, volup, voldown, lcdup, lcddown, lcdtogg, lcdpath, lcddef=170):
- """ Init the required pin and LCD variables """
- # Configure which GPIO pin handles which function:
- self.volup = volup
- self.voldown = voldown
- self.lcdup = lcdup
- self.lcddown = lcddown
- self.lcdtogg = lcdtogg
- # Define the full path needed for the Britghness Control:
- self.lcdpath = lcdpath
- # Define the default brightness value
- self.lcddef = lcddef
- # To avoid weird behaviour, allow just one button press at any given time.
- self.buttonpressed = 0
- def start(self):
- """ Startup and keep running in background """
- self.setup()
- # Make a KILL signal trigger the Cleanup so the GPIO is cleaned up on kill.
- #signal.signal(signal.SIGTERM, self.cleanup())
- # Allow KeyboardInterrupt (crtl+c) to interrupt and clean the GPIO before exitting.
- try:
- while True:
- time.sleep(10)
- except KeyboardInterrupt:
- self.cleanup()
- def cleanup(self):
- """ Cleanup and exit """
- GPIO.remove_event_detect(self.volup)
- GPIO.remove_event_detect(self.voldown)
- GPIO.remove_event_detect(self.lcdup)
- GPIO.remove_event_detect(self.lcddown)
- GPIO.remove_event_detect(self.lcdtogg)
- GPIO.cleanup()
- sys.exit(0)
- def setup(self):
- """ Setup all GPIO Pins """
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(self.volup, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- GPIO.add_event_detect(self.volup, GPIO.FALLING, callback=self.volupcallback)
- GPIO.setup(self.voldown, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- GPIO.add_event_detect(self.voldown, GPIO.FALLING, callback=self.voldowncallback)
- GPIO.setup(self.lcdup, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- GPIO.add_event_detect(self.lcdup, GPIO.FALLING, callback=self.lcdupcallback)
- GPIO.setup(self.lcddown, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- GPIO.add_event_detect(self.lcddown, GPIO.FALLING, callback=self.lcddowncallback)
- GPIO.setup(self.lcdtogg, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- GPIO.add_event_detect(self.lcdtogg, GPIO.FALLING, callback=self.lcdtoggcallback)
- def volupcallback(self, pin):
- """ Action for Volume Up button trigger """
- if self.buttonpressed == 1:
- return
- self.buttonpressed = 1
- subprocess.call(['/usr/bin/kodi-send', '--action=VolumeUp'])
- time.sleep(0.3)
- self.buttonpressed = 0
- def voldowncallback(self, pin):
- """ Action for Volume down button trigger """
- if self.buttonpressed == 1:
- return
- self.buttonpressed = 1
- subprocess.call(['/usr/bin/kodi-send', '--action=VolumeDown'])
- time.sleep(0.3)
- self.buttonpressed = 0
- def lcdupcallback(self, pin):
- """ Action for LCD Brightness Up button trigger """
- if self.buttonpressed == 1:
- return
- self.buttonpressed = 1
- with open(self.lcdpath, 'r+') as brightness:
- cur_br = int(brightness.readline())
- if cur_br <= 245:
- new_br = cur_br + 10
- else:
- new_br = 255
- print("Changed brightness up from %s to %s" % (cur_br, new_br))
- brightness.write('%d' % new_br)
- time.sleep(0.3)
- self.buttonpressed = 0
- def lcddowncallback(self, pin):
- """ Action for LCD Brightness Down button trigger """
- if self.buttonpressed == 1:
- return
- self.buttonpressed = 1
- with open(self.lcdpath, 'r+') as brightness:
- cur_br = int(brightness.readline())
- if cur_br >= 10:
- new_br = cur_br - 10
- else:
- new_br = 0
- print("Changed brightness down from %s to %s" % (cur_br, new_br))
- brightness.write('%d' % new_br)
- time.sleep(0.3)
- self.buttonpressed = 0
- def lcdtoggcallback(self, pin):
- """ Action for LCD On/Off toggle button trigger """
- if self.buttonpressed == 1:
- return
- self.buttonpressed = 1
- with open(self.lcdpath, 'r+') as brightness:
- cur_br = int(brightness.readline())
- if cur_br <= 10:
- new_br = self.lcddef
- else:
- new_br = 0
- print("Toggled brightness from %s to %s" % (cur_br, new_br))
- brightness.write('%d' % new_br)
- time.sleep(0.3)
- self.buttonpressed = 0
- if __name__ == '__main__':
- monitor = GpioButtons(BUTTON1, BUTTON2, BUTTON3, BUTTON4, BUTTON5, LCDPATH)
- monitor.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement