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/NTDigitalInput.h" |
| 6 | |
| 7 | #include <fmt/format.h> |
| 8 | #include <wpi/StringExtras.h> |
| 9 | |
| 10 | using namespace glass; |
| 11 | |
| 12 | NTDigitalInputModel::NTDigitalInputModel(std::string_view path) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 13 | : NTDigitalInputModel{nt::NetworkTableInstance::GetDefault(), path} {} |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 14 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 15 | NTDigitalInputModel::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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 21 | m_valueData{fmt::format("NT_DIn:{}", path)}, |
| 22 | m_nameValue{wpi::rsplit(path, '/').second} { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 23 | m_valueData.SetDigital(true); |
| 24 | } |
| 25 | |
| 26 | void NTDigitalInputModel::Update() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 27 | 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | bool NTDigitalInputModel::Exists() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 36 | return m_value.Exists(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 37 | } |