srd: drop deprecated PyEval_InitThreads() on Python 3.9+
[libsigrokdecode/gsi.git] / decoders / nes_gamepad / pd.py
bloba393abfac6b103fcc9d411b8b5afeb1dbf3aefbc
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2019 Stephan Thiele <stephan.thiele@mailbox.org>
5 ##
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):
23 api_version = 3
24 id = 'nes_gamepad'
25 name = 'NES gamepad'
26 longname = 'Nintendo Entertainment System gamepad'
27 desc = 'NES gamepad button states.'
28 license = 'gplv2+'
29 inputs = ['spi']
30 outputs = []
31 tags = ['Retro computing']
32 options = (
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',)},
38 annotations = (
39 ('button', 'Button state'),
40 ('no-press', 'No button press'),
41 ('not-connected', 'Gamepad unconnected')
43 annotation_rows = (
44 ('buttons', 'Button states', (0,)),
45 ('no-presses', 'No button presses', (1,)),
46 ('not-connected-vals', 'Gamepad unconnected', (2,)),
49 def __init__(self):
50 self.reset()
52 def reset(self):
53 self.variant = None
55 def start(self):
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):
63 if value == 0xff:
64 self.putg(ss, es, 1, 'No button is pressed')
65 return
67 if value == 0x00:
68 self.putg(ss, es, 2, 'Gamepad is not connected')
69 return
71 buttons = [
72 'A',
73 'B',
74 'Select',
75 'Start',
76 'North',
77 'South',
78 'West',
79 'East',
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):
88 ptype, _, _ = data
89 if ptype == 'DATA':
90 _, _, miso = data
91 self.handle_data(ss, es, miso)
92 return