blob: 3dc351a18819d609d3b05c4c58ae34908eff6c00 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#include "glass/networktables/NTSpeedController.h"
6
7#include <fmt/format.h>
8#include <wpi/StringExtras.h>
9
10using namespace glass;
11
12NTSpeedControllerModel::NTSpeedControllerModel(std::string_view path)
13 : NTSpeedControllerModel(nt::GetDefaultInstance(), path) {}
14
15NTSpeedControllerModel::NTSpeedControllerModel(NT_Inst instance,
16 std::string_view path)
17 : m_nt(instance),
18 m_value(m_nt.GetEntry(fmt::format("{}/Value", path))),
19 m_name(m_nt.GetEntry(fmt::format("{}/.name", path))),
20 m_controllable(m_nt.GetEntry(fmt::format("{}/.controllable", path))),
21 m_valueData(fmt::format("NT_SpdCtrl:{}", path)),
22 m_nameValue(wpi::rsplit(path, '/').second) {
23 m_nt.AddListener(m_value);
24 m_nt.AddListener(m_name);
25 m_nt.AddListener(m_controllable);
26}
27
28void NTSpeedControllerModel::SetPercent(double value) {
29 nt::SetEntryValue(m_value, nt::NetworkTableValue::MakeDouble(value));
30}
31
32void NTSpeedControllerModel::Update() {
33 for (auto&& event : m_nt.PollListener()) {
34 if (event.entry == m_value) {
35 if (event.value && event.value->IsDouble()) {
36 m_valueData.SetValue(event.value->GetDouble());
37 }
38 } else if (event.entry == m_name) {
39 if (event.value && event.value->IsString()) {
40 m_nameValue = event.value->GetString();
41 }
42 } else if (event.entry == m_controllable) {
43 if (event.value && event.value->IsBoolean()) {
44 m_controllableValue = event.value->GetBoolean();
45 }
46 }
47 }
48}
49
50bool NTSpeedControllerModel::Exists() {
51 return m_nt.IsConnected() && nt::GetEntryType(m_value) != NT_UNASSIGNED;
52}