]>
Commit | Line | Data |
---|---|---|
3e10fce7 TF |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2020 Tom Flanagan <knio@zkpq.ca> | |
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, see <http://www.gnu.org/licenses/>. | |
18 | ## | |
19 | ||
195cb7b3 | 20 | from common.srdhelper import bitpack |
3e10fce7 | 21 | from .lists import ADDRESSES |
195cb7b3 | 22 | import sigrokdecode as srd |
3e10fce7 TF |
23 | |
24 | class SamplerateError(Exception): | |
25 | pass | |
26 | ||
27 | class SIRCError(Exception): | |
28 | pass | |
29 | ||
30 | class SIRCErrorSilent(SIRCError): | |
31 | pass | |
32 | ||
33 | class Decoder(srd.Decoder): | |
34 | api_version = 3 | |
35 | id = 'ir_sirc' | |
36 | name = 'IR SIRC' | |
c449c583 GS |
37 | longname = 'Sony IR (SIRC)' |
38 | desc = 'Sony infrared remote control protocol (SIRC).' | |
3e10fce7 | 39 | license = 'gplv2+' |
c449c583 | 40 | tags = ['IR'] |
3e10fce7 | 41 | inputs = ['logic'] |
c449c583 | 42 | outputs = [] |
3e10fce7 | 43 | channels = ( |
c449c583 | 44 | {'id': 'ir', 'name': 'IR', 'desc': 'IR data line'}, |
3e10fce7 TF |
45 | ) |
46 | options = ( | |
c449c583 GS |
47 | {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-low', |
48 | 'values': ('active-low', 'active-high')}, | |
3e10fce7 TF |
49 | ) |
50 | annotations = ( | |
51 | ('bit', 'Bit'), | |
52 | ('agc', 'AGC'), | |
53 | ('pause', 'Pause'), | |
3e10fce7 TF |
54 | ('start', 'Start'), |
55 | ('command', 'Command'), | |
56 | ('address', 'Address'), | |
57 | ('extended', 'Extended'), | |
3e10fce7 | 58 | ('remote', 'Remote'), |
c449c583 | 59 | ('warning', 'Warning'), |
3e10fce7 TF |
60 | ) |
61 | annotation_rows = ( | |
62 | ('bits', 'Bits', (0, 1, 2)), | |
63 | ('fields', 'Fields', (3, 4, 5, 6)), | |
c449c583 | 64 | ('remotes', 'Remotes', (7,)), |
3e10fce7 TF |
65 | ('warnings', 'Warnings', (8,)), |
66 | ) | |
67 | ||
68 | def __init__(self): | |
c449c583 | 69 | self.reset() |
3e10fce7 TF |
70 | |
71 | def reset(self): | |
72 | pass | |
73 | ||
74 | def start(self): | |
75 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
76 | self.active = self.options['polarity'] == 'active-high' | |
77 | ||
78 | def metadata(self, key, value): | |
79 | if key == srd.SRD_CONF_SAMPLERATE: | |
80 | self.samplerate = value | |
81 | ||
195cb7b3 GS |
82 | def putg(self, ss, es, cls, texts): |
83 | self.put(ss, es, self.out_ann, [cls, texts]) | |
84 | ||
3e10fce7 TF |
85 | def tolerance(self, start, end, expected): |
86 | microseconds = 1000000 * (end - start) / self.samplerate | |
87 | tolerance = expected * 0.30 | |
88 | return (expected - tolerance) < microseconds < (expected + tolerance) | |
89 | ||
90 | def wait(self, *conds, timeout=None): | |
91 | conds = list(conds) | |
92 | if timeout is not None: | |
93 | to = int(self.samplerate * timeout / 1000000) | |
94 | conds.append({'skip': to}) | |
95 | start = self.samplenum | |
96 | signals = super(Decoder, self).wait(conds) | |
97 | end = self.samplenum | |
98 | return signals, start, end, self.matched | |
99 | ||
100 | def read_pulse(self, high, time): | |
101 | e = 'f' if high else 'r' | |
102 | max_time = int(time * 1.30) | |
103 | signals, start, end, (edge, timeout) = self.wait({0: e}, timeout=max_time) | |
104 | if timeout or not self.tolerance(start, end, time): | |
105 | raise SIRCError('Timeout') | |
106 | return signals, start, end, (edge, timeout) | |
107 | ||
108 | def read_bit(self): | |
109 | e = 'f' if self.active else 'r' | |
195cb7b3 | 110 | signals, high_start, high_end, (edge, timeout) = self.wait({0: e}, timeout=2000) |
3e10fce7 TF |
111 | if timeout: |
112 | raise SIRCError('Bit High Timeout') | |
113 | if self.tolerance(high_start, high_end, 1200): | |
114 | bit = 1 | |
115 | elif self.tolerance(high_start, high_end, 600): | |
116 | bit = 0 | |
117 | else: | |
118 | raise SIRCError('Bit Low Timeout') | |
119 | try: | |
120 | signals, low_start, low_end, matched = self.read_pulse(not self.active, 600) | |
121 | good = True | |
122 | except SIRCError: | |
123 | low_end = high_end + int(600 * self.samplerate / 1000000) | |
124 | good = False | |
195cb7b3 | 125 | self.putg(high_start, low_end, 0, ['{}'.format(bit)]) |
3e10fce7 TF |
126 | return bit, high_start, low_end, good |
127 | ||
128 | def read_signal(self): | |
129 | # Start code | |
130 | try: | |
131 | signals, agc_start, agc_end, matched = self.read_pulse(self.active, 2400) | |
132 | signals, pause_start, pause_end, matched = self.read_pulse(not self.active, 600) | |
133 | except SIRCError: | |
134 | raise SIRCErrorSilent('not an SIRC message') | |
195cb7b3 GS |
135 | self.putg(agc_start, agc_end, 1, ['AGC', 'A']) |
136 | self.putg(pause_start, pause_end, 2, ['Pause', 'P']) | |
137 | self.putg(agc_start, pause_end, 3, ['Start', 'S']) | |
3e10fce7 TF |
138 | |
139 | # Read bits | |
140 | bits = [] | |
141 | while True: | |
142 | bit, start, end, good = self.read_bit() | |
143 | bits.append((bit, start, end)) | |
144 | if len(bits) > 20: | |
145 | raise SIRCError('too many bits') | |
146 | if not good: | |
147 | if len(bits) == 12: | |
148 | command = bits[0:7] | |
149 | address = bits[7:12] | |
150 | extended = [] | |
151 | elif len(bits) == 15: | |
152 | command = bits[0:7] | |
153 | address = bits[7:15] | |
154 | extended = [] | |
155 | elif len(bits) == 20: | |
156 | command = bits[0:7] | |
157 | address = bits[7:12] | |
158 | extended = bits[12:20] | |
159 | else: | |
160 | raise SIRCError('incorrect number of bits: {}'.format(len(bits))) | |
161 | break | |
162 | ||
195cb7b3 GS |
163 | command_num = bitpack([b[0] for b in command]) |
164 | address_num = bitpack([b[0] for b in address]) | |
3e10fce7 TF |
165 | command_str = '0x{:02X}'.format(command_num) |
166 | address_str = '0x{:02X}'.format(address_num) | |
195cb7b3 GS |
167 | self.putg(command[0][1], command[-1][2], 4, [ |
168 | 'Command: {}'.format(command_str), | |
169 | 'C:{}'.format(command_str), | |
170 | ]) | |
171 | self.putg(address[0][1], address[-1][2], 5, [ | |
172 | 'Address: {}'.format(address_str), | |
173 | 'A:{}'.format(address_str), | |
174 | ]) | |
3e10fce7 TF |
175 | extended_num = None |
176 | if extended: | |
195cb7b3 | 177 | extended_num = bitpack([b[0] for b in extended]) |
3e10fce7 | 178 | extended_str = '0x{:02X}'.format(extended_num) |
195cb7b3 GS |
179 | self.putg(extended[0][1], extended[-1][2], 6, [ |
180 | 'Extended: {}'.format(extended_str), | |
181 | 'E:{}'.format(extended_str), | |
182 | ]) | |
3e10fce7 TF |
183 | return address_num, command_num, extended_num, bits[0][1], bits[-1][2] |
184 | ||
185 | def decode(self): | |
186 | if not self.samplerate: | |
187 | raise SamplerateError('Cannot decode without samplerate.') | |
188 | ||
189 | while True: | |
190 | e = 'h' if self.active else 'l' | |
195cb7b3 | 191 | signal, start, end, matched = self.wait({0: e}) |
3e10fce7 TF |
192 | try: |
193 | address, command, extended, payload_start, payload_end = self.read_signal() | |
194 | names, commands = ADDRESSES.get((address, extended), (['Unknown Device: ', 'UNK: '], {})) | |
195 | text = commands.get(command, 'Unknown') | |
195cb7b3 | 196 | self.putg(end, payload_end, 7, [n + text for n in names]) |
3e10fce7 TF |
197 | except SIRCErrorSilent as e: |
198 | continue | |
199 | except SIRCError as e: | |
195cb7b3 GS |
200 | self.putg(end, self.samplenum, 8, [ |
201 | 'Error: {}'.format(e), | |
202 | 'Error', | |
203 | 'E', | |
204 | ]) | |
3e10fce7 | 205 | continue |