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) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 12 | : NTDigitalOutputModel{nt::NetworkTableInstance::GetDefault(), path} {} |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 13 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 14 | NTDigitalOutputModel::NTDigitalOutputModel(nt::NetworkTableInstance inst, |
| 15 | std::string_view path) |
| 16 | : m_inst{inst}, |
| 17 | m_value{ |
| 18 | inst.GetBooleanTopic(fmt::format("{}/Value", path)).GetEntry(false)}, |
| 19 | m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")}, |
| 20 | m_controllable{inst.GetBooleanTopic(fmt::format("{}/.controllable", path)) |
| 21 | .Subscribe(false)}, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 22 | m_valueData{fmt::format("NT_DOut:{}", path)} { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 23 | m_valueData.SetDigital(true); |
| 24 | } |
| 25 | |
| 26 | void NTDigitalOutputModel::SetValue(bool val) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 27 | m_value.Set(val); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void NTDigitalOutputModel::Update() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 31 | for (auto&& v : m_value.ReadQueue()) { |
| 32 | m_valueData.SetValue(v.value, v.time); |
| 33 | } |
| 34 | for (auto&& v : m_name.ReadQueue()) { |
| 35 | m_nameValue = std::move(v.value); |
| 36 | } |
| 37 | for (auto&& v : m_controllable.ReadQueue()) { |
| 38 | m_controllableValue = v.value; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | bool NTDigitalOutputModel::Exists() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 43 | return m_value.Exists(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 44 | } |