setLedColor method

void setLedColor(
  1. int r,
  2. int g,
  3. int b
)

Commands the LED lightbar via 48-byte Output Reports (0x02).

r, g and b must be in the 0-255 range; out-of-range values are clamped automatically. Does nothing if no controller is connected.

Implementation

void setLedColor(int r, int g, int b) {
  if (_device == ffi.nullptr) return;

  final ffi.Pointer<ffi.Uint8> outBuffer = pkg_ffi.calloc<ffi.Uint8>(48);
  outBuffer[0] = 0x02; // Output report ID
  outBuffer[1] = 0xFF; // valid_flag0 (rumble/haptics select)
  outBuffer[2] = 0x04; // valid_flag1: LIGHTBAR_CONTROL_ENABLE — REQUIRED, or
  //                      the controller ignores the RGB below and the
  //                      lightbar never changes.
  outBuffer[45] = r.clamp(0, 255); // Red channel
  outBuffer[46] = g.clamp(0, 255); // Green channel
  outBuffer[47] = b.clamp(0, 255); // Blue channel

  _bindings.hidWrite(_device, outBuffer, 48);
  pkg_ffi.calloc.free(outBuffer);
}