]> sigrok.org Git - libsigrokdecode.git/blob - decoders/rgb_led_ws281x/pd.py
rgb_led_ws281x: refactor bit/bits handling, use more common code
[libsigrokdecode.git] / decoders / rgb_led_ws281x / pd.py
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 common.srdhelper import bitpack_msb
22
23 class SamplerateError(Exception):
24     pass
25
26 ( ANN_BIT, ANN_RESET, ANN_RGB, ) = range(3)
27
28 class Decoder(srd.Decoder):
29     api_version = 3
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']
36     outputs = []
37     tags = ['Display', 'IC']
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 = (
47         ('bits', 'Bits', (ANN_BIT, ANN_RESET,)),
48         ('rgb-vals', 'RGB values', (ANN_RGB,)),
49     )
50     options = (
51         {'id': 'type', 'desc': 'RGB or RGBW', 'default': 'RGB',
52          'values': ('RGB', 'RGBW')},
53     )
54
55     def __init__(self):
56         self.reset()
57
58     def reset(self):
59         self.samplerate = None
60         self.oldpin = None
61         self.ss = None
62         self.es = None
63         self.bits = []
64         self.inreset = False
65
66     def start(self):
67         self.out_ann = self.register(srd.OUTPUT_ANN)
68
69     def metadata(self, key, value):
70         if key == srd.SRD_CONF_SAMPLERATE:
71             self.samplerate = value
72
73     def putg(self, ss, es, cls, text):
74         self.put(ss, es, self.out_ann, [cls, text])
75
76     def handle_bits(self):
77         if len(self.bits) < self.need_bits:
78             return
79         grb = bitpack_msb(self.bits, 0)
80         if self.options['type'] == 'RGB':
81             rgb = (grb & 0xff0000) >> 8 | (grb & 0x00ff00) << 8 | (grb & 0x0000ff)
82             text = '#{:06x}'.format(rgb)
83         else:
84             rgb = (grb & 0xff0000) >> 8 | (grb & 0x00ff00) << 8 | (grb & 0xff0000ff)
85             text = '#{:08x}'.format(rgb)
86         ss_packet, es_packet = self.bits[0][1], self.bits[-1][2]
87         self.putg(ss_packet, es_packet, ANN_RGB, [text])
88         self.bits.clear()
89
90     def handle_bit(self, ss, es, value, ann_late = False):
91         if not ann_late:
92             text = ['{:d}'.format(value)]
93             self.putg(ss, es, ANN_BIT, text)
94         item = (value, ss, es)
95         self.bits.append(item)
96         self.handle_bits()
97         if ann_late:
98             text = ['{:d}'.format(value)]
99             self.putg(ss, es, ANN_BIT, text)
100
101     def decode(self):
102         if not self.samplerate:
103             raise SamplerateError('Cannot decode without samplerate.')
104         self.need_bits = len(self.options['type']) * 8
105
106         while True:
107             # TODO: Come up with more appropriate self.wait() conditions.
108             (pin,) = self.wait()
109
110             if self.oldpin is None:
111                 self.oldpin = pin
112                 continue
113
114             # Check RESET condition (manufacturer recommends 50 usec minimal,
115             # but real minimum is ~10 usec).
116             if not self.inreset and not pin and self.es is not None and \
117                     self.ss is not None and \
118                     (self.samplenum - self.es) / self.samplerate > 50e-6:
119
120                 # Decode last bit value.
121                 tH = (self.es - self.ss) / self.samplerate
122                 bit_ = True if tH >= 625e-9 else False
123
124                 self.handle_bit(self.ss, self.es, bit_, True)
125
126                 text = ['RESET', 'RST', 'R']
127                 self.putg(self.es, self.samplenum, ANN_RESET, text)
128
129                 self.inreset = True
130                 self.bits.clear()
131                 self.ss = None
132
133             if not self.oldpin and pin:
134                 # Rising edge.
135                 if self.ss and self.es:
136                     period = self.samplenum - self.ss
137                     duty = self.es - self.ss
138                     # Ideal duty for T0H: 33%, T1H: 66%.
139                     bit_ = (duty / period) > 0.5
140                     self.handle_bit(self.ss, self.samplenum, bit_)
141                 self.ss = self.samplenum
142
143             elif self.oldpin and not pin:
144                 # Falling edge.
145                 self.inreset = False
146                 self.es = self.samplenum
147
148             self.oldpin = pin