diff --git a/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp b/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp index 57dcc1ea55..14fc91e401 100644 --- a/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp +++ b/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp @@ -14,6 +14,7 @@ #include "Commands/Scheduler.h" #include "LiveWindow/LiveWindow.h" +#include "SmartDashboard/SmartDashboard.h" using namespace frc; @@ -192,5 +193,6 @@ void IterativeRobotBase::LoopFunc() { TestPeriodic(); } RobotPeriodic(); + SmartDashboard::UpdateValues(); LiveWindow::GetInstance()->UpdateValues(); } diff --git a/wpilibc/src/main/native/cpp/SmartDashboard/SendableBuilderImpl.cpp b/wpilibc/src/main/native/cpp/SmartDashboard/SendableBuilderImpl.cpp index 72c42a43d2..cb72b62861 100644 --- a/wpilibc/src/main/native/cpp/SmartDashboard/SendableBuilderImpl.cpp +++ b/wpilibc/src/main/native/cpp/SmartDashboard/SendableBuilderImpl.cpp @@ -29,16 +29,24 @@ void SendableBuilderImpl::UpdateTable() { if (m_updateTable) m_updateTable(); } -void SendableBuilderImpl::StartLiveWindowMode() { - if (m_safeState) m_safeState(); +void SendableBuilderImpl::StartListeners() { for (auto& property : m_properties) property.StartListener(); } -void SendableBuilderImpl::StopLiveWindowMode() { - if (m_safeState) m_safeState(); +void SendableBuilderImpl::StopListeners() { for (auto& property : m_properties) property.StopListener(); } +void SendableBuilderImpl::StartLiveWindowMode() { + if (m_safeState) m_safeState(); + StartListeners(); +} + +void SendableBuilderImpl::StopLiveWindowMode() { + StopListeners(); + if (m_safeState) m_safeState(); +} + void SendableBuilderImpl::SetSmartDashboardType(const llvm::Twine& type) { m_table->GetEntry(".type").SetString(type); } diff --git a/wpilibc/src/main/native/cpp/SmartDashboard/SmartDashboard.cpp b/wpilibc/src/main/native/cpp/SmartDashboard/SmartDashboard.cpp index e45f4c8e7c..0781b35185 100644 --- a/wpilibc/src/main/native/cpp/SmartDashboard/SmartDashboard.cpp +++ b/wpilibc/src/main/native/cpp/SmartDashboard/SmartDashboard.cpp @@ -20,7 +20,11 @@ using namespace frc; namespace { -struct SmartDashboardData { +class SmartDashboardData { + public: + SmartDashboardData() = default; + explicit SmartDashboardData(Sendable* sendable_) : sendable(sendable_) {} + Sendable* sendable = nullptr; SendableBuilderImpl builder; }; @@ -157,11 +161,12 @@ void SmartDashboard::PutData(llvm::StringRef key, Sendable* data) { std::lock_guard lock(inst.tablesToDataMutex); auto& sddata = inst.tablesToData[key]; if (!sddata.sendable || sddata.sendable != data) { - sddata.sendable = data; + sddata = SmartDashboardData(data); sddata.builder.SetTable(inst.table->GetSubTable(key)); data->InitSendable(sddata.builder); + sddata.builder.UpdateTable(); + sddata.builder.StartListeners(); } - sddata.builder.UpdateTable(); } /** @@ -541,3 +546,14 @@ std::string SmartDashboard::GetRaw(llvm::StringRef key, llvm::StringRef defaultValue) { return Singleton::GetInstance().table->GetEntry(key).GetRaw(defaultValue); } + +/** + * Puts all sendable data to the dashboard. + */ +void SmartDashboard::UpdateValues() { + auto& inst = Singleton::GetInstance(); + std::lock_guard lock(inst.tablesToDataMutex); + for (auto& i : inst.tablesToData) { + i.getValue().builder.UpdateTable(); + } +} diff --git a/wpilibc/src/main/native/include/SmartDashboard/SendableBuilderImpl.h b/wpilibc/src/main/native/include/SmartDashboard/SendableBuilderImpl.h index 8dde86b991..d1c29865cc 100644 --- a/wpilibc/src/main/native/include/SmartDashboard/SendableBuilderImpl.h +++ b/wpilibc/src/main/native/include/SmartDashboard/SendableBuilderImpl.h @@ -52,12 +52,24 @@ class SendableBuilderImpl : public SendableBuilder { void UpdateTable(); /** - * Start LiveWindow mode by hooking the setters for all properties. + * Hook setters for all properties. + */ + void StartListeners(); + + /** + * Unhook setters for all properties. + */ + void StopListeners(); + + /** + * Start LiveWindow mode by hooking the setters for all properties. Also + * calls the SafeState function if one was provided. */ void StartLiveWindowMode(); /** - * Stop LiveWindow mode by unhooking the setters for all properties. + * Stop LiveWindow mode by unhooking the setters for all properties. Also + * calls the SafeState function if one was provided. */ void StopLiveWindowMode(); diff --git a/wpilibc/src/main/native/include/SmartDashboard/SmartDashboard.h b/wpilibc/src/main/native/include/SmartDashboard/SmartDashboard.h index a03dedd302..6b19d62950 100644 --- a/wpilibc/src/main/native/include/SmartDashboard/SmartDashboard.h +++ b/wpilibc/src/main/native/include/SmartDashboard/SmartDashboard.h @@ -84,6 +84,8 @@ class SmartDashboard : public SensorBase { std::shared_ptr defaultValue); static std::shared_ptr GetValue(llvm::StringRef keyName); + static void UpdateValues(); + private: virtual ~SmartDashboard() = default; }; diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobotBase.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobotBase.java index 4e08409f63..bc0385b454 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobotBase.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/IterativeRobotBase.java @@ -9,6 +9,7 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.wpilibj.hal.HAL; import edu.wpi.first.wpilibj.livewindow.LiveWindow; +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; /** * IterativeRobotBase implements a specific type of robot program framework, extending the RobotBase @@ -222,6 +223,7 @@ public abstract class IterativeRobotBase extends RobotBase { testPeriodic(); } robotPeriodic(); + SmartDashboard.updateValues(); LiveWindow.updateValues(); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableBuilderImpl.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableBuilderImpl.java index 5bb2a8fc38..62e6491beb 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableBuilderImpl.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SendableBuilderImpl.java @@ -88,26 +88,42 @@ public class SendableBuilderImpl implements SendableBuilder { } /** - * Start LiveWindow mode by hooking the setters for all properties. + * Hook setters for all properties. */ - public void startLiveWindowMode() { - if (m_safeState != null) { - m_safeState.run(); - } + public void startListeners() { for (Property property : m_properties) { property.startListener(); } } /** - * Stop LiveWindow mode by unhooking the setters for all properties. + * Unhook setters for all properties. */ - public void stopLiveWindowMode() { + public void stopListeners() { + for (Property property : m_properties) { + property.stopListener(); + } + } + + /** + * Start LiveWindow mode by hooking the setters for all properties. Also + * calls the safeState function if one was provided. + */ + public void startLiveWindowMode() { if (m_safeState != null) { m_safeState.run(); } - for (Property property : m_properties) { - property.stopListener(); + startListeners(); + } + + /** + * Stop LiveWindow mode by unhooking the setters for all properties. Also + * calls the safeState function if one was provided. + */ + public void stopLiveWindowMode() { + stopListeners(); + if (m_safeState != null) { + m_safeState.run(); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java index db76bcac4b..7e273b0dc9 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java @@ -33,7 +33,11 @@ public class SmartDashboard { NetworkTableInstance.getDefault().getTable("SmartDashboard"); private static class Data { - Sendable m_sendable; + Data(Sendable sendable) { + m_sendable = sendable; + } + + final Sendable m_sendable; final SendableBuilderImpl m_builder = new SendableBuilderImpl(); } @@ -57,16 +61,17 @@ public class SmartDashboard { */ public static synchronized void putData(String key, Sendable data) { Data sddata = tablesToData.get(key); - if (sddata == null) { - sddata = new Data(); + if (sddata == null || sddata.m_sendable != data) { + if (sddata != null) { + sddata.m_builder.stopListeners(); + } + sddata = new Data(data); tablesToData.put(key, sddata); - } - if (sddata.m_sendable == null || sddata.m_sendable != data) { - sddata.m_sendable = data; sddata.m_builder.setTable(table.getSubTable(key)); data.initSendable(sddata.m_builder); + sddata.m_builder.updateTable(); + sddata.m_builder.startListeners(); } - sddata.m_builder.updateTable(); } /** @@ -506,4 +511,13 @@ public class SmartDashboard { public static byte[] getRaw(String key, byte[] defaultValue) { return getEntry(key).getRaw(defaultValue); } + + /** + * Puts all sendable data to the dashboard. + */ + public static synchronized void updateValues() { + for (Data data : tablesToData.values()) { + data.m_builder.updateTable(); + } + } }