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