]> sigrok.org Git - libsigrokdecode.git/blame - decoders/ir_nec/pd.py
Rename 'ir_nec6122' PD to 'ir_nec', minor fixes and simplifications.
[libsigrokdecode.git] / decoders / ir_nec / pd.py
CommitLineData
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
21import sigrokdecode as srd
22
23class 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 = {
00962e76
UH
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],
5e6fa9cc 43 'polarity': ['Polarity', 'active-low'],
00962e76 44 }
5e6fa9cc
GY
45 annotations = [
46 ['bit', 'Bit'],
00962e76 47 ['lc', 'Leader code'],
5e6fa9cc
GY
48 ['info', 'Info'],
49 ['error', 'Error'],
50 ]
51 annotation_rows = (
5e6fa9cc 52 ('bits', 'Bits', (0,)),
00962e76 53 ('fields', 'Fields', (1, 2, 3)),
5e6fa9cc
GY
54 )
55
5e6fa9cc
GY
56 def putx(self, data):
57 self.put(self.ss_start, self.samplenum, self.out_ann, data)
58
59 def putb(self, data):
60 self.put(self.ss_bit, self.samplenum, self.out_ann, data)
61
62 def __init__(self, **kwargs):
5e6fa9cc 63 self.ss_bit = 0
5e6fa9cc 64 self.state = 'IDLE'
00962e76
UH
65 self.data = 0
66 self.count = 0
5e6fa9cc
GY
67 self.ss_start = 0
68 self.act_polar = 0
69
70 def start(self):
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
75
76 def metadata(self, key, value):
77 if key == srd.SRD_CONF_SAMPLERATE:
78 self.samplerate = value
79 samplerate = float(self.samplerate)
80
81 x = float(self.options['cnt_accuracy']) / 1000000.0
82 self.margin = int(samplerate * x) - 1
00962e76
UH
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
5e6fa9cc
GY
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
95
96 def handle_bits(self, tick):
97 ret = 0xff
00962e76 98 if tick in range(self.dazero - self.margin, self.dazero + self.margin):
5e6fa9cc 99 ret = 0
00962e76 100 elif tick in range(self.daone - self.margin, self.daone + self.margin):
5e6fa9cc
GY
101 ret = 1
102
103 if ret < 2:
104 self.putb([0, ['%d' % ret]])
105 self.data = self.data * 2 + ret
106 self.count = self.count + 1
107
108 self.ss_bit = self.samplenum
00962e76 109 return ret
5e6fa9cc
GY
110
111 def data_judge(self, name):
112 buf = int((self.data & 0xff00) / 0x100)
113 nbuf = int(self.data & 0xff)
114 ret = buf & nbuf
115 if ret == 0:
116 self.putx([2, ['%s: 0x%02x' % (name, buf)]])
117 else:
118 self.putx([3, ['%s Error: 0x%04x' % (name, self.data)]])
00962e76 119
5e6fa9cc
GY
120 self.data = self.count = 0
121 self.ss_bit = self.ss_start = self.samplenum
122 return ret
123
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:
128 self.ir = pins[0]
00962e76 129
5e6fa9cc
GY
130 # Wait for any edge (rising or falling).
131 if self.old_ir == self.ir:
132 continue
133
134 if self.old_ir == self.act_polar:
135 b = self.samplenum - self.ss_bit
136 # State machine.
137 if self.state == 'IDLE':
00962e76
UH
138 if b in range(self.lc - self.margin, self.lc + self.margin):
139 self.putx([1, ['Leader code', 'Leader', 'LC', 'L']])
5e6fa9cc
GY
140 self.data = self.count = 0
141 self.state = 'ADDRESS'
00962e76
UH
142 elif b in range(self.rc - self.margin, self.rc + self.margin):
143 self.putx([1, ['Repeat code', 'Repeat', 'RC', 'R']])
5e6fa9cc
GY
144 self.data = self.count = 0
145 self.ss_bit = self.ss_start = self.samplenum
146 elif self.state == 'ADDRESS':
147 self.handle_bits(b)
148 if self.count > 15:
149 if self.data_judge(self.state) == 0:
00962e76 150 self.state = 'COMMAND'
5e6fa9cc
GY
151 else:
152 self.state = 'IDLE'
00962e76 153 elif self.state == 'COMMAND':
5e6fa9cc
GY
154 self.handle_bits(b)
155 if self.count > 15:
156 self.data_judge(self.state)
157 self.state = 'IDLE'
158
159 self.old_ir = self.ir
160