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/NTStringChooser.h" |
| 6 | |
| 7 | #include <fmt/format.h> |
| 8 | |
| 9 | using namespace glass; |
| 10 | |
| 11 | NTStringChooserModel::NTStringChooserModel(std::string_view path) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 12 | : NTStringChooserModel{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 | NTStringChooserModel::NTStringChooserModel(nt::NetworkTableInstance inst, |
| 15 | std::string_view path) |
| 16 | : m_inst{inst}, |
| 17 | m_default{ |
| 18 | m_inst.GetStringTopic(fmt::format("{}/default", path)).Subscribe("")}, |
| 19 | m_selected{ |
| 20 | m_inst.GetStringTopic(fmt::format("{}/selected", path)).GetEntry("")}, |
| 21 | m_active{ |
| 22 | m_inst.GetStringTopic(fmt::format("{}/active", path)).Subscribe("")}, |
| 23 | m_options{m_inst.GetStringArrayTopic(fmt::format("{}/options", path)) |
| 24 | .Subscribe({})} { |
| 25 | m_selected.GetTopic().SetRetained(true); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void NTStringChooserModel::SetSelected(std::string_view val) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 29 | m_selected.Set(val); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void NTStringChooserModel::Update() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 33 | if (!m_default.Exists()) { |
| 34 | m_defaultValue.clear(); |
| 35 | } |
| 36 | for (auto&& v : m_default.ReadQueue()) { |
| 37 | m_defaultValue = std::move(v.value); |
| 38 | } |
| 39 | |
| 40 | if (!m_selected.Exists()) { |
| 41 | m_selectedValue.clear(); |
| 42 | } |
| 43 | for (auto&& v : m_selected.ReadQueue()) { |
| 44 | m_selectedValue = std::move(v.value); |
| 45 | } |
| 46 | |
| 47 | if (!m_active.Exists()) { |
| 48 | m_activeValue.clear(); |
| 49 | } |
| 50 | for (auto&& v : m_active.ReadQueue()) { |
| 51 | m_activeValue = std::move(v.value); |
| 52 | } |
| 53 | |
| 54 | if (!m_options.Exists()) { |
| 55 | m_optionsValue.clear(); |
| 56 | } |
| 57 | for (auto&& v : m_options.ReadQueue()) { |
| 58 | m_optionsValue = std::move(v.value); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | bool NTStringChooserModel::Exists() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 63 | return m_options.Exists(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 64 | } |