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