@@ -29,6 +29,7 @@ import {
29
29
MicrobitRadioBridgeConnection ,
30
30
} from "../lib/usb-radio-bridge" ;
31
31
import "./demo.css" ;
32
+ import { Logging } from "../lib/logging" ;
32
33
33
34
type ConnectionType = "usb" | "bluetooth" | "radio" ;
34
35
@@ -37,16 +38,31 @@ type TypedConnection =
37
38
| { type : "bluetooth" ; connection : MicrobitWebBluetoothConnection }
38
39
| { type : "usb" ; connection : MicrobitWebUSBConnection } ;
39
40
41
+ class NullLogging implements Logging {
42
+ event ( e : unknown ) : void {
43
+ console . log ( e ) ;
44
+ }
45
+ error ( e : unknown ) : void {
46
+ console . log ( e ) ;
47
+ }
48
+ log ( e : unknown ) : void {
49
+ console . log ( e ) ;
50
+ }
51
+ }
52
+
53
+ const logging = new NullLogging ( ) ;
54
+
40
55
const createConnections = (
41
56
type : "usb" | "bluetooth" | "radio" ,
42
57
) : TypedConnection => {
43
58
switch ( type ) {
44
59
case "bluetooth" :
45
- return { type, connection : createWebBluetoothConnection ( ) } ;
60
+ return { type, connection : createWebBluetoothConnection ( { logging } ) } ;
46
61
case "usb" :
47
62
return {
48
63
type,
49
64
connection : createWebUSBConnection ( {
65
+ logging,
50
66
deviceSelectionMode : DeviceSelectionMode . UseAnyAllowed ,
51
67
} ) ,
52
68
} ;
@@ -55,10 +71,10 @@ const createConnections = (
55
71
// To use with a remote micro:bit we need a UI flow that grabs and sets the remote id.
56
72
const connection = createRadioBridgeConnection (
57
73
createWebUSBConnection ( {
74
+ logging,
58
75
deviceSelectionMode : DeviceSelectionMode . UseAnyAllowed ,
59
76
} ) ,
60
77
) ;
61
- connection . setRemoteDeviceId ( 0 ) ;
62
78
return { type, connection } ;
63
79
}
64
80
} ;
@@ -109,6 +125,7 @@ const createConnectSection = (): Section => {
109
125
const statusParagraph = crelt ( "p" ) ;
110
126
let name = "" ;
111
127
let exclusionFilters = JSON . stringify ( [ { serialNumber : "XXXX" } ] ) ;
128
+ let remoteDeviceId = 0 ;
112
129
const dom = crelt (
113
130
"section" ,
114
131
crelt ( "h2" , "Connect" ) ,
@@ -158,6 +175,21 @@ const createConnectSection = (): Section => {
158
175
} ) ,
159
176
)
160
177
: undefined ,
178
+ type === "radio"
179
+ ? crelt (
180
+ "label" ,
181
+ "Remote device id" ,
182
+ crelt ( "input" , {
183
+ type : "number" ,
184
+ value : remoteDeviceId ,
185
+ onchange : ( e : Event ) => {
186
+ remoteDeviceId = parseInt (
187
+ ( e . currentTarget as HTMLInputElement ) . value ,
188
+ ) ;
189
+ } ,
190
+ } ) ,
191
+ )
192
+ : undefined ,
161
193
crelt (
162
194
"button" ,
163
195
{
@@ -174,6 +206,8 @@ const createConnectSection = (): Section => {
174
206
connection . setRequestDeviceExclusionFilters ( parsedExclusionFilters ) ;
175
207
} else if ( type === "bluetooth" ) {
176
208
connection . setNameFilter ( name ) ;
209
+ } else if ( type === "radio" ) {
210
+ connection . setRemoteDeviceId ( remoteDeviceId ) ;
177
211
}
178
212
void connection . connect ( ) ;
179
213
} ,
@@ -196,6 +230,9 @@ const createConnectSection = (): Section => {
196
230
statusParagraph . textContent = status . toString ( ) ;
197
231
} ;
198
232
const handleDisplayStatusChange = ( event : ConnectionStatusEvent ) => {
233
+ if ( type === "usb" && event . status === ConnectionStatus . CONNECTED ) {
234
+ console . log ( "Device id:" , ( connection as any ) . getDeviceId ( ) ) ;
235
+ }
199
236
displayStatus ( event . status ) ;
200
237
} ;
201
238
const backgroundErrorListener = ( event : BackgroundErrorEvent ) => {
0 commit comments