C/C++ library for reading dualshock4 controllers on windows and linux supports button input, rumble, led colors, gyro/accel data, and reading battery level
add to your 'CMakeLists.txt':
include(cmake/CPM.cmake)
CPMAddPackage(
NAME libds4
GITHUB_REPOSITORY Tm24sense/libds4
GIT_TAG main
)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE libds4::ds4pp)create xmake.lua:
set_project("my_app")
set_languages("c++17")
include("external/libds4")-- or wherever you installed the repo to
target("my_app")
set_kind("binary")
add_files("src/*.cpp")
add_packages("libds4") --Or add_packages("ds4pp") for c++ wrapper#include <ds4pp/Device.hpp>
#include <iostream>
#include <thread>
int main() {
DS4::DualShock4 controller;
controller.Connect();
while (true) {
controller.Update();
if (controller.IsButtonPressed(ControllerButton::PS)) {
break;
}
auto [gx, gy, gz] = controller.GetGyroData();
std::cout << "Gyro: " << gx << ", " << gy << ", " << gz << "\n";
controller.Rumble(200, 100,
std::chrono::milliseconds(50));
controller.SetLed(255, 0, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
return 0;
}#include <ds4/ds4.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
int main() {
ds4_handle* controller = ds4_open_device();
if (!controller) {
printf("Failed to open device\n");
return 1;
}
while (true) {
ds4_state state = ds4_update(controller);
if (state.PS_Button) {
break;
}
ds4_motion_t gyro = ds4_gyro_query(&state);
printf("Gyro: %d, %d, %d\n", gyro.x, gyro.y, gyro.z);
ds4_message msg = ds4_begin_message();
ds4_set_vibration(&msg, 200, 100);
ds4_set_led(&msg, 255, 0, 0);
ds4_send_commands(controller, &msg);
}
ds4_destroy_handle(controller); // always destory the handle cuz it can prevent the controller from being recognized
return 0;
}controller.Update();
if (controller.IsButtonPressed(ControllerButton::Circle)) {
std::cout << "Circle pressed\n";
}ds4_state state = ds4_update(controller);
if (ds4_button_pressed(&state, DS_BTN_Circle)) {
printf("Circle pressed\n");
}controller.Rumble(255 /*max*/, 255,
std::chrono::milliseconds(100));
controller.EndRumble();ds4_message msg = ds4_begin_message();
ds4_set_vibration(&msg, 255, 128);
ds4_send_commands(controller, &msg);controller.SetLed(255, 0, 0); // red
controller.EnableFlash(true); // You always need to enable flash before setting flash or else it wont work
controller.SetFlash(50, 200);ds4_message msg = ds4_begin_message();
ds4_set_led(&msg, 255, 0, 0);
ds4_enable_flash(&msg, 50, 200, true);//same here
ds4_send_commands(controller, &msg);auto [gx, gy, gz] = controller.GetGyroData();
auto [ax, ay, az] = controller.GetAccelData();
uint8_t battery = controller.GetBatteryLevel();ds4_state state = ds4_update(controller);
ds4_motion_t gyro = ds4_gyro_query(&state);
ds4_motion_t accel = ds4_accel_query(&state);
uint8_t battery = state.battery_level;Clone and build with CMake also be sure to set BUILD_SHARED
git clone https://github.com/Tm24sense/libds4.git
cd libds4
mkdir build && cd build
cmake .. -DBUILD_SHARED=ON
cmake --build . --config ReleaseOr with xmake:
xmake build -aBuild with tests:
cmake -DBUILD_TESTS=ON ..
cmake --build .
./cpp_testOr with xmake:
xmake f --build_tests=y
xmake build -a
xmake run cpp_test