blob: a09d424446a1349aa8fc77d639cbeb39d5dbf60e [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/NTDigitalOutput.h"
6
7#include <fmt/format.h>
8
9using namespace glass;
10
11NTDigitalOutputModel::NTDigitalOutputModel(std::string_view path)
12 : NTDigitalOutputModel{nt::GetDefaultInstance(), path} {}
13
14NTDigitalOutputModel::NTDigitalOutputModel(NT_Inst inst, std::string_view path)
15 : m_nt{inst},
16 m_value{m_nt.GetEntry(fmt::format("{}/Value", path))},
17 m_name{m_nt.GetEntry(fmt::format("{}/.name", path))},
18 m_controllable{m_nt.GetEntry(fmt::format("{}/.controllable", path))},
19 m_valueData{fmt::format("NT_DOut:{}", path)} {
20 m_nt.AddListener(m_value);
21 m_nt.AddListener(m_name);
22 m_nt.AddListener(m_controllable);
23
24 m_valueData.SetDigital(true);
25}
26
27void NTDigitalOutputModel::SetValue(bool val) {
28 nt::SetEntryValue(m_value, nt::Value::MakeBoolean(val));
29}
30
31void NTDigitalOutputModel::Update() {
32 for (auto&& event : m_nt.PollListener()) {
33 if (event.entry == m_value) {
34 if (event.value && event.value->IsBoolean()) {
35 m_valueData.SetValue(event.value->GetBoolean());
36 }
37 } else if (event.entry == m_name) {
38 if (event.value && event.value->IsString()) {
39 m_nameValue = event.value->GetString();
40 }
41 } else if (event.entry == m_controllable) {
42 if (event.value && event.value->IsBoolean()) {
43 m_controllableValue = event.value->GetBoolean();
44 }
45 }
46 }
47}
48
49bool NTDigitalOutputModel::Exists() {
50 return m_nt.IsConnected() && nt::GetEntryType(m_value) != NT_UNASSIGNED;
51}