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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [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( |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame^] | 22 | "/aos", [this](const ::aos::JoystickState &joystick_state) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | this->HandleData(&joystick_state); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 24 | }); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 25 | event_loop->SetRuntimeRealtimePriority(29); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 26 | } |
| 27 | |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 28 | protected: |
| 29 | int mode() const { return mode_; } |
| 30 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 31 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 32 | void HandleData(const ::aos::JoystickState *joystick_state); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 33 | |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 34 | // Subclasses should do whatever they want with data here. |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 35 | virtual void RunIteration(const driver_station::Data &data) = 0; |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 36 | |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 37 | EventLoop *event_loop_; |
| 38 | driver_station::Data data_; |
| 39 | |
| 40 | int mode_; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | } // namespace input |
| 44 | } // namespace aos |
| 45 | |
Brian Silverman | c206573 | 2015-11-28 22:55:30 +0000 | [diff] [blame] | 46 | #endif // AOS_INPUT_JOYSTICK_INPUT_H_ |