From bb5af7af06b8fb1b37a88a03a94db3e35a7d15de Mon Sep 17 00:00:00 2001 From: Gerhard Sittig Date: Sun, 30 Jul 2023 17:03:39 +0200 Subject: 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. --- decoders/rgb_led_spi/pd.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'decoders') 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)]) -- cgit v1.2.3-70-g09d2