Skip to content

Commit

Permalink
rgb_led_ws281x: emit annotations for individual colour components
Browse files Browse the repository at this point in the history
The previous implementation presented bits and per-LED RGB-values (in
a from similar to HTML colours). This commit introduces annotations
for individual colour components (R/G/B/W) between these two levels
of abstraction.
  • Loading branch information
gsigh committed Jul 29, 2023
1 parent 6300d97 commit 3378f52
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions decoders/rgb_led_ws281x/pd.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class SamplerateError(Exception):
class DecoderError(Exception):
pass

( ANN_BIT, ANN_RESET, ANN_RGB, ) = range(3)
(
ANN_BIT, ANN_RESET, ANN_RGB,
ANN_COMP_R, ANN_COMP_G, ANN_COMP_B, ANN_COMP_W,
) = range(7)

class Decoder(srd.Decoder):
api_version = 3
Expand All @@ -73,9 +76,14 @@ class Decoder(srd.Decoder):
('bit', 'Bit'),
('reset', 'RESET'),
('rgb', 'RGB'),
('r', 'R'),
('g', 'G'),
('b', 'B'),
('w', 'W'),
)
annotation_rows = (
('bits', 'Bits', (ANN_BIT, ANN_RESET,)),
('rgb-comps', 'RGB components', (ANN_COMP_R, ANN_COMP_G, ANN_COMP_B, ANN_COMP_W,)),
('rgb-vals', 'RGB values', (ANN_RGB,)),
)
options = (
Expand Down Expand Up @@ -114,7 +122,12 @@ def handle_bits(self):
comp_bits = self.bits[first_idx:after_idx]
comp_ss, comp_es = comp_bits[0][1], comp_bits[-1][2]
comp_value = bitpack_msb(comp_bits, 0)
comp_item = (comp_ss, comp_es, comp_value)
comp_text = '{:02x}'.format(comp_value)
comp_ann = {
'r': ANN_COMP_R, 'g': ANN_COMP_G,
'b': ANN_COMP_B, 'w': ANN_COMP_W,
}.get(c.lower(), None)
comp_item = (comp_ss, comp_es, comp_ann, comp_value, comp_text)
comps.append(comp_item)
if c.lower() == 'r':
r = comp_value
Expand All @@ -126,10 +139,11 @@ def handle_bits(self):
w = comp_value
wt = '' if w is None else '{:02x}'.format(w)
if self.textformat == 'wire':
rgb_text = ['{:02x}'.format(c[-1]) for c in comps]
rgb_text = '#' + ''.join(rgb_text)
rgb_text = '#' + ''.join([c[-1] for c in comps])
else:
rgb_text = self.textformat.format(r = r, g = g, b = b, w = w, wt = wt)
for ss_comp, es_comp, cls_comp, value_comp, text_comp in comps:
self.putg(ss_comp, es_comp, cls_comp, [text_comp])
if rgb_text:
self.putg(ss_packet, es_packet, ANN_RGB, [rgb_text])
self.bits.clear()
Expand Down

0 comments on commit 3378f52

Please sign in to comment.