Improve LED color calculation, don't set LED unless left thumbstick is moved
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
diff --git a/test/testgamecontroller.c b/test/testgamecontroller.c
index 9102314..6b0f512 100644
--- a/test/testgamecontroller.c
+++ b/test/testgamecontroller.c
@@ -65,6 +65,7 @@ SDL_Window *window = NULL;
SDL_Renderer *screen = NULL;
SDL_bool retval = SDL_FALSE;
SDL_bool done = SDL_FALSE;
+SDL_bool set_LED = SDL_FALSE;
SDL_Texture *background_front, *background_back, *button, *axis;
SDL_GameController *gamecontroller;
@@ -238,14 +239,30 @@ loop(void *arg)
/* Update LED based on left thumbstick position */
{
- Uint8 r, g, b;
Sint16 x = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_LEFTX);
Sint16 y = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_LEFTY);
- r = (Uint8)((float)(((int)x + 32767) * 255) / 65535);
- b = (Uint8)((float)(((int)y + 32767) * 255) / 65535);
- g = (Uint8)((int)(r + b) / 2);
- SDL_GameControllerSetLED(gamecontroller, r, g, b);
+ if (!set_LED) {
+ set_LED = (x < -8000 || x > 8000 || y > 8000);
+ }
+ if (set_LED) {
+ Uint8 r, g, b;
+
+ if (x < 0) {
+ r = (Uint8)(((int)(~x) * 255) / 32767);
+ b = 0;
+ } else {
+ r = 0;
+ b = (Uint8)(((int)(x) * 255) / 32767);
+ }
+ if (y > 0) {
+ g = (Uint8)(((int)(y) * 255) / 32767);
+ } else {
+ g = 0;
+ }
+
+ SDL_GameControllerSetLED(gamecontroller, r, g, b);
+ }
}
/* Update rumble based on trigger state */