Simple test¶
Ensure your device works with this simple test.
examples/ps2controller_simpletest.py¶
1# SPDX-FileCopyrightText: Copyright (c) 2023 Tod Kurt
2#
3# SPDX-License-Identifier: Unlicense
4
5import board
6from ps2controller import PS2Controller
7
8# one way to wire this up, for example on a Pico
9ps2 = PS2Controller(dat=board.GP2, cmd=board.GP3, att=board.GP4, clk=board.GP5)
10
11print("hi press buttons")
12while True:
13 events = ps2.update()
14 if events:
15 print("events", events)
16 print("sticks: L:", ps2.analog_left(), "R:", ps2.analog_right())
USB HID with PSX controller¶
This example uses a button-to-key map to let you send keybaord commands with a PS2 controller.
examples/ps2controller_usbkeys.py¶
1# SPDX-FileCopyrightText: Copyright (c) 2023 Tod Kurt
2#
3# SPDX-License-Identifier: MIT
4
5import board
6import usb_hid
7from adafruit_hid.keyboard import Keyboard
8from adafruit_hid.keycode import Keycode
9from ps2controller import PS2Controller
10
11buttons_to_keys = {
12 "UP": (Keycode.W,), # WASD for D-pad
13 "DOWN": (Keycode.S,),
14 "LEFT": (Keycode.A,),
15 "RIGHT": (Keycode.D,),
16 "TRIANGLE": (Keycode.K,), # HJKL for action buttons
17 "CROSS": (Keycode.J,),
18 "SQUARE": (Keycode.H,),
19 "CIRCLE": (Keycode.L,),
20}
21
22keyboard = Keyboard(usb_hid.devices)
23
24# one way to wire this up, for example on a Pico
25ps2 = PS2Controller(dat=board.GP2, cmd=board.GP3, att=board.GP4, clk=board.GP5)
26
27print("hi, press buttons")
28while True:
29 events = ps2.update()
30 if events:
31 print("events", events)
32 for event in events:
33 # find keys to send for button, undefined buttons send SPACE
34 keycodes = buttons_to_keys.get(event.name, (Keycode.SPACE,))
35 if event.pressed:
36 keyboard.press(*keycodes)
37 elif event.released:
38 keyboard.release(*keycodes)
Controller data dump¶
Show all the data returned from the controller
examples/ps2controller_datadump.py¶
1# SPDX-FileCopyrightText: Copyright (c) 2023 Tod Kurt
2#
3# SPDX-License-Identifier: Unlicense
4# fmt: off
5
6import board
7from ps2controller import PS2Controller
8
9# one way to wire this up, for example on a Pico
10ps2 = PS2Controller(dat=board.GP2, cmd=board.GP3, att=board.GP4, clk=board.GP5,
11 enable_pressure=True)
12
13print("be prepared to be spammed, this is raw data from the controller")
14while True:
15 events = ps2.update()
16 print("dt:%2d" % int(ps2.last_dt*1000),
17 "data:",' '.join("%2x" % v for v in ps2.data),
18 "events:",len(events) if events is not None else 'None')