Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple Python script to be called from Unified Remote with a particular remote code name as an extra named Args. The script receives the remote code name (string), looks the name up in the 'codes' dict to determine the binary data to send, and finally sends the binary data to the Arduino over USB.
#!python.exe
'''This script receives the remote code name (string), looks the name up in the 'codes' dict to determine the binary data to send, and finally sends the binary data to the Arduino over USB.'''
import site
import d2xx as usb
import struct
import binascii
import time
import sys
import re
pPredata = re.compile(r'pre_data.*(0x[0-9]+)\n')
pCodes = re.compile(r'\s([A-Z|_|\+|\-]+).*0x([0-9a-fA-F]+)')
preVal = ""
codes = {}
config = 'c:\irRemote\lircd.conf'
def parse(conf):
'''Parses the lircd config and stores the data in the codes global dict'''
global codes
global preVal
preVal = pPredata.findall(conf)[0]
bc = re.search('begin codes\n', conf)
ec = re.search('end codes', conf)
codeRegion = conf[bc.end():ec.start()]
codeMatches = pCodes.findall(codeRegion)
for match in codeMatches:
codeName, codeVal = match
codes[codeName] = preVal + codeVal
def sendIrPacket(h, data):
print "data = " + str(data)
try:
k = struct.pack('<Q', long(data,16))
h.write(k)
except Exception as e:
print "exception occurred: " + e.strerror
def setup():
f = open(config, 'r')
conf = f.read()
f.close()
parse(conf)
h = usb.open(0)
h.setBaudRate(9600)
return h
def main():
try:
h = setup()
code = codes[sys.argv[1]]
if code.startswith('0x'):
sendIrPacket(h, code)
finally:
pass
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment