]> sigrok.org Git - libsigrokdecode.git/blame - decoders/rgb_led_spi/pd.py
rgb_led_spi: adjust decoder implementation's Python style
[libsigrokdecode.git] / decoders / rgb_led_spi / pd.py
CommitLineData
2c173272
MR
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2014 Matt Ranostay <mranostay@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 2 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/>.
2c173272
MR
18##
19
20import sigrokdecode as srd
21
bb5af7af
GS
22( ANN_RGB, ) = range(1)
23
2c173272 24class Decoder(srd.Decoder):
b197383c 25 api_version = 3
ae036d34 26 id = 'rgb_led_spi'
e85b0440
UH
27 name = 'RGB LED (SPI)'
28 longname = 'RGB LED string decoder (SPI)'
a7c8dc5e 29 desc = 'RGB LED string protocol (RGB values clocked over SPI).'
9eac0fe3 30 license = 'gplv2+'
2c173272 31 inputs = ['spi']
6cbba91f 32 outputs = []
d6d8a8a4 33 tags = ['Display']
da9bcbd9 34 annotations = (
e144452b 35 ('rgb', 'RGB value'),
da9bcbd9 36 )
2c173272 37
92b7b49f 38 def __init__(self):
10aeb8ea
GS
39 self.reset()
40
41 def reset(self):
bb5af7af 42 self.ss_cmd = None
2c173272
MR
43 self.mosi_bytes = []
44
45 def start(self):
46 self.out_ann = self.register(srd.OUTPUT_ANN)
47
bb5af7af
GS
48 def putg(self, ss, es, cls, text):
49 self.put(ss, es, self.out_ann, [cls, text])
2c173272
MR
50
51 def decode(self, ss, es, data):
bb5af7af 52 ptype = data[0]
2c173272 53
bb5af7af
GS
54 # Grab the payload of three DATA packets. These hold the
55 # RGB values (in this very order).
2c173272
MR
56 if ptype != 'DATA':
57 return
bb5af7af
GS
58 _, mosi, _ = data
59 if not self.mosi_bytes:
486b19ce 60 self.ss_cmd = ss
2c173272 61 self.mosi_bytes.append(mosi)
bb5af7af 62 if len(self.mosi_bytes) < 3:
2c173272
MR
63 return
64
bb5af7af
GS
65 # Emit annotations. Invalidate accumulated details as soon as
66 # they were processed, to prepare the next iteration.
67 ss_cmd, es_cmd = self.ss_cmd, es
68 self.ss_cmd = None
69 red, green, blue = self.mosi_bytes[:3]
70 self.mosi_bytes.clear()
35b380b1 71 rgb_value = int(red) << 16 | int(green) << 8 | int(blue)
bb5af7af 72 self.putg(ss_cmd, es_cmd, ANN_RGB, ['#{:06x}'.format(rgb_value)])