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):
27 longname = '1-Wire Infrared remote controller NEC 6122'
28 desc = 'Unidirectional, asynchronous serial bus.'
31 outputs = ['ir_nec6122']
33 {'id': 'ir', 'name': 'IR', 'desc': 'Data line'},
37 'level': ['Tirgger Level L/H', 0],
38 'cnt_peroid': ['Peroid time (us)', 13500],
39 'cnt_repeat': ['Repeat time (us)', 11250],
40 'cnt_repeat_end': ['Repeat end time (us)', 562],
41 'cnt_accuracy': ['Accuracy range (us)', 100],
42 'cnt_dazero': ['Data 0 time (us)', 1125],
43 'cnt_daone': ['Data 1 time (us)', 2250],
44 'polarity': ['Polarity', 'active-low'],
53 ('fields', 'Fields', (1, 2, 3, 4, 5, 6)),
54 ('bits', 'Bits', (0,)),
58 self.put(self.ss_edge, self.samplenum, self.out_ann, data)
61 self.put(self.ss_start, self.samplenum, self.out_ann, data)
64 self.put(self.ss_bit, self.samplenum, self.out_ann, data)
66 def __init__(self, **kwargs):
70 self.first_transition = True
79 # self.out_python = self.register(srd.OUTPUT_PYTHON)
80 self.out_ann = self.register(srd.OUTPUT_ANN)
81 self.act_polar = 1 if self.options['polarity'] == 'active-low' else 0
82 self.old_ir = self.act_polar
84 def metadata(self, key, value):
85 if key == srd.SRD_CONF_SAMPLERATE:
86 self.samplerate = value
87 samplerate = float(self.samplerate)
89 x = float(self.options['cnt_accuracy']) / 1000000.0
90 self.margin = int(samplerate * x) - 1
91 x = float(self.options['cnt_peroid']) / 1000000.0
92 self.preoid = int(samplerate * x) - 1
93 x = float(self.options['cnt_repeat']) / 1000000.0
94 self.repeat = int(samplerate * x) - 1
95 x = float(self.options['cnt_repeat_end']) / 1000000.0
96 self.repeat_end = int(samplerate * x) - 1
97 x = float(self.options['cnt_dazero']) / 1000000.0
98 self.dazero = int(samplerate * x) - 1
99 x = float(self.options['cnt_daone']) / 1000000.0
100 self.daone = int(samplerate * x) - 1
101 x = float(10000) / 1000000.0
102 self.end = int(samplerate * x) - 1
104 def handle_bits(self, tick):
106 if tick in range(self.dazero - self.margin,
107 self.dazero + self.margin):
109 elif tick in range(self.daone - self.margin,
110 self.daone + self.margin):
114 self.putb([0, ['%d' % ret]])
115 self.data = self.data * 2 + ret
116 self.count = self.count + 1
118 self.ss_bit = self.samplenum
121 def data_judge(self, name):
122 buf = int((self.data & 0xff00) / 0x100)
123 nbuf = int(self.data & 0xff)
126 self.putx([2, ['%s: 0x%02x' % (name, buf)]])
128 self.putx([3, ['%s Error: 0x%04x' % (name, self.data)]])
130 self.data = self.count = 0
131 self.ss_bit = self.ss_start = self.samplenum
134 def decode(self, ss, es, data):
135 if self.samplerate is None:
136 raise Exception("Cannot decode without samplerate.")
137 for (self.samplenum, pins) in data:
140 # Wait for any edge (rising or falling).
141 if self.old_ir == self.ir:
144 if self.old_ir == self.act_polar:
145 b = self.samplenum - self.ss_bit
147 if self.state == 'IDLE':
148 if b in range(self.preoid - self.margin,
149 self.preoid + self.margin):
150 self.putx([1, ['Preoid', 'Pre', 'P']])
151 self.data = self.count = 0
152 self.state = 'ADDRESS'
153 elif b in range(self.repeat - self.margin,
154 self.repeat + self.margin):
155 self.putx([1, ['Repeat', 'Rep', 'R']])
156 self.data = self.count = 0
157 self.ss_bit = self.ss_start = self.samplenum
158 elif self.state == 'ADDRESS':
161 if self.data_judge(self.state) == 0:
165 elif self.state == 'CODE':
168 self.data_judge(self.state)
171 self.old_ir = self.ir