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 'polarity': ['Polarity', 'active-low'],
41 ['lc', 'Leader code'],
46 ('bits', 'Bits', (0,)),
47 ('fields', 'Fields', (1, 2, 3)),
51 self.put(self.ss_start, self.samplenum, self.out_ann, data)
54 self.put(self.ss_bit, self.samplenum, self.out_ann, data)
56 def __init__(self, **kwargs):
58 self.ss_bit = self.ss_start = 0
59 self.data = self.count = self.active = self.old_ir = None
62 # self.out_python = self.register(srd.OUTPUT_PYTHON)
63 self.out_ann = self.register(srd.OUTPUT_ANN)
64 self.active = 0 if self.options['polarity'] == 'active-low' else 1
65 self.old_ir = 1 if self.active == 0 else 0
67 def metadata(self, key, value):
68 if key == srd.SRD_CONF_SAMPLERATE:
69 self.samplerate = value
70 self.margin = int(self.samplerate * 0.0001) - 1 # 0.1ms
71 self.lc = int(self.samplerate * 0.0135) - 1 # 13.5ms
72 self.rc = int(self.samplerate * 0.01125) - 1 # 11.25ms
73 self.dazero = int(self.samplerate * 0.001125) - 1 # 1.125ms
74 self.daone = int(self.samplerate * 0.00225) - 1 # 2.25ms
76 def handle_bits(self, tick):
78 if tick in range(self.dazero - self.margin, self.dazero + self.margin):
80 elif tick in range(self.daone - self.margin, self.daone + self.margin):
84 self.putb([0, ['%d' % ret]])
85 self.data = self.data * 2 + ret
86 self.count = self.count + 1
88 self.ss_bit = self.samplenum
92 ret, name = (self.data >> 8) & (self.data & 0xff), self.state.title()
94 self.putx([2, ['%s: 0x%02x' % (name, self.data >> 8)]])
96 self.putx([3, ['%s error: 0x%04x' % (name, self.data)]])
97 self.data = self.count = 0
98 self.ss_bit = self.ss_start = self.samplenum
101 def decode(self, ss, es, data):
102 if self.samplerate is None:
103 raise Exception("Cannot decode without samplerate.")
104 for (self.samplenum, pins) in data:
107 # Wait for an "active" edge (default: falling edge).
108 if self.old_ir == self.ir or self.ir != self.active:
109 self.old_ir = self.ir
112 b = self.samplenum - self.ss_bit
115 if self.state == 'IDLE':
116 if b in range(self.lc - self.margin, self.lc + self.margin):
117 self.putx([1, ['Leader code', 'Leader', 'LC', 'L']])
118 self.data = self.count = 0
119 self.state = 'ADDRESS'
120 elif b in range(self.rc - self.margin, self.rc + self.margin):
121 self.putx([1, ['Repeat code', 'Repeat', 'RC', 'R']])
122 self.data = self.count = 0
123 self.ss_bit = self.ss_start = self.samplenum
124 elif self.state == 'ADDRESS':
127 self.state = 'COMMAND' if self.data_judge() == 0 else 'IDLE'
128 elif self.state == 'COMMAND':
134 self.old_ir = self.ir