]>
Commit | Line | Data |
---|---|---|
5e6fa9cc GY |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2014 Gump Yang <gump.yang@gmail.com> | |
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/>. |
5e6fa9cc GY |
18 | ## |
19 | ||
025c728e | 20 | from common.srdhelper import bitpack |
12fecc8f | 21 | from .lists import * |
025c728e | 22 | import sigrokdecode as srd |
5e6fa9cc | 23 | |
21cda951 UH |
24 | class SamplerateError(Exception): |
25 | pass | |
26 | ||
34ed4b3f GS |
27 | class Pin: |
28 | IR, = range(1) | |
29 | ||
30 | class Ann: | |
31 | BIT, AGC, LONG_PAUSE, SHORT_PAUSE, STOP_BIT, \ | |
32 | LEADER_CODE, ADDR, ADDR_INV, CMD, CMD_INV, REPEAT_CODE, \ | |
33 | REMOTE, WARN = range(13) | |
34 | ||
5e6fa9cc | 35 | class Decoder(srd.Decoder): |
5844bb0f | 36 | api_version = 3 |
00962e76 UH |
37 | id = 'ir_nec' |
38 | name = 'IR NEC' | |
39 | longname = 'IR NEC' | |
40 | desc = 'NEC infrared remote control protocol.' | |
5e6fa9cc GY |
41 | license = 'gplv2+' |
42 | inputs = ['logic'] | |
6cbba91f | 43 | outputs = [] |
d6d8a8a4 | 44 | tags = ['IR'] |
6a15597a | 45 | channels = ( |
5e6fa9cc | 46 | {'id': 'ir', 'name': 'IR', 'desc': 'Data line'}, |
bee57ee8 UH |
47 | ) |
48 | options = ( | |
49 | {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-low', | |
50 | 'values': ('active-low', 'active-high')}, | |
fb1870b0 | 51 | {'id': 'cd_freq', 'desc': 'Carrier Frequency', 'default': 0}, |
17e0ba22 GS |
52 | {'id': 'extended', 'desc': 'Extended NEC Protocol', |
53 | 'default': 'no', 'values': ('yes', 'no')}, | |
bee57ee8 UH |
54 | ) |
55 | annotations = ( | |
56 | ('bit', 'Bit'), | |
57 | ('agc-pulse', 'AGC pulse'), | |
58 | ('longpause', 'Long pause'), | |
59 | ('shortpause', 'Short pause'), | |
60 | ('stop-bit', 'Stop bit'), | |
61 | ('leader-code', 'Leader code'), | |
62 | ('addr', 'Address'), | |
63 | ('addr-inv', 'Address#'), | |
64 | ('cmd', 'Command'), | |
65 | ('cmd-inv', 'Command#'), | |
66 | ('repeat-code', 'Repeat code'), | |
67 | ('remote', 'Remote'), | |
e144452b | 68 | ('warning', 'Warning'), |
bee57ee8 | 69 | ) |
5e6fa9cc | 70 | annotation_rows = ( |
34ed4b3f GS |
71 | ('bits', 'Bits', (Ann.BIT, Ann.AGC, Ann.LONG_PAUSE, Ann.SHORT_PAUSE, Ann.STOP_BIT)), |
72 | ('fields', 'Fields', (Ann.LEADER_CODE, Ann.ADDR, Ann.ADDR_INV, Ann.CMD, Ann.CMD_INV, Ann.REPEAT_CODE)), | |
73 | ('remote-vals', 'Remote', (Ann.REMOTE,)), | |
74 | ('warnings', 'Warnings', (Ann.WARN,)), | |
5e6fa9cc GY |
75 | ) |
76 | ||
5e6fa9cc GY |
77 | def putx(self, data): |
78 | self.put(self.ss_start, self.samplenum, self.out_ann, data) | |
79 | ||
80 | def putb(self, data): | |
81 | self.put(self.ss_bit, self.samplenum, self.out_ann, data) | |
82 | ||
17e0ba22 | 83 | def putd(self, data, bit_count): |
70835fd4 | 84 | name = self.state.title() |
34ed4b3f GS |
85 | d = {'ADDRESS': Ann.ADDR, 'ADDRESS#': Ann.ADDR_INV, |
86 | 'COMMAND': Ann.CMD, 'COMMAND#': Ann.CMD_INV} | |
70835fd4 UH |
87 | s = {'ADDRESS': ['ADDR', 'A'], 'ADDRESS#': ['ADDR#', 'A#'], |
88 | 'COMMAND': ['CMD', 'C'], 'COMMAND#': ['CMD#', 'C#']} | |
17e0ba22 | 89 | fmt = '{{}}: 0x{{:0{}X}}'.format(bit_count // 4) |
34ed4b3f | 90 | self.putx([d[self.state], [ |
17e0ba22 GS |
91 | fmt.format(name, data), |
92 | fmt.format(s[self.state][0], data), | |
93 | fmt.format(s[self.state][1], data), | |
34ed4b3f GS |
94 | s[self.state][1], |
95 | ]]) | |
70835fd4 UH |
96 | |
97 | def putstop(self, ss): | |
98 | self.put(ss, ss + self.stop, self.out_ann, | |
34ed4b3f | 99 | [Ann.STOP_BIT, ['Stop bit', 'Stop', 'St', 'S']]) |
70835fd4 UH |
100 | |
101 | def putpause(self, p): | |
102 | self.put(self.ss_start, self.ss_other_edge, self.out_ann, | |
34ed4b3f GS |
103 | [Ann.AGC, ['AGC pulse', 'AGC', 'A']]) |
104 | idx = Ann.LONG_PAUSE if p == 'Long' else Ann.SHORT_PAUSE | |
105 | self.put(self.ss_other_edge, self.samplenum, self.out_ann, [idx, [ | |
106 | '{} pause'.format(p), | |
107 | '{}-pause'.format(p[0]), | |
108 | '{}P'.format(p[0]), | |
109 | 'P', | |
110 | ]]) | |
70835fd4 | 111 | |
12fecc8f UH |
112 | def putremote(self): |
113 | dev = address.get(self.addr, 'Unknown device') | |
d478372a GS |
114 | buttons = command.get(self.addr, {}) |
115 | btn = buttons.get(self.cmd, ['Unknown', 'Unk']) | |
34ed4b3f GS |
116 | self.put(self.ss_remote, self.ss_bit + self.stop, self.out_ann, [Ann.REMOTE, [ |
117 | '{}: {}'.format(dev, btn[0]), | |
118 | '{}: {}'.format(dev, btn[1]), | |
119 | '{}'.format(btn[1]), | |
120 | ]]) | |
12fecc8f | 121 | |
92b7b49f | 122 | def __init__(self): |
10aeb8ea GS |
123 | self.reset() |
124 | ||
125 | def reset(self): | |
5e6fa9cc | 126 | self.state = 'IDLE' |
12fecc8f | 127 | self.ss_bit = self.ss_start = self.ss_other_edge = self.ss_remote = 0 |
025c728e | 128 | self.data = [] |
12fecc8f | 129 | self.addr = self.cmd = None |
5e6fa9cc GY |
130 | |
131 | def start(self): | |
5e6fa9cc | 132 | self.out_ann = self.register(srd.OUTPUT_ANN) |
5844bb0f | 133 | |
5e6fa9cc GY |
134 | def metadata(self, key, value): |
135 | if key == srd.SRD_CONF_SAMPLERATE: | |
136 | self.samplerate = value | |
b3f83fda GS |
137 | |
138 | def calc_rate(self): | |
82ea183f | 139 | self.tolerance = 0.05 # +/-5% |
73fc79e0 UH |
140 | self.lc = int(self.samplerate * 0.0135) - 1 # 13.5ms |
141 | self.rc = int(self.samplerate * 0.01125) - 1 # 11.25ms | |
142 | self.dazero = int(self.samplerate * 0.001125) - 1 # 1.125ms | |
143 | self.daone = int(self.samplerate * 0.00225) - 1 # 2.25ms | |
70835fd4 | 144 | self.stop = int(self.samplerate * 0.000652) - 1 # 0.652ms |
24490b8a | 145 | self.idle_to = int(self.samplerate * 0.020) - 1 # 20ms, arbitrary choice |
5e6fa9cc | 146 | |
82ea183f GM |
147 | def compare_with_tolerance(self, measured, base): |
148 | return (measured >= base * (1 - self.tolerance) | |
149 | and measured <= base * (1 + self.tolerance)) | |
150 | ||
70835fd4 | 151 | def handle_bit(self, tick): |
5bb61a25 | 152 | ret = None |
82ea183f | 153 | if self.compare_with_tolerance(tick, self.dazero): |
5e6fa9cc | 154 | ret = 0 |
82ea183f | 155 | elif self.compare_with_tolerance(tick, self.daone): |
5e6fa9cc | 156 | ret = 1 |
5bb61a25 | 157 | if ret in (0, 1): |
34ed4b3f | 158 | self.putb([Ann.BIT, ['{:d}'.format(ret)]]) |
025c728e | 159 | self.data.append(ret) |
5e6fa9cc | 160 | self.ss_bit = self.samplenum |
5e6fa9cc | 161 | |
17e0ba22 | 162 | def data_ok(self, check, want_len): |
d478372a | 163 | name = self.state.title() |
025c728e GS |
164 | normal, inverted = bitpack(self.data[:8]), bitpack(self.data[8:]) |
165 | valid = (normal ^ inverted) == 0xff | |
166 | show = inverted if self.state.endswith('#') else normal | |
17e0ba22 GS |
167 | is_ext_addr = self.is_extended and self.state == 'ADDRESS' |
168 | if is_ext_addr: | |
169 | normal = bitpack(self.data) | |
170 | show = normal | |
171 | valid = True | |
172 | if len(self.data) == want_len: | |
12fecc8f | 173 | if self.state == 'ADDRESS': |
025c728e | 174 | self.addr = normal |
12fecc8f | 175 | if self.state == 'COMMAND': |
025c728e | 176 | self.cmd = normal |
17e0ba22 | 177 | self.putd(show, want_len) |
70835fd4 | 178 | self.ss_start = self.samplenum |
17e0ba22 GS |
179 | if is_ext_addr: |
180 | self.data = [] | |
181 | self.ss_bit = self.ss_start = self.samplenum | |
70835fd4 | 182 | return True |
24490b8a | 183 | self.putd(show, want_len) |
d478372a | 184 | if check and not valid: |
025c728e GS |
185 | warn_show = bitpack(self.data) |
186 | self.putx([Ann.WARN, ['{} error: 0x{:04X}'.format(name, warn_show)]]) | |
025c728e | 187 | self.data = [] |
5e6fa9cc | 188 | self.ss_bit = self.ss_start = self.samplenum |
d478372a | 189 | return valid |
5e6fa9cc | 190 | |
5844bb0f | 191 | def decode(self): |
21cda951 UH |
192 | if not self.samplerate: |
193 | raise SamplerateError('Cannot decode without samplerate.') | |
b3f83fda | 194 | self.calc_rate() |
fb1870b0 GS |
195 | |
196 | cd_count = None | |
197 | if self.options['cd_freq']: | |
198 | cd_count = int(self.samplerate / self.options['cd_freq']) + 1 | |
1865f48d PM |
199 | prev_ir = None |
200 | ||
025c728e | 201 | active = 0 if self.options['polarity'] == 'active-low' else 1 |
17e0ba22 GS |
202 | self.is_extended = self.options['extended'] == 'yes' |
203 | want_addr_len = 16 if self.is_extended else 8 | |
fb1870b0 | 204 | |
5844bb0f | 205 | while True: |
fb1870b0 GS |
206 | # Detect changes in the presence of an active input signal. |
207 | # The decoder can either be fed an already filtered RX signal | |
208 | # or optionally can detect the presence of a carrier. Periods | |
209 | # of inactivity (signal changes slower than the carrier freq, | |
210 | # if specified) pass on the most recently sampled level. This | |
211 | # approach works for filtered and unfiltered input alike, and | |
212 | # only slightly extends the active phase of input signals with | |
213 | # carriers included by one period of the carrier frequency. | |
214 | # IR based communication protocols can cope with this slight | |
215 | # inaccuracy just fine by design. Enabling carrier detection | |
216 | # on already filtered signals will keep the length of their | |
217 | # active period, but will shift their signal changes by one | |
218 | # carrier period before they get passed to decoding logic. | |
219 | if cd_count: | |
34ed4b3f | 220 | (cur_ir,) = self.wait([{Pin.IR: 'e'}, {'skip': cd_count}]) |
fb1870b0 | 221 | if self.matched[0]: |
025c728e | 222 | cur_ir = active |
fb1870b0 GS |
223 | if cur_ir == prev_ir: |
224 | continue | |
225 | prev_ir = cur_ir | |
226 | self.ir = cur_ir | |
227 | else: | |
34ed4b3f | 228 | (self.ir,) = self.wait({Pin.IR: 'e'}) |
00962e76 | 229 | |
025c728e | 230 | if self.ir != active: |
85f616ed B |
231 | # Save the location of the non-active edge (recessive), |
232 | # then wait for the next edge. Immediately process the | |
233 | # end of the STOP bit which completes an IR frame. | |
70835fd4 | 234 | self.ss_other_edge = self.samplenum |
85f616ed B |
235 | if self.state != 'STOP': |
236 | continue | |
5e6fa9cc | 237 | |
24490b8a GS |
238 | # Reset internal state for long periods of idle level. |
239 | width = self.samplenum - self.ss_bit | |
240 | if width >= self.idle_to and self.state != 'STOP': | |
241 | self.reset() | |
73fc79e0 UH |
242 | |
243 | # State machine. | |
244 | if self.state == 'IDLE': | |
24490b8a | 245 | if self.compare_with_tolerance(width, self.lc): |
70835fd4 | 246 | self.putpause('Long') |
34ed4b3f | 247 | self.putx([Ann.LEADER_CODE, ['Leader code', 'Leader', 'LC', 'L']]) |
12fecc8f | 248 | self.ss_remote = self.ss_start |
025c728e | 249 | self.data = [] |
73fc79e0 | 250 | self.state = 'ADDRESS' |
24490b8a | 251 | elif self.compare_with_tolerance(width, self.rc): |
70835fd4 UH |
252 | self.putpause('Short') |
253 | self.putstop(self.samplenum) | |
254 | self.samplenum += self.stop | |
34ed4b3f | 255 | self.putx([Ann.REPEAT_CODE, ['Repeat code', 'Repeat', 'RC', 'R']]) |
025c728e | 256 | self.data = [] |
73fc79e0 UH |
257 | self.ss_bit = self.ss_start = self.samplenum |
258 | elif self.state == 'ADDRESS': | |
24490b8a | 259 | self.handle_bit(width) |
17e0ba22 GS |
260 | if len(self.data) == want_addr_len: |
261 | self.data_ok(False, want_addr_len) | |
262 | self.state = 'COMMAND' if self.is_extended else 'ADDRESS#' | |
70835fd4 | 263 | elif self.state == 'ADDRESS#': |
24490b8a | 264 | self.handle_bit(width) |
025c728e | 265 | if len(self.data) == 16: |
24490b8a GS |
266 | self.data_ok(True, 8) |
267 | self.state = 'COMMAND' | |
73fc79e0 | 268 | elif self.state == 'COMMAND': |
24490b8a | 269 | self.handle_bit(width) |
025c728e | 270 | if len(self.data) == 8: |
17e0ba22 | 271 | self.data_ok(False, 8) |
d478372a | 272 | self.state = 'COMMAND#' |
70835fd4 | 273 | elif self.state == 'COMMAND#': |
24490b8a | 274 | self.handle_bit(width) |
025c728e | 275 | if len(self.data) == 16: |
24490b8a GS |
276 | self.data_ok(True, 8) |
277 | self.state = 'STOP' | |
70835fd4 UH |
278 | elif self.state == 'STOP': |
279 | self.putstop(self.ss_bit) | |
12fecc8f | 280 | self.putremote() |
70835fd4 UH |
281 | self.ss_bit = self.ss_start = self.samplenum |
282 | self.state = 'IDLE' |