Convert joystick_reader over to event loops.
Change-Id: I97b78254b0fc96853f162b66ef75ec97a6c9c50b
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");
}