-
Notifications
You must be signed in to change notification settings - Fork 5
/
servo.html
254 lines (190 loc) · 6.21 KB
/
servo.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Servo</title>
<style>
@import url(https://fonts.googleapis.com/css?family=Open+Sans|Roboto:400,500,700,300);
body {
font-family: "Roboto";
}
div {
margin: 10px;
}
h1, h3 {
text-align: center;
}
ul {
margin-top: 10px;
margin-bottom: 30px;
font-size: 14px;
}
label {
display: inline-block;
width: 120px;
font-weight: 500;
}
input[type="text"] {
width: 220px;
padding: 5px;
}
button {
padding: 10px;
margin-right: 30px;
border: 1px solid white;
font-weight: 700;
box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, 0.1);
background-color: #CCC;
}
#main {
max-width: 500px;
margin: 20px auto;
border: 1px solid #DDD;
padding: 20px;
box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, 0.1);
}
.button-container {
margin: 10px auto 30px auto;
max-width: 460px;
text-align: center;
}
#ledon, #ledoff {
color: white;
margin-top: 20px;
margin-bottom: 20px;
}
.servo-btn {
padding: 15px;
font-size: 30px;
}
</style>
<script>
// If we're running on a real web server (as opposed to localhost, which is whitelisted),
// then change the protocol to HTTPS.
// See https://goo.gl/lq4gCo for an explanation as to why this is needed for some features.
(function() {
var isLocalhost = !!(window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/));
if (window.location.protocol === 'http:' && !isLocalhost) {
// Redirect to https: if we're currently using http: and we're not on localhost.
window.location.protocol = 'https:';
}
})();
window.addEventListener('error', function(error) {
if (ChromeSamples && ChromeSamples.setStatus) {
ChromeSamples.setStatus(error.message + ' (Nettleseren din støtter ikke Web Bluetooth.)');
error.preventDefault();
}
});
</script>
</head>
<body>
<div id="main">
<h1> Styring av servo</h1>
<form>
<div><label for="service">Service: </label><input id="service" type="text" list="services" autofocus placeholder="Bluetooth Service" value="00001523-1212-efde-1523-785feabcd123"></div>
<div><label for="service">Characteristic: </label><input id="characteristic" type="text" list="characteristics" placeholder="Bluetooth Characteristic" value="00001525-1212-efde-1523-785feabcd123"></div>
<div class='button-container'>
<button id="connect" >Koble til enhet</button>
</div>
</form>
<div class="button-container">
<button class='servo-btn' onclick="ledOn()">←</button>
<button class="servo-btn" onclick="ledOff()">→</button>
</div>
<script>
var ChromeSamples = {
log: function() {
var line = Array.prototype.slice.call(arguments).map(function(argument) {
return typeof argument === 'string' ? argument : JSON.stringify(argument);
}).join(' ');
document.querySelector('#log').textContent += line + '\n';
},
clearLog: function() {
document.querySelector('#log').textContent = '';
},
setStatus: function(status) {
document.querySelector('#status').textContent = status;
},
setContent: function(newContent) {
var content = document.querySelector('#content');
while(content.hasChildNodes()) {
content.removeChild(content.lastChild);
}
content.appendChild(newContent);
}
};
</script>
<h3>Logg</h3>
<div id="output" class="output">
<div id="content"></div>
<div id="status"></div>
<pre id="log"></pre>
</div>
<!-- // END OF MAIN -->
</div>
<script>
var myCharacteristic;
// Søker etter enheter
function onFormSubmit() {
'use strict';
let serviceUuid = document.getElementById('service').value;
if (serviceUuid.startsWith('0x')) {
serviceUuid = parseInt(serviceUuid, 16);
}
let characteristicUuid = document.getElementById('characteristic').value;
if (characteristicUuid.startsWith('0x')) {
characteristicUuid = parseInt(characteristicUuid, 16);
}
log('Søker etter enheter med valgt service og characteristic...');
navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]})
.then(device => device.connectGATT())
.then(server => server.getPrimaryService(serviceUuid))
.then(service => service.getCharacteristic(characteristicUuid))
.then(characteristic => {
myCharacteristic = characteristic;
log('> Tilkoblet enhet. LED kan nå styres.');
})
.catch(error => {
log('Argh! ' + error);
});
}
// Funksjon for å skru LED på
function ledOn() {
'use strict';
if(myCharacteristic) {
log('> Skrur LED på...');
let ledValue = new Uint8Array([1]);
myCharacteristic.writeValue(ledValue);
}
};
// Funksjon for å skru LED av
function ledOff() {
'use strict';
if(myCharacteristic) {
log('> Skrur LED av...');
let ledValue = new Uint8Array([0]);
myCharacteristic.writeValue(ledValue);
}
};
</script>
<script>
document.querySelector('form').addEventListener('submit', function(event) {
event.stopPropagation();
event.preventDefault();
if (!navigator.bluetooth) {
ChromeSamples.setStatus('Web Bluetooth API er ikke tilgjengelig.\n' +
'Husk å aktivere Web Bluetooth-flagget: chrome://flags');
} else {
ChromeSamples.clearLog();
onFormSubmit();
}
});
log = ChromeSamples.log;
</script>
</body>
</html>