blob: 28b916cfbbaa81df7fd198320bc4bd2ce3e26ecb [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)
James Kuszmaulcf324122023-01-14 14:07:17 -080013 : NTDigitalInputModel{nt::NetworkTableInstance::GetDefault(), path} {}
Austin Schuh812d0d12021-11-04 20:16:48 -070014
James Kuszmaulcf324122023-01-14 14:07:17 -080015NTDigitalInputModel::NTDigitalInputModel(nt::NetworkTableInstance inst,
16 std::string_view path)
17 : m_inst{inst},
18 m_value{
19 inst.GetBooleanTopic(fmt::format("{}/Value", path)).Subscribe(false)},
20 m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
Austin Schuh812d0d12021-11-04 20:16:48 -070021 m_valueData{fmt::format("NT_DIn:{}", path)},
22 m_nameValue{wpi::rsplit(path, '/').second} {
Austin Schuh812d0d12021-11-04 20:16:48 -070023 m_valueData.SetDigital(true);
24}
25
26void NTDigitalInputModel::Update() {
James Kuszmaulcf324122023-01-14 14:07:17 -080027 for (auto&& v : m_value.ReadQueue()) {
28 m_valueData.SetValue(v.value, v.time);
29 }
30 for (auto&& v : m_name.ReadQueue()) {
31 m_nameValue = std::move(v.value);
Austin Schuh812d0d12021-11-04 20:16:48 -070032 }
33}
34
35bool NTDigitalInputModel::Exists() {
James Kuszmaulcf324122023-01-14 14:07:17 -080036 return m_value.Exists();
Austin Schuh812d0d12021-11-04 20:16:48 -070037}