Brian Silverman | c206573 | 2015-11-28 22:55:30 +0000 | [diff] [blame] | 1 | #ifndef AOS_INPUT_JOYSTICK_INPUT_H_ |
| 2 | #define AOS_INPUT_JOYSTICK_INPUT_H_ |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 3 | |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 4 | #include <atomic> |
| 5 | |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame^] | 6 | #include "aos/events/event-loop.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 7 | #include "aos/input/driver_station_data.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 8 | |
| 9 | namespace aos { |
| 10 | namespace input { |
| 11 | |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 12 | // A class for handling joystick packet values. |
| 13 | // It will call RunIteration each time a new packet is received. |
| 14 | // |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 15 | // This class automatically handles updating ::aos::joystick_state and logging |
| 16 | // (at INFO) button edges. |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 17 | class JoystickInput { |
| 18 | public: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame^] | 19 | 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 Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 28 | void Run(); |
| 29 | |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame^] | 30 | protected: |
| 31 | int mode() const { return mode_; } |
| 32 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 33 | private: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame^] | 34 | void HandleData(const ::aos::JoystickState &joystick_state); |
| 35 | |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 36 | // Subclasses should do whatever they want with data here. |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 37 | virtual void RunIteration(const driver_station::Data &data) = 0; |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 38 | |
| 39 | static void Quit(int /*signum*/); |
| 40 | |
| 41 | static ::std::atomic<bool> run_; |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame^] | 42 | |
| 43 | EventLoop *event_loop_; |
| 44 | driver_station::Data data_; |
| 45 | |
| 46 | int mode_; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 49 | // Class which will proxy joystick information from UDP packets to the queues. |
| 50 | class JoystickProxy { |
| 51 | public: |
| 52 | void Run(); |
| 53 | }; |
| 54 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 55 | } // namespace input |
| 56 | } // namespace aos |
| 57 | |
Brian Silverman | c206573 | 2015-11-28 22:55:30 +0000 | [diff] [blame] | 58 | #endif // AOS_INPUT_JOYSTICK_INPUT_H_ |