Added lirc.
[irreco.git] / lirc-0.8.4a / tools / pronto2lirc.py
blob6afe568a546e8830947bc295c235dbb1f4765219
2 # A tool for converting Pronto format hex codes to lircd.conf format
4 # Copyright by Olavi Akerman <olavi.akerman@gmail.com>
6 # pronto2lirc is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 class CodeSequence: # Handles codesequences parsing and conversion
23 def ProcessPreamble(self,sPreamble):
24 if sPreamble[0]<>"0000":
25 raise "Formats other than starting with 0000 are not supported!"
27 self.dIRFrequency=1000000/(long(sPreamble[1],16) * 0.241246) # Frequency of the IR carrier in Khz
29 self.lOnceSequenceLength=long(sPreamble[2],16) # No of pulses that is sent once when button is pressed
30 self.lRepeatableSequenceLength=long(sPreamble[3],16) # No of pulses that are repeatable while button pressed
32 def CreatePulses(self,sItems):
33 self.dPulseWidths=[] # Table of Pulse widths. Length is repsented in microseconds
35 for i in sItems:
36 self.dPulseWidths.append(1000000*long(i,16)/self.dIRFrequency) # Convert pulse widths to uS
38 if len(self.dPulseWidths)<>2*(self.lOnceSequenceLength+self.lRepeatableSequenceLength):
39 raise "Number of actual codes does not match the header information!"
41 def AnalyzeCode(self,sCodeName,sHexCodes):
43 sHexTable=sHexCodes.split()
44 self.sCodeName=sCodeName.rstrip() # Name of the Code associated with code sequence
46 self.ProcessPreamble(sHexTable[:4]) # First four sequences make up Preamble
47 self.CreatePulses(sHexTable[4:]) # The rest are OnceSequence + RepeatableSequence
48 return self.dPulseWidths[-1] # Final gap=last off signal length
50 def WriteCodeSection(self,fOut):
51 fOut.write('\n\t\t\tname '+self.sCodeName+'\n')
52 for i in range(len(self.dPulseWidths)-1): # Do not write the last signal as lircd.conf
53 # does not contain last off signal length
54 if (i%6) ==0:
55 fOut.write('\t\t\t\t')
57 fOut.write('%d ' % round(self.dPulseWidths[i]))
59 if (i+1)%6 ==0: # Group codes as six per line
60 fOut.write('\n')
62 fOut.write('\n') # Final EOL
64 class HexParser:
65 def __init__(self,sFileName):
66 f=open(sFileName,'r')
67 self.sRemoteName=sFileName.split('.')[:1][0] # Name of the remote
68 self.sCodes=[] # Codes contained in file
69 self.lGap=0 # Final Gap
71 while True:
72 sLine=f.readline()
74 if sLine=='' or sLine.strip()=='': # EOF?
75 break
77 [sCodeName,sHexCodes]=sLine.split(':')
78 seq=CodeSequence()
79 finalgap=seq.AnalyzeCode(sCodeName,sHexCodes)
80 if finalgap>self.lGap:
81 self.lGap=finalgap
83 self.sCodes.append(seq)
85 f.close()
87 def WriteLIRCConf(self,sOutFileName):
88 f=open(sOutFileName,'w')
90 f.write('begin remote\n')
91 f.write('\tname\t'+self.sRemoteName+'\n')
92 f.write('\tflags\tRAW_CODES\n')
93 f.write('\teps\t30\n')
94 f.write('\taeps\t100\n')
95 f.write('\tgap\t%d\n' % self.lGap )
96 f.write('\t\tbegin raw_codes\n')
98 for i in self.sCodes:
99 i.WriteCodeSection(f)
101 f.write('\t\tend raw_codes\n')
102 f.write('end remote\n')
103 f.close()
105 # Main
107 import sys
109 if len(sys.argv)<>2:
110 print "Pronto codes converter to lircd.conf format (version 1.00)"
111 print
112 print "Usage: pronto2lirc.py inputfile.hex "
113 print
114 print "Input file must be in format where each line contains all codes"
115 print " associated with a button like:"
116 print " Button1:0000 00ac 000b 00de ..."
117 print
118 print "Result: lircd.conf file is written to the current directory"
119 print " containing all the Pronto codes extracted from"
120 print " the input file"
121 print
122 else:
123 p=HexParser(sys.argv[1])
124 p.WriteLIRCConf('lircd.conf')