]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2018 Steve R <steversig@virginmedia.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, see <http://www.gnu.org/licenses/>. | |
18 | ## | |
19 | ||
20 | import sigrokdecode as srd | |
21 | ||
22 | bitvals = ('0', '1', 'f', 'U') | |
23 | ||
24 | def decode_bit(edges): | |
25 | # Datasheet says long pulse is 3 times short pulse. | |
26 | lmin = 2 # long min multiplier | |
27 | lmax = 5 # long max multiplier | |
28 | eqmin = 0.5 # equal min multiplier | |
29 | eqmax = 1.5 # equal max multiplier | |
30 | if ( # 0 -___-___ | |
31 | (edges[1] >= edges[0] * lmin and edges[1] <= edges[0] * lmax) and | |
32 | (edges[2] >= edges[0] * eqmin and edges[2] <= edges[0] * eqmax) and | |
33 | (edges[3] >= edges[0] * lmin and edges[3] <= edges[0] * lmax)): | |
34 | return '0' | |
35 | elif ( # 1 ---_---_ | |
36 | (edges[0] >= edges[1] * lmin and edges[0] <= edges[1] * lmax) and | |
37 | (edges[0] >= edges[2] * eqmin and edges[0] <= edges[2] * eqmax) and | |
38 | (edges[0] >= edges[3] * lmin and edges[0] <= edges[3] * lmax)): | |
39 | return '1' | |
40 | elif ( # float ---_-___ | |
41 | (edges[1] >= edges[0] * lmin and edges[1] <= edges[0] * lmax) and | |
42 | (edges[2] >= edges[0] * lmin and edges[2] <= edges[0]* lmax) and | |
43 | (edges[3] >= edges[0] * eqmin and edges[3] <= edges[0] * eqmax)): | |
44 | return 'f' | |
45 | else: | |
46 | return 'U' | |
47 | ||
48 | def pinlabels(bit_count): | |
49 | if bit_count <= 6: | |
50 | return 'A%i' % (bit_count - 1) | |
51 | else: | |
52 | return 'A%i/D%i' % (bit_count - 1, 12 - bit_count) | |
53 | ||
54 | def decode_model(model, bits): | |
55 | if model == 'maplin_l95ar': | |
56 | address = 'Addr' # Address pins A0 to A5 | |
57 | for i in range(0, 6): | |
58 | address += ' %i:' % (i + 1) + ('on' if bits[i][0] == '0' else 'off') | |
59 | button = 'Button' | |
60 | # Button pins A6/D5 to A11/D0 | |
61 | if bits[6][0] == '0' and bits[11][0] == '0': | |
62 | button += ' A ON/OFF' | |
63 | elif bits[7][0] == '0' and bits[11][0] == '0': | |
64 | button += ' B ON/OFF' | |
65 | elif bits[9][0] == '0' and bits[11][0] == '0': | |
66 | button += ' C ON/OFF' | |
67 | elif bits[8][0] == '0' and bits[11][0] == '0': | |
68 | button += ' D ON/OFF' | |
69 | else: | |
70 | button += ' Unknown' | |
71 | return ['%s' % address, bits[0][1], bits[5][2], \ | |
72 | '%s' % button, bits[6][1], bits[11][2]] | |
73 | ||
74 | class Decoder(srd.Decoder): | |
75 | api_version = 3 | |
76 | id = 'rc_encode' | |
77 | name = 'RC encode' | |
78 | longname = 'Remote control encoder' | |
79 | desc = 'PT2262/HX2262/SC5262 remote control encoder protocol.' | |
80 | license = 'gplv2+' | |
81 | inputs = ['logic'] | |
82 | outputs = [] | |
83 | tags = ['IC', 'IR'] | |
84 | channels = ( | |
85 | {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, | |
86 | ) | |
87 | annotations = ( | |
88 | ('bit-0', 'Bit 0'), | |
89 | ('bit-1', 'Bit 1'), | |
90 | ('bit-f', 'Bit f'), | |
91 | ('bit-U', 'Bit U'), | |
92 | ('bit-sync', 'Bit sync'), | |
93 | ('pin', 'Pin'), | |
94 | ('code-word-addr', 'Code word address'), | |
95 | ('code-word-data', 'Code word data'), | |
96 | ) | |
97 | annotation_rows = ( | |
98 | ('bits', 'Bits', (0, 1, 2, 3, 4)), | |
99 | ('pins', 'Pins', (5,)), | |
100 | ('code-words', 'Code words', (6, 7)), | |
101 | ) | |
102 | options = ( | |
103 | {'id': 'remote', 'desc': 'Remote', 'default': 'none', | |
104 | 'values': ('none', 'maplin_l95ar')}, | |
105 | ) | |
106 | ||
107 | def __init__(self): | |
108 | self.reset() | |
109 | ||
110 | def reset(self): | |
111 | self.samplenumber_last = None | |
112 | self.pulses = [] | |
113 | self.bits = [] | |
114 | self.labels = [] | |
115 | self.bit_count = 0 | |
116 | self.ss = None | |
117 | self.es = None | |
118 | self.state = 'IDLE' | |
119 | ||
120 | def start(self): | |
121 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
122 | self.model = self.options['remote'] | |
123 | ||
124 | def putx(self, data): | |
125 | self.put(self.ss, self.es, self.out_ann, data) | |
126 | ||
127 | def decode(self): | |
128 | while True: | |
129 | pin = self.wait({0: 'e'}) | |
130 | self.state = 'DECODING' | |
131 | ||
132 | if not self.samplenumber_last: # Set counters to start of signal. | |
133 | self.samplenumber_last = self.samplenum | |
134 | self.ss = self.samplenum | |
135 | continue | |
136 | ||
137 | if self.bit_count < 12: # Decode A0 to A11. | |
138 | self.bit_count += 1 | |
139 | for i in range(0, 4): # Get four pulses for each bit. | |
140 | if i > 0: | |
141 | pin = self.wait({0: 'e'}) # Get next 3 edges. | |
142 | samples = self.samplenum - self.samplenumber_last | |
143 | self.pulses.append(samples) # Save the pulse width. | |
144 | self.samplenumber_last = self.samplenum | |
145 | self.es = self.samplenum | |
146 | self.bits.append([decode_bit(self.pulses), self.ss, | |
147 | self.es]) # Save states and times. | |
148 | idx = bitvals.index(decode_bit(self.pulses)) | |
149 | self.putx([idx, [decode_bit(self.pulses)]]) # Write decoded bit. | |
150 | self.putx([5, [pinlabels(self.bit_count)]]) # Write pin labels. | |
151 | self.pulses = [] | |
152 | self.ss = self.samplenum | |
153 | else: | |
154 | if self.model != 'none': | |
155 | self.labels = decode_model(self.model, self.bits) | |
156 | self.put(self.labels[1], self.labels[2], self.out_ann, | |
157 | [6, [self.labels[0]]]) # Write model decode. | |
158 | self.put(self.labels[4], self.labels[5], self.out_ann, | |
159 | [7, [self.labels[3]]]) # Write model decode. | |
160 | samples = self.samplenum - self.samplenumber_last | |
161 | pin = self.wait({'skip': 8 * samples}) # Wait for end of sync bit. | |
162 | self.es = self.samplenum | |
163 | self.putx([4, ['Sync']]) # Write sync label. | |
164 | self.reset() # Reset and wait for next set of pulses. | |
165 | self.state = 'DECODE_TIMEOUT' | |
166 | if not self.state == 'DECODE_TIMEOUT': | |
167 | self.samplenumber_last = self.samplenum |