Sunday, October 18, 2015

Python Banana Pi IR event handler


This code is from project: Make use of your Banana PI IR receiver with python!

ir.py

#!/usr/bin/python
from evdev import InputDevice, list_devices
from mpd import MPDClient
import socket
import os

def mpd_func(func_name):
   try:
      c = MPDClient()
      c.connect("localhost", 6600)
      getattr(c, func_name)()
      c.disconnect()
   except socket.error:
      print "Cannot connect to MPD"
   
def MPD_stop():
   mpd_func("stop")
   
def MPD_pause():
   mpd_func("pause")

def MPD_play():
   mpd_func("play")

def MPD_next():
   mpd_func("next")

def MPD_prev():
   mpd_func("previous")

def SYS_halt():
   os.system("halt") 
 
# function to keycode binding
fun_tbl = { 
  1   : MPD_stop,
  2   : MPD_pause,
  3   : MPD_next,
  4   : MPD_prev,
  15  : SYS_halt,
  223 : MPD_play,
}

for dev in [ InputDevice(fn) for fn in list_devices()]:
   if dev.name == "sunxi-ir":
      for event in dev.read_loop():
         if event.value == 1:
            try:
               fun_tbl[event.code]()
            except KeyError:
               print "Action for %d code is not defined" % (event.code)


No comments:

Post a Comment