vulp  2.3.0
Joystick.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2022 Stéphane Caron
3 
4 #pragma once
5 
6 #include <fcntl.h>
7 #include <linux/joystick.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 
11 #include <string>
12 
14 
16 
18 constexpr double kJoystickDeadband = 0.1;
19 
27 class Joystick : public Source {
28  public:
33  Joystick(const std::string& device_path = "/dev/input/js0");
34 
36  ~Joystick() override;
37 
39  inline bool present() const noexcept { return (fd_ >= 0); }
40 
42  inline std::string prefix() const noexcept final { return "joystick"; }
43 
48  void write(Dictionary& output) final;
49 
50  private:
52  void read_event();
53 
54  private:
56  int fd_;
57 
59  struct js_event event_;
60 
62  Eigen::Vector2d left_axis_ = Eigen::Vector2d::Zero();
63 
65  double left_trigger_ = -1.0;
66 
68  Eigen::Vector2d right_axis_ = Eigen::Vector2d::Zero();
69 
71  double right_trigger_ = -1.0;
72 
74  Eigen::Vector2d pad_axis_ = Eigen::Vector2d::Zero();
75 
77  bool cross_button_ = false;
78 
80  bool left_button_ = false;
81 
83  bool right_button_ = false;
84 
86  bool square_button_ = false;
87 
89  bool triangle_button_ = false;
90 };
91 
92 } // namespace vulp::observation::sources
Base class for sources.
Definition: Source.h:20
Source for a joystick controller.
Definition: Joystick.h:27
std::string prefix() const noexcept final
Prefix of output in the observation dictionary.
Definition: Joystick.h:42
~Joystick() override
Close device file.
Definition: Joystick.cpp:16
bool present() const noexcept
Check if the device file was opened successfully.
Definition: Joystick.h:39
void write(Dictionary &output) final
Write output to a dictionary.
Definition: Joystick.cpp:115
Joystick(const std::string &device_path="/dev/input/js0")
Open the device file.
Definition: Joystick.cpp:8
constexpr double kJoystickDeadband
Deadband between 0.0 and 1.0.
Definition: Joystick.h:18