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