Skip to content

Commit 321e102

Browse files
committed
Add auto port finding for the Arduino (& minimum error port exit!)
1 parent 5f50ab9 commit 321e102

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

etch-a-bits.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var cncserver = require("cncserver");
22
var serialport = require("./node_modules/cncserver/node_modules/serialport/serialport.js");
33
var SerialPort = serialport.SerialPort;
44

5-
var port = process.argv[2] ? process.argv[2] : "/dev/ttyACM0";
5+
var port = process.argv[2];
66

77
// Actually try to init the connection and handle the various callbacks
88
cncserver.start({
@@ -15,7 +15,9 @@ cncserver.start({
1515
connect: function() {
1616
console.log('Bot Connected!');
1717
cncserver.setHeight('up');
18-
startReading();
18+
connectLeonardo(function(){
19+
startReading();
20+
});
1921
},
2022
disconnect: function() {
2123
console.log("Bot disconnected!")
@@ -123,3 +125,23 @@ function getWater(destPt) {
123125
});
124126
});
125127
}
128+
129+
130+
// Helper function for automatically connecting the littleBits leonardo
131+
function connectLeonardo(callback) {
132+
serialport.list(function (err, ports) {
133+
for (var portID in ports){
134+
if (ports[portID].serialNumber === 'Arduino_LLC_Arduino_Leonardo') {
135+
port = ports[portID].comName;
136+
}
137+
}
138+
139+
if (!port) {
140+
console.error('Littlebits Leonardo not found! ERR 16');
141+
process.exit(16);
142+
} else {
143+
console.log('Leonardo found on port:', port);
144+
callback();
145+
}
146+
});
147+
}

0 commit comments

Comments
 (0)