Convert joystick_reader over to event loops.

Change-Id: I97b78254b0fc96853f162b66ef75ec97a6c9c50b
diff --git a/aos/input/BUILD b/aos/input/BUILD
index f27bf98..dd79c5e 100644
--- a/aos/input/BUILD
+++ b/aos/input/BUILD
@@ -1,49 +1,50 @@
-package(default_visibility = ['//visibility:public'])
+package(default_visibility = ["//visibility:public"])
 
 cc_library(
-  name = 'joystick_input',
-  srcs = [
-    'joystick_input.cc',
-  ],
-  hdrs = [
-    'joystick_input.h',
-  ],
-  deps = [
-    '//aos/input:driver_station_data',
-    '//aos/robot_state:robot_state',
-    '//aos/logging',
-    '//aos/logging:queue_logging',
-  ],
+    name = "joystick_input",
+    srcs = [
+        "joystick_input.cc",
+    ],
+    hdrs = [
+        "joystick_input.h",
+    ],
+    deps = [
+        "//aos/events:event-loop",
+        "//aos/input:driver_station_data",
+        "//aos/logging",
+        "//aos/logging:queue_logging",
+        "//aos/robot_state",
+    ],
 )
 
 cc_library(
-  name = 'drivetrain_input',
-  srcs = [
-    'drivetrain_input.cc'
-  ],
-  hdrs = [
-    'drivetrain_input.h',
-  ],
-  deps = [
-    '//aos/input:driver_station_data',
-    '//aos/logging',
-    '//aos/logging:queue_logging',
-    '//aos/robot_state:robot_state',
-    '//aos:math',
-    '//frc971/control_loops/drivetrain:drivetrain_queue',
-    '//frc971/control_loops/drivetrain:drivetrain_config',
-  ],
+    name = "drivetrain_input",
+    srcs = [
+        "drivetrain_input.cc",
+    ],
+    hdrs = [
+        "drivetrain_input.h",
+    ],
+    deps = [
+        "//aos:math",
+        "//aos/input:driver_station_data",
+        "//aos/logging",
+        "//aos/logging:queue_logging",
+        "//aos/robot_state",
+        "//frc971/control_loops/drivetrain:drivetrain_config",
+        "//frc971/control_loops/drivetrain:drivetrain_queue",
+    ],
 )
 
 cc_library(
-  name = 'driver_station_data',
-  srcs = [
-    'driver_station_data.cc',
-  ],
-  hdrs = [
-    'driver_station_data.h',
-  ],
-  deps = [
-    '//aos/robot_state:robot_state',
-  ],
+    name = "driver_station_data",
+    srcs = [
+        "driver_station_data.cc",
+    ],
+    hdrs = [
+        "driver_station_data.h",
+    ],
+    deps = [
+        "//aos/robot_state",
+    ],
 )
diff --git a/aos/input/joystick_input.cc b/aos/input/joystick_input.cc
index 1620fdb..a208f1e 100644
--- a/aos/input/joystick_input.cc
+++ b/aos/input/joystick_input.cc
@@ -14,7 +14,63 @@
 
 void JoystickInput::Quit(int /*signum*/) { run_ = false; }
 
+void JoystickInput::HandleData(const ::aos::JoystickState &joystick_state) {
+  data_.Update(joystick_state);
+
+  mode_ = static_cast<int>(joystick_state.switch_left) |
+          (static_cast<int>(joystick_state.scale_left) << 1);
+
+  {
+    using driver_station::JoystickFeature;
+    using driver_station::ButtonLocation;
+    for (int joystick = 1; joystick <= JoystickFeature::kJoysticks;
+         ++joystick) {
+      for (int button = 1; button <= ButtonLocation::kButtons; ++button) {
+        ButtonLocation location(joystick, button);
+        if (data_.PosEdge(location)) {
+          LOG(INFO, "PosEdge(%d, %d)\n", joystick, button);
+        }
+        if (data_.NegEdge(location)) {
+          LOG(INFO, "NegEdge(%d, %d)\n", joystick, button);
+        }
+      }
+      if (data_.GetPOV(joystick) != data_.GetOldPOV(joystick)) {
+        LOG(INFO, "POV %d %d->%d\n", joystick, data_.GetOldPOV(joystick),
+            data_.GetPOV(joystick));
+      }
+    }
+  }
+  {
+    using driver_station::ControlBit;
+    if (data_.PosEdge(ControlBit::kFmsAttached)) {
+      LOG(INFO, "PosEdge(kFmsAttached)\n");
+    }
+    if (data_.NegEdge(ControlBit::kFmsAttached)) {
+      LOG(INFO, "NegEdge(kFmsAttached)\n");
+    }
+    if (data_.PosEdge(ControlBit::kAutonomous)) {
+      LOG(INFO, "PosEdge(kAutonomous)\n");
+    }
+    if (data_.NegEdge(ControlBit::kAutonomous)) {
+      LOG(INFO, "NegEdge(kAutonomous)\n");
+    }
+    if (data_.PosEdge(ControlBit::kEnabled)) {
+      LOG(INFO, "PosEdge(kEnabled)\n");
+    }
+    if (data_.NegEdge(ControlBit::kEnabled)) {
+      LOG(INFO, "NegEdge(kEnabled)\n");
+    }
+  }
+
+  RunIteration(data_);
+
+  if (!run_) {
+    event_loop_->Exit();
+  }
+}
+
 void JoystickInput::Run() {
+  // TODO(austin): We need a better sigint story for event loops in general.
   run_ = true;
   struct sigaction action;
   action.sa_handler = &JoystickInput::Quit;
@@ -25,56 +81,8 @@
   PCHECK(sigaction(SIGQUIT, &action, nullptr));
   PCHECK(sigaction(SIGINT, &action, nullptr));
 
-  driver_station::Data data;
-  while (run_) {
-    joystick_state.FetchAnother();
+  event_loop_->Run();
 
-    data.Update(*joystick_state);
-
-    {
-      using driver_station::JoystickFeature;
-      using driver_station::ButtonLocation;
-      for (int joystick = 1; joystick <= JoystickFeature::kJoysticks;
-           ++joystick) {
-        for (int button = 1; button <= ButtonLocation::kButtons; ++button) {
-          ButtonLocation location(joystick, button);
-          if (data.PosEdge(location)) {
-            LOG(INFO, "PosEdge(%d, %d)\n", joystick, button);
-          }
-          if (data.NegEdge(location)) {
-            LOG(INFO, "NegEdge(%d, %d)\n", joystick, button);
-          }
-        }
-        if (data.GetPOV(joystick) != data.GetOldPOV(joystick)) {
-          LOG(INFO, "POV %d %d->%d\n", joystick, data.GetOldPOV(joystick),
-              data.GetPOV(joystick));
-        }
-      }
-    }
-    {
-      using driver_station::ControlBit;
-      if (data.PosEdge(ControlBit::kFmsAttached)) {
-        LOG(INFO, "PosEdge(kFmsAttached)\n");
-      }
-      if (data.NegEdge(ControlBit::kFmsAttached)) {
-        LOG(INFO, "NegEdge(kFmsAttached)\n");
-      }
-      if (data.PosEdge(ControlBit::kAutonomous)) {
-        LOG(INFO, "PosEdge(kAutonomous)\n");
-      }
-      if (data.NegEdge(ControlBit::kAutonomous)) {
-        LOG(INFO, "NegEdge(kAutonomous)\n");
-      }
-      if (data.PosEdge(ControlBit::kEnabled)) {
-        LOG(INFO, "PosEdge(kEnabled)\n");
-      }
-      if (data.NegEdge(ControlBit::kEnabled)) {
-        LOG(INFO, "NegEdge(kEnabled)\n");
-      }
-    }
-
-    RunIteration(data);
-  }
   LOG(INFO, "Shutting down\n");
 }
 
diff --git a/aos/input/joystick_input.h b/aos/input/joystick_input.h
index 4b5c5a6..98a89b1 100644
--- a/aos/input/joystick_input.h
+++ b/aos/input/joystick_input.h
@@ -3,6 +3,7 @@
 
 #include <atomic>
 
+#include "aos/events/event-loop.h"
 #include "aos/input/driver_station_data.h"
 
 namespace aos {
@@ -15,15 +16,34 @@
 // (at INFO) button edges.
 class JoystickInput {
  public:
+  explicit JoystickInput(::aos::EventLoop *event_loop)
+      : event_loop_(event_loop) {
+    event_loop_->MakeWatcher(
+        ".aos.joystick_state",
+        [this](const ::aos::JoystickState &joystick_state) {
+          this->HandleData(joystick_state);
+        });
+  }
+
   void Run();
 
+ protected:
+  int mode() const { return mode_; }
+
  private:
+  void HandleData(const ::aos::JoystickState &joystick_state);
+
   // Subclasses should do whatever they want with data here.
   virtual void RunIteration(const driver_station::Data &data) = 0;
 
   static void Quit(int /*signum*/);
 
   static ::std::atomic<bool> run_;
+
+  EventLoop *event_loop_;
+  driver_station::Data data_;
+
+  int mode_;
 };
 
 // Class which will proxy joystick information from UDP packets to the queues.