]>
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 | ||
26 | class 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'] | |
6cbba91f | 34 | outputs = [] |
2787cf2a | 35 | tags = ['Display', 'IC'] |
66fc6e7c UH |
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 | ||
92b7b49f | 49 | def __init__(self): |
10aeb8ea GS |
50 | self.reset() |
51 | ||
52 | def reset(self): | |
66fc6e7c UH |
53 | self.samplerate = None |
54 | self.oldpin = None | |
5b0b88ce | 55 | self.ss_packet = None |
66fc6e7c UH |
56 | self.ss = None |
57 | self.es = None | |
58 | self.bits = [] | |
59 | self.inreset = False | |
60 | ||
61 | def start(self): | |
62 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
63 | ||
64 | def metadata(self, key, value): | |
65 | if key == srd.SRD_CONF_SAMPLERATE: | |
66 | self.samplerate = value | |
67 | ||
68 | def handle_bits(self, samplenum): | |
69 | if len(self.bits) == 24: | |
70 | grb = reduce(lambda a, b: (a << 1) | b, self.bits) | |
71 | rgb = (grb & 0xff0000) >> 8 | (grb & 0x00ff00) << 8 | (grb & 0x0000ff) | |
5b0b88ce | 72 | self.put(self.ss_packet, samplenum, self.out_ann, |
66fc6e7c UH |
73 | [2, ['#%06x' % rgb]]) |
74 | self.bits = [] | |
5b0b88ce | 75 | self.ss_packet = None |
66fc6e7c | 76 | |
7a85bbbe | 77 | def decode(self): |
66fc6e7c UH |
78 | if not self.samplerate: |
79 | raise SamplerateError('Cannot decode without samplerate.') | |
80 | ||
7a85bbbe UH |
81 | while True: |
82 | # TODO: Come up with more appropriate self.wait() conditions. | |
1b9ef18b | 83 | (pin,) = self.wait() |
7a85bbbe | 84 | |
66fc6e7c UH |
85 | if self.oldpin is None: |
86 | self.oldpin = pin | |
87 | continue | |
88 | ||
89 | # Check RESET condition (manufacturer recommends 50 usec minimal, | |
90 | # but real minimum is ~10 usec). | |
91 | if not self.inreset and not pin and self.es is not None and \ | |
ee60935a | 92 | (self.samplenum - self.es) / self.samplerate > 50e-6: |
66fc6e7c UH |
93 | |
94 | # Decode last bit value. | |
95 | tH = (self.es - self.ss) / self.samplerate | |
96 | bit_ = True if tH >= 625e-9 else False | |
97 | ||
98 | self.bits.append(bit_) | |
99 | self.handle_bits(self.es) | |
100 | ||
101 | self.put(self.ss, self.es, self.out_ann, [0, ['%d' % bit_]]) | |
ee60935a | 102 | self.put(self.es, self.samplenum, self.out_ann, |
66fc6e7c UH |
103 | [1, ['RESET', 'RST', 'R']]) |
104 | ||
105 | self.inreset = True | |
106 | self.bits = [] | |
5b0b88ce | 107 | self.ss_packet = None |
66fc6e7c UH |
108 | self.ss = None |
109 | ||
110 | if not self.oldpin and pin: | |
111 | # Rising edge. | |
112 | if self.ss and self.es: | |
ee60935a | 113 | period = self.samplenum - self.ss |
66fc6e7c UH |
114 | duty = self.es - self.ss |
115 | # Ideal duty for T0H: 33%, T1H: 66%. | |
116 | bit_ = (duty / period) > 0.5 | |
117 | ||
ee60935a | 118 | self.put(self.ss, self.samplenum, self.out_ann, |
66fc6e7c UH |
119 | [0, ['%d' % bit_]]) |
120 | ||
121 | self.bits.append(bit_) | |
ee60935a | 122 | self.handle_bits(self.samplenum) |
66fc6e7c | 123 | |
5b0b88ce | 124 | if self.ss_packet is None: |
ee60935a | 125 | self.ss_packet = self.samplenum |
66fc6e7c | 126 | |
ee60935a | 127 | self.ss = self.samplenum |
66fc6e7c UH |
128 | |
129 | elif self.oldpin and not pin: | |
130 | # Falling edge. | |
131 | self.inreset = False | |
ee60935a | 132 | self.es = self.samplenum |
66fc6e7c UH |
133 | |
134 | self.oldpin = pin |