]>
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'], | |
00962e76 | 41 | ['lc', 'Leader code'], |
5e6fa9cc GY |
42 | ['info', 'Info'], |
43 | ['error', 'Error'], | |
44 | ] | |
45 | annotation_rows = ( | |
5e6fa9cc | 46 | ('bits', 'Bits', (0,)), |
00962e76 | 47 | ('fields', 'Fields', (1, 2, 3)), |
5e6fa9cc GY |
48 | ) |
49 | ||
5e6fa9cc GY |
50 | def putx(self, data): |
51 | self.put(self.ss_start, self.samplenum, self.out_ann, data) | |
52 | ||
53 | def putb(self, data): | |
54 | self.put(self.ss_bit, self.samplenum, self.out_ann, data) | |
55 | ||
56 | def __init__(self, **kwargs): | |
5e6fa9cc | 57 | self.state = 'IDLE' |
73fc79e0 UH |
58 | self.ss_bit = self.ss_start = 0 |
59 | self.data = self.count = self.active = self.old_ir = None | |
5e6fa9cc GY |
60 | |
61 | def start(self): | |
62 | # self.out_python = self.register(srd.OUTPUT_PYTHON) | |
63 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
73fc79e0 UH |
64 | self.active = 0 if self.options['polarity'] == 'active-low' else 1 |
65 | self.old_ir = 1 if self.active == 0 else 0 | |
5e6fa9cc GY |
66 | |
67 | def metadata(self, key, value): | |
68 | if key == srd.SRD_CONF_SAMPLERATE: | |
69 | self.samplerate = value | |
73fc79e0 UH |
70 | self.margin = int(self.samplerate * 0.0001) - 1 # 0.1ms |
71 | self.lc = int(self.samplerate * 0.0135) - 1 # 13.5ms | |
72 | self.rc = int(self.samplerate * 0.01125) - 1 # 11.25ms | |
73 | self.dazero = int(self.samplerate * 0.001125) - 1 # 1.125ms | |
74 | self.daone = int(self.samplerate * 0.00225) - 1 # 2.25ms | |
5e6fa9cc GY |
75 | |
76 | def handle_bits(self, tick): | |
77 | ret = 0xff | |
00962e76 | 78 | if tick in range(self.dazero - self.margin, self.dazero + self.margin): |
5e6fa9cc | 79 | ret = 0 |
00962e76 | 80 | elif tick in range(self.daone - self.margin, self.daone + self.margin): |
5e6fa9cc GY |
81 | ret = 1 |
82 | ||
83 | if ret < 2: | |
84 | self.putb([0, ['%d' % ret]]) | |
85 | self.data = self.data * 2 + ret | |
86 | self.count = self.count + 1 | |
87 | ||
88 | self.ss_bit = self.samplenum | |
00962e76 | 89 | return ret |
5e6fa9cc | 90 | |
73fc79e0 UH |
91 | def data_judge(self): |
92 | ret, name = (self.data >> 8) & (self.data & 0xff), self.state.title() | |
5e6fa9cc | 93 | if ret == 0: |
73fc79e0 | 94 | self.putx([2, ['%s: 0x%02x' % (name, self.data >> 8)]]) |
5e6fa9cc | 95 | else: |
73fc79e0 | 96 | self.putx([3, ['%s error: 0x%04x' % (name, self.data)]]) |
5e6fa9cc GY |
97 | self.data = self.count = 0 |
98 | self.ss_bit = self.ss_start = self.samplenum | |
99 | return ret | |
100 | ||
101 | def decode(self, ss, es, data): | |
102 | if self.samplerate is None: | |
103 | raise Exception("Cannot decode without samplerate.") | |
104 | for (self.samplenum, pins) in data: | |
105 | self.ir = pins[0] | |
00962e76 | 106 | |
73fc79e0 UH |
107 | # Wait for an "active" edge (default: falling edge). |
108 | if self.old_ir == self.ir or self.ir != self.active: | |
109 | self.old_ir = self.ir | |
5e6fa9cc GY |
110 | continue |
111 | ||
73fc79e0 UH |
112 | b = self.samplenum - self.ss_bit |
113 | ||
114 | # State machine. | |
115 | if self.state == 'IDLE': | |
116 | if b in range(self.lc - self.margin, self.lc + self.margin): | |
117 | self.putx([1, ['Leader code', 'Leader', 'LC', 'L']]) | |
118 | self.data = self.count = 0 | |
119 | self.state = 'ADDRESS' | |
120 | elif b in range(self.rc - self.margin, self.rc + self.margin): | |
121 | self.putx([1, ['Repeat code', 'Repeat', 'RC', 'R']]) | |
122 | self.data = self.count = 0 | |
123 | self.ss_bit = self.ss_start = self.samplenum | |
124 | elif self.state == 'ADDRESS': | |
125 | self.handle_bits(b) | |
126 | if self.count > 15: | |
127 | self.state = 'COMMAND' if self.data_judge() == 0 else 'IDLE' | |
128 | elif self.state == 'COMMAND': | |
129 | self.handle_bits(b) | |
130 | if self.count > 15: | |
131 | self.data_judge() | |
132 | self.state = 'IDLE' | |
5e6fa9cc GY |
133 | |
134 | self.old_ir = self.ir | |
135 |