Skip to content

Commit d96c714

Browse files
committed
Add device connect/disconnect detection
1 parent ea942b8 commit d96c714

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

client.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ function onRender(err){
7373
newFile,
7474
showNewVersionOverlay,
7575
changeFile,
76-
changeProject
76+
changeProject,
77+
deviceAdded
7778
} = handlers;
7879

7980
// Finish Loading Plugin
@@ -92,6 +93,15 @@ function onRender(err){
9293
showNewVersionOverlay();
9394
})
9495
.catch(console.error.bind(console));
96+
97+
chrome.usb.onDeviceAdded.addListener(function(device){
98+
console.log('device added: ', device);
99+
deviceAdded(device);
100+
});
101+
102+
chrome.usb.onDeviceRemoved.addListener(function(device){
103+
console.log('device removed: ', device);
104+
});
95105
}
96106

97107
function onRegister(err){

manifest.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
"serial",
99
"unlimitedStorage",
1010
"syncFileSystem",
11-
"storage"
11+
"storage",
12+
"usb",
13+
{
14+
"usbDevices": [
15+
{ "vendorId": 1027, "productId": 24577 },
16+
{ "vendorId": 1027, "productId": 24597 }
17+
]
18+
}
1219
],
1320
"icons": {
1421
"16": "icons/icon16.png",

src/plugins/handlers.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,33 @@ function handlers(app, opts, done){
615615
download();
616616
}
617617

618+
function deviceAdded(device){
619+
const { device } = store.getState();
620+
const { content } = workspace.getState();
621+
622+
const scanOpts = {
623+
reject: [
624+
/Bluetooth-Incoming-Port/,
625+
/Bluetooth-Modem/,
626+
/dev\/cu\./
627+
],
628+
source: content
629+
};
630+
631+
app.scanBoards(scanOpts)
632+
.then(function(devices){
633+
store.dispatch(creators.updateDevices(devices));
634+
if(device.selected && device.path && !device.connected && _.some(devices, 'path', device.selected.path)){
635+
var board = app.getBoard(device.selected);
636+
if(board){
637+
console.log('opening', board);
638+
board.open();
639+
connect();
640+
}
641+
}
642+
});
643+
}
644+
618645
function enableAutoDownload(){
619646
store.dispatch(creators.enableAutoDownload());
620647
}
@@ -672,7 +699,8 @@ function handlers(app, opts, done){
672699
download,
673700
toggleEcho,
674701
enableAutoDownload,
675-
disableAutoDownload
702+
disableAutoDownload,
703+
deviceAdded
676704
});
677705

678706
done();

0 commit comments

Comments
 (0)