]> sigrok.org Git - libsigrokdecode.git/blame - decoders/rgb_led_ws281x/pd.py
log: Use human-readable output type name everywhere.
[libsigrokdecode.git] / decoders / rgb_led_ws281x / pd.py
CommitLineData
66fc6e7c
UH
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
4539e9ca 17## along with this program; if not, see <http://www.gnu.org/licenses/>.
66fc6e7c
UH
18##
19
20import sigrokdecode as srd
21from functools import reduce
22
23class SamplerateError(Exception):
24 pass
25
26class Decoder(srd.Decoder):
7a85bbbe 27 api_version = 3
66fc6e7c
UH
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 = ['rgb_led_ws281x']
35 channels = (
36 {'id': 'din', 'name': 'DIN', 'desc': 'DIN data line'},
37 )
38 annotations = (
39 ('bit', 'Bit'),
40 ('reset', 'RESET'),
41 ('rgb', 'RGB'),
42 )
43 annotation_rows = (
44 ('bit', 'Bits', (0, 1)),
45 ('rgb', 'RGB', (2,)),
46 )
47
92b7b49f 48 def __init__(self):
10aeb8ea
GS
49 self.reset()
50
51 def reset(self):
66fc6e7c
UH
52 self.samplerate = None
53 self.oldpin = None
5b0b88ce 54 self.ss_packet = None
66fc6e7c
UH
55 self.ss = None
56 self.es = None
57 self.bits = []
58 self.inreset = False
59
60 def start(self):
61 self.out_ann = self.register(srd.OUTPUT_ANN)
62
63 def metadata(self, key, value):
64 if key == srd.SRD_CONF_SAMPLERATE:
65 self.samplerate = value
66
67 def handle_bits(self, samplenum):
68 if len(self.bits) == 24:
69 grb = reduce(lambda a, b: (a << 1) | b, self.bits)
70 rgb = (grb & 0xff0000) >> 8 | (grb & 0x00ff00) << 8 | (grb & 0x0000ff)
5b0b88ce 71 self.put(self.ss_packet, samplenum, self.out_ann,
66fc6e7c
UH
72 [2, ['#%06x' % rgb]])
73 self.bits = []
5b0b88ce 74 self.ss_packet = None
66fc6e7c 75
7a85bbbe 76 def decode(self):
66fc6e7c
UH
77 if not self.samplerate:
78 raise SamplerateError('Cannot decode without samplerate.')
79
7a85bbbe
UH
80 while True:
81 # TODO: Come up with more appropriate self.wait() conditions.
1b9ef18b 82 (pin,) = self.wait()
7a85bbbe 83
66fc6e7c
UH
84 if self.oldpin is None:
85 self.oldpin = pin
86 continue
87
88 # Check RESET condition (manufacturer recommends 50 usec minimal,
89 # but real minimum is ~10 usec).
90 if not self.inreset and not pin and self.es is not None and \
ee60935a 91 (self.samplenum - self.es) / self.samplerate > 50e-6:
66fc6e7c
UH
92
93 # Decode last bit value.
94 tH = (self.es - self.ss) / self.samplerate
95 bit_ = True if tH >= 625e-9 else False
96
97 self.bits.append(bit_)
98 self.handle_bits(self.es)
99
100 self.put(self.ss, self.es, self.out_ann, [0, ['%d' % bit_]])
ee60935a 101 self.put(self.es, self.samplenum, self.out_ann,
66fc6e7c
UH
102 [1, ['RESET', 'RST', 'R']])
103
104 self.inreset = True
105 self.bits = []
5b0b88ce 106 self.ss_packet = None
66fc6e7c
UH
107 self.ss = None
108
109 if not self.oldpin and pin:
110 # Rising edge.
111 if self.ss and self.es:
ee60935a 112 period = self.samplenum - self.ss
66fc6e7c
UH
113 duty = self.es - self.ss
114 # Ideal duty for T0H: 33%, T1H: 66%.
115 bit_ = (duty / period) > 0.5
116
ee60935a 117 self.put(self.ss, self.samplenum, self.out_ann,
66fc6e7c
UH
118 [0, ['%d' % bit_]])
119
120 self.bits.append(bit_)
ee60935a 121 self.handle_bits(self.samplenum)
66fc6e7c 122
5b0b88ce 123 if self.ss_packet is None:
ee60935a 124 self.ss_packet = self.samplenum
66fc6e7c 125
ee60935a 126 self.ss = self.samplenum
66fc6e7c
UH
127
128 elif self.oldpin and not pin:
129 # Falling edge.
130 self.inreset = False
ee60935a 131 self.es = self.samplenum
66fc6e7c
UH
132
133 self.oldpin = pin