]> sigrok.org Git - libsigrokdecode.git/blob - decoders/rgb_led_spi/pd.py
decoders: Add/update tags for each PD.
[libsigrokdecode.git] / decoders / rgb_led_spi / pd.py
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
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21
22 class Decoder(srd.Decoder):
23     api_version = 3
24     id = 'rgb_led_spi'
25     name = 'RGB LED (SPI)'
26     longname = 'RGB LED string decoder (SPI)'
27     desc = 'RGB LED string protocol (RGB values clocked over SPI).'
28     license = 'gplv2+'
29     inputs = ['spi']
30     outputs = ['rgb_led_spi']
31     tags = ['Display']
32     annotations = (
33         ('rgb', 'RGB values'),
34     )
35
36     def __init__(self):
37         self.reset()
38
39     def reset(self):
40         self.ss_cmd, self.es_cmd = 0, 0
41         self.mosi_bytes = []
42
43     def start(self):
44         self.out_ann = self.register(srd.OUTPUT_ANN)
45
46     def putx(self, data):
47         self.put(self.ss_cmd, self.es_cmd, self.out_ann, data)
48
49     def decode(self, ss, es, data):
50         ptype, mosi, miso = data
51
52         # Only care about data packets.
53         if ptype != 'DATA':
54             return
55         self.ss, self.es = ss, es
56
57         if len(self.mosi_bytes) == 0:
58             self.ss_cmd = ss
59         self.mosi_bytes.append(mosi)
60
61         # RGB value == 3 bytes
62         if len(self.mosi_bytes) != 3:
63             return
64
65         red, green, blue = self.mosi_bytes
66         rgb_value = int(red) << 16 | int(green) << 8 | int(blue)
67
68         self.es_cmd = es
69         self.putx([0, ['#%.6x' % rgb_value]])
70         self.mosi_bytes = []