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