blob: b198bea8274908463afa70d26ef02bb4eac4d6d5 [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2011. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#include "SmartDashboard/SendableChooser.h"
8#include "networktables2/type/StringArray.h"
9
10#include <stdio.h>
11
12static const char *kDefault = "default";
13static const char *kOptions = "options";
14static const char *kSelected = "selected";
15
16SendableChooser::SendableChooser()
17{
18 m_defaultChoice = "";
19}
20
21/**
22 * Adds the given object to the list of options. On the {@link SmartDashboard} on the desktop,
23 * the object will appear as the given name.
24 * @param name the name of the option
25 * @param object the option
26 */
27void SendableChooser::AddObject(const char *name, void *object)
28{
29 m_choices[name] = object;
30}
31
32/**
33 * Add the given object to the list of options and marks it as the default.
34 * Functionally, this is very close to {@link SendableChooser#AddObject(const char *name, void *object) AddObject(...)}
35 * except that it will use this as the default option if none other is explicitly selected.
36 * @param name the name of the option
37 * @param object the option
38 */
39void SendableChooser::AddDefault(const char *name, void *object)
40{
41 m_defaultChoice = name;
42 AddObject(name, object);
43}
44
45/**
46 * Returns the selected option. If there is none selected, it will return the default. If there is none selected
47 * and no default, then it will return {@code NULL}.
48 * @return the option selected
49 */
50void *SendableChooser::GetSelected()
51{
52 std::string selected = m_table->GetString(kSelected, m_defaultChoice);
53 if (selected == "")
54 return NULL;
55 else
56 return m_choices[selected];
57}
58
59void SendableChooser::InitTable(ITable* subtable) {
60 StringArray keys;
61 m_table = subtable;
62 if (m_table != NULL) {
63 std::map<std::string, void *>::iterator iter;
64 for (iter = m_choices.begin(); iter != m_choices.end(); iter++) {
65 keys.add(iter->first);
66 }
67 m_table->PutValue(kOptions, keys);
68 m_table->PutString(kDefault, m_defaultChoice);
69 }
70}
71
72ITable* SendableChooser::GetTable() {
73 return m_table;
74}
75
76std::string SendableChooser::GetSmartDashboardType() {
77 return "String Chooser";
78}