2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2014 Gump Yang <gump.yang@gmail.com>
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.
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.
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
21 import sigrokdecode as srd
23 class Decoder(srd.Decoder):
28 desc = 'NEC infrared remote control protocol.'
33 {'id': 'ir', 'name': 'IR', 'desc': 'Data line'},
37 'polarity': ['Polarity', 'active-low'],
41 ['agc-pulse', 'AGC pulse'],
42 ['longpause', 'Long pause'],
43 ['shortpause', 'Short pause'],
44 ['stop-bit', 'Stop bit'],
45 ['leader-code', 'Leader code'],
47 ['addr-inv', 'Address#'],
49 ['cmd-inv', 'Command#'],
50 ['repeat-code', 'Repeat code'],
51 ['warnings', 'Warnings'],
54 ('bits', 'Bits', (0, 1, 2, 3, 4)),
55 ('fields', 'Fields', (5, 6, 7, 8, 9, 10)),
56 ('warnings', 'Warnings', (11,)),
60 self.put(self.ss_start, self.samplenum, self.out_ann, data)
63 self.put(self.ss_bit, self.samplenum, self.out_ann, 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]]])
74 def putstop(self, ss):
75 self.put(ss, ss + self.stop, self.out_ann,
76 [4, ['Stop bit', 'Stop', 'St', 'S']])
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']])
85 def __init__(self, **kwargs):
87 self.ss_bit = self.ss_start = self.ss_other_edge = 0
88 self.data = self.count = self.active = self.old_ir = None
91 # self.out_python = self.register(srd.OUTPUT_PYTHON)
92 self.out_ann = self.register(srd.OUTPUT_ANN)
93 self.active = 0 if self.options['polarity'] == 'active-low' else 1
94 self.old_ir = 1 if self.active == 0 else 0
96 def metadata(self, key, value):
97 if key == srd.SRD_CONF_SAMPLERATE:
98 self.samplerate = value
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
104 self.stop = int(self.samplerate * 0.000652) - 1 # 0.652ms
106 def handle_bit(self, tick):
108 if tick in range(self.dazero - self.margin, self.dazero + self.margin):
110 elif tick in range(self.daone - self.margin, self.daone + self.margin):
113 self.putb([0, ['%d' % ret]])
114 self.data = self.data * 2 + ret
115 self.count = self.count + 1
116 self.ss_bit = self.samplenum
119 ret, name = (self.data >> 8) & (self.data & 0xff), self.state.title()
122 self.ss_start = self.samplenum
125 self.putd(self.data & 0xff)
127 self.putx([11, ['%s error: 0x%04X' % (name, self.data)]])
128 self.data = self.count = 0
129 self.ss_bit = self.ss_start = self.samplenum
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:
138 # Wait for an "interesting" edge, but also record the other ones.
139 if self.old_ir == self.ir:
141 if self.ir != self.active:
142 self.ss_other_edge = self.samplenum
143 self.old_ir = self.ir
146 b = self.samplenum - self.ss_bit
149 if self.state == 'IDLE':
150 if b in range(self.lc - self.margin, self.lc + self.margin):
151 self.putpause('Long')
152 self.putx([5, ['Leader code', 'Leader', 'LC', 'L']])
153 self.data = self.count = 0
154 self.state = 'ADDRESS'
155 elif b in range(self.rc - self.margin, self.rc + self.margin):
156 self.putpause('Short')
157 self.putstop(self.samplenum)
158 self.samplenum += self.stop
159 self.putx([10, ['Repeat code', 'Repeat', 'RC', 'R']])
160 self.data = self.count = 0
161 self.ss_bit = self.ss_start = self.samplenum
162 elif self.state == 'ADDRESS':
165 self.state = 'ADDRESS#' if self.data_ok() else 'IDLE'
166 elif self.state == 'ADDRESS#':
169 self.state = 'COMMAND' if self.data_ok() else 'IDLE'
170 elif self.state == 'COMMAND':
173 self.state = 'COMMAND#' if self.data_ok() else 'IDLE'
174 elif self.state == 'COMMAND#':
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
183 raise Exception('Invalid state: %s' % self.state)
185 self.old_ir = self.ir