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