diff options
author | Gerhard Sittig <gerhard.sittig@gmx.net> | 2023-07-28 08:08:38 +0200 |
---|---|---|
committer | Gerhard Sittig <gerhard.sittig@gmx.net> | 2023-07-29 21:29:03 +0200 |
commit | 8f1dd70dd15916813186135bae71aa0683851da9 (patch) | |
tree | 7402e2df0294167746f9d1122b16080a4ceb0e8d /decoders/nes_gamepad/pd.py | |
parent | 782c35b0158a84bbdbcfd2fc2f0ed7e507f17650 (diff) | |
download | libsigrokdecode-8f1dd70dd15916813186135bae71aa0683851da9.tar.gz libsigrokdecode-8f1dd70dd15916813186135bae71aa0683851da9.zip |
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.
Diffstat (limited to 'decoders/nes_gamepad/pd.py')
-rw-r--r-- | decoders/nes_gamepad/pd.py | 49 |
1 files changed, 18 insertions, 31 deletions
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 |