diff options
author | Gerhard Sittig <gerhard.sittig@gmx.net> | 2023-07-30 17:03:39 +0200 |
---|---|---|
committer | Gerhard Sittig <gerhard.sittig@gmx.net> | 2023-07-30 17:06:58 +0200 |
commit | bb5af7af06b8fb1b37a88a03a94db3e35a7d15de (patch) | |
tree | 7f71f4d3907a5e2078a20eb549d4ad405a1c3bec /decoders | |
parent | e6962b3fe8260382bb9932a1cfdd7ee7090ce267 (diff) | |
download | libsigrokdecode-bb5af7af06b8fb1b37a88a03a94db3e35a7d15de.tar.gz libsigrokdecode-bb5af7af06b8fb1b37a88a03a94db3e35a7d15de.zip |
rgb_led_spi: adjust decoder implementation's Python style
Eliminate unneeded instance variables. Start from None when values are
unknown. Only access data after checking its availability. Invalidate
previously accumulated details after processing. Prefer .format() over
string modulo. Use list .clear() where applicable. Use symbolic names
for annotation classes.
Diffstat (limited to 'decoders')
-rw-r--r-- | decoders/rgb_led_spi/pd.py | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/decoders/rgb_led_spi/pd.py b/decoders/rgb_led_spi/pd.py index 82877b3..899a64a 100644 --- a/decoders/rgb_led_spi/pd.py +++ b/decoders/rgb_led_spi/pd.py @@ -19,6 +19,8 @@ import sigrokdecode as srd +( ANN_RGB, ) = range(1) + class Decoder(srd.Decoder): api_version = 3 id = 'rgb_led_spi' @@ -37,34 +39,34 @@ class Decoder(srd.Decoder): self.reset() def reset(self): - self.ss_cmd, self.es_cmd = 0, 0 + self.ss_cmd = None self.mosi_bytes = [] def start(self): self.out_ann = self.register(srd.OUTPUT_ANN) - def putx(self, data): - self.put(self.ss_cmd, self.es_cmd, self.out_ann, data) + def putg(self, ss, es, cls, text): + self.put(ss, es, self.out_ann, [cls, text]) def decode(self, ss, es, data): - ptype, mosi, miso = data + ptype = data[0] - # Only care about data packets. + # Grab the payload of three DATA packets. These hold the + # RGB values (in this very order). if ptype != 'DATA': return - self.ss, self.es = ss, es - - if len(self.mosi_bytes) == 0: + _, mosi, _ = data + if not self.mosi_bytes: self.ss_cmd = ss self.mosi_bytes.append(mosi) - - # RGB value == 3 bytes - if len(self.mosi_bytes) != 3: + if len(self.mosi_bytes) < 3: return - red, green, blue = self.mosi_bytes + # Emit annotations. Invalidate accumulated details as soon as + # they were processed, to prepare the next iteration. + ss_cmd, es_cmd = self.ss_cmd, es + self.ss_cmd = None + red, green, blue = self.mosi_bytes[:3] + self.mosi_bytes.clear() rgb_value = int(red) << 16 | int(green) << 8 | int(blue) - - self.es_cmd = es - self.putx([0, ['#%.6x' % rgb_value]]) - self.mosi_bytes = [] + self.putg(ss_cmd, es_cmd, ANN_RGB, ['#{:06x}'.format(rgb_value)]) |