blob: 5de6c2954e7b28e7647e8fbc954804e4374db75d [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/NTDigitalInput.h"
6
7#include <fmt/format.h>
8#include <wpi/StringExtras.h>
9
10using namespace glass;
11
12NTDigitalInputModel::NTDigitalInputModel(std::string_view path)
13 : NTDigitalInputModel{nt::GetDefaultInstance(), path} {}
14
15NTDigitalInputModel::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
27void 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
41bool NTDigitalInputModel::Exists() {
42 return m_nt.IsConnected() && nt::GetEntryType(m_value) != NT_UNASSIGNED;
43}