Advertisement
Guest User

gpio-buttons.py

a guest
Jun 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.70 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Tested on RPi 3B+ with the official 7" touch-screen
  3. # Confirmed running on LibreELEC 2.8.5 and can be added to .config/autostart.sh
  4. #
  5. import subprocess, time, sys, signal
  6. sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
  7. try:
  8.   import RPi.GPIO as GPIO
  9. except ImportError:
  10.   subprocess.call(['/usr/bin/kodi-send', '--action=InstallAddon(virtual.rpi-tools)'])
  11.   sleep(10)
  12.   import RPi.GPIO as GPIO
  13.  
  14. # Define GPIO pins in use:
  15. # Button 1 (white cable) - Right most button on panel
  16. BUTTON1 = 7
  17. # Button 2 (yellow cable)
  18. BUTTON2 = 11
  19. # Button 3 (green cable)
  20. BUTTON3 = 12
  21. # Button 4 (blue cable)
  22. BUTTON4 = 13
  23. # Button 5 (brown cable) - Left most button on panel
  24. BUTTON5 = 15
  25. # Full path for LCD Brightness Control:
  26. LCDPATH = "/sys/devices/platform/rpi_backlight/backlight/rpi_backlight/brightness"
  27.  
  28.  
  29. class GpioButtons(object):
  30.   """ Not so cleanly written class object to handle tactile buttons on RPi GPIO header """
  31.  
  32.   def __init__(self, volup, voldown, lcdup, lcddown, lcdtogg, lcdpath, lcddef=170):
  33.     """ Init the required pin and LCD variables """
  34.     # Configure which GPIO pin handles which function:
  35.     self.volup = volup
  36.     self.voldown = voldown
  37.     self.lcdup = lcdup
  38.     self.lcddown = lcddown
  39.     self.lcdtogg = lcdtogg
  40.     # Define the full path needed for the Britghness Control:
  41.     self.lcdpath = lcdpath
  42.     # Define the default brightness value
  43.     self.lcddef = lcddef
  44.     # To avoid weird behaviour, allow just one button press at any given time.
  45.     self.buttonpressed = 0
  46.  
  47.   def start(self):
  48.     """ Startup and keep running in background """
  49.     self.setup()
  50.     # Make a KILL signal trigger the Cleanup so the GPIO is cleaned up on kill.
  51.     #signal.signal(signal.SIGTERM, self.cleanup())
  52.     # Allow KeyboardInterrupt (crtl+c) to interrupt and clean the GPIO before exitting.
  53.     try:
  54.       while True:
  55.         time.sleep(10)
  56.     except KeyboardInterrupt:
  57.       self.cleanup()
  58.  
  59.   def cleanup(self):
  60.     """ Cleanup and exit """
  61.     GPIO.remove_event_detect(self.volup)
  62.     GPIO.remove_event_detect(self.voldown)
  63.     GPIO.remove_event_detect(self.lcdup)
  64.     GPIO.remove_event_detect(self.lcddown)
  65.     GPIO.remove_event_detect(self.lcdtogg)
  66.     GPIO.cleanup()
  67.     sys.exit(0)
  68.  
  69.   def setup(self):
  70.     """ Setup all GPIO Pins """
  71.     GPIO.setmode(GPIO.BOARD)
  72.     GPIO.setup(self.volup, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  73.     GPIO.add_event_detect(self.volup, GPIO.FALLING, callback=self.volupcallback)
  74.     GPIO.setup(self.voldown, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)                          
  75.     GPIO.add_event_detect(self.voldown, GPIO.FALLING, callback=self.voldowncallback)  
  76.     GPIO.setup(self.lcdup, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)                          
  77.     GPIO.add_event_detect(self.lcdup, GPIO.FALLING, callback=self.lcdupcallback)  
  78.     GPIO.setup(self.lcddown, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)                          
  79.     GPIO.add_event_detect(self.lcddown, GPIO.FALLING, callback=self.lcddowncallback)  
  80.     GPIO.setup(self.lcdtogg, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)                          
  81.     GPIO.add_event_detect(self.lcdtogg, GPIO.FALLING, callback=self.lcdtoggcallback)  
  82.  
  83.  
  84.   def volupcallback(self, pin):
  85.     """ Action for Volume Up button trigger """
  86.     if self.buttonpressed == 1:
  87.       return
  88.     self.buttonpressed = 1
  89.     subprocess.call(['/usr/bin/kodi-send', '--action=VolumeUp'])
  90.     time.sleep(0.3)
  91.     self.buttonpressed = 0
  92.  
  93.   def voldowncallback(self, pin):                                                            
  94.     """ Action for Volume down button trigger """                                          
  95.     if self.buttonpressed == 1:                                                        
  96.       return                                                                          
  97.     self.buttonpressed = 1                                                            
  98.     subprocess.call(['/usr/bin/kodi-send', '--action=VolumeDown'])                        
  99.     time.sleep(0.3)                                                                    
  100.     self.buttonpressed = 0
  101.  
  102.   def lcdupcallback(self, pin):                                                            
  103.     """ Action for LCD Brightness Up button trigger """                                          
  104.     if self.buttonpressed == 1:                                                        
  105.       return                                                                          
  106.     self.buttonpressed = 1                                                            
  107.     with open(self.lcdpath, 'r+') as brightness:
  108.       cur_br = int(brightness.readline())
  109.       if cur_br <= 245:
  110.         new_br = cur_br + 10
  111.       else:
  112.         new_br = 255
  113.       print("Changed brightness up from %s to %s" % (cur_br, new_br))
  114.       brightness.write('%d' % new_br)
  115.     time.sleep(0.3)                                                                    
  116.     self.buttonpressed = 0
  117.  
  118.   def lcddowncallback(self, pin):                                                            
  119.     """ Action for LCD Brightness Down button trigger """                                          
  120.     if self.buttonpressed == 1:                                                        
  121.       return                                                                          
  122.     self.buttonpressed = 1                                                            
  123.     with open(self.lcdpath, 'r+') as brightness:                                          
  124.       cur_br = int(brightness.readline())                                                
  125.       if cur_br >= 10:                                                                  
  126.         new_br = cur_br - 10                                                              
  127.       else:                                                                              
  128.         new_br = 0                                                                      
  129.       print("Changed brightness down from %s to %s" % (cur_br, new_br))
  130.       brightness.write('%d' % new_br)                                                                                                  
  131.     time.sleep(0.3)                                                                    
  132.     self.buttonpressed = 0
  133.  
  134.   def lcdtoggcallback(self, pin):                                                            
  135.     """ Action for LCD On/Off toggle button trigger """                                          
  136.     if self.buttonpressed == 1:                                                        
  137.       return                                                                          
  138.     self.buttonpressed = 1                                                            
  139.     with open(self.lcdpath, 'r+') as brightness:                                          
  140.       cur_br = int(brightness.readline())                                                
  141.       if cur_br <= 10:                                                                  
  142.         new_br = self.lcddef                                                              
  143.       else:                                                                              
  144.         new_br = 0                                                                      
  145.       print("Toggled brightness from %s to %s" % (cur_br, new_br))
  146.       brightness.write('%d' % new_br)                                                                                                  
  147.     time.sleep(0.3)                                                                    
  148.     self.buttonpressed = 0
  149.  
  150. if __name__ == '__main__':
  151.   monitor = GpioButtons(BUTTON1, BUTTON2, BUTTON3, BUTTON4, BUTTON5, LCDPATH)
  152.   monitor.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement