]>
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 | |
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 | ||
23 | class Decoder(srd.Decoder): | |
24 | api_version = 1 | |
00962e76 UH |
25 | id = 'ir_nec' |
26 | name = 'IR NEC' | |
27 | longname = 'IR NEC' | |
28 | desc = 'NEC infrared remote control protocol.' | |
5e6fa9cc GY |
29 | license = 'gplv2+' |
30 | inputs = ['logic'] | |
00962e76 | 31 | outputs = ['ir_nec'] |
5e6fa9cc GY |
32 | probes = [ |
33 | {'id': 'ir', 'name': 'IR', 'desc': 'Data line'}, | |
34 | ] | |
35 | optional_probes = [] | |
36 | options = { | |
5e6fa9cc | 37 | 'polarity': ['Polarity', 'active-low'], |
00962e76 | 38 | } |
5e6fa9cc GY |
39 | annotations = [ |
40 | ['bit', 'Bit'], | |
70835fd4 UH |
41 | ['agc-pulse', 'AGC pulse'], |
42 | ['longpause', 'Long pause'], | |
43 | ['shortpause', 'Short pause'], | |
44 | ['stop-bit', 'Stop bit'], | |
45 | ['leader-code', 'Leader code'], | |
46 | ['addr', 'Address'], | |
47 | ['addr-inv', 'Address#'], | |
48 | ['cmd', 'Command'], | |
49 | ['cmd-inv', 'Command#'], | |
50 | ['repeat-code', 'Repeat code'], | |
51 | ['warnings', 'Warnings'], | |
5e6fa9cc GY |
52 | ] |
53 | annotation_rows = ( | |
70835fd4 UH |
54 | ('bits', 'Bits', (0, 1, 2, 3, 4)), |
55 | ('fields', 'Fields', (5, 6, 7, 8, 9, 10)), | |
56 | ('warnings', 'Warnings', (11,)), | |
5e6fa9cc GY |
57 | ) |
58 | ||
5e6fa9cc GY |
59 | def putx(self, data): |
60 | self.put(self.ss_start, self.samplenum, self.out_ann, data) | |
61 | ||
62 | def putb(self, data): | |
63 | self.put(self.ss_bit, self.samplenum, self.out_ann, data) | |
64 | ||
70835fd4 UH |
65 | def putd(self, data): |
66 | name = self.state.title() | |
67 | d = {'ADDRESS': 6, 'ADDRESS#': 7, 'COMMAND': 8, 'COMMAND#': 9} | |
68 | s = {'ADDRESS': ['ADDR', 'A'], 'ADDRESS#': ['ADDR#', 'A#'], | |
69 | 'COMMAND': ['CMD', 'C'], 'COMMAND#': ['CMD#', 'C#']} | |
70 | self.putx([d[self.state], ['%s: 0x%02X' % (name, data), | |
71 | '%s: 0x%02X' % (s[self.state][0], data), | |
72 | '%s: 0x%02X' % (s[self.state][1], data), s[self.state][1]]]) | |
73 | ||
74 | def putstop(self, ss): | |
75 | self.put(ss, ss + self.stop, self.out_ann, | |
76 | [4, ['Stop bit', 'Stop', 'St', 'S']]) | |
77 | ||
78 | def putpause(self, p): | |
79 | self.put(self.ss_start, self.ss_other_edge, self.out_ann, | |
80 | [1, ['AGC pulse', 'AGC', 'A']]) | |
81 | idx = 2 if p == 'Long' else 3 | |
82 | self.put(self.ss_other_edge, self.samplenum, self.out_ann, | |
83 | [idx, [p + ' pause', '%s-pause' % p[0], '%sP' % p[0], 'P']]) | |
84 | ||
5e6fa9cc | 85 | def __init__(self, **kwargs): |
5e6fa9cc | 86 | self.state = 'IDLE' |
70835fd4 | 87 | self.ss_bit = self.ss_start = self.ss_other_edge = 0 |
73fc79e0 | 88 | self.data = self.count = self.active = self.old_ir = None |
5e6fa9cc GY |
89 | |
90 | def start(self): | |
91 | # self.out_python = self.register(srd.OUTPUT_PYTHON) | |
92 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
73fc79e0 UH |
93 | self.active = 0 if self.options['polarity'] == 'active-low' else 1 |
94 | self.old_ir = 1 if self.active == 0 else 0 | |
5e6fa9cc GY |
95 | |
96 | def metadata(self, key, value): | |
97 | if key == srd.SRD_CONF_SAMPLERATE: | |
98 | self.samplerate = value | |
73fc79e0 UH |
99 | self.margin = int(self.samplerate * 0.0001) - 1 # 0.1ms |
100 | self.lc = int(self.samplerate * 0.0135) - 1 # 13.5ms | |
101 | self.rc = int(self.samplerate * 0.01125) - 1 # 11.25ms | |
102 | self.dazero = int(self.samplerate * 0.001125) - 1 # 1.125ms | |
103 | self.daone = int(self.samplerate * 0.00225) - 1 # 2.25ms | |
70835fd4 | 104 | self.stop = int(self.samplerate * 0.000652) - 1 # 0.652ms |
5e6fa9cc | 105 | |
70835fd4 | 106 | def handle_bit(self, tick): |
5bb61a25 | 107 | ret = None |
00962e76 | 108 | if tick in range(self.dazero - self.margin, self.dazero + self.margin): |
5e6fa9cc | 109 | ret = 0 |
00962e76 | 110 | elif tick in range(self.daone - self.margin, self.daone + self.margin): |
5e6fa9cc | 111 | ret = 1 |
5bb61a25 | 112 | if ret in (0, 1): |
5e6fa9cc | 113 | self.putb([0, ['%d' % ret]]) |
5bb61a25 | 114 | self.data |= (ret << self.count) # LSB-first |
5e6fa9cc | 115 | self.count = self.count + 1 |
5e6fa9cc | 116 | self.ss_bit = self.samplenum |
5e6fa9cc | 117 | |
70835fd4 | 118 | def data_ok(self): |
73fc79e0 | 119 | ret, name = (self.data >> 8) & (self.data & 0xff), self.state.title() |
70835fd4 UH |
120 | if self.count == 8: |
121 | self.putd(self.data) | |
122 | self.ss_start = self.samplenum | |
123 | return True | |
5e6fa9cc | 124 | if ret == 0: |
5bb61a25 | 125 | self.putd(self.data >> 8) |
5e6fa9cc | 126 | else: |
70835fd4 | 127 | self.putx([11, ['%s error: 0x%04X' % (name, self.data)]]) |
5e6fa9cc GY |
128 | self.data = self.count = 0 |
129 | self.ss_bit = self.ss_start = self.samplenum | |
70835fd4 | 130 | return ret == 0 |
5e6fa9cc GY |
131 | |
132 | def decode(self, ss, es, data): | |
133 | if self.samplerate is None: | |
134 | raise Exception("Cannot decode without samplerate.") | |
135 | for (self.samplenum, pins) in data: | |
136 | self.ir = pins[0] | |
00962e76 | 137 | |
70835fd4 UH |
138 | # Wait for an "interesting" edge, but also record the other ones. |
139 | if self.old_ir == self.ir: | |
140 | continue | |
141 | if self.ir != self.active: | |
142 | self.ss_other_edge = self.samplenum | |
73fc79e0 | 143 | self.old_ir = self.ir |
5e6fa9cc GY |
144 | continue |
145 | ||
73fc79e0 UH |
146 | b = self.samplenum - self.ss_bit |
147 | ||
148 | # State machine. | |
149 | if self.state == 'IDLE': | |
150 | if b in range(self.lc - self.margin, self.lc + self.margin): | |
70835fd4 UH |
151 | self.putpause('Long') |
152 | self.putx([5, ['Leader code', 'Leader', 'LC', 'L']]) | |
73fc79e0 UH |
153 | self.data = self.count = 0 |
154 | self.state = 'ADDRESS' | |
155 | elif b in range(self.rc - self.margin, self.rc + self.margin): | |
70835fd4 UH |
156 | self.putpause('Short') |
157 | self.putstop(self.samplenum) | |
158 | self.samplenum += self.stop | |
159 | self.putx([10, ['Repeat code', 'Repeat', 'RC', 'R']]) | |
73fc79e0 UH |
160 | self.data = self.count = 0 |
161 | self.ss_bit = self.ss_start = self.samplenum | |
162 | elif self.state == 'ADDRESS': | |
70835fd4 UH |
163 | self.handle_bit(b) |
164 | if self.count == 8: | |
165 | self.state = 'ADDRESS#' if self.data_ok() else 'IDLE' | |
166 | elif self.state == 'ADDRESS#': | |
167 | self.handle_bit(b) | |
168 | if self.count == 16: | |
169 | self.state = 'COMMAND' if self.data_ok() else 'IDLE' | |
73fc79e0 | 170 | elif self.state == 'COMMAND': |
70835fd4 UH |
171 | self.handle_bit(b) |
172 | if self.count == 8: | |
173 | self.state = 'COMMAND#' if self.data_ok() else 'IDLE' | |
174 | elif self.state == 'COMMAND#': | |
175 | self.handle_bit(b) | |
176 | if self.count == 16: | |
177 | self.state = 'STOP' if self.data_ok() else 'IDLE' | |
178 | elif self.state == 'STOP': | |
179 | self.putstop(self.ss_bit) | |
180 | self.ss_bit = self.ss_start = self.samplenum | |
181 | self.state = 'IDLE' | |
182 | else: | |
183 | raise Exception('Invalid state: %s' % self.state) | |
5e6fa9cc GY |
184 | |
185 | self.old_ir = self.ir | |
186 |