summaryrefslogtreecommitdiff
path: root/decoders/ir_irmp/pd.py
blob: b8df8190301cba34b34d567ce17231615e0ba3b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2014 Gump Yang <gump.yang@gmail.com>
## Copyright (C) 2019 Rene Staffen
## Copyright (C) 2020-2021 Gerhard Sittig <gerhard.sittig@gmx.net>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##

from . import irmp_library
import sigrokdecode as srd

class SamplerateError(Exception):
    pass

class LibraryError(Exception):
    pass

class Decoder(srd.Decoder):
    api_version = 3
    id = 'ir_irmp'
    name = 'IR IRMP'
    longname = 'IR IRMP'
    desc = 'IRMP infrared remote control multi protocol.'
    license = 'gplv2+'
    inputs = ['logic']
    outputs = []
    tags = ['IR']
    channels = (
        {'id': 'ir', 'name': 'IR', 'desc': 'Data line'},
    )
    options = (
        {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-low',
            'values': ('active-low', 'active-high')},
    )
    annotations = (
        ('packet', 'Packet'),
    )
    annotation_rows = (
        ('packets', 'IR Packets', (0,)),
    )

    def putframe(self, data):
        '''Emit annotation for an IR frame.'''

        # Cache result data fields in local variables. Get the ss/es
        # timestamps, scaled to sample numbers.
        nr = data['proto_nr']
        name = data['proto_name']
        addr = data['address']
        cmd = data['command']
        repeat = data['repeat']
        release = data['release']
        ss = data['start'] * self.rate_factor
        es = data['end'] * self.rate_factor

        # Prepare display texts for several zoom levels.
        # Implementor's note: Keep list lengths for flags aligned during
        # maintenance. Make sure there are as many flags text variants
        # as are referenced by annotation text variants. Differing list
        # lengths or dynamic refs will severely complicate the logic.
        rep_txts = ['repeat', 'rep', 'r']
        rel_txts = ['release', 'rel', 'R']
        flag_txts = [None,] * len(rep_txts)
        for zoom in range(len(flag_txts)):
            flag_txts[zoom] = []
            if repeat:
                flag_txts[zoom].append(rep_txts[zoom])
            if release:
                flag_txts[zoom].append(rel_txts[zoom])
        flag_txts = [' '.join(t) or '-' for t in flag_txts]
        flg = flag_txts # Short name for .format() references.
        txts = [
            'Protocol: {name} ({nr}), Address 0x{addr:04x}, Command: 0x{cmd:04x}, Flags: {flg[0]}'.format(**locals()),
            'P: {name} ({nr}), Addr: 0x{addr:x}, Cmd: 0x{cmd:x}, Flg: {flg[1]}'.format(**locals()),
            'P: {nr} A: 0x{addr:x} C: 0x{cmd:x} F: {flg[1]}'.format(**locals()),
            'C:{cmd:x} A:{addr:x} {flg[2]}'.format(**locals()),
            'C:{cmd:x}'.format(**locals()),
        ]

        # Emit the annotation from details which were constructed above.
        self.put(ss, es, self.out_ann, [0, txts])

    def __init__(self):
        self.irmp = None
        self.reset()

    def reset(self):
        pass

    def start(self):
        self.out_ann = self.register(srd.OUTPUT_ANN)

    def metadata(self, key, value):
        if key == srd.SRD_CONF_SAMPLERATE:
            self.samplerate = value

    def decode(self):
        if not self.irmp:
            try:
                self.irmp = irmp_library.IrmpLibrary()
            except Exception as e:
                txt = e.args[0]
                raise LibraryError(txt)
        if not self.irmp:
            raise LibraryError('Cannot access IRMP library.')
        if not self.samplerate:
            raise SamplerateError('Cannot decode without samplerate.')
        lib_rate = self.irmp.get_sample_rate()
        if not lib_rate:
            raise LibraryError('Cannot determine IRMP library\'s samplerate.')
        if self.samplerate % lib_rate:
            raise SamplerateError('Capture samplerate must be multiple of library samplerate ({})'.format(lib_rate))

        self.rate_factor = int(self.samplerate / lib_rate)
        active = 0 if self.options['polarity'] == 'active-low' else 1

        ir, = self.wait()
        with self.irmp:
            self.irmp.reset_state()
            while True:
                if active == 1:
                    ir = 1 - ir
                if self.irmp.add_one_sample(ir):
                    data = self.irmp.get_result_data()
                    self.putframe(data)
                ir, = self.wait([{'skip': self.rate_factor}])