-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuart.html
297 lines (266 loc) · 12.1 KB
/
uart.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!doctype html>
<!--
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<title>BBC micro:bit UART Demo</title>
<meta name="description" content="UART demo with Web Bluetooth.">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<script src="./web/bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<!-- Polymer components -->
<link rel="import" href="./web/bower_components/paper-progress/paper-progress.html">
<link rel="import" href="./web/bower_components/paper-button/paper-button.html">
<link rel="import" href="./web/bower_components/iron-icons/iron-icons.html">
<link rel="import" href="./web/bower_components/iron-icons/image-icons.html">
<link rel="import" href="./web/bower_components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="./web/bower_components/paper-card/paper-card.html">
<link rel="import" href="./web/bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="./web/bower_components/paper-toggle-button/paper-toggle-button.html">
<link rel="import" href="./web/bower_components/iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="./web/bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="./web/bower_components/paper-button/paper-button.html">
<link rel="import" href="./web/bower_components/paper-input/paper-input.html">
<!-- https://github.com/David-Mulder/paper-color-picker -->
<!--link rel="import" href="bower_components/paper-color-picker/paper-color-picker.html"-->
<link rel="import" href="./web/bower_components/paper-styles/color.html">
<link rel="stylesheet" href="./web/bower_components/paper-styles/demo.css">
<style is="custom-style">
body {
background-color: var(--paper-grey-50);
}
paper-progress {
width: 100%;
}
paper-progress.blue {
paper-progress-active-color: var(--paper-light-blue-500);
paper-progress-secondary-color: var(--paper-light-blue-100);
}
paper-toggle-button.blue {
--paper-toggle-button-checked-bar-color: var(--paper-light-blue-500);
--paper-toggle-button-checked-button-color: var(--paper-light-blue-500);
--paper-toggle-button-checked-ink-color: var(--paper-light-blue-500);
--paper-toggle-button-unchecked-bar-color: var(--paper-light-blue-900);
--paper-toggle-button-unchecked-button-color: var(--paper-light-blue-900);
--paper-toggle-button-unchecked-ink-color: var(--paper-light-blue-900);
}
paper-button {
display: block;
width: 80px;
height: 40px;
min-width: 0em;
margin: 0.2em 0.2em;
}
paper-button.blue {
color: var(--paper-light-blue-500);
paper-button-flat-focus-color: var(--paper-light-blue-50);
}
#cards {
margin-left: auto;
margin-right: auto;
max-width: 400px;
}
paper-card {
margin-bottom: 5px;
margin-top: 5px;
width: 100%;
}
.flex {
@apply(--layout-horizontal);
}
paper-button.indigo {
background-color: var(--paper-indigo-500);
color: white;
--paper-button-raised-keyboard-focus: {
background-color: var(--paper-pink-a200) !important;
color: white !important;
}
;
}
</style>
</head>
<body unresolved>
<template id="template" is="dom-bind">
<div id="cards">
<paper-card heading="BBC u:bit UART Bluetooth Demo">
<div class="card-content">
<paper-toggle-button class="blue" id="connect">Connect</paper-toggle-button>
<paper-progress id="progress" indeterminate></paper-progress>
</div>
<div class="card-content">
<paper-input label="Status" id="txtstatus" disabled>></paper-input>
</div>
<div class="card-content">
<paper-input label="Message" id="txtmsg" char-counter maxlength="15"></paper-input>
</div>
<div class="card-content">
<paper-button raised class="indigo" id="buttonsend">Send</paper-button>
</div>
</paper-card>
<paper-dialog id="no-bluetooth">
<h2>No Web Bluetooth</h2>
<p>The Web Bluetooth API is missing. Please enable it at chrome://flags/#enable-web-bluetooth and try again.
</p>
</paper-dialog>
<paper-dialog id="errorDialog">
<h2>Error</h2>
<p>Could not connect to bluetooth device!</p>
</paper-dialog>
<paper-dialog id="alertButtonA" modal role="alertdialog">
<h2>Alert</h2>
<p>Button A was pressed</p>
<div class="buttons">
<paper-button dialog-dismiss>Ok</paper-button>
</div>
</paper-dialog>
<paper-dialog id="alertButtonB" modal role="alertdialog">
<h2>Alert</h2>
<p>Button B was pressed</p>
<div class="buttons">
<paper-button dialog-dismiss>Ok</paper-button>
</div>
</paper-dialog>
</div>
</template>
<script>
'use strict';
document.addEventListener('WebComponentsReady', () => {
let connectToggle = document.querySelector('#connect');
let progress = document.querySelector('#progress');
let dialog = document.querySelector('#errorDialog');
progress.hidden = true;
let gattServer;
let MBIT_UART_SERVICE = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'.toLowerCase(); //to send TO the microbit
let MBIT_UART_RX_CHARACTERISTIC = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'.toLowerCase(); //to send TO the microbit
let MBIT_UART_TX_CHARACTERISTIC = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'.toLowerCase(); //to receive data FROM the microbit
function appendToLog(moreText) {
document.getElementById("txtstatus").value = moreText;
}
let ourMicrobitUART;
let bluetoothSearchOptions = {
filters: [{
namePrefix: "BBC micro:bit",
}],
optionalServices: [MBIT_UART_SERVICE]
};
class MicroBitUART {
constructor(rxCharacteristic, txCharacteristic) {
this.messageSubscribers = [];
this.rxCharacteristic = rxCharacteristic;
this.txCharacteristic = txCharacteristic;
this.decoder = new TextDecoder();
this.txCharacteristic.startNotifications().then(characteristic => {
characteristic.addEventListener('characteristicvaluechanged', ev => {
let value = (event.target).value;
let valueAsString = new TextDecoder().decode(value);
this.handleNewMessage(valueAsString);
});
});
}
subscribeToMessages(receiver) {
this.messageSubscribers.push(receiver);
}
handleNewMessage(message) {
this.messageSubscribers.forEach(subscriber => {
subscriber(message);
});
}
send(value) {
let kvstring = `${value}#`;
let encoder = new TextEncoder('utf-8');
let encoded = encoder.encode(kvstring);
this.rxCharacteristic.writeValue(encoded);
appendToLog("Sent:" + kvstring);
}
}
function connectClicked(e) {
if (gattServer != null && gattServer.connected) {
if (gattServer.disconnect) {
console.log('Disconnecting...');
appendToLog('Disconnecting...');
gattServer.disconnect();
appendToLog('Disconnected');
}
//resetVariables();
} else {
console.log('Connecting...');
progress.hidden = false;
navigator.bluetooth.requestDevice(bluetoothSearchOptions).then(device => {
appendToLog(`Found: ${device.name}`);
return device.gatt.connect();
}).then(server => {
gattServer = server;
appendToLog("Connected");
return server.getPrimaryService(MBIT_UART_SERVICE);
}).then(service => {
return Promise.all([service.getCharacteristic(MBIT_UART_RX_CHARACTERISTIC),
service.getCharacteristic(MBIT_UART_TX_CHARACTERISTIC)
]);
}).then(rxandtx => {
let rx;
let tx;
[rx, tx] = rxandtx;
ourMicrobitUART = new MicroBitUART(rx, tx);
appendToLog("Made a UART!!");
startReadingFromUART(ourMicrobitUART);
progress.hidden = true;
}).catch(error => {
console.log(error);
});
}
}
function startReadingFromUART(mbit) {
mbit.subscribeToMessages((s) => {
appendToLog("Read:" + s);
if(s == 'A#')
document.getElementById('alertButtonA').toggle();
else
document.getElementById('alertButtonB').toggle();
});
//mbit.subscribeToMessages(sayHelloBack);
}
function helloClicked(e) {
ourMicrobitUART.send("dude");
}
function sayHelloBack(message) {
ourMicrobitUART.send("hi");
}
function buttonClicked(event) {
let msg = document.getElementById("txtmsg").value;
ourMicrobitUART.send(msg);
}
/**
* Check if browser supports Web Bluetooth API.
*/
if (navigator.bluetooth == undefined) {
document.getElementById("no-bluetooth").style.display = "block";
document.getElementById("no-bluetooth").open();
}
/**
* Set the default display buttons color and hook up click listener.
*/
let button = document.querySelector('#buttonsend');
button.addEventListener('click', buttonClicked);
/**
* Connect to command characteristic.
*/
connectToggle.addEventListener('click', () => {
connectClicked();
});
});
</script>
</body>
</html>