2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
6 ## This program 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 import sigrokdecode as srd
24 class SamplerateError(Exception):
27 class Decoder(srd.Decoder):
32 desc = 'RC-5 infrared remote control protocol.'
37 {'id': 'ir', 'name': 'IR', 'desc': 'IR data line'},
40 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-low',
41 'values': ('active-low', 'active-high')},
42 {'id': 'protocol', 'desc': 'Protocol type', 'default': 'standard',
43 'values': ('standard', 'extended')},
47 ('startbit1', 'Startbit 1'),
48 ('startbit2', 'Startbit 2'),
49 ('togglebit-0', 'Toggle bit 0'),
50 ('togglebit-1', 'Toggle bit 1'),
51 ('address', 'Address'),
52 ('command', 'Command'),
55 ('bits', 'Bits', (0,)),
56 ('fields', 'Fields', (1, 2, 3, 4, 5, 6)),
59 def __init__(self, **kwargs):
60 self.samplerate = None
62 self.edges, self.bits, self.bits_ss_es = [], [], []
66 self.out_ann = self.register(srd.OUTPUT_ANN)
67 self.old_ir = 1 if self.options['polarity'] == 'active-low' else 0
69 def metadata(self, key, value):
70 if key == srd.SRD_CONF_SAMPLERATE:
71 self.samplerate = value
72 # One bit: 1.78ms (one half low, one half high).
73 self.halfbit = int((self.samplerate * 0.00178) / 2.0)
75 def putb(self, bit1, bit2, data):
76 ss, es = self.bits_ss_es[bit1][0], self.bits_ss_es[bit2][1]
77 self.put(ss, es, self.out_ann, data)
79 def handle_bits(self):
80 a, c, b = 0, 0, self.bits
81 # Individual raw bits.
84 ss = max(0, self.bits[0][0] - self.halfbit)
86 ss = self.bits_ss_es[i - 1][1]
87 es = self.bits[i][0] + self.halfbit
88 self.bits_ss_es.append([ss, es])
89 self.putb(i, i, [0, ['%d' % self.bits[i][1]]])
90 # Bits[0:0]: Startbit 1
91 s = ['Startbit1: %d' % b[0][1], 'SB1: %d' % b[0][1], 'SB1', 'S1', 'S']
92 self.putb(0, 0, [1, s])
93 # Bits[1:1]: Startbit 2
95 s = ['Startbit2: %d' % b[1][1], 'SB2: %d' % b[1][1], 'SB2', 'S2', 'S']
96 if self.options['protocol'] == 'extended':
97 s = ['CMD[6]#: %d' % b[1][1], 'C6#: %d' % b[1][1], 'C6#', 'C#', 'C']
99 self.putb(1, 1, [ann_idx, s])
100 # Bits[2:2]: Toggle bit
101 s = ['Togglebit: %d' % b[2][1], 'Toggle: %d' % b[2][1],
102 'TB: %d' % b[2][1], 'TB', 'T']
103 self.putb(2, 2, [3 if b[2][1] == 0 else 4, s])
104 # Bits[3:7]: Address (MSB-first)
106 a |= (b[3 + i][1] << (4 - i))
107 x = system.get(a, ['Unknown', 'Unk'])
108 s = ['Address: %d (%s)' % (a, x[0]), 'Addr: %d (%s)' % (a, x[1]),
109 'Addr: %d' % a, 'A: %d' % a, 'A']
110 self.putb(3, 7, [5, s])
111 # Bits[8:13]: Command (MSB-first)
113 c |= (b[8 + i][1] << (5 - i))
114 if self.options['protocol'] == 'extended':
115 inverted_bit6 = 1 if b[1][1] == 0 else 0
116 c |= (inverted_bit6 << 6)
117 cmd_type = 'VCR' if x[1] in ('VCR1', 'VCR2') else 'TV'
118 x = command[cmd_type].get(c, ['Unknown', 'Unk'])
119 s = ['Command: %d (%s)' % (c, x[0]), 'Cmd: %d (%s)' % (c, x[1]),
120 'Cmd: %d' % c, 'C: %d' % c, 'C']
121 self.putb(8, 13, [6, s])
124 # Categorize according to distance from last edge (short/long).
125 distance = self.samplenum - self.edges[-1]
126 s, l, margin = self.halfbit, self.halfbit * 2, int(self.halfbit / 2)
127 if distance in range(l - margin, l + margin + 1):
129 elif distance in range(s - margin, s + margin + 1):
132 return 'e' # Error, invalid edge distance.
134 def reset_decoder_state(self):
135 self.edges, self.bits, self.bits_ss_es = [], [], []
138 def decode(self, ss, es, data):
139 if not self.samplerate:
140 raise SamplerateError('Cannot decode without samplerate.')
141 for (self.samplenum, pins) in data:
145 # Wait for any edge (rising or falling).
146 if self.old_ir == self.ir:
150 if self.state == 'IDLE':
151 self.edges.append(self.samplenum)
152 self.bits.append([self.samplenum, 1])
154 self.old_ir = self.ir
156 edge = self.edge_type()
158 self.reset_decoder_state() # Reset state machine upon errors.
160 if self.state == 'MID1':
161 self.state = 'START1' if edge == 's' else 'MID0'
162 bit = None if edge == 's' else 0
163 elif self.state == 'MID0':
164 self.state = 'START0' if edge == 's' else 'MID1'
165 bit = None if edge == 's' else 1
166 elif self.state == 'START1':
169 bit = 1 if edge == 's' else None
170 elif self.state == 'START0':
173 bit = 0 if edge == 's' else None
175 self.edges.append(self.samplenum)
177 self.bits.append([self.samplenum, bit])
179 if len(self.bits) == 14:
181 self.reset_decoder_state()
183 self.old_ir = self.ir