blob: b892a2c789e6261b35a00bfa2ccf8f50677e3f14 [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/NTStringChooser.h"
6
7#include <fmt/format.h>
8
9using namespace glass;
10
11NTStringChooserModel::NTStringChooserModel(std::string_view path)
James Kuszmaulcf324122023-01-14 14:07:17 -080012 : NTStringChooserModel{nt::NetworkTableInstance::GetDefault(), path} {}
Austin Schuh812d0d12021-11-04 20:16:48 -070013
James Kuszmaulcf324122023-01-14 14:07:17 -080014NTStringChooserModel::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 Schuh812d0d12021-11-04 20:16:48 -070026}
27
28void NTStringChooserModel::SetSelected(std::string_view val) {
James Kuszmaulcf324122023-01-14 14:07:17 -080029 m_selected.Set(val);
Austin Schuh812d0d12021-11-04 20:16:48 -070030}
31
32void NTStringChooserModel::Update() {
James Kuszmaulcf324122023-01-14 14:07:17 -080033 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 Schuh812d0d12021-11-04 20:16:48 -070059 }
60}
61
62bool NTStringChooserModel::Exists() {
James Kuszmaulcf324122023-01-14 14:07:17 -080063 return m_options.Exists();
Austin Schuh812d0d12021-11-04 20:16:48 -070064}