blob: 98a89b162a5150ee9e9da9101b46447adb1f108d [file] [log] [blame]
Brian Silvermanc2065732015-11-28 22:55:30 +00001#ifndef AOS_INPUT_JOYSTICK_INPUT_H_
2#define AOS_INPUT_JOYSTICK_INPUT_H_
Brian Silvermanba3de7e2013-05-08 16:18:15 -07003
Austin Schuhb58ceb62017-02-05 14:21:57 -08004#include <atomic>
5
Austin Schuh3e45c752019-02-02 12:19:11 -08006#include "aos/events/event-loop.h"
John Park33858a32018-09-28 23:05:48 -07007#include "aos/input/driver_station_data.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -07008
9namespace aos {
10namespace input {
11
Brian Silvermanc25bc892013-05-09 19:09:34 -070012// A class for handling joystick packet values.
13// It will call RunIteration each time a new packet is received.
14//
Brian Silverman699f0cb2015-02-05 19:45:01 -050015// This class automatically handles updating ::aos::joystick_state and logging
16// (at INFO) button edges.
Brian Silvermanba3de7e2013-05-08 16:18:15 -070017class JoystickInput {
18 public:
Austin Schuh3e45c752019-02-02 12:19:11 -080019 explicit JoystickInput(::aos::EventLoop *event_loop)
20 : event_loop_(event_loop) {
21 event_loop_->MakeWatcher(
22 ".aos.joystick_state",
23 [this](const ::aos::JoystickState &joystick_state) {
24 this->HandleData(joystick_state);
25 });
26 }
27
Brian Silvermanba3de7e2013-05-08 16:18:15 -070028 void Run();
29
Austin Schuh3e45c752019-02-02 12:19:11 -080030 protected:
31 int mode() const { return mode_; }
32
Brian Silvermanba3de7e2013-05-08 16:18:15 -070033 private:
Austin Schuh3e45c752019-02-02 12:19:11 -080034 void HandleData(const ::aos::JoystickState &joystick_state);
35
Brian Silvermanc25bc892013-05-09 19:09:34 -070036 // Subclasses should do whatever they want with data here.
Brian Silvermanba3de7e2013-05-08 16:18:15 -070037 virtual void RunIteration(const driver_station::Data &data) = 0;
Austin Schuhb58ceb62017-02-05 14:21:57 -080038
39 static void Quit(int /*signum*/);
40
41 static ::std::atomic<bool> run_;
Austin Schuh3e45c752019-02-02 12:19:11 -080042
43 EventLoop *event_loop_;
44 driver_station::Data data_;
45
46 int mode_;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070047};
48
Austin Schuh374fd172014-10-25 17:57:54 -070049// Class which will proxy joystick information from UDP packets to the queues.
50class JoystickProxy {
51 public:
52 void Run();
53};
54
Brian Silvermanba3de7e2013-05-08 16:18:15 -070055} // namespace input
56} // namespace aos
57
Brian Silvermanc2065732015-11-28 22:55:30 +000058#endif // AOS_INPUT_JOYSTICK_INPUT_H_