2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2016 Vladimir Ermakov <vooon341@gmail.com>
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.
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.
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/>.
20 import sigrokdecode as srd
21 from functools import reduce
23 class SamplerateError(Exception):
26 ( ANN_BIT, ANN_RESET, ANN_RGB, ) = range(3)
28 class Decoder(srd.Decoder):
31 name = 'RGB LED (WS281x)'
32 longname = 'RGB LED string decoder (WS281x)'
33 desc = 'RGB LED string protocol (WS281x).'
37 tags = ['Display', 'IC']
39 {'id': 'din', 'name': 'DIN', 'desc': 'DIN data line'},
47 ('bits', 'Bits', (ANN_BIT, ANN_RESET,)),
48 ('rgb-vals', 'RGB values', (ANN_RGB,)),
51 {'id': 'type', 'desc': 'RGB or RGBW', 'default': 'RGB',
52 'values': ('RGB', 'RGBW')},
59 self.samplerate = None
68 self.out_ann = self.register(srd.OUTPUT_ANN)
70 def metadata(self, key, value):
71 if key == srd.SRD_CONF_SAMPLERATE:
72 self.samplerate = value
74 def putg(self, ss, es, cls, text):
75 self.put(ss, es, self.out_ann, [cls, text])
77 def handle_bits(self, samplenum):
78 if self.options['type'] == 'RGB':
79 if len(self.bits) == 24:
80 grb = reduce(lambda a, b: (a << 1) | b, self.bits)
81 rgb = (grb & 0xff0000) >> 8 | (grb & 0x00ff00) << 8 | (grb & 0x0000ff)
82 text = ['#{:06x}'.format(rgb)]
83 self.putg(self.ss_packet, samplenum, ANN_RGB, text)
87 if len(self.bits) == 32:
88 grb = reduce(lambda a, b: (a << 1) | b, self.bits)
89 rgb = (grb & 0xff0000) >> 8 | (grb & 0x00ff00) << 8 | (grb & 0xff0000ff)
90 text = ['#{:08x}'.format(rgb)]
91 self.putg(self.ss_packet, samplenum, ANN_RGB, text)
96 if not self.samplerate:
97 raise SamplerateError('Cannot decode without samplerate.')
100 # TODO: Come up with more appropriate self.wait() conditions.
103 if self.oldpin is None:
107 # Check RESET condition (manufacturer recommends 50 usec minimal,
108 # but real minimum is ~10 usec).
109 if not self.inreset and not pin and self.es is not None and \
110 self.ss is not None and \
111 (self.samplenum - self.es) / self.samplerate > 50e-6:
113 # Decode last bit value.
114 tH = (self.es - self.ss) / self.samplerate
115 bit_ = True if tH >= 625e-9 else False
117 self.bits.append(bit_)
118 self.handle_bits(self.es)
120 text = ['{:d}'.format(bit_)]
121 self.putg(self.ss, self.es, ANN_BIT, text)
122 text = ['RESET', 'RST', 'R']
123 self.putg(self.es, self.samplenum, ANN_RESET, text)
127 self.ss_packet = None
130 if not self.oldpin and pin:
132 if self.ss and self.es:
133 period = self.samplenum - self.ss
134 duty = self.es - self.ss
135 # Ideal duty for T0H: 33%, T1H: 66%.
136 bit_ = (duty / period) > 0.5
138 text = ['{:d}'.format(bit_)]
139 self.putg(self.ss, self.samplenum, ANN_BIT, text)
141 self.bits.append(bit_)
142 self.handle_bits(self.samplenum)
144 if self.ss_packet is None:
145 self.ss_packet = self.samplenum
147 self.ss = self.samplenum
149 elif self.oldpin and not pin:
152 self.es = self.samplenum