]> sigrok.org Git - libsigrokdecode.git/blame - decoders/ir_rc5/pd.py
ir_rc5: Only determine edge type once.
[libsigrokdecode.git] / decoders / ir_rc5 / pd.py
CommitLineData
914f0b99
UH
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
5##
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.
10##
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.
15##
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
19##
20
21import sigrokdecode as srd
22from .lists import *
23
24class Decoder(srd.Decoder):
25 api_version = 1
26 id = 'ir_rc5'
27 name = 'IR RC-5'
28 longname = 'IR RC-5'
29 desc = 'RC-5 infrared remote control protocol.'
30 license = 'gplv2+'
31 inputs = ['logic']
32 outputs = ['ir_rc5']
33 probes = [
34 {'id': 'ir', 'name': 'IR', 'desc': 'IR data line'},
35 ]
36 optional_probes = []
37 options = {
38 'polarity': ['Polarity', 'active-low'],
39 'protocol': ['Protocol type', 'standard'],
40 }
41 annotations = [
42 ['bit', 'Bit'],
43 ['startbit1', 'Startbit 1'],
44 ['startbit2', 'Startbit 2'],
45 ['togglebit-0', 'Toggle bit 0'],
46 ['togglebit-1', 'Toggle bit 1'],
47 ['address', 'Address'],
48 ['command', 'Command'],
49 ]
50 annotation_rows = (
51 ('bits', 'Bits', (0,)),
52 ('fields', 'Fields', (1, 2, 3, 4, 5, 6)),
53 )
54
55 def __init__(self, **kwargs):
56 self.samplerate = None
57 self.samplenum = None
58 self.edges, self.bits, self.bits_ss_es = [], [], []
59 self.state = 'IDLE'
60
61 def start(self):
62 self.out_ann = self.register(srd.OUTPUT_ANN)
63 self.old_ir = 1 if self.options['polarity'] == 'active-low' else 0
64
65 def metadata(self, key, value):
66 if key == srd.SRD_CONF_SAMPLERATE:
67 self.samplerate = value
68 # One bit: 1.78ms (one half low, one half high).
69 self.halfbit = int((self.samplerate * 0.00178) / 2.0)
70
71 def putb(self, bit1, bit2, data):
72 ss, es = self.bits_ss_es[bit1][0], self.bits_ss_es[bit2][1]
73 self.put(ss, es, self.out_ann, data)
74
75 def handle_bits(self):
76 a, c, b = 0, 0, self.bits
77 # Individual raw bits.
78 for i in range(14):
79 if i == 0:
80 ss = max(0, self.bits[0][0] - self.halfbit)
81 else:
82 ss = self.bits_ss_es[i - 1][1]
83 es = self.bits[i][0] + self.halfbit
84 self.bits_ss_es.append([ss, es])
85 self.putb(i, i, [0, ['%d' % self.bits[i][1]]])
86 # Bits[0:0]: Startbit 1
87 s = ['Startbit1: %d' % b[0][1], 'SB1: %d' % b[0][1], 'SB1', 'S1', 'S']
88 self.putb(0, 0, [1, s])
89 # Bits[1:1]: Startbit 2
90 ann_idx = 2
91 s = ['Startbit2: %d' % b[1][1], 'SB2: %d' % b[1][1], 'SB2', 'S2', 'S']
92 if self.options['protocol'] == 'extended':
93 s = ['CMD[6]#: %d' % b[1][1], 'C6#: %d' % b[1][1], 'C6#', 'C#', 'C']
94 ann_idx = 6
95 self.putb(1, 1, [ann_idx, s])
96 # Bits[2:2]: Toggle bit
97 s = ['Togglebit: %d' % b[2][1], 'Toggle: %d' % b[2][1],
98 'TB: %d' % b[2][1], 'TB', 'T']
99 self.putb(2, 2, [3 if b[2][1] == 0 else 4, s])
100 # Bits[3:7]: Address (MSB-first)
101 for i in range(5):
102 a |= (b[3 + i][1] << (4 - i))
103 x = system.get(a, ['Unknown', 'Unk'])
104 s = ['Address: %d (%s)' % (a, x[0]), 'Addr: %d (%s)' % (a, x[1]),
105 'Addr: %d' % a, 'A: %d' % a, 'A']
106 self.putb(3, 7, [5, s])
107 # Bits[8:13]: Command (MSB-first)
108 for i in range(6):
109 c |= (b[8 + i][1] << (5 - i))
110 if self.options['protocol'] == 'extended':
111 inverted_bit6 = 1 if b[1][1] == 0 else 0
112 c |= (inverted_bit6 << 6)
113 cmd_type = 'VCR' if x[1] in ('VCR1', 'VCR2') else 'TV'
114 x = command[cmd_type].get(c, ['Unknown', 'Unk'])
115 s = ['Command: %d (%s)' % (c, x[0]), 'Cmd: %d (%s)' % (c, x[1]),
116 'Cmd: %d' % c, 'C: %d' % c, 'C']
117 self.putb(8, 13, [6, s])
118
119 def edge_type(self):
120 # Categorize according to distance from last edge (short/long).
121 distance = self.samplenum - self.edges[-1]
122 s, l, margin = self.halfbit, self.halfbit * 2, int(self.halfbit / 2)
123 if distance in range(l - margin, l + margin + 1):
124 return 'l'
125 elif distance in range(s - margin, s + margin + 1):
126 return 's'
127 else:
128 raise Exception('Invalid edge distance: %d' % distance)
129
130 def decode(self, ss, es, data):
131 if self.samplerate is None:
132 raise Exception("Cannot decode without samplerate.")
133 for (self.samplenum, pins) in data:
134
135 self.ir = pins[0]
136
137 # Wait for any edge (rising or falling).
138 if self.old_ir == self.ir:
139 continue
140
141 # State machine.
142 if self.state == 'IDLE':
143 self.edges.append(self.samplenum)
144 self.bits.append([self.samplenum, 1])
145 self.state = 'MID1'
146 self.old_ir = self.ir
147 continue
92d992d2 148 edge = self.edge_type()
914f0b99 149 if self.state == 'MID1':
92d992d2
UH
150 self.state = 'START1' if edge == 's' else 'MID0'
151 bit = None if edge == 's' else 0
914f0b99 152 elif self.state == 'MID0':
92d992d2
UH
153 self.state = 'START0' if edge == 's' else 'MID1'
154 bit = None if edge == 's' else 1
914f0b99 155 elif self.state == 'START1':
92d992d2 156 if edge == 's':
914f0b99 157 self.state = 'MID1'
92d992d2 158 bit = 1 if edge == 's' else None
914f0b99 159 elif self.state == 'START0':
92d992d2 160 if edge == 's':
914f0b99 161 self.state = 'MID0'
92d992d2 162 bit = 0 if edge == 's' else None
914f0b99
UH
163 else:
164 raise Exception('Invalid state: %s' % self.state)
165
166 self.edges.append(self.samplenum)
167 if bit != None:
168 self.bits.append([self.samplenum, bit])
169
170 if len(self.bits) == 14 + 1:
171 self.handle_bits()
172 self.edges, self.bits, self.bits_ss_es = [], [], []
173 self.state = 'IDLE'
174
175 self.old_ir = self.ir
176