-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuning.js
101 lines (97 loc) · 4.38 KB
/
tuning.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Create aliases for various ODM elements relating to tuning.
ui.tuning = {
list: document.getElementById('tuning'),
button: document.getElementById('tuning-button'),
name: document.getElementById('name'),
value: document.getElementById('value'),
set: document.getElementById('set'),
get: document.getElementById('get')
};
// Sets function to be called when any NetworkTables key/value changes
NetworkTables.addGlobalListener(onValueChanged, true);
function onValueChanged(key, value, isNew) {
// Sometimes, NetworkTables will pass booleans as strings. This corrects for that.
if (value == 'true') {
value = true;
}
else if (value == 'false') {
value = false;
}
// The following code manages tuning section of the interface.
// This section displays a list of all NetworkTables variables (that start with /SmartDashboard/) and allows you to directly manipulate them.
var propName = key.substring(16, key.length);
// Check if value is new and doesn't have a spot on the list yet
if (isNew && !document.getElementsByName(propName)[0]) {
// Make sure name starts with /SmartDashboard/. Properties that don't are technical and don't need to be shown on the list.
if (/^\/SmartDashboard\//.test(key)) {
// Make a new div for this value
var div = document.createElement('div'); // Make div
ui.tuning.list.appendChild(div); // Add the div to the page
var p = document.createElement('p'); // Make a <p> to display the name of the property
p.appendChild(document.createTextNode(propName)); // Make content of <p> have the name of the NetworkTables value
div.appendChild(p); // Put <p> in div
var input = document.createElement('input'); // Create input
input.name = propName; // Make its name property be the name of the NetworkTables value
input.value = value; // Set
// The following statement figures out which data type the variable is.
// If it's a boolean, it will make the input be a checkbox. If it's a number,
// it will make it a number chooser with up and down arrows in the box. Otherwise, it will make it a textbox.
if (typeof value === 'boolean') {
input.type = 'checkbox';
input.checked = value; // value property doesn't work on checkboxes, we'll need to use the checked property instead
input.onchange = function () {
// For booleans, send bool of whether or not checkbox is checked
NetworkTables.putValue(key, this.checked);
};
}
else if (!isNaN(value)) {
input.type = 'number';
input.onchange = function () {
// For number values, send value of input as an int.
NetworkTables.putValue(key, parseInt(this.value));
};
}
else {
input.type = 'text';
input.onchange = function () {
// For normal text values, just send the value.
NetworkTables.putValue(key, this.value);
};
}
// Put the input into the div.
div.appendChild(input);
}
}
else {
// Find already-existing input for changing this variable
var oldInput = document.getElementsByName(propName)[0];
if (oldInput) {
if (oldInput.type === 'checkbox') {
oldInput.checked = value;
}
else {
oldInput.value = value;
}
}
else {
console.log('Error: Non-new variable ' + key + ' not present in tuning list!');
}
}
}
// Open tuning section when button is clicked
ui.tuning.button.onclick = function() {
if (ui.tuning.list.style.display === 'none') {
ui.tuning.list.style.display = 'block';
} else {
ui.tuning.list.style.display = 'none';
}
};
// Manages get and set buttons at the top of the tuning pane
ui.tuning.set.onclick = function() {
if (ui.tuning.name.value && ui.tuning.value.value) { // Make sure the inputs have content
NetworkTables.setValue('/SmartDashboard/' + ui.tuning.name.value, ui.tuning.value.value);
}
};
ui.tuning.get.onclick = function() {
ui.tuning.value.value = NetworkTables.getValue(ui.tuning.name.value);
};