9 fd_ = ::open(device_path.c_str(), O_RDONLY | O_NONBLOCK);
11 spdlog::warn(
"[Joystick] Observer disabled: no joystick found at {}",
22 void Joystick::read_event() {
27 ssize_t bytes = ::read(fd_, &event_,
sizeof(event_));
28 if (bytes !=
sizeof(event_)) {
32 double normalized_value =
static_cast<double>(event_.value) / 32767;
34 normalized_value = 0.0;
37 switch (event_.type) {
39 switch (event_.number) {
41 cross_button_ = event_.value;
44 throw std::runtime_error(event_.value ?
"Stop button pressed"
45 :
"Stop button released");
48 triangle_button_ = event_.value;
51 square_button_ = event_.value;
54 left_button_ = event_.value;
57 right_button_ = event_.value;
74 spdlog::warn(
"Button number {} is out of range", event_.number);
79 switch (event_.number) {
81 left_axis_.x() = normalized_value;
84 left_axis_.y() = normalized_value;
87 left_trigger_ = normalized_value;
90 right_axis_.x() = normalized_value;
93 right_axis_.y() = normalized_value;
96 right_trigger_ = normalized_value;
99 pad_axis_.x() = normalized_value;
102 pad_axis_.y() = normalized_value;
105 spdlog::warn(
"Axis number {} is out of range", event_.number);
117 auto& output = observation(
prefix());
118 output(
"cross_button") = cross_button_;
119 output(
"left_axis") = left_axis_;
120 output(
"left_button") = left_button_;
121 output(
"left_trigger") = left_trigger_;
122 output(
"pad_axis") = pad_axis_;
123 output(
"right_axis") = right_axis_;
124 output(
"right_button") = right_button_;
125 output(
"right_trigger") = right_trigger_;
126 output(
"square_button") = square_button_;
127 output(
"triangle_button") = triangle_button_;
std::string prefix() const noexcept final
Prefix of output in the observation dictionary.
~Joystick() override
Close device file.
void write(Dictionary &output) final
Write output to a dictionary.
Joystick(const std::string &device_path="/dev/input/js0")
Open the device file.
constexpr double kJoystickDeadband
Deadband between 0.0 and 1.0.