Skip to content

Commit ab26b5c

Browse files
committed
Added Joystick.set_led
1 parent efd5615 commit ab26b5c

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

docs/reST/ref/joystick.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,21 @@ variable. See :ref:`environment variables <environment-variables>` for more deta
356356

357357
.. ## Joystick.stop_rumble ##
358358
359+
.. method:: set_led
360+
361+
| :sl:`Set the LED color of the joystick`
362+
| :sg:`set_led(color_arg) -> bool`
363+
364+
Set the color of the LED on the joystick. The argument is a ``pygame.Color``
365+
instance or a tuple of RGB(A) values (alpha being ignored). The color
366+
will be set to the joystick's LED, if it has one. If the joystick does
367+
not have an LED, then this method will do nothing and return False.
368+
Returns True if the LED was set successfully.
369+
370+
.. versionadded:: 2.5.6
371+
372+
.. ## Joystick.set_led ##
373+
359374
.. ## pygame.joystick.Joystick ##
360375
361376
.. ## pygame.joystick ##

src_c/doc/joystick_doc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@
2323
#define DOC_JOYSTICK_JOYSTICK_GETHAT "get_hat(hat_number, /) -> x, y\nget the position of a joystick hat"
2424
#define DOC_JOYSTICK_JOYSTICK_RUMBLE "rumble(low_frequency, high_frequency, duration) -> bool\nStart a rumbling effect"
2525
#define DOC_JOYSTICK_JOYSTICK_STOPRUMBLE "stop_rumble() -> None\nStop any rumble effect playing"
26+
#define DOC_JOYSTICK_JOYSTICK_SETLED "set_led(color_arg) -> bool\nSet the LED color of the joystick"

src_c/joystick.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,29 @@ joy_get_hat(PyObject *self, PyObject *args)
531531
return pg_tuple_couple_from_values_int(px, py);
532532
}
533533

534+
static PyObject *
535+
joy_set_led(PyObject *self, PyObject *arg)
536+
{
537+
SDL_Joystick *joy = pgJoystick_AsSDL(self);
538+
539+
JOYSTICK_INIT_CHECK();
540+
if (!joy) {
541+
return RAISE(pgExc_SDLError, "Joystick not initialized");
542+
}
543+
544+
Uint8 colors[4] = {0, 0, 0, 0};
545+
546+
if (!pg_RGBAFromObj(arg, colors)) {
547+
return RAISE(PyExc_TypeError,
548+
"set_led must be passed in a Color-compatible argument");
549+
}
550+
551+
if (SDL_JoystickSetLED(joy, colors[0], colors[1], colors[2]) < 0) {
552+
Py_RETURN_FALSE;
553+
}
554+
Py_RETURN_TRUE;
555+
}
556+
534557
static PyMethodDef joy_methods[] = {
535558
{"init", joy_init, METH_NOARGS, DOC_JOYSTICK_JOYSTICK_INIT},
536559
{"quit", joy_quit, METH_NOARGS, DOC_JOYSTICK_JOYSTICK_QUIT},
@@ -560,6 +583,7 @@ static PyMethodDef joy_methods[] = {
560583
{"get_numhats", joy_get_numhats, METH_NOARGS,
561584
DOC_JOYSTICK_JOYSTICK_GETNUMHATS},
562585
{"get_hat", joy_get_hat, METH_VARARGS, DOC_JOYSTICK_JOYSTICK_GETHAT},
586+
{"set_led", joy_set_led, METH_O, DOC_JOYSTICK_JOYSTICK_SETLED},
563587

564588
{NULL, NULL, 0, NULL}};
565589

0 commit comments

Comments
 (0)