ps2controller

CircuitPython library to read Sony PS1 and PS2 wired game controllers

  • Author(s): Tod Kurt

Implementation Notes

Hardware:

  • Sony PS1 or PS2 controller or compatible device

Software and Dependencies:

class ps2controller.PS2Button

PS2 Button constants mapping button name to number

CIRCLE = 13

Circle (red) action button, digital and analog (pressure)

CROSS = 14

Cross (blue) action button, digital and analog (pressure)

DOWN = 6

D-pad DOWN, digital and analog (pressure)

L1 = 10

Left upper shoulder trigger button, digital and analog (pressure)

L2 = 8

Left lower shoulder trigger button, digital and analog (pressure)

L3 = 1

L3 button (left stick push), digital only

LEFT = 7

D-pad LEFT, digital and analog (pressure)

R1 = 11

Right upper shoulder trigger button, digital and analog (pressure)

R2 = 9

Right lower shoulder trigger button, digital and analog (pressure)

R3 = 2

R3 button (right stick push), digital only

RIGHT = 5

D-pad RIGHT, digital and analog (pressure)

SELECT = 0

SELECT button, digital only

SQUARE = 15

Square (pink) action button, digital and analog (pressure)

START = 3

START button, digital only (pressure)

TRIANGLE = 12

Triangle (green) action button, digital and analog (pressure)

UP = 4

D-pad UP, digital and analog (pressure)

button_to_analog_id = (-1, -1, -1, -1, 11, 9, 12, 10, 19, 20, 17, 18, 13, 14, 15, 16)

Map digital button id to button name

class ps2controller.PS2ButtonEvent(id, pressed, released, name)

The event ‘objects’ returned by ps2.update()

id

Alias for field number 0

name

Alias for field number 3

pressed

Alias for field number 1

released

Alias for field number 2

class ps2controller.PS2Controller(clk, cmd, att, dat, enable_sticks=True, enable_rumble=False, enable_pressure=False)

Driver to read and control a Sony PS1 or PS2 wired controller

Parameters:
  • clk (Pin) – PS2 clock pin (blue wire)

  • cmd (Pin) – PS2 command pin (orange wire)

  • att (Pin) – PS2 attention pin (yellow wire)

  • dat (Pin) – PS2 data pin (brown wire)

  • enable_sticks (bool) – True to enable analog sticks (otherwise faster digital only reads)

  • enable_rumble (bool) – True to enable controlling rumble motors (needs extra voltage and current)

  • enable_pressue (bool) – True to enable reading analog button pressure

analog_button(button_id)

Return analog pressure (0-255) value for button, if in pressure mode, otherwise -1.

:param int button_id 0-15 id number PS2ButtonEvent.id or PS2Button.*

analog_left()

Return (x,y) tuple (0-255,0-255) of the left analog stick, if present.

analog_right()

Return (x,y) tuple (0-255,0-255) of the right analog stick, if present.

button(button_id)

Return True if button is currently pressed

Parameters:

button_id (int) – 0-15 id number from PS2ButtonEvent.id or PS2Button.*

buttons()

Return a 16-bit bitfield of the state of all buttons

connected()

Returns True if controller was connected as of last update()

read()

Read from the controller. Must be called frequently. Called by update().

update()

Read the controller and return a list of PS2ButtonEvents. Must be called frequently or the controller disconnects.