Skip to content

Added set_led method for Joystick and Controller objects #3507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 29, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions buildconfig/stubs/pygame/_sdl2/controller.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import final

from pygame.joystick import JoystickType
from pygame.typing import ColorLike

def init() -> None: ...
def get_init() -> bool: ...
Expand Down Expand Up @@ -30,3 +31,4 @@ class Controller:
self, low_frequency: float, high_frequency: float, duration: int
) -> bool: ...
def stop_rumble(self) -> None: ...
def set_led(self, color: ColorLike) -> bool: ...
2 changes: 2 additions & 0 deletions buildconfig/stubs/pygame/joystick.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import final

from pygame.typing import ColorLike
from typing_extensions import deprecated # added in 3.13

def init() -> None: ...
Expand Down Expand Up @@ -31,6 +32,7 @@ class JoystickType:
self, low_frequency: float, high_frequency: float, duration: int
) -> bool: ...
def stop_rumble(self) -> None: ...
def set_led(self, color: ColorLike) -> bool: ...

# according to the current implementation, Joystick is a function that returns
# a JoystickType instance. In the future, when the C implementation is fixed to
Expand Down
16 changes: 16 additions & 0 deletions docs/reST/ref/joystick.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,22 @@ variable. See :ref:`environment variables <environment-variables>` for more deta

.. ## Joystick.stop_rumble ##

.. method:: set_led

| :sl:`Set the LED color of the joystick`
| :sg:`set_led(color_arg) -> bool`

Set the color of the LED on the joystick. The argument is a
``pygame.Color``-compatible value (alpha being ignored). The
joystick's LED, if it has one, will be set to the input color.
If the joystick does not have an addressable LED, then this
method will do nothing and return False. Returns True if the
LED was set successfully.

.. versionadded:: 2.5.6

.. ## Joystick.set_led ##

.. ## pygame.joystick.Joystick ##

.. ## pygame.joystick ##
Expand Down
16 changes: 16 additions & 0 deletions docs/reST/ref/sdl2_controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,20 @@ events related to controllers.

.. ## Controller.stop_rumble ##

.. method:: set_led

| :sl:`Set the LED color of the controller`
| :sg:`set_led(color_arg) -> bool`

Set the color of the LED on the controller. The argument is a
``pygame.Color``-compatible value (alpha being ignored). The
controller's LED, if it has one, will be set to the input color.
If the controller does not have an addressable LED, then this
method will do nothing and return False. Returns True if the
LED was set successfully.

.. versionadded:: 2.5.6

.. ## Controller.set_led ##

.. ## pygame._sdl2.controller ##
35 changes: 35 additions & 0 deletions src_c/_sdl2/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,35 @@ controller_stop_rumble(pgControllerObject *self, PyObject *_null)
Py_RETURN_NONE;
}

static PyObject*
controller_set_led(pgControllerObject *self, PyObject *arg)
{
CONTROLLER_INIT_CHECK();

Uint8 colors[4] = {0, 0, 0, 0};

if (!pg_RGBAFromObjEx(arg, colors, PG_COLOR_HANDLE_ALL)) {
return RAISE(PyExc_TypeError,
"set_led must be passed in a Color-compatible argument");
}

#if !SDL_VERSION_ATLEAST(3, 0, 0)
if (SDL_GameControllerSetLED(self->controller, colors[0], colors[1], colors[2]) < 0) {
Py_RETURN_FALSE;
}
Py_RETURN_TRUE;
#else
// SDL3 renames the function and sets an error message on failure
bool result = SDL_SetGamepadLED(self->controller, colors[0], colors[1], colors[2]);
if (!result) {
// Clear the SDL error message that SDL set, for example if it didn't
// have an addressable LED
(void)SDL_GetError();
}
return PyBool_FromLong(result);
#endif
}

static PyMethodDef controller_methods[] = {
{"from_joystick", (PyCFunction)controller_from_joystick,
METH_CLASS | METH_VARARGS | METH_KEYWORDS,
Expand All @@ -414,6 +443,7 @@ static PyMethodDef controller_methods[] = {
DOC_SDL2_CONTROLLER_CONTROLLER_RUMBLE},
{"stop_rumble", (PyCFunction)controller_stop_rumble, METH_NOARGS,
DOC_SDL2_CONTROLLER_CONTROLLER_STOPRUMBLE},
{"set_led", (PyCFunction)controller_set_led, METH_O, DOC_SDL2_CONTROLLER_CONTROLLER_SETLED},
{NULL, NULL, 0, NULL}};

static PyMemberDef controller_members[] = {
Expand Down Expand Up @@ -569,6 +599,11 @@ MODINIT_DEFINE(controller)
return NULL;
}

import_pygame_color();
if (PyErr_Occurred()) {
return NULL;
}

import_pygame_joystick();
if (PyErr_Occurred()) {
return NULL;
Expand Down
1 change: 1 addition & 0 deletions src_c/doc/joystick_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
#define DOC_JOYSTICK_JOYSTICK_GETHAT "get_hat(hat_number, /) -> x, y\nget the position of a joystick hat"
#define DOC_JOYSTICK_JOYSTICK_RUMBLE "rumble(low_frequency, high_frequency, duration) -> bool\nStart a rumbling effect"
#define DOC_JOYSTICK_JOYSTICK_STOPRUMBLE "stop_rumble() -> None\nStop any rumble effect playing"
#define DOC_JOYSTICK_JOYSTICK_SETLED "set_led(color_arg) -> bool\nSet the LED color of the joystick"
1 change: 1 addition & 0 deletions src_c/doc/sdl2_controller_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
#define DOC_SDL2_CONTROLLER_CONTROLLER_SETMAPPING "set_mapping(mapping) -> int\nAssign a mapping to the controller"
#define DOC_SDL2_CONTROLLER_CONTROLLER_RUMBLE "rumble(low_frequency, high_frequency, duration) -> bool\nStart a rumbling effect"
#define DOC_SDL2_CONTROLLER_CONTROLLER_STOPRUMBLE "stop_rumble() -> None\nStop any rumble effect playing"
#define DOC_SDL2_CONTROLLER_CONTROLLER_SETLED "set_led(color_arg) -> bool\nSet the LED color of the controller"
40 changes: 40 additions & 0 deletions src_c/joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,40 @@ joy_get_hat(PyObject *self, PyObject *args)
return pg_tuple_couple_from_values_int(px, py);
}

static PyObject *
joy_set_led(PyObject *self, PyObject *arg)
{
SDL_Joystick *joy = pgJoystick_AsSDL(self);

JOYSTICK_INIT_CHECK();
if (!joy) {
return RAISE(pgExc_SDLError, "Joystick not initialized");
}

Uint8 colors[4] = {0, 0, 0, 0};

if (!pg_RGBAFromObjEx(arg, colors, PG_COLOR_HANDLE_ALL)) {
return RAISE(PyExc_TypeError,
"set_led must be passed in a Color-compatible argument");
}

#if !SDL_VERSION_ATLEAST(3, 0, 0)
if (SDL_JoystickSetLED(joy, colors[0], colors[1], colors[2]) < 0) {
Py_RETURN_FALSE;
}
Py_RETURN_TRUE;
#else
// SDL3 renames the function and sets an error message on failure
bool result = SDL_SetJoystickLED(joy, colors[0], colors[1], colors[2]);
if (!result) {
// Clear the SDL error message that SDL set, for example if it didn't
// have an addressable LED
(void)SDL_GetError();
}
return PyBool_FromLong(result);
#endif
}

static PyMethodDef joy_methods[] = {
{"init", joy_init, METH_NOARGS, DOC_JOYSTICK_JOYSTICK_INIT},
{"quit", joy_quit, METH_NOARGS, DOC_JOYSTICK_JOYSTICK_QUIT},
Expand Down Expand Up @@ -560,6 +594,7 @@ static PyMethodDef joy_methods[] = {
{"get_numhats", joy_get_numhats, METH_NOARGS,
DOC_JOYSTICK_JOYSTICK_GETNUMHATS},
{"get_hat", joy_get_hat, METH_VARARGS, DOC_JOYSTICK_JOYSTICK_GETHAT},
{"set_led", joy_set_led, METH_O, DOC_JOYSTICK_JOYSTICK_SETLED},

{NULL, NULL, 0, NULL}};

Expand Down Expand Up @@ -665,6 +700,11 @@ MODINIT_DEFINE(joystick)
return NULL;
}

import_pygame_color();
if (PyErr_Occurred()) {
return NULL;
}

/* type preparation */
if (PyType_Ready(&pgJoystick_Type) == -1) {
return NULL;
Expand Down
Loading