trikRuntime
gamepad.h
Go to the documentation of this file.
1 /* Copyright 2013 - 2016 Matvey Bryksin, Yurii Litvinov and CyberTech Labs Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14 
15 #pragma once
16 
17 #include <QtCore/QScopedPointer>
18 #include <QtCore/QThread>
19 #include <QtCore/QHash>
20 #include <QtCore/QSet>
21 #include <QtCore/QTimer>
22 
23 #include <trikKernel/configurer.h>
25 
26 #include "deviceInterface.h"
27 #include "fifo.h"
28 #include "gamepadInterface.h"
29 
30 namespace trikControl {
31 
34 {
35  Q_OBJECT
36 
37 public:
39  Gamepad(const trikKernel::Configurer &configurer
40  , const trikHal::HardwareAbstractionInterface &hardwareAbstraction);
41 
42  ~Gamepad() override = default;
43 
44  Status status() const override;
45 
46 public slots:
47  void reset() override;
48 
49  bool buttonWasPressed(int buttonNumber) override;
50 
51  bool buttonIsPressed(int buttonNumber) override;
52 
53  bool isPadPressed(int pad) const override;
54 
55  int padX(int pad) const override;
56 
57  int padY(int pad) const override;
58 
59  int wheel() const override;
60 
61  bool isConnected() const override;
62 
63  bool disconnect() override;
64 
65 private slots:
66  void onNewData(const QString &data);
67 
68  void onButtonStateClearTimerTimeout();
69 
70 private:
71  Q_DISABLE_COPY(Gamepad)
72 
73  struct PadStatus {
74  int x;
75  int y;
76  bool isPressed;
77  };
78 
79  void handlePadUp(int padId);
80 
81  void handleWheel(int percent);
82 
83  void handlePad(int padId, int x, int y);
84 
85  void handleButton(int button, int pressed);
86 
87  void handleKeepalive(int waitForMs);
88 
89  void handleCustom(const QString &message);
90 
91  Fifo mUnderlyingFifo;
92 
93  QSet<int> mButtonWasPressed;
94  QHash<int, bool> mButtonState;
95 
99  QHash<int, QTimer*> mButtonStateClearTimers;
100 
101  QHash<int, PadStatus> mPads;
102  int mWheelPercent { -101 };
103 
104  QTimer mKeepaliveTimer;
105  QString mLastCustomMessage;
106 
107  bool mConnected { false };
108 };
109 
110 }
int wheel() const override
Definition: gamepad.cpp:66
Gamepad(const trikKernel::Configurer &configurer, const trikHal::HardwareAbstractionInterface &hardwareAbstraction)
Constructor.
Definition: gamepad.cpp:21
Generic configuration helper, parses configuration XML files and presents configuration as a set of a...
Definition: configurer.h:28
void pad(int pad, int x, int y)
Emitted when user pushes or moves his finger on a pad.
void button(int button, int pressed)
Emitted when user pushes a button on gamepad.
Class that represents linux FIFO file, which is commonly used by various sensors. ...
Definition: fifo.h:35
void reset() override
Definition: gamepad.cpp:30
Status status() const override
Returns current status of the device.
Definition: gamepad.cpp:36
bool buttonIsPressed(int buttonNumber) override
Definition: gamepad.cpp:46
int padY(int pad) const override
Definition: gamepad.cpp:61
bool isConnected() const override
Definition: gamepad.cpp:71
Status
Device status.
Definition: deviceInterface.h:33
Definition: trikCommunicator.h:27
bool buttonWasPressed(int buttonNumber) override
Definition: gamepad.cpp:41
Remote control of a robot via Android gamepad.
Definition: gamepadInterface.h:24
~Gamepad() override=default
Implementation of remote control interface.
Definition: gamepad.h:33
int padX(int pad) const override
Definition: gamepad.cpp:56
TRIK device interface, base interface for everything that can be plugged to a brick or already on boa...
Definition: deviceInterface.h:23
var x
Definition: file-test.js:2
bool isPadPressed(int pad) const override
Definition: gamepad.cpp:51
var y
Definition: file-test.js:5
bool disconnect() override
Definition: gamepad.cpp:76
Hardware abstraction, provides devices that are used to communicate with robot hardware or emulate it...
Definition: hardwareAbstractionInterface.h:29