#!/usr/bin/python # IgorplugUDP.py -- python transcoder for Igorplug-UDP to lirc # Dr. Frank Borkenhagen __version__ = "0.0.1" __author__ = "Dr. Frank Borkenhagen" __homepage__= "http://home.arcor.de/borkenhagen/IgorplugUDP" def __doc__(): print """ ================================================================= = Igorplug-UDP - transcodes IgorPlug-UDP packages into lircd = = UDP packages and sends to localhost:8765 = ================================================================= """ print "Version: ", __version__ from socket import * host = "192.168.1.255" #used network for Igorplug-UDP device port = 6668 #listen to UDP port for Igorplug UDP addr = (host,port) buf = 64 #max size of UDP package UDPsignature=[0x23,0x37,0x39,0x8C] #the 1st four bytes in Igor's UDP data on=1 off=0 debug=off highlowtoggle=[chr(0x80),chr(0x00)] #each UDP package will be parsed, validated, a 8 byte tuple will be returned #each byte stands for one bit def parseUDP(data): if not (data[0:4]==UDPsignature): return 0 #dump UDP package cnt=1 #locking coded data bit result=[] for i in data: if (cnt>4 and (not cnt%5)): if i==0x80: result.append(1) elif i==0x00: result.append(0) else: print "Unexpected Byte in one UDP package found", cnt+=1 return result if __name__ == '__main__': # Create socket and bind to address UDPSock = socket(AF_INET,SOCK_DGRAM) UDPSock.bind(addr) recording=0 while 1: data=[] #read one UDP package dataraw,addr = UDPSock.recvfrom(buf) for i in dataraw: data.append(ord(i)) #convert string to tuple of integers parsedUDP=parseUDP(data) #parse each Igor's UDP package if debug: print "ATMEGA48 sent:", parsedUDP, #check sort of packages if not parsedUDP: if debug: print "UDP package dumped" continue elif parsedUDP.count(0x00)==8: #UDP signature+40*0x00 -> start if debug: print "Start", recording=on highlowtoggle=[chr(0x80),chr(0x00)] send2lirc="" send2lirc += chr(0xFF) continue #goto read next UDP package elif parsedUDP.count(0x01)==8: #UDP signature+40*0xFF -> stop if debug: print "Stop", recording=off #one last space is required and one termination #Todo: check id 0x0F should be dynamic #send2lirc += chr(0x00)+chr(0x0F)+chr(0xFF) send2lirc += chr(0x00)+chr(0xFF)+chr(0xFF) if debug: print ; print for i in send2lirc: print hex(ord(i)), LircUDP = socket(AF_INET, SOCK_DGRAM) LircUDP.sendto(send2lirc ,("127.0.0.1", 8765)) LircUDP.close continue #goto read next UDP package if debug: print "Data ", if debug: if not recording: print "Header" if recording: if debug: print highlowtoggle, if debug: print parsedUDP cnt=Signallen=0 for i in parsedUDP: if i: Signallen += (2**cnt) cnt += 1 #Todo: check why the theoretical value does not wor for all #Signallen=int(Signallen//1.13) #1.1921 #adjust timing Signallen=int(Signallen//1.1921) #adjust timing send2lirc += highlowtoggle[0]+chr(Signallen) highlowtoggle=highlowtoggle[::-1] UDPSock.close() #EOF