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