-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added command line options to drive behavior, some basic error handling
- Loading branch information
Joonas Trussmann
committed
May 11, 2021
1 parent
e47afb2
commit eef4a70
Showing
4 changed files
with
169 additions
and
58 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,99 @@ | ||
var usb = require('usb') | ||
const usb = require('usb') | ||
const yargs = require('yargs') | ||
const fs = require('fs') | ||
|
||
|
||
const argv = yargs | ||
.option('f', { | ||
alias: 'file', | ||
describe: 'output video feed to file', | ||
type: 'string' | ||
}) | ||
.option('o', { | ||
alias: 'stdout', | ||
describe: 'send video feed to stdout for playback. eg: node index.js -o | ffplay -', | ||
type: 'boolean' | ||
}) | ||
.option('s', { | ||
alias: 'readsize', | ||
describe: 'size in bytes to queue for usb bulk interface reads', | ||
default: 512, | ||
type: 'integer' | ||
}) | ||
.option('q', { | ||
alias: 'queuesize', | ||
describe: 'number of polling usb bulk read requests to keep in flight', | ||
default: 3, | ||
type: 'integer' | ||
}) | ||
.option('v', { | ||
alias: 'verbose', | ||
describe: 'be noisy - doesn not play well with -o', | ||
type: 'boolean' | ||
}) | ||
.help() | ||
.alias('help', 'h') | ||
.argv; | ||
|
||
var goggles = usb.findByIds("0x2ca3", "0x1f") | ||
console.log(goggles) | ||
|
||
if(!goggles) { | ||
console.error("Goggles USB device not found. Please connect your goggles and restart the script.") | ||
process.exit(1) | ||
} | ||
goggles.open() | ||
console.log(goggles.interfaces) | ||
if(!goggles.interfaces) { | ||
console.error("Couldn't open Goggles USB device") | ||
process.exit(1) | ||
} | ||
var interface = goggles.interface(3) | ||
console.log(interface) | ||
interface.claim() | ||
if(!interface.endpoints) { | ||
console.error("Couldn't claim bulk interface") | ||
process.exit(1) | ||
} | ||
|
||
if(!argv.f && !argv.o) { | ||
console.log("warning: no outputs specified") | ||
argv.v = true | ||
} | ||
|
||
const fs = require('fs'); | ||
var fd | ||
|
||
// specify the path to the file, and create a buffer with characters we want to write | ||
let path = 'out.bin'; | ||
var inpoint = interface.endpoints[1] | ||
inpoint.timeout = 100 | ||
//outpoint.timeout = 200 | ||
|
||
// open the file in writing mode, adding a callback function where we do the actual writing | ||
fs.open(path, 'w', function(err, fd) { | ||
if (err) { | ||
throw 'could not open file: ' + err; | ||
} | ||
var inpoint = interface.endpoints[1] | ||
inpoint.timeout = 100 | ||
|
||
|
||
var outpoint = interface.endpoints[0] | ||
//var magic = Buffer.from("54564d52", "hex") | ||
var magic = Buffer.from("524d5654", "hex") | ||
console.log(magic) | ||
|
||
outpoint.transfer(magic, function() { | ||
console.log("magic written") | ||
/*inpoint.transfer(20000, function(error, data){ | ||
console.log("error", error) | ||
console.log("data", data) | ||
setTimeout(getData) | ||
})*/ | ||
|
||
}) | ||
inpoint.addListener("data", function(data) { | ||
console.log(data) | ||
fs.writeSync(fd, data) | ||
}) | ||
inpoint.addListener("error", function(error) { | ||
console.log(error) | ||
}) | ||
inpoint.startPoll(3, 20000) | ||
|
||
// write the contents of the buffer, from position 0 to the end, to the file descriptor returned in opening our file | ||
var outpoint = interface.endpoints[0] | ||
var magic = Buffer.from("524d5654", "hex") | ||
|
||
outpoint.transfer(magic, function(error) { | ||
if(error) { | ||
console.error(error) | ||
} | ||
console.debug("send magic bytes") | ||
|
||
}); | ||
|
||
}) | ||
inpoint.addListener("data", function(data) { | ||
if(argv.o) { | ||
process.stdout.write(data) | ||
} | ||
if(argv.v) { | ||
console.debug("received "+data.length+" bytes") | ||
} | ||
if(argv.f) { | ||
if(!fd) { | ||
fd = fs.openSync(argv.f, "w") | ||
if(!fd) { | ||
console.error("couldn't open file "+ argv.f + ": "+err) | ||
process.exit(1) | ||
} | ||
} | ||
fs.writeSync(fd, data) | ||
} | ||
}) | ||
inpoint.addListener("error", function(error) { | ||
console.error(error) | ||
}) | ||
inpoint.startPoll(argv.q, argv.s) | ||
|
||
/*endpoint.transfer(512, function(error, data){ | ||
console.log(error) | ||
console.log(data) | ||
})*/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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