#!/usr/bin/env python # # Script to receive commands from uir/irman device and issue actions for them # # (C) 2005 Nedko Arnaudov # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import serial import time import sys import os # unix device file of serial port where device is attached serial_device = '/dev/ttyS1' # how many times code should be received, before issuing command threshold = 2 # repeat period, in count of times code is received repeat_period = 3 # codes are for JVC RM-C71 commands = { '0067C0': {'description': 'mute'}, '019C': {'description': 'power'}, '01F8': {'description': '1'}, 'F8': {'description': '2'}, '0183C0': {'description': '3'}, '78': {'description': '4'}, '01C3C0': {'description': '5'}, '00C3C0': {'description': '6'}, '019E': {'description': '7'}, '38': {'description': '8'}, '01E3C0': {'description': '9'}, '0F': {'description': '0'}, '0FCCF0': {'description': 'red', 'type': 'system', 'command': 'mpc play'}, '3C66': {'description': 'green', 'type': 'system', 'command': 'mpc pause'}, '3E66': {'description': 'yellow', 'type': 'system', 'command': 'mpc stop'}, '3CCCF0': {'description': 'blue', 'type': 'system', 'command': 'mpc toggle'}, '0F3C': {'description': 'teletext'}, '01E670': {'description': 'P.MODE/OK'}, '0187C0': {'description': 'AV'}, '00E670': {'description': 'MENU'}, '0199F0': {'description': 'left/volume down', 'type': 'system', 'command': 'mpc volume -5'}, '01E7C0': {'description': 'up/next program', 'type': 'system', 'command': 'mpc next'}, 'CC': {'description': 'right/volume up', 'type': 'system', 'command': 'mpc volume +5'}, '30': {'description': 'down/previous program', 'type': 'system', 'command': 'mpc prev'}, }; def receive_cmd(): bytes = [] bytes.append(ord(serial_port.read())) bytes.append(ord(serial_port.read())) bytes.append(ord(serial_port.read())) bytes.append(ord(serial_port.read())) bytes.append(ord(serial_port.read())) bytes.append(ord(serial_port.read())) # strip traling zero bytes for i in [5,4,3,2,0]: if bytes[i] != 0: break del bytes[i] cmd = "" for byte in bytes: cmd += "%02X" % byte return cmd def print_cmd(cmd): if not commands.has_key(cmd): print "unknown: " + cmd return if commands[cmd].has_key('description'): print "\"%s\"" % commands[cmd]['description'] else: print commands[cmd] def execute_cmd(cmd, repeat): if repeat: print "repeat:", print_cmd(cmd) if cmd == "0738": raise "exit" if not commands.has_key(cmd): return if not commands[cmd].has_key('type'): return if commands[cmd]['type'] == 'system': print "Executing \"%s\"" % commands[cmd]['command'] os.system(commands[cmd]['command']) try: # Create serial port object print "Opening \"%s\" ..." % serial_device serial_port = serial.Serial(serial_device,9600,8) # Start Initialization print "Initializing device ..." serial_port.setDTR() serial_port.setRTS() time.sleep(1) serial_port.write("I") time.sleep(.5) serial_port.write("R") time.sleep(1) # Read device response print "Reading device response..." x = serial_port.read() y = serial_port.read() print "%c%c"%(x,y) if x != 'O' or y != 'K': print "Bad initialization response from device" sys.exit(0) repeated = 0 while 1: #loop forever cmd = receive_cmd(); if repeated != 0 and last == cmd: repeated += 1 else: repeated = 1 last = cmd if repeated == threshold: execute_cmd(cmd, False) elif repeated > threshold and ((repeated - threshold) % repeat_period) == 0: execute_cmd(cmd, True) except "exit": print "Exiting because IR command" sys.exit(0) except KeyboardInterrupt: print "Exiting because of keyboard interrupt" sys.exit(1)