Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // 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 | |
| 10 | using namespace glass; |
| 11 | |
| 12 | NTSpeedControllerModel::NTSpeedControllerModel(std::string_view path) |
| 13 | : NTSpeedControllerModel(nt::GetDefaultInstance(), path) {} |
| 14 | |
| 15 | NTSpeedControllerModel::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 | |
| 28 | void NTSpeedControllerModel::SetPercent(double value) { |
| 29 | nt::SetEntryValue(m_value, nt::NetworkTableValue::MakeDouble(value)); |
| 30 | } |
| 31 | |
| 32 | void 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 | |
| 50 | bool NTSpeedControllerModel::Exists() { |
| 51 | return m_nt.IsConnected() && nt::GetEntryType(m_value) != NT_UNASSIGNED; |
| 52 | } |