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 state'),
40 ('no-press', 'No button press'),
41 ('not-connected', 'Gamepad unconnected')
44 ('buttons', 'Button states', (0,)),
45 ('no-presses', 'No button presses', (1,)),
46 ('not-connected-vals', 'Gamepad unconnected', (2,)),
56 self
.out_ann
= self
.register(srd
.OUTPUT_ANN
)
57 self
.variant
= self
.options
['variant']
59 def putg(self
, ss
, es
, cls
, text
):
60 self
.put(ss
, es
, self
.out_ann
, [cls
, [text
]])
62 def handle_data(self
, ss
, es
, value
):
64 self
.putg(ss
, es
, 1, 'No button is pressed')
68 self
.putg(ss
, es
, 2, 'Gamepad is not connected')
82 bits
= '{:08b}'.format(value
)
83 text
= [buttons
[i
] for i
, b
in enumerate(bits
) if b
== '0']
84 text
= ' + '.join(text
)
85 self
.putg(ss
, es
, 0, text
)
87 def decode(self
, ss
, es
, data
):
91 self
.handle_data(ss
, es
, miso
)