2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2019 Stephan Thiele <stephan.thiele@mailbox.org>
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
22 class Decoder(srd.Decoder):
26 longname = 'Nintendo Entertainment System gamepad'
27 desc = 'NES gamepad button states.'
31 tags = ['Retro computing']
33 # Currently only the standard controller is supported. This might be
34 # extended by special controllers like the Nintendo Zapper light gun.
35 {'id': 'variant', 'desc': 'Gamepad variant',
36 'default': 'Standard gamepad', 'values': ('Standard gamepad',)},
39 ('button', 'Button states'),
40 ('no-press', 'No button press'),
41 ('not-connected', 'Gamepad unconnected')
44 ('buttons', 'Button states', (0,)),
45 ('no-press', 'No button press', (1,)),
46 ('not-connected', 'Gamepad unconnected', (2,)),
58 self.out_ann = self.register(srd.OUTPUT_ANN)
59 self.variant = self.options['variant']
62 self.put(self.ss_block, self.es_block, self.out_ann, data)
64 def handle_data(self, value):
66 self.putx([1, ['No button is pressed']])
70 self.putx([2, ['Gamepad is not connected']])
84 bits = format(value, '08b')
87 for b in enumerate(bits):
89 button_is_pressed = b[1] == '0'
94 button_str += buttons[button_index]
96 self.putx([0, ['%s' % button_str]])
98 def decode(self, ss, es, data):
99 ptype, mosi, miso = data
100 self.ss_block, self.es_block = ss, es
105 self.handle_data(miso)