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 'cnt_lc': ['Leader code time (µs)', 13500],
38 'cnt_rc': ['Repeat code time (µs)', 11250],
39 'cnt_rc_end': ['Repeat code end time (µs)', 562],
40 'cnt_accuracy': ['Accuracy range (µs)', 100],
41 'cnt_dazero': ['Data 0 time (µs)', 1125],
42 'cnt_daone': ['Data 1 time (µs)', 2250],
43 'polarity': ['Polarity', 'active-low'],
47 ['lc', 'Leader code'],
52 ('bits', 'Bits', (0,)),
53 ('fields', 'Fields', (1, 2, 3)),
57 self.put(self.ss_start, self.samplenum, self.out_ann, data)
60 self.put(self.ss_bit, self.samplenum, self.out_ann, data)
62 def __init__(self, **kwargs):
71 # self.out_python = self.register(srd.OUTPUT_PYTHON)
72 self.out_ann = self.register(srd.OUTPUT_ANN)
73 self.act_polar = 1 if self.options['polarity'] == 'active-low' else 0
74 self.old_ir = self.act_polar
76 def metadata(self, key, value):
77 if key == srd.SRD_CONF_SAMPLERATE:
78 self.samplerate = value
79 samplerate = float(self.samplerate)
81 x = float(self.options['cnt_accuracy']) / 1000000.0
82 self.margin = int(samplerate * x) - 1
83 x = float(self.options['cnt_lc']) / 1000000.0
84 self.lc = int(samplerate * x) - 1
85 x = float(self.options['cnt_rc']) / 1000000.0
86 self.rc = int(samplerate * x) - 1
87 x = float(self.options['cnt_rc_end']) / 1000000.0
88 self.rc_end = int(samplerate * x) - 1
89 x = float(self.options['cnt_dazero']) / 1000000.0
90 self.dazero = int(samplerate * x) - 1
91 x = float(self.options['cnt_daone']) / 1000000.0
92 self.daone = int(samplerate * x) - 1
93 x = float(10000) / 1000000.0
94 self.end = int(samplerate * x) - 1
96 def handle_bits(self, tick):
98 if tick in range(self.dazero - self.margin, self.dazero + self.margin):
100 elif tick in range(self.daone - self.margin, self.daone + self.margin):
104 self.putb([0, ['%d' % ret]])
105 self.data = self.data * 2 + ret
106 self.count = self.count + 1
108 self.ss_bit = self.samplenum
111 def data_judge(self, name):
112 buf = int((self.data & 0xff00) / 0x100)
113 nbuf = int(self.data & 0xff)
116 self.putx([2, ['%s: 0x%02x' % (name, buf)]])
118 self.putx([3, ['%s Error: 0x%04x' % (name, self.data)]])
120 self.data = self.count = 0
121 self.ss_bit = self.ss_start = self.samplenum
124 def decode(self, ss, es, data):
125 if self.samplerate is None:
126 raise Exception("Cannot decode without samplerate.")
127 for (self.samplenum, pins) in data:
130 # Wait for any edge (rising or falling).
131 if self.old_ir == self.ir:
134 if self.old_ir == self.act_polar:
135 b = self.samplenum - self.ss_bit
137 if self.state == 'IDLE':
138 if b in range(self.lc - self.margin, self.lc + self.margin):
139 self.putx([1, ['Leader code', 'Leader', 'LC', 'L']])
140 self.data = self.count = 0
141 self.state = 'ADDRESS'
142 elif b in range(self.rc - self.margin, self.rc + self.margin):
143 self.putx([1, ['Repeat code', 'Repeat', 'RC', 'R']])
144 self.data = self.count = 0
145 self.ss_bit = self.ss_start = self.samplenum
146 elif self.state == 'ADDRESS':
149 if self.data_judge(self.state) == 0:
150 self.state = 'COMMAND'
153 elif self.state == 'COMMAND':
156 self.data_judge(self.state)
159 self.old_ir = self.ir