2017-12-04 23:28:33 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-08-03 18:08:06 -04:00
|
|
|
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
|
2017-12-04 23:28:33 -08:00
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/smartdashboard/SendableBuilderImpl.h"
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <ntcore_cpp.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/SmallString.h>
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2019-08-03 18:08:06 -04:00
|
|
|
#include "frc/smartdashboard/SmartDashboard.h"
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::SetTable(std::shared_ptr<nt::NetworkTable> table) {
|
|
|
|
|
m_table = table;
|
2018-07-28 10:42:31 -07:00
|
|
|
m_controllableEntry = table->GetEntry(".controllable");
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<nt::NetworkTable> SendableBuilderImpl::GetTable() {
|
|
|
|
|
return m_table;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-17 22:01:31 -07:00
|
|
|
bool SendableBuilderImpl::HasTable() const { return m_table != nullptr; }
|
|
|
|
|
|
2018-07-28 14:04:46 -07:00
|
|
|
bool SendableBuilderImpl::IsActuator() const { return m_actuator; }
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void SendableBuilderImpl::UpdateTable() {
|
|
|
|
|
uint64_t time = nt::Now();
|
|
|
|
|
for (auto& property : m_properties) {
|
|
|
|
|
if (property.update) property.update(property.entry, time);
|
|
|
|
|
}
|
|
|
|
|
if (m_updateTable) m_updateTable();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 17:18:02 -06:00
|
|
|
void SendableBuilderImpl::StartListeners() {
|
|
|
|
|
for (auto& property : m_properties) property.StartListener();
|
2018-12-07 22:38:22 -05:00
|
|
|
if (m_controllableEntry) m_controllableEntry.SetBoolean(true);
|
2017-12-26 17:18:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::StopListeners() {
|
|
|
|
|
for (auto& property : m_properties) property.StopListener();
|
2018-12-07 22:38:22 -05:00
|
|
|
if (m_controllableEntry) m_controllableEntry.SetBoolean(false);
|
2017-12-26 17:18:02 -06:00
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void SendableBuilderImpl::StartLiveWindowMode() {
|
|
|
|
|
if (m_safeState) m_safeState();
|
2017-12-26 17:18:02 -06:00
|
|
|
StartListeners();
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::StopLiveWindowMode() {
|
2017-12-26 17:18:02 -06:00
|
|
|
StopListeners();
|
2017-12-04 23:28:33 -08:00
|
|
|
if (m_safeState) m_safeState();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-17 22:01:31 -07:00
|
|
|
void SendableBuilderImpl::ClearProperties() { m_properties.clear(); }
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void SendableBuilderImpl::SetSmartDashboardType(const wpi::Twine& type) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_table->GetEntry(".type").SetString(type);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-28 14:04:46 -07:00
|
|
|
void SendableBuilderImpl::SetActuator(bool value) {
|
|
|
|
|
m_table->GetEntry(".actuator").SetBoolean(value);
|
|
|
|
|
m_actuator = value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void SendableBuilderImpl::SetSafeState(std::function<void()> func) {
|
|
|
|
|
m_safeState = func;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::SetUpdateTable(std::function<void()> func) {
|
|
|
|
|
m_updateTable = func;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
nt::NetworkTableEntry SendableBuilderImpl::GetEntry(const wpi::Twine& key) {
|
2017-12-04 23:28:33 -08:00
|
|
|
return m_table->GetEntry(key);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void SendableBuilderImpl::AddBooleanProperty(const wpi::Twine& key,
|
2017-12-04 23:28:33 -08:00
|
|
|
std::function<bool()> getter,
|
|
|
|
|
std::function<void(bool)> setter) {
|
|
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
|
|
|
|
entry.SetValue(nt::Value::MakeBoolean(getter(), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsBoolean()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetBoolean()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddDoubleProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key, std::function<double()> getter,
|
2017-12-04 23:28:33 -08:00
|
|
|
std::function<void(double)> setter) {
|
|
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
|
|
|
|
entry.SetValue(nt::Value::MakeDouble(getter(), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsDouble()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetDouble()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddStringProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key, std::function<std::string()> getter,
|
|
|
|
|
std::function<void(wpi::StringRef)> setter) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
|
|
|
|
entry.SetValue(nt::Value::MakeString(getter(), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsString()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetString()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddBooleanArrayProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key, std::function<std::vector<int>()> getter,
|
|
|
|
|
std::function<void(wpi::ArrayRef<int>)> setter) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
|
|
|
|
entry.SetValue(nt::Value::MakeBooleanArray(getter(), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsBooleanArray()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetBooleanArray()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddDoubleArrayProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key, std::function<std::vector<double>()> getter,
|
|
|
|
|
std::function<void(wpi::ArrayRef<double>)> setter) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
|
|
|
|
entry.SetValue(nt::Value::MakeDoubleArray(getter(), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsDoubleArray()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetDoubleArray()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddStringArrayProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key, std::function<std::vector<std::string>()> getter,
|
|
|
|
|
std::function<void(wpi::ArrayRef<std::string>)> setter) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
|
|
|
|
entry.SetValue(nt::Value::MakeStringArray(getter(), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsStringArray()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetStringArray()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddRawProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key, std::function<std::string()> getter,
|
|
|
|
|
std::function<void(wpi::StringRef)> setter) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
|
|
|
|
entry.SetValue(nt::Value::MakeRaw(getter(), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsRaw()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetRaw()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddValueProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key, std::function<std::shared_ptr<nt::Value>()> getter,
|
2017-12-04 23:28:33 -08:00
|
|
|
std::function<void(std::shared_ptr<nt::Value>)> setter) {
|
|
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
|
|
|
|
entry.SetValue(getter());
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
2019-08-03 18:08:06 -04:00
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
SmartDashboard::PostListenerTask([=] { setter(event.value); });
|
|
|
|
|
},
|
2017-12-04 23:28:33 -08:00
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallStringProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key,
|
|
|
|
|
std::function<wpi::StringRef(wpi::SmallVectorImpl<char>& buf)> getter,
|
|
|
|
|
std::function<void(wpi::StringRef)> setter) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<128> buf;
|
2017-12-04 23:28:33 -08:00
|
|
|
entry.SetValue(nt::Value::MakeString(getter(buf), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsString()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetString()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallBooleanArrayProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key,
|
|
|
|
|
std::function<wpi::ArrayRef<int>(wpi::SmallVectorImpl<int>& buf)> getter,
|
|
|
|
|
std::function<void(wpi::ArrayRef<int>)> setter) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallVector<int, 16> buf;
|
2017-12-04 23:28:33 -08:00
|
|
|
entry.SetValue(nt::Value::MakeBooleanArray(getter(buf), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsBooleanArray()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetBooleanArray()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallDoubleArrayProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key,
|
|
|
|
|
std::function<wpi::ArrayRef<double>(wpi::SmallVectorImpl<double>& buf)>
|
2017-12-04 23:28:33 -08:00
|
|
|
getter,
|
2018-04-29 23:33:19 -07:00
|
|
|
std::function<void(wpi::ArrayRef<double>)> setter) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallVector<double, 16> buf;
|
2017-12-04 23:28:33 -08:00
|
|
|
entry.SetValue(nt::Value::MakeDoubleArray(getter(buf), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsDoubleArray()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetDoubleArray()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallStringArrayProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key,
|
2017-12-04 23:28:33 -08:00
|
|
|
std::function<
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::ArrayRef<std::string>(wpi::SmallVectorImpl<std::string>& buf)>
|
2017-12-04 23:28:33 -08:00
|
|
|
getter,
|
2018-04-29 23:33:19 -07:00
|
|
|
std::function<void(wpi::ArrayRef<std::string>)> setter) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallVector<std::string, 16> buf;
|
2017-12-04 23:28:33 -08:00
|
|
|
entry.SetValue(nt::Value::MakeStringArray(getter(buf), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsStringArray()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetStringArray()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallRawProperty(
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& key,
|
|
|
|
|
std::function<wpi::StringRef(wpi::SmallVectorImpl<char>& buf)> getter,
|
|
|
|
|
std::function<void(wpi::StringRef)> setter) {
|
2017-12-04 23:28:33 -08:00
|
|
|
m_properties.emplace_back(*m_table, key);
|
|
|
|
|
if (getter) {
|
|
|
|
|
m_properties.back().update = [=](nt::NetworkTableEntry entry,
|
|
|
|
|
uint64_t time) {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallVector<char, 128> buf;
|
2017-12-04 23:28:33 -08:00
|
|
|
entry.SetValue(nt::Value::MakeRaw(getter(buf), time));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
|
|
|
|
m_properties.back().createListener =
|
|
|
|
|
[=](nt::NetworkTableEntry entry) -> NT_EntryListener {
|
|
|
|
|
return entry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsRaw()) return;
|
2019-08-03 18:08:06 -04:00
|
|
|
SmartDashboard::PostListenerTask(
|
|
|
|
|
[=] { setter(event.value->GetRaw()); });
|
2017-12-04 23:28:33 -08:00
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|