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/NTDigitalOutput.h" |
| 6 | |
| 7 | #include <fmt/format.h> |
| 8 | |
| 9 | using namespace glass; |
| 10 | |
| 11 | NTDigitalOutputModel::NTDigitalOutputModel(std::string_view path) |
| 12 | : NTDigitalOutputModel{nt::GetDefaultInstance(), path} {} |
| 13 | |
| 14 | NTDigitalOutputModel::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 | |
| 27 | void NTDigitalOutputModel::SetValue(bool val) { |
| 28 | nt::SetEntryValue(m_value, nt::Value::MakeBoolean(val)); |
| 29 | } |
| 30 | |
| 31 | void 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 | |
| 49 | bool NTDigitalOutputModel::Exists() { |
| 50 | return m_nt.IsConnected() && nt::GetEntryType(m_value) != NT_UNASSIGNED; |
| 51 | } |