]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ir_nec/pd.py
ir_nec: Add support for more fields.
[libsigrokdecode.git] / decoders / ir_nec / 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_nec'
26     name = 'IR NEC'
27     longname = 'IR NEC'
28     desc = 'NEC infrared remote control protocol.'
29     license = 'gplv2+'
30     inputs = ['logic']
31     outputs = ['ir_nec']
32     probes = [
33         {'id': 'ir', 'name': 'IR', 'desc': 'Data line'},
34     ]
35     optional_probes = []
36     options = {
37         'polarity': ['Polarity', 'active-low'],
38     }
39     annotations = [
40         ['bit', 'Bit'],
41         ['agc-pulse', 'AGC pulse'],
42         ['longpause', 'Long pause'],
43         ['shortpause', 'Short pause'],
44         ['stop-bit', 'Stop bit'],
45         ['leader-code', 'Leader code'],
46         ['addr', 'Address'],
47         ['addr-inv', 'Address#'],
48         ['cmd', 'Command'],
49         ['cmd-inv', 'Command#'],
50         ['repeat-code', 'Repeat code'],
51         ['warnings', 'Warnings'],
52     ]
53     annotation_rows = (
54         ('bits', 'Bits', (0, 1, 2, 3, 4)),
55         ('fields', 'Fields', (5, 6, 7, 8, 9, 10)),
56         ('warnings', 'Warnings', (11,)),
57     )
58
59     def putx(self, data):
60         self.put(self.ss_start, self.samplenum, self.out_ann, data)
61
62     def putb(self, data):
63         self.put(self.ss_bit, self.samplenum, self.out_ann, data)
64
65     def putd(self, 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]]])
73
74     def putstop(self, ss):
75         self.put(ss, ss + self.stop, self.out_ann,
76                  [4, ['Stop bit', 'Stop', 'St', 'S']])
77
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']])
84
85     def __init__(self, **kwargs):
86         self.state = 'IDLE'
87         self.ss_bit = self.ss_start = self.ss_other_edge = 0
88         self.data = self.count = self.active = self.old_ir = None
89
90     def start(self):
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
95
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
105
106     def handle_bit(self, tick):
107         ret = 0xff
108         if tick in range(self.dazero - self.margin, self.dazero + self.margin):
109             ret = 0
110         elif tick in range(self.daone - self.margin, self.daone + self.margin):
111             ret = 1
112         if ret < 2:
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
117
118     def data_ok(self):
119         ret, name = (self.data >> 8) & (self.data & 0xff), self.state.title()
120         if self.count == 8:
121             self.putd(self.data)
122             self.ss_start = self.samplenum
123             return True
124         if ret == 0:
125             self.putd(self.data & 0xff)
126         else:
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
130         return ret == 0
131
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:
136             self.ir = pins[0]
137
138             # Wait for an "interesting" edge, but also record the other ones.
139             if self.old_ir == self.ir:
140                 continue
141             if self.ir != self.active:
142                 self.ss_other_edge = self.samplenum
143                 self.old_ir = self.ir
144                 continue
145
146             b = self.samplenum - self.ss_bit
147
148             # State machine.
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':
163                 self.handle_bit(b)
164                 if self.count == 8:
165                     self.state = 'ADDRESS#' if self.data_ok() else 'IDLE'
166             elif self.state == 'ADDRESS#':
167                 self.handle_bit(b)
168                 if self.count == 16:
169                     self.state = 'COMMAND' if self.data_ok() else 'IDLE'
170             elif self.state == 'COMMAND':
171                 self.handle_bit(b)
172                 if self.count == 8:
173                     self.state = 'COMMAND#' if self.data_ok() else 'IDLE'
174             elif self.state == 'COMMAND#':
175                 self.handle_bit(b)
176                 if self.count == 16:
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
181                 self.state = 'IDLE'
182             else:
183                 raise Exception('Invalid state: %s' % self.state)
184
185             self.old_ir = self.ir
186