]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ir_nec6122/pd.py
ba1cdf94f94db91f30c21d54938cca9b47d1967e
[libsigrokdecode.git] / decoders / ir_nec6122 / pd.py
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
25     id = 'ir_nec6122'
26     name = 'IR NEC 6122'
27     longname = '1-Wire Infrared remote controller NEC 6122'
28     desc = 'Unidirectional, asynchronous serial bus.'
29     license = 'gplv2+'
30     inputs = ['logic']
31     outputs = ['ir_nec6122']
32     probes = [
33         {'id': 'ir', 'name': 'IR', 'desc': 'Data line'},
34     ]
35     optional_probes = []
36     options = {
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'],
45             }
46     annotations = [
47         ['bit', 'Bit'],
48         ['preoid', 'Preoid'],
49         ['info', 'Info'],
50         ['error', 'Error'],
51     ]
52     annotation_rows = (
53         ('fields', 'Fields', (1, 2, 3, 4, 5, 6)),
54         ('bits', 'Bits', (0,)),
55     )
56
57     def putx(self, data):
58         self.put(self.ss_edge, self.samplenum, self.out_ann, data)
59
60     def putx(self, data):
61         self.put(self.ss_start, self.samplenum, self.out_ann, data)
62
63     def putb(self, data):
64         self.put(self.ss_bit, self.samplenum, self.out_ann, data)
65
66     def __init__(self, **kwargs):
67         self.olddata = None
68         self.ss_edge = 0
69         self.ss_bit = 0
70         self.first_transition = True
71         self.bitwidth = None
72         self.state = 'IDLE'
73         self.data = 0;
74         self.count = 0;
75         self.ss_start = 0
76         self.act_polar = 0
77
78     def start(self):
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 
83
84     def metadata(self, key, value):
85         if key == srd.SRD_CONF_SAMPLERATE:
86             self.samplerate = value
87         samplerate = float(self.samplerate)
88
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
103
104     def handle_bits(self, tick):
105         ret = 0xff
106         if tick in range(self.dazero - self.margin,
107                 self.dazero + self.margin):
108             ret = 0
109         elif tick in range(self.daone - self.margin,
110                 self.daone + self.margin):
111             ret = 1
112
113         if ret < 2:
114             self.putb([0, ['%d' % ret]])
115             self.data = self.data * 2 + ret
116             self.count = self.count + 1
117
118         self.ss_bit = self.samplenum
119         return ret;
120
121     def data_judge(self, name):
122         buf = int((self.data & 0xff00) / 0x100)
123         nbuf = int(self.data & 0xff)
124         ret = buf & nbuf
125         if ret == 0:
126             self.putx([2, ['%s: 0x%02x' % (name, buf)]])
127         else:
128             self.putx([3, ['%s Error: 0x%04x' % (name, self.data)]])
129             
130         self.data = self.count = 0
131         self.ss_bit = self.ss_start = self.samplenum
132         return ret
133
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:
138             self.ir = pins[0]
139             
140             # Wait for any edge (rising or falling).
141             if self.old_ir == self.ir:
142                 continue
143
144             if self.old_ir == self.act_polar:
145                 b = self.samplenum - self.ss_bit
146                 # State machine.
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':
159                     self.handle_bits(b)
160                     if self.count > 15:
161                         if self.data_judge(self.state) == 0:
162                             self.state = 'CODE'
163                         else:
164                             self.state = 'IDLE'
165                 elif self.state == 'CODE':
166                     self.handle_bits(b)
167                     if self.count > 15:
168                         self.data_judge(self.state)
169                         self.state = 'IDLE'
170
171             self.old_ir = self.ir
172