]> sigrok.org Git - libsigrokdecode.git/blame - decoders/ir_irmp/pd.py
ir_irmp: add support for button "release" flag
[libsigrokdecode.git] / decoders / ir_irmp / pd.py
CommitLineData
4032dedd 1##
e8e8bec6
GS
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2014 Gump Yang <gump.yang@gmail.com>
5## Copyright (C) 2019 Rene Staffen
6##
4032dedd
GS
7## This program is free software; you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published by
9## the Free Software Foundation; either version 2 of the License, or
10## (at your option) any later version.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with this program; if not, see <http://www.gnu.org/licenses/>.
19##
20
e8e8bec6 21from . import irmp_library
4032dedd
GS
22import sigrokdecode as srd
23
4032dedd
GS
24class SamplerateError(Exception):
25 pass
26
27class Decoder(srd.Decoder):
28 api_version = 3
29 id = 'ir_irmp'
30 name = 'IR IRMP'
e8e8bec6
GS
31 longname = 'IR IRMP'
32 desc = 'IRMP infrared remote control multi protocol.'
4032dedd
GS
33 license = 'gplv2+'
34 inputs = ['logic']
35 outputs = []
36 tags = ['IR']
37 channels = (
38 {'id': 'ir', 'name': 'IR', 'desc': 'Data line'},
39 )
40 options = (
41 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-low',
42 'values': ('active-low', 'active-high')},
4032dedd
GS
43 )
44 annotations = (
45 ('packet', 'Packet'),
4032dedd
GS
46 )
47 annotation_rows = (
48 ('packets', 'IR Packets', (0,)),
4032dedd 49 )
4032dedd 50
e8e8bec6 51 def putframe(self, data):
8c3291c7
GS
52 '''Emit annotation for an IR frame.'''
53
54 # Cache result data fields in local variables. Get the ss/es
55 # timestamps, scaled to sample numbers.
e8e8bec6
GS
56 nr = data['proto_nr']
57 name = data['proto_name']
58 addr = data['address']
59 cmd = data['command']
60 repeat = data['repeat']
8c3291c7 61 release = data['release']
e8e8bec6
GS
62 ss = data['start'] * self.rate_factor
63 es = data['end'] * self.rate_factor
8c3291c7
GS
64
65 # Prepare display texts for several zoom levels.
66 # Implementor's note: Keep list lengths for flags aligned during
67 # maintenance. Make sure there are as many flags text variants
68 # as are referenced by annotation text variants. Differing list
69 # lengths or dynamic refs will severely complicate the logic.
70 rep_txts = ['repeat', 'rep', 'r']
71 rel_txts = ['release', 'rel', 'R']
72 flag_txts = [None,] * len(rep_txts)
73 for zoom in range(len(flag_txts)):
74 flag_txts[zoom] = []
75 if repeat:
76 flag_txts[zoom].append(rep_txts[zoom])
77 if release:
78 flag_txts[zoom].append(rel_txts[zoom])
79 flag_txts = [' '.join(t) or '-' for t in flag_txts]
80 flg = flag_txts # Short name for .format() references.
81 txts = [
82 'Protocol: {name} ({nr}), Address 0x{addr:04x}, Command: 0x{cmd:04x}, Flags: {flg[0]}'.format(**locals()),
83 'P: {name} ({nr}), Addr: 0x{addr:x}, Cmd: 0x{cmd:x}, Flg: {flg[1]}'.format(**locals()),
84 'P: {nr} A: 0x{addr:x} C: 0x{cmd:x} F: {flg[1]}'.format(**locals()),
85 'C:{cmd:x} A:{addr:x} {flg[2]}'.format(**locals()),
e8e8bec6 86 'C:{cmd:x}'.format(**locals()),
8c3291c7
GS
87 ]
88
89 # Emit the annotation from details which were constructed above.
90 self.put(ss, es, self.out_ann, [0, txts])
4032dedd
GS
91
92 def __init__(self):
e8e8bec6
GS
93 self.irmp = irmp_library.IrmpLibrary()
94 self.lib_rate = self.irmp.get_sample_rate()
4032dedd
GS
95 self.reset()
96
97 def reset(self):
e8e8bec6 98 self.irmp.reset_state()
4032dedd
GS
99
100 def start(self):
101 self.out_ann = self.register(srd.OUTPUT_ANN)
102
103 def metadata(self, key, value):
104 if key == srd.SRD_CONF_SAMPLERATE:
105 self.samplerate = value
106
4032dedd
GS
107 def decode(self):
108 if not self.samplerate:
109 raise SamplerateError('Cannot decode without samplerate.')
e8e8bec6
GS
110 if self.samplerate % self.lib_rate:
111 raise SamplerateError('capture samplerate must be multiple of library samplerate ({})'.format(self.lib_rate))
112 self.rate_factor = int(self.samplerate / self.lib_rate)
4032dedd 113
4032dedd 114 self.active = 0 if self.options['polarity'] == 'active-low' else 1
e8e8bec6 115 ir, = self.wait()
4032dedd 116 while True:
4032dedd
GS
117 if self.active == 1:
118 ir = 1 - ir
e8e8bec6
GS
119 if self.irmp.add_one_sample(ir):
120 data = self.irmp.get_result_data()
121 self.putframe(data)
122 ir, = self.wait([{'skip': self.rate_factor}])