-
Notifications
You must be signed in to change notification settings - Fork 40
/
restduino_control.html
131 lines (115 loc) · 5.42 KB
/
restduino_control.html
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<html>
<head>
<title>RESDuino Controller</title>
</head>
<body>
<h3>digital pin values:</h3>
<ol>
<li><input id="d1Input" name="d1_input" type="radio" value="HIGH" onMouseeUp="set_pin('D1', this.value)"> HIGH <input id="d1Input" name="d1_input" type="radio" value="LOW" checked onMouseUp="set_pin('D1', this.value)"> LOW
<input id="display_d1" type="text" value="0"></li>
<li><input id="d2Input" type="range" min="0" max="255" value="0" onChange="update_display('display2',this.value)" onMouseUp="set_pin('2', this.value)">
<input id="display2" type="text" value="0"></li>
<li><input id="d3Input" type="range" min="0" max="255" value="0" onChange="update_display('display3',this.value)" onMouseUp="set_pin('3', this.value)">
<input id="display3" type="text" value="0"></li>
<li><input id="d4Input" type="range" min="0" max="255" value="0" onChange="update_display('display4',this.value)" onMouseUp="set_pin('4',this.value)">
<input id="display4" type="text" value="0"></li>
<li><input id="d5Input" type="range" min="0" max="255" value="0" onChange="update_display('display5',this.value)" onMouseUp="set_pin('5',this.value)">
<input id="display5" type="text" value="0"></li>
<li><input id="d6Input" type="range" min="0" max="255" value="0" onChange="update_display('display6',this.value)" onMouseUp="set_pin('6',this.value)">
<input id="display6" type="text" value="0"></li>
<li><input id="d7Input" type="range" min="0" max="255" value="0" onChange="update_display('display7',this.value)" onMouseUp="set_pin('7', this.value)">
<input id="display7" type="text" value="0"></li>
<li><input id="d8Input" type="range" min="0" max="255" value="0" onChange="update_display('display8',this.value)" onMouseUp="set_pin('8', this.value)">
<input id="display8" type="text" value="0"></li>
<li><input id="d9Input" type="range" min="0" max="255" value="0" onChange="update_display('display9',this.value)" onMouseUp="set_pin('9',this.value)">
<input id="display9" type="text" value="0"></li>
<li><input id="d10Input" type="range" min="0" max="255" value="0" onChange="update_display('display10',this.value)" onMouseUp="set_pin('10',this.value)">
<input id="display10" type="text" value="0"></li>
<li><input id="d11Input" type="range" min="0" max="255" value="0" onChange="update_display('display11',this.value)" onMouseUp="set_pin('11',this.value)">
<input id="display11" type="text" value="0"></li>
<li><input id="d12Input" type="range" min="0" max="255" value="0" onChange="update_display('display12',this.value)" onMouseUp="set_pin('12', this.value)">
<input id="display12" type="text" value="0"></li>
<li><input id="d13Input" type="range" min="0" max="255" value="0" onChange="update_display('display13',this.value)" onMouseUp="set_pin('13',this.value)">
<input id="display13" type="text" value="0"></li>
</ol>
<h3>analog pin values:</h3>
<ol>
<li><input id="a1_input" type="range" min="0" max="1023" value="0" readonly>
<input id="display_a1" type="text" value="0"></li>
</ol>
<script>
var restduino_host = "restduino.local";
// check initial status
update_status();
// start timer to periodically update status
setInterval(update_status, 1000);
function update_status(){
// check to see if we can reach RESTduino
get_pin(restduino_host, "1", function(connected){
if(connected){
// todo: enable controls
/*
// read selected digital pins
get_pin(restduino_host, "D1", function(valid_response){
// debug
console.log(valid_response);
if(valid_response){
update_display("display_d1", valid_response.D1);
}
});
*/
// read analog pins
get_pin(restduino_host, "a1", function(valid_response){
// debug
console.log(valid_response.A1);
if(valid_response){
update_display("a1_input", valid_response.A1);
update_display("display_a1", valid_response.A1);
}
});
} else {
// todo: disable controls
}
});
}
// check pin value
function get_pin(restduino_host, pin, callback){
console.log("Attempting to read pin " + pin + " on " + restduino_host + "...");
ping_req = new XMLHttpRequest();
ping_req.open("GET", "http://" + restduino_host + "/" + pin, true);
ping_req.onreadystatechange = function(){
console.log("ping request state: " + this.readyState);
if(this.readyState === 4){
if(this.status === 200){
console.log("RESTduino responded with HTTP status " + this.status);
callback(JSON.parse(this.responseText));
} else {
console.log("Unable to reach RESTduino");
callback(false);
}
}
};
ping_req.send();
}
function update_display(control, value){
document.getElementById(control).value = value;
}
function set_pin(pin, value){
// debug
console.log('sending ' + value + ' to pin ' + pin);
restduinoReq = new XMLHttpRequest();
restduinoReq.open('GET', 'http://restduino.local/' + pin + '/' + value, true);
restduinoReq.onreadystatechange = restduinoRes;
restduinoReq.send();
function restduinoRes(){
console.log('got response from restduino');
if(this.readyState == 4){
if(this.status == 200){
console.log(this.status);
}
}
}
}
</script>
</body>
</html>