TechTheTime-HighLevel 0.0.1
The high level robot's code using ros2-foxy for the robotics competition(Erobot-2022)
joystick.hpp
Go to the documentation of this file.
1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// Copyright Drew Noakes 2013-2016
14
15#ifndef __JOYSTICK_HH__
16#define __JOYSTICK_HH__
17
18#include <string>
19#include <iostream>
20
21#define JS_EVENT_BUTTON 0x01 // button pressed/released
22#define JS_EVENT_AXIS 0x02 // joystick moved
23#define JS_EVENT_INIT 0x80 // initial state of device
24
29{
30public:
32 static const short MIN_AXES_VALUE = -32768;
33
35 static const short MAX_AXES_VALUE = 32767;
36
40 unsigned int time;
41
47 short value;
48
52 unsigned char type;
53
57 unsigned char number;
58
62 bool isButton()
63 {
64 return (type & JS_EVENT_BUTTON) != 0;
65 }
66
70 bool isAxis()
71 {
72 return (type & JS_EVENT_AXIS) != 0;
73 }
74
80 {
81 return (type & JS_EVENT_INIT) != 0;
82 }
83
88 friend std::ostream& operator<<(std::ostream& os, const JoystickEvent& e);
89};
90
95std::ostream& operator<<(std::ostream& os, const JoystickEvent& e);
96
101{
102private:
103 void openPath(std::string devicePath, bool blocking=false);
104
105 int _fd;
106
107public:
108 ~Joystick();
109
113 Joystick();
114
119 Joystick(int joystickNumber);
120
124 Joystick(std::string devicePath);
125
129 Joystick(Joystick const&) = delete;
130
134 Joystick(Joystick &&) = default;
135
140 Joystick(std::string devicePath, bool blocking);
141
145 bool isFound();
146
151 bool sample(JoystickEvent* event);
152};
153
154#endif
Encapsulates all data relevant to a sampled joystick event.
Definition: joystick.hpp:29
friend std::ostream & operator<<(std::ostream &os, const JoystickEvent &e)
The ostream inserter needs to be a friend so it can access the internal data structures.
Definition: joystick.cpp:75
bool isInitialState()
Returns true if this event is part of the initial state obtained when the joystick is first connected...
Definition: joystick.hpp:79
unsigned int time
The timestamp of the event, in milliseconds.
Definition: joystick.hpp:40
short value
The value associated with this joystick event.
Definition: joystick.hpp:47
bool isAxis()
Returns true if this event is the result of an axis movement.
Definition: joystick.hpp:70
bool isButton()
Returns true if this event is the result of a button press.
Definition: joystick.hpp:62
unsigned char type
The event type.
Definition: joystick.hpp:52
unsigned char number
The axis/button number.
Definition: joystick.hpp:57
static const short MAX_AXES_VALUE
Maximum value of axes range.
Definition: joystick.hpp:35
static const short MIN_AXES_VALUE
Minimum value of axes range.
Definition: joystick.hpp:32
Represents a joystick device.
Definition: joystick.hpp:101
Joystick(Joystick const &)=delete
Joystick objects cannot be copied.
bool isFound()
Returns true if the joystick was found and may be used, otherwise false.
Definition: joystick.cpp:65
int _fd
Definition: joystick.hpp:105
Joystick(Joystick &&)=default
Joystick objects can be moved.
~Joystick()
Definition: joystick.cpp:70
Joystick()
Initialises an instance for the first joystick: /dev/input/js0.
Definition: joystick.cpp:25
void openPath(std::string devicePath, bool blocking=false)
Definition: joystick.cpp:47
bool sample(JoystickEvent *event)
Attempts to populate the provided JoystickEvent instance with data from the joystick.
Definition: joystick.cpp:53
#define JS_EVENT_INIT
Definition: joystick.hpp:23
std::ostream & operator<<(std::ostream &os, const JoystickEvent &e)
Stream insertion function so you can do this: cout << event << endl;.
Definition: joystick.cpp:75
#define JS_EVENT_BUTTON
Definition: joystick.hpp:21
#define JS_EVENT_AXIS
Definition: joystick.hpp:22