blob: 7a6e8c35a08109f6b03ff2c2378df3ccb92993d0 [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
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#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(
Austin Schuhed5b26d2019-12-05 20:51:59 -080022 "/aos", [this](const ::aos::JoystickState &joystick_state) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070023 this->HandleData(&joystick_state);
Austin Schuh3e45c752019-02-02 12:19:11 -080024 });
Austin Schuh9fe68f72019-08-10 19:32:03 -070025 event_loop->SetRuntimeRealtimePriority(29);
Austin Schuh3e45c752019-02-02 12:19:11 -080026 }
27
Austin Schuh3e45c752019-02-02 12:19:11 -080028 protected:
29 int mode() const { return mode_; }
30
Brian Silvermanba3de7e2013-05-08 16:18:15 -070031 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -070032 void HandleData(const ::aos::JoystickState *joystick_state);
Austin Schuh3e45c752019-02-02 12:19:11 -080033
Brian Silvermanc25bc892013-05-09 19:09:34 -070034 // Subclasses should do whatever they want with data here.
Brian Silvermanba3de7e2013-05-08 16:18:15 -070035 virtual void RunIteration(const driver_station::Data &data) = 0;
Austin Schuhb58ceb62017-02-05 14:21:57 -080036
Austin Schuh3e45c752019-02-02 12:19:11 -080037 EventLoop *event_loop_;
38 driver_station::Data data_;
39
40 int mode_;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070041};
42
43} // namespace input
44} // namespace aos
45
Brian Silvermanc2065732015-11-28 22:55:30 +000046#endif // AOS_INPUT_JOYSTICK_INPUT_H_