From 0384aa1898db8cfe067563bd49b477f2fea61bcf Mon Sep 17 00:00:00 2001 From: Thad House Date: Thu, 7 Nov 2019 10:42:04 -0800 Subject: [PATCH 1/5] Add callback handlers for LiveWindow --- .../wpilibj2/command/CommandScheduler.java | 17 ++++++++++++++++- .../cpp/frc2/command/CommandScheduler.cpp | 16 +++++++++++++++- .../wpi/first/wpilibj/command/Scheduler.java | 10 ++++++++++ .../main/native/cpp/commands/Scheduler.cpp | 14 +++++++++++++- .../main/native/cpp/livewindow/LiveWindow.cpp | 2 ++ .../include/frc/livewindow/LiveWindow.h | 4 ++++ .../first/wpilibj/livewindow/LiveWindow.java | 19 +++++++++++++++++++ 7 files changed, 79 insertions(+), 3 deletions(-) diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java index b34f20e1fcf..662981b8f5b 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java @@ -24,6 +24,7 @@ import edu.wpi.first.networktables.NetworkTableEntry; import edu.wpi.first.wpilibj.RobotState; import edu.wpi.first.wpilibj.Sendable; +import edu.wpi.first.wpilibj.livewindow.LiveWindow; import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder; import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; @@ -35,7 +36,7 @@ * Subsystem#periodic()} methods to be called and for their default commands to be scheduled. */ @SuppressWarnings({"PMD.GodClass", "PMD.TooManyMethods", "PMD.TooManyFields"}) -public final class CommandScheduler implements Sendable { +public final class CommandScheduler implements Sendable, AutoCloseable { /** * The Singleton Instance. */ @@ -91,6 +92,20 @@ public static synchronized CommandScheduler getInstance() { CommandScheduler() { HAL.report(tResourceType.kResourceType_Command, tInstances.kCommand_Scheduler); SendableRegistry.addLW(this, "Scheduler"); + LiveWindow.setEnabledListener(() -> { + disable(); + cancelAll(); + }); + LiveWindow.setDisabledListener(() -> { + enable(); + }); + } + + @Override + public void close() { + SendableRegistry.remove(this); + LiveWindow.setEnabledListener(null); + LiveWindow.setDisabledListener(null); } /** diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp index 9ac69b87042..6280130dd54 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -68,9 +69,22 @@ static bool ContainsKey(const TMap& map, TKey keyToCheck) { CommandScheduler::CommandScheduler() : m_impl(new Impl) { frc::SendableRegistry::GetInstance().AddLW(this, "Scheduler"); + auto scheduler = frc::LiveWindow::GetInstance(); + scheduler->enabled = [this]{ + this->Disable(); + this->CancelAll(); + }; + scheduler->disabled = [this]{ + this->Enable(); + }; } -CommandScheduler::~CommandScheduler() {} +CommandScheduler::~CommandScheduler() { + frc::SendableRegistry::GetInstance().Remove(this); + auto scheduler = frc::LiveWindow::GetInstance(); + scheduler->enabled = nullptr; + scheduler->disabled = nullptr; +} CommandScheduler& CommandScheduler::GetInstance() { static CommandScheduler scheduler; diff --git a/wpilibOldCommands/src/main/java/edu/wpi/first/wpilibj/command/Scheduler.java b/wpilibOldCommands/src/main/java/edu/wpi/first/wpilibj/command/Scheduler.java index e25a5a7470a..7ddda2d9a8d 100644 --- a/wpilibOldCommands/src/main/java/edu/wpi/first/wpilibj/command/Scheduler.java +++ b/wpilibOldCommands/src/main/java/edu/wpi/first/wpilibj/command/Scheduler.java @@ -17,6 +17,7 @@ import edu.wpi.first.networktables.NetworkTableEntry; import edu.wpi.first.wpilibj.Sendable; import edu.wpi.first.wpilibj.buttons.Trigger.ButtonScheduler; +import edu.wpi.first.wpilibj.livewindow.LiveWindow; import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder; import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; @@ -98,11 +99,20 @@ public static synchronized Scheduler getInstance() { private Scheduler() { HAL.report(tResourceType.kResourceType_Command, tInstances.kCommand_Scheduler); SendableRegistry.addLW(this, "Scheduler"); + LiveWindow.setEnabledListener(() -> { + disable(); + removeAll(); + }); + LiveWindow.setDisabledListener(() -> { + enable(); + }); } @Override public void close() { SendableRegistry.remove(this); + LiveWindow.setEnabledListener(null); + LiveWindow.setDisabledListener(null); } /** diff --git a/wpilibOldCommands/src/main/native/cpp/commands/Scheduler.cpp b/wpilibOldCommands/src/main/native/cpp/commands/Scheduler.cpp index f406d74a27e..459b61b66ac 100644 --- a/wpilibOldCommands/src/main/native/cpp/commands/Scheduler.cpp +++ b/wpilibOldCommands/src/main/native/cpp/commands/Scheduler.cpp @@ -20,6 +20,7 @@ #include "frc/buttons/ButtonScheduler.h" #include "frc/commands/Command.h" #include "frc/commands/Subsystem.h" +#include "frc/livewindow/LiveWindow.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -198,9 +199,20 @@ Scheduler::Scheduler() : m_impl(new Impl) { HAL_Report(HALUsageReporting::kResourceType_Command, HALUsageReporting::kCommand_Scheduler); SendableRegistry::GetInstance().AddLW(this, "Scheduler"); + auto scheduler = frc::LiveWindow::GetInstance(); + scheduler->enabled = [this] { + this->SetEnabled(false); + this->RemoveAll(); + }; + scheduler->disabled = [this] { this->SetEnabled(true); }; } -Scheduler::~Scheduler() {} +Scheduler::~Scheduler() { + SendableRegistry::GetInstance().Remove(this); + auto scheduler = frc::LiveWindow::GetInstance(); + scheduler->enabled = nullptr; + scheduler->disabled = nullptr; +} void Scheduler::Impl::Remove(Command* command) { if (!commands.erase(command)) return; diff --git a/wpilibc/src/main/native/cpp/livewindow/LiveWindow.cpp b/wpilibc/src/main/native/cpp/livewindow/LiveWindow.cpp index 10996e4ffeb..34f5292e279 100644 --- a/wpilibc/src/main/native/cpp/livewindow/LiveWindow.cpp +++ b/wpilibc/src/main/native/cpp/livewindow/LiveWindow.cpp @@ -104,10 +104,12 @@ void LiveWindow::SetEnabled(bool enabled) { // Force table generation now to make sure everything is defined UpdateValuesUnsafe(); if (enabled) { + this->enabled(); } else { m_impl->registry.ForeachLiveWindow(m_impl->dataHandle, [&](auto& cbdata) { cbdata.builder.StopLiveWindowMode(); }); + this->disabled(); } m_impl->enabledEntry.SetBoolean(enabled); } diff --git a/wpilibc/src/main/native/include/frc/livewindow/LiveWindow.h b/wpilibc/src/main/native/include/frc/livewindow/LiveWindow.h index c7e0ec2a835..04eaeabafc1 100644 --- a/wpilibc/src/main/native/include/frc/livewindow/LiveWindow.h +++ b/wpilibc/src/main/native/include/frc/livewindow/LiveWindow.h @@ -7,6 +7,7 @@ #pragma once +#include #include namespace frc { @@ -22,6 +23,9 @@ class LiveWindow { LiveWindow(const LiveWindow&) = delete; LiveWindow& operator=(const LiveWindow&) = delete; + std::function enabled; + std::function disabled; + /** * Get an instance of the LiveWindow main class. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java index 5a4fb36e40e..94cbfe85306 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java @@ -7,6 +7,8 @@ package edu.wpi.first.wpilibj.livewindow; +import java.util.function.Function; + import edu.wpi.first.networktables.NetworkTable; import edu.wpi.first.networktables.NetworkTableEntry; import edu.wpi.first.networktables.NetworkTableInstance; @@ -33,6 +35,9 @@ private static class Component { private static boolean liveWindowEnabled; private static boolean telemetryEnabled = true; + private static Runnable enabledListener; + private static Runnable disabledListener; + private static Component getOrAdd(Sendable sendable) { Component data = (Component) SendableRegistry.getData(sendable, dataHandle); if (data == null) { @@ -46,6 +51,14 @@ private LiveWindow() { throw new UnsupportedOperationException("This is a utility class!"); } + public static synchronized void setEnabledListener(Runnable runnable) { + enabledListener = runnable; + } + + public static synchronized void setDisabledListener(Runnable runnable) { + disabledListener = runnable; + } + public static synchronized boolean isEnabled() { return liveWindowEnabled; } @@ -65,11 +78,17 @@ public static synchronized void setEnabled(boolean enabled) { updateValues(); // Force table generation now to make sure everything is defined if (enabled) { System.out.println("Starting live window mode."); + if (enabledListener != null) { + enabledListener.run(); + } } else { System.out.println("stopping live window mode."); SendableRegistry.foreachLiveWindow(dataHandle, cbdata -> { cbdata.builder.stopLiveWindowMode(); }); + if (disabledListener != null) { + disabledListener.run(); + } } enabledEntry.setBoolean(enabled); } From 22b6b1cb40dd9db2d1a8127ff3ff2f3a5f0320e8 Mon Sep 17 00:00:00 2001 From: Thad House Date: Sat, 14 Dec 2019 14:22:24 -0800 Subject: [PATCH 2/5] Fix checkstyle --- .../main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java index 94cbfe85306..b0a5743a93d 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java @@ -7,8 +7,6 @@ package edu.wpi.first.wpilibj.livewindow; -import java.util.function.Function; - import edu.wpi.first.networktables.NetworkTable; import edu.wpi.first.networktables.NetworkTableEntry; import edu.wpi.first.networktables.NetworkTableInstance; From c1b68c5c419524c69ce7ff5405ed4f680cb55987 Mon Sep 17 00:00:00 2001 From: Thad House Date: Sat, 14 Dec 2019 14:33:15 -0800 Subject: [PATCH 3/5] Fix styleguide --- .../src/main/native/cpp/frc2/command/CommandScheduler.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp index 0db6dde3f3d..c95cf1b79c8 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp @@ -70,13 +70,11 @@ static bool ContainsKey(const TMap& map, TKey keyToCheck) { CommandScheduler::CommandScheduler() : m_impl(new Impl) { frc::SendableRegistry::GetInstance().AddLW(this, "Scheduler"); auto scheduler = frc::LiveWindow::GetInstance(); - scheduler->enabled = [this]{ + scheduler->enabled = [this] { this->Disable(); this->CancelAll(); }; - scheduler->disabled = [this]{ - this->Enable(); - }; + scheduler->disabled = [this] { this->Enable(); }; } CommandScheduler::~CommandScheduler() { From 092d6867241ea5ce5facac5960987df74183a438 Mon Sep 17 00:00:00 2001 From: Thad House Date: Sat, 14 Dec 2019 15:48:35 -0800 Subject: [PATCH 4/5] Fix styleguide --- .../src/main/native/cpp/frc2/command/CommandScheduler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp index c95cf1b79c8..144b4a842d2 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp @@ -9,9 +9,9 @@ #include #include +#include #include #include -#include #include #include #include From 4cd2b13956b796e3517820f5129192e752b2b448 Mon Sep 17 00:00:00 2001 From: Thad House Date: Sat, 4 Jan 2020 13:24:28 -0800 Subject: [PATCH 5/5] Formatting --- .../java/edu/wpi/first/wpilibj2/command/CommandScheduler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java index f028be4852b..3d32ed0c467 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */