3
3
##
4
4
## Copyright (C) 2016 Vladimir Ermakov <[email protected] >
5
5
## Copyright (C) 2019 DreamSourceLab <[email protected] >
6
+ ## Copyright (C) 2021 Michael Miller <[email protected] >
6
7
##
7
8
## This program is free software; you can redistribute it and/or modify
8
9
## it under the terms of the GNU General Public License as published by
@@ -27,16 +28,22 @@ class SamplerateError(Exception):
27
28
class Decoder (srd .Decoder ):
28
29
api_version = 3
29
30
id = 'rgb_led_ws281x'
30
- name = 'RGB LED (WS281x) '
31
- longname = 'RGB LED string decoder (WS281x) '
32
- desc = 'RGB LED string protocol (WS281x) .'
31
+ name = 'RGB LED WS2812+ '
32
+ longname = 'RGB LED color decoder'
33
+ desc = 'Decodes colors from bus pulses for single wire RGB leds like APA106, WS2811, WS2812, WS2813, SK6812, TM1829, TM1814, and TX1812 .'
33
34
license = 'gplv3+'
34
35
inputs = ['logic' ]
35
36
outputs = []
36
37
tags = ['Display' , 'IC' ]
37
38
channels = (
38
39
{'id' : 'din' , 'name' : 'DIN' , 'desc' : 'DIN data line' },
39
40
)
41
+ options = (
42
+ {'id' : 'colors' , 'desc' : 'Colors' , 'default' : 'GRB' ,
43
+ 'values' : ( 'GRB' , 'RGB' , 'BRG' , 'RBG' , 'BGR' , 'GRBW' , 'RGBW' , 'WRGB' , 'LBGR' , 'LGRB' , 'LRGB' , 'LRBG' , 'LGBR' , 'LBRG' )},
44
+ {'id' : 'polarity' , 'desc' : 'Polarity' , 'default' : 'normal' ,
45
+ 'values' : ('normal' , 'inverted' )},
46
+ )
40
47
annotations = (
41
48
('bit' , 'Bit' ),
42
49
('reset' , 'RESET' ),
@@ -58,22 +65,67 @@ def reset(self):
58
65
self .es = None
59
66
self .bits = []
60
67
self .bit_ = None
68
+ self .colorsize = None
61
69
62
70
def start (self ):
63
71
self .out_ann = self .register (srd .OUTPUT_ANN )
64
72
73
+
65
74
def metadata (self , key , value ):
66
75
if key == srd .SRD_CONF_SAMPLERATE :
67
76
self .samplerate = value
68
77
69
78
def handle_bits (self , samplenum ):
70
- if len (self .bits ) == 24 :
71
- grb = reduce (lambda a , b : (a << 1 ) | b , self .bits )
72
- rgb = (grb & 0xff0000 ) >> 8 | (grb & 0x00ff00 ) << 8 | (grb & 0x0000ff )
73
- self .put (self .ss_packet , samplenum , self .out_ann ,
74
- [2 , ['#%06x' % rgb ]])
75
- self .bits = []
76
- self .ss_packet = samplenum
79
+ if len (self .bits ) == self .colorsize :
80
+ elems = reduce (lambda a , b : (a << 1 ) | b , self .bits )
81
+ if self .colorsize == 24 :
82
+ if self .options ['colors' ] == 'GRB' :
83
+ rgb = (elems & 0xff0000 ) >> 8 | (elems & 0x00ff00 ) << 8 | (elems & 0x0000ff )
84
+ elif self .options ['colors' ] == 'RGB' :
85
+ rgb = elems
86
+ elif self .options ['colors' ] == 'BRG' :
87
+ rgb = (elems & 0xff0000 ) >> 16 | (elems & 0x00ffff ) << 8
88
+ elif self .options ['colors' ] == 'RBG' :
89
+ rgb = (elems & 0xff0000 ) | (elems & 0x00ff00 ) >> 8 | (elems & 0x0000ff ) << 8
90
+ elif self .options ['colors' ] == 'BGR' :
91
+ rgb = (elems & 0xff0000 ) >> 16 | (elems & 0x00ff00 ) | (elems & 0x0000ff ) << 16
92
+
93
+ self .put (self .ss_packet , samplenum , self .out_ann ,
94
+ [2 , ['RGB#%06x' % rgb ]])
95
+ else :
96
+ if self .options ['colors' ] == 'GRBW' :
97
+ rgb = (elems & 0xff000000 ) >> 16 | (elems & 0x00ff0000 ) | (elems & 0x0000ff00 ) >> 8
98
+ w = (elems & 0x000000ff )
99
+ elif self .options ['colors' ] == 'RGBW' :
100
+ rgb = (elems & 0xffffff00 ) >> 8
101
+ w = (elems & 0x000000ff )
102
+ elif self .options ['colors' ] == 'WRGB' :
103
+ rgb = (elems & 0x00ffffff )
104
+ w = (elems & 0xff000000 ) >> 32
105
+ elif self .options ['colors' ] == 'LBGR' :
106
+ rgb = (elems & 0x0000ff00 ) | (elems & 0x00ff0000 ) >> 16 | (elems & 0x000000ff ) << 16
107
+ w = (elems & 0xff000000 ) >> 32
108
+ elif self .options ['colors' ] == 'LGRB' :
109
+ rgb = (elems & 0x000000ff ) | (elems & 0x00ff0000 ) >> 8 | (elems & 0x0000ff00 ) << 8
110
+ w = (elems & 0xff000000 ) >> 32
111
+ elif self .options ['colors' ] == 'LRGB' :
112
+ rgb = (elems & 0x00ffffff )
113
+ w = (elems & 0xff000000 ) >> 32
114
+ elif self .options ['colors' ] == 'LRBG' :
115
+ rgb = (elems & 0x00ff0000 ) | (elems & 0x0000ff00 ) >> 8 | (elems & 0x000000ff ) << 8
116
+ w = (elems & 0xff000000 ) >> 32
117
+ elif self .options ['colors' ] == 'LGBR' :
118
+ rgb = (elems & 0x00ffff00 ) >> 8 | (elems & 0x000000ff ) << 16
119
+ w = (elems & 0xff000000 ) >> 32
120
+ elif self .options ['colors' ] == 'LBRG' :
121
+ rgb = (elems & 0x00ff0000 ) >> 16 | (elems & 0x0000ffff ) << 8
122
+ w = (elems & 0xff000000 ) >> 32
123
+
124
+ self .put (self .ss_packet , samplenum , self .out_ann ,
125
+ [2 , ['RGB#%06x #%02x' % (rgb , w )]])
126
+
127
+ self .bits = []
128
+ self .ss_packet = samplenum
77
129
78
130
def check_bit_ (self , samplenum ):
79
131
period = samplenum - self .ss
@@ -90,30 +142,35 @@ def decode(self):
90
142
if not self .samplerate :
91
143
raise SamplerateError ('Cannot decode without samplerate.' )
92
144
145
+ if len (self .options ['colors' ]) == 4 :
146
+ self .colorsize = 32
147
+ else :
148
+ self .colorsize = 24
149
+
93
150
while True :
94
151
if self .state == 'FIND RESET' :
95
- self .wait ({0 : 'l' })
152
+ self .wait ({0 : 'l' if self . options [ 'polarity' ] == 'normal' else 'h' })
96
153
self .ss = self .samplenum
97
- self .wait ({0 : 'r ' })
154
+ self .wait ({0 : 'e ' })
98
155
self .es = self .samplenum
99
156
if ((self .es - self .ss ) / self .samplerate > 50e-6 ):
100
157
self .state = 'RESET'
101
158
elif ((self .es - self .ss ) / self .samplerate > 3e-6 ):
102
159
self .bits = []
103
160
self .ss = self .samplenum
104
161
self .ss_packet = self .samplenum
105
- self .wait ({0 : 'f ' })
162
+ self .wait ({0 : 'e ' })
106
163
self .state = 'BIT FALLING'
107
164
elif self .state == 'RESET' :
108
165
self .put (self .ss , self .es , self .out_ann , [1 , ['RESET' , 'RST' , 'R' ]])
109
166
self .bits = []
110
167
self .ss = self .samplenum
111
168
self .ss_packet = self .samplenum
112
- self .wait ({0 : 'f ' })
169
+ self .wait ({0 : 'e ' })
113
170
self .state = 'BIT FALLING'
114
171
elif self .state == 'BIT FALLING' :
115
172
self .es = self .samplenum
116
- self .wait ({0 : 'r ' })
173
+ self .wait ({0 : 'e ' })
117
174
if ((self .samplenum - self .es ) / self .samplerate > 50e-6 ):
118
175
self .check_bit_ (self .samplenum )
119
176
self .put (self .ss , self .es , self .out_ann ,
@@ -134,5 +191,5 @@ def decode(self):
134
191
self .handle_bits (self .samplenum )
135
192
136
193
self .ss = self .samplenum
137
- self .wait ({0 : 'f ' })
194
+ self .wait ({0 : 'e ' })
138
195
self .state = 'BIT FALLING'
0 commit comments