From: Gerhard Sittig Date: Fri, 28 Jul 2023 06:08:38 +0000 (+0200) Subject: nes_gamepad: adjust Python style of decoder implementation X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=8f1dd70dd15916813186135bae71aa0683851da9;p=libsigrokdecode.git nes_gamepad: adjust Python style of decoder implementation Rephrase the NES gamepad decoder to become more idiomatic Python. Address nits to better match the sigrok project's coding style. Eliminate hidden coupling by means of instance variables. --- diff --git a/decoders/nes_gamepad/pd.py b/decoders/nes_gamepad/pd.py index b276e5d..a393abf 100644 --- a/decoders/nes_gamepad/pd.py +++ b/decoders/nes_gamepad/pd.py @@ -51,24 +51,22 @@ class Decoder(srd.Decoder): def reset(self): self.variant = None - self.ss_block = None - self.es_block = None def start(self): self.out_ann = self.register(srd.OUTPUT_ANN) self.variant = self.options['variant'] - def putx(self, data): - self.put(self.ss_block, self.es_block, self.out_ann, data) + def putg(self, ss, es, cls, text): + self.put(ss, es, self.out_ann, [cls, [text]]) - def handle_data(self, value): - if value == 0xFF: - self.putx([1, ['No button is pressed']]) - return + def handle_data(self, ss, es, value): + if value == 0xff: + self.putg(ss, es, 1, 'No button is pressed') + return if value == 0x00: - self.putx([2, ['Gamepad is not connected']]) - return + self.putg(ss, es, 2, 'Gamepad is not connected') + return buttons = [ 'A', @@ -78,28 +76,17 @@ class Decoder(srd.Decoder): 'North', 'South', 'West', - 'East' + 'East', ] - bits = format(value, '08b') - button_str = '' - - for b in enumerate(bits): - button_index = b[0] - button_is_pressed = b[1] == '0' - - if button_is_pressed: - if button_str != '': - button_str += ' + ' - button_str += buttons[button_index] - - self.putx([0, ['%s' % button_str]]) + bits = '{:08b}'.format(value) + text = [buttons[i] for i, b in enumerate(bits) if b == '0'] + text = ' + '.join(text) + self.putg(ss, es, 0, text) def decode(self, ss, es, data): - ptype, mosi, miso = data - self.ss_block, self.es_block = ss, es - - if ptype != 'DATA': - return - - self.handle_data(miso) + ptype, _, _ = data + if ptype == 'DATA': + _, _, miso = data + self.handle_data(ss, es, miso) + return