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
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
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
62 fOut
.write('\n') # Final EOL
65 def __init__(self
,sFileName
):
67 self
.sRemoteName
=sFileName
.split('.')[:1][0] # Name of the remote
68 self
.sCodes
=[] # Codes contained in file
69 self
.lGap
=0 # Final Gap
74 if sLine
=='' or sLine
.strip()=='': # EOF?
77 [sCodeName
,sHexCodes
]=sLine
.split(':')
79 finalgap
=seq
.AnalyzeCode(sCodeName
,sHexCodes
)
80 if finalgap
>self
.lGap
:
83 self
.sCodes
.append(seq
)
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')
101 f
.write('\t\tend raw_codes\n')
102 f
.write('end remote\n')
110 print "Pronto codes converter to lircd.conf format (version 1.00)"
112 print "Usage: pronto2lirc.py inputfile.hex "
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 ..."
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"
123 p
=HexParser(sys
.argv
[1])
124 p
.WriteLIRCConf('lircd.conf')