]> sigrok.org Git - libsigrokdecode.git/blob - decoders/nes_gamepad/pd.py
All PDs: Consistently use singular/plural for annotation classes/rows.
[libsigrokdecode.git] / decoders / nes_gamepad / pd.py
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.
10 ##
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.
15 ##
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/>.
18 ##
19
20 import sigrokdecode as srd
21
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',)},
37     )
38     annotations = (
39         ('button', 'Button state'),
40         ('no-press', 'No button press'),
41         ('not-connected', 'Gamepad unconnected')
42     )
43     annotation_rows = (
44         ('buttons', 'Button states', (0,)),
45         ('no-presses', 'No button presses', (1,)),
46         ('not-connected-vals', 'Gamepad unconnected', (2,)),
47     )
48
49     def __init__(self):
50         self.reset()
51
52     def reset(self):
53         self.variant = None
54         self.ss_block = None
55         self.es_block = None
56
57     def start(self):
58         self.out_ann = self.register(srd.OUTPUT_ANN)
59         self.variant = self.options['variant']
60
61     def putx(self, data):
62         self.put(self.ss_block, self.es_block, self.out_ann, data)
63
64     def handle_data(self, value):
65         if value == 0xFF:
66           self.putx([1, ['No button is pressed']])
67           return
68
69         if value == 0x00:
70           self.putx([2, ['Gamepad is not connected']])
71           return
72
73         buttons = [
74             'A',
75             'B',
76             'Select',
77             'Start',
78             'North',
79             'South',
80             'West',
81             'East'
82         ]
83
84         bits = format(value, '08b')
85         button_str = ''
86
87         for b in enumerate(bits):
88             button_index = b[0]
89             button_is_pressed = b[1] == '0'
90
91             if button_is_pressed:
92                 if button_str != '':
93                     button_str += ' + '
94                 button_str += buttons[button_index]
95
96         self.putx([0, ['%s' % button_str]])
97
98     def decode(self, ss, es, data):
99         ptype, mosi, miso = data
100         self.ss_block, self.es_block = ss, es
101
102         if ptype != 'DATA':
103           return
104
105         self.handle_data(miso)