]> sigrok.org Git - libsigrokdecode.git/blob - decoders/rgb_led_ws281x/pd.py
Use consistent __init__() format across all PDs.
[libsigrokdecode.git] / decoders / rgb_led_ws281x / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2016 Vladimir Ermakov <vooon341@gmail.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 3 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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 import sigrokdecode as srd
22 from functools import reduce
23
24 class SamplerateError(Exception):
25     pass
26
27 class Decoder(srd.Decoder):
28     api_version = 2
29     id = 'rgb_led_ws281x'
30     name = 'RGB LED (WS281x)'
31     longname = 'RGB LED string decoder (WS281x)'
32     desc = 'RGB LED string protocol (WS281x).'
33     license = 'gplv3+'
34     inputs = ['logic']
35     outputs = ['rgb_led_ws281x']
36     channels = (
37         {'id': 'din', 'name': 'DIN', 'desc': 'DIN data line'},
38     )
39     annotations = (
40         ('bit', 'Bit'),
41         ('reset', 'RESET'),
42         ('rgb', 'RGB'),
43     )
44     annotation_rows = (
45         ('bit', 'Bits', (0, 1)),
46         ('rgb', 'RGB', (2,)),
47     )
48
49     def __init__(self):
50         self.samplerate = None
51         self.oldpin = None
52         self.packet_ss = None
53         self.ss = None
54         self.es = None
55         self.bits = []
56         self.inreset = False
57
58     def start(self):
59         self.out_ann = self.register(srd.OUTPUT_ANN)
60
61     def metadata(self, key, value):
62         if key == srd.SRD_CONF_SAMPLERATE:
63             self.samplerate = value
64
65     def handle_bits(self, samplenum):
66         if len(self.bits) == 24:
67             grb = reduce(lambda a, b: (a << 1) | b, self.bits)
68             rgb = (grb & 0xff0000) >> 8 | (grb & 0x00ff00) << 8 | (grb & 0x0000ff)
69             self.put(self.packet_ss, samplenum, self.out_ann,
70                      [2, ['#%06x' % rgb]])
71             self.bits = []
72             self.packet_ss = None
73
74     def decode(self, ss, es, data):
75         if not self.samplerate:
76             raise SamplerateError('Cannot decode without samplerate.')
77
78         for (samplenum, (pin, )) in data:
79             if self.oldpin is None:
80                 self.oldpin = pin
81                 continue
82
83             # Check RESET condition (manufacturer recommends 50 usec minimal,
84             # but real minimum is ~10 usec).
85             if not self.inreset and not pin and self.es is not None and \
86                     (samplenum - self.es) / self.samplerate > 50e-6:
87
88                 # Decode last bit value.
89                 tH = (self.es - self.ss) / self.samplerate
90                 bit_ = True if tH >= 625e-9 else False
91
92                 self.bits.append(bit_)
93                 self.handle_bits(self.es)
94
95                 self.put(self.ss, self.es, self.out_ann, [0, ['%d' % bit_]])
96                 self.put(self.es, samplenum, self.out_ann,
97                          [1, ['RESET', 'RST', 'R']])
98
99                 self.inreset = True
100                 self.bits = []
101                 self.packet_ss = None
102                 self.ss = None
103
104             if not self.oldpin and pin:
105                 # Rising edge.
106                 if self.ss and self.es:
107                     period = samplenum - self.ss
108                     duty = self.es - self.ss
109                     # Ideal duty for T0H: 33%, T1H: 66%.
110                     bit_ = (duty / period) > 0.5
111
112                     self.put(self.ss, samplenum, self.out_ann,
113                              [0, ['%d' % bit_]])
114
115                     self.bits.append(bit_)
116                     self.handle_bits(samplenum)
117
118                 if self.packet_ss is None:
119                     self.packet_ss = samplenum
120
121                 self.ss = samplenum
122
123             elif self.oldpin and not pin:
124                 # Falling edge.
125                 self.inreset = False
126                 self.es = samplenum
127
128             self.oldpin = pin