blob: 5e8bf5acc90f90eab888fb4f766903382b879dcf [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(
22 ".aos.joystick_state",
23 [this](const ::aos::JoystickState &joystick_state) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070024 this->HandleData(&joystick_state);
Austin Schuh3e45c752019-02-02 12:19:11 -080025 });
Austin Schuh9fe68f72019-08-10 19:32:03 -070026 event_loop->SetRuntimeRealtimePriority(29);
Austin Schuh3e45c752019-02-02 12:19:11 -080027 }
28
Austin Schuh3e45c752019-02-02 12:19:11 -080029 protected:
30 int mode() const { return mode_; }
31
Brian Silvermanba3de7e2013-05-08 16:18:15 -070032 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 void HandleData(const ::aos::JoystickState *joystick_state);
Austin Schuh3e45c752019-02-02 12:19:11 -080034
Brian Silvermanc25bc892013-05-09 19:09:34 -070035 // Subclasses should do whatever they want with data here.
Brian Silvermanba3de7e2013-05-08 16:18:15 -070036 virtual void RunIteration(const driver_station::Data &data) = 0;
Austin Schuhb58ceb62017-02-05 14:21:57 -080037
Austin Schuh3e45c752019-02-02 12:19:11 -080038 EventLoop *event_loop_;
39 driver_station::Data data_;
40
41 int mode_;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070042};
43
44} // namespace input
45} // namespace aos
46
Brian Silvermanc2065732015-11-28 22:55:30 +000047#endif // AOS_INPUT_JOYSTICK_INPUT_H_