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