Created
December 13, 2017 18:48
-
-
Save zwade/92684df9785401a4daa9cdaf0957d41a to your computer and use it in GitHub Desktop.
Capture Script for Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
let yargs = require("yargs") | |
let serial = require("serialport") | |
let fs = require("fs") | |
let { join } = require("path") | |
let glob = require("glob") | |
function getPorts() { | |
let files = glob.sync("/dev/tty.usbmodem*") | |
return files.map((file) => file.split("/dev/tty.usbmodem")[1]) | |
} | |
let ports = getPorts() | |
function begin(outfile, portFile) { | |
if (!portFile) { | |
if (ports.length == 1) { | |
portFile = ports[0] | |
} else if (ports.length == 0) { | |
console.error("No arduino found"); | |
return; | |
} else { | |
console.error(`Multiple serial ports found: ${ports}`) | |
return; | |
} | |
} | |
let port = new serial("/dev/tty.usbmodem" + portFile); | |
let stream = fs.open(join(__dirname, outfile), "w+", (err, fd) => { | |
if (err) { | |
console.error(err) | |
return; | |
} | |
port.on("data", (d) => { | |
fs.write(fd, d.toString(), () => true) | |
}) | |
}) | |
} | |
yargs.usage("capture <cmd> [args]") | |
.command("run [file]", "Start listening for data", (yargs) => { | |
yargs.positional("file", { | |
type: "string", | |
default: "data.csv", | |
describe: "the location for data storage" | |
}) | |
}, (argv) => begin(argv.file, argv.port)) | |
.argv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "capture", | |
"version": "0.1.0", | |
"description": "", | |
"main": "capture.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"glob": "^7.1.2", | |
"serialport": "^6.0.4", | |
"yargs": "^10.0.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment