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/NTGyro.h" |
| 6 | |
| 7 | #include <fmt/format.h> |
| 8 | #include <wpi/StringExtras.h> |
| 9 | |
| 10 | using namespace glass; |
| 11 | |
| 12 | NTGyroModel::NTGyroModel(std::string_view path) |
| 13 | : NTGyroModel(nt::GetDefaultInstance(), path) {} |
| 14 | |
| 15 | NTGyroModel::NTGyroModel(NT_Inst instance, std::string_view path) |
| 16 | : m_nt(instance), |
| 17 | m_angle(m_nt.GetEntry(fmt::format("{}/Value", path))), |
| 18 | m_name(m_nt.GetEntry(fmt::format("{}/.name", path))), |
| 19 | m_angleData(fmt::format("NT_Gyro:{}", path)), |
| 20 | m_nameValue(wpi::rsplit(path, '/').second) { |
| 21 | m_nt.AddListener(m_angle); |
| 22 | m_nt.AddListener(m_name); |
| 23 | } |
| 24 | |
| 25 | void NTGyroModel::Update() { |
| 26 | for (auto&& event : m_nt.PollListener()) { |
| 27 | if (event.entry == m_angle) { |
| 28 | if (event.value && event.value->IsDouble()) { |
| 29 | m_angleData.SetValue(event.value->GetDouble()); |
| 30 | } |
| 31 | } else if (event.entry == m_name) { |
| 32 | if (event.value && event.value->IsString()) { |
| 33 | m_nameValue = event.value->GetString(); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | bool NTGyroModel::Exists() { |
| 40 | return m_nt.IsConnected() && nt::GetEntryType(m_angle) != NT_UNASSIGNED; |
| 41 | } |