]> sigrok.org Git - libsigrokdecode.git/blob - decoders/rgb_led_ws281x/pd.py
43fbce4d0e2c9c89baacdb263649a706c2e21f38
[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, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21 from functools import reduce
22
23 class SamplerateError(Exception):
24     pass
25
26 class Decoder(srd.Decoder):
27     api_version = 3
28     id = 'rgb_led_ws281x'
29     name = 'RGB LED (WS281x)'
30     longname = 'RGB LED string decoder (WS281x)'
31     desc = 'RGB LED string protocol (WS281x).'
32     license = 'gplv3+'
33     inputs = ['logic']
34     outputs = []
35     tags = ['Display', 'IC']
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         ('bits', 'Bits', (0, 1)),
46         ('rgb-vals', 'RGB values', (2,)),
47     )
48     options = (
49         {'id': 'type', 'desc': 'RGB or RGBW', 'default': 'RGB',
50          'values': ('RGB', 'RGBW')},
51     )
52
53     def __init__(self):
54         self.reset()
55
56     def reset(self):
57         self.samplerate = None
58         self.oldpin = None
59         self.ss_packet = None
60         self.ss = None
61         self.es = None
62         self.bits = []
63         self.inreset = False
64
65     def start(self):
66         self.out_ann = self.register(srd.OUTPUT_ANN)
67
68     def metadata(self, key, value):
69         if key == srd.SRD_CONF_SAMPLERATE:
70             self.samplerate = value
71
72     def handle_bits(self, samplenum):
73         if self.options['type'] == 'RGB':
74             if len(self.bits) == 24:
75                 grb = reduce(lambda a, b: (a << 1) | b, self.bits)
76                 rgb = (grb & 0xff0000) >> 8 | (grb & 0x00ff00) << 8 | (grb & 0x0000ff)
77                 self.put(self.ss_packet, samplenum, self.out_ann,
78                          [2, ['#%06x' % rgb]])
79                 self.bits = []
80                 self.ss_packet = None
81         else:
82             if len(self.bits) == 32:
83                 grb = reduce(lambda a, b: (a << 1) | b, self.bits)
84                 rgb = (grb & 0xff0000) >> 8 | (grb & 0x00ff00) << 8 | (grb & 0xff0000ff)
85                 self.put(self.ss_packet, samplenum, self.out_ann,
86                          [2, ['#%08x' % rgb]])
87                 self.bits = []
88                 self.ss_packet = None
89
90     def decode(self):
91         if not self.samplerate:
92             raise SamplerateError('Cannot decode without samplerate.')
93
94         while True:
95             # TODO: Come up with more appropriate self.wait() conditions.
96             (pin,) = self.wait()
97
98             if self.oldpin is None:
99                 self.oldpin = pin
100                 continue
101
102             # Check RESET condition (manufacturer recommends 50 usec minimal,
103             # but real minimum is ~10 usec).
104             if not self.inreset and not pin and self.es is not None and \
105                     self.ss is not None and \
106                     (self.samplenum - self.es) / self.samplerate > 50e-6:
107
108                 # Decode last bit value.
109                 tH = (self.es - self.ss) / self.samplerate
110                 bit_ = True if tH >= 625e-9 else False
111
112                 self.bits.append(bit_)
113                 self.handle_bits(self.es)
114
115                 self.put(self.ss, self.es, self.out_ann, [0, ['%d' % bit_]])
116                 self.put(self.es, self.samplenum, self.out_ann,
117                          [1, ['RESET', 'RST', 'R']])
118
119                 self.inreset = True
120                 self.bits = []
121                 self.ss_packet = None
122                 self.ss = None
123
124             if not self.oldpin and pin:
125                 # Rising edge.
126                 if self.ss and self.es:
127                     period = self.samplenum - self.ss
128                     duty = self.es - self.ss
129                     # Ideal duty for T0H: 33%, T1H: 66%.
130                     bit_ = (duty / period) > 0.5
131
132                     self.put(self.ss, self.samplenum, self.out_ann,
133                              [0, ['%d' % bit_]])
134
135                     self.bits.append(bit_)
136                     self.handle_bits(self.samplenum)
137
138                 if self.ss_packet is None:
139                     self.ss_packet = self.samplenum
140
141                 self.ss = self.samplenum
142
143             elif self.oldpin and not pin:
144                 # Falling edge.
145                 self.inreset = False
146                 self.es = self.samplenum
147
148             self.oldpin = pin