Skip to content

Commit

Permalink
Refactor and document command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Dec 11, 2017
1 parent 52ef972 commit 4e14638
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ Alternatively install the module globally like in the old days:
airplanejs
```

### Options

The following options are available when running `airplanejs`:

- `--help` - Show help (alias: `-h`)
- `--version` - Output AirplaneJS version (alias: `-v`)
- `--device <index>` - Select RTL dongle (alias: `-d`, default: `0`)
- `--frequency <hz>` - Set custom frequency (alias: `-f`, default: `2000000`)
- `--gain <gain>` - Set custom tuner gain (alias: `-g`)
- `--max-gain` - Set tuner gain to highest possible value (default: on)
- `--auto-gain` - Disable manual tuner gain (default: off)
- `--enable-agc` - Use Automatic Gain Control (default: off)
- `--port <port>` - Set custom HTTP server port (alias: `-p`, default: `3000`)
- `--no-browser` - Disable automatic opening of default browser

## License

MIT
Expand Down
78 changes: 52 additions & 26 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ const DEFAULT_RATE = 2000000
const DEFAULT_FREQ = 1090000000
const ASYNC_BUF_NUMBER = 12
const DATA_LEN = 16 * 16384 // 256k
const AUTO_GAIN = -100 // Use automatic gain
const MAX_GAIN = 999999 // Use max available gain

process.on('SIGINT', exit)

const argv = require('minimist')(process.argv.slice(2))
argv.openBrowser = argv.openBrowser !== false // default to true

if (argv.help || argv.h) {
help()
process.exit()
}
if (argv.version || argv.v) {
console.log(require('./package').version)
process.exit()
}

const ASSETS_FILES = fs.readdirSync(path.join(__dirname, 'assets'))

Expand All @@ -49,13 +55,13 @@ function onMsg (msg) {
store.addMessage(msg)
}

function setupDevice (opts) {
opts = {
gain: MAX_GAIN,
dev_index: 0,
enable_agc: true,
freq: DEFAULT_FREQ
}
function setupDevice () {
let gain = argv.gain || argv.g
const findMaxGain = !('gain' in argv || 'g' in argv)
const autoGain = argv['auto-gain'] || false
const enableAgc = argv['enable-agc'] || false
const devIndex = argv.device || argv.d || 0
const freq = argv.frequency || argv.f || DEFAULT_FREQ

const ppmError = 0
const vendor = Buffer.alloc(256)
Expand All @@ -71,33 +77,34 @@ function setupDevice (opts) {
debug('Found %d device(s):', deviceCount)
for (let j = 0; j < deviceCount; j++) {
rtlsdr.get_device_usb_strings(j, vendor, product, serial)
debug('%d: %s, %s, SN: %s %s', j, vendor, product, serial, j === opts.dev_index ? '(currently selected)' : '')
debug('%d: %s, %s, SN: %s %s', j, vendor, product, serial, j === devIndex ? '(currently selected)' : '')
}

const dev = rtlsdr.open(opts.dev_index)
const dev = rtlsdr.open(devIndex)
if (typeof dev === 'number') {
console.error('Error opening the RTLSDR device: %s', dev)
process.exit(1)
}

// Set gain, frequency, sample rate, and reset the device
rtlsdr.set_tuner_gain_mode(dev, opts.gain === AUTO_GAIN ? 0 : 1)
if (opts.gain !== AUTO_GAIN) {
if (opts.gain === MAX_GAIN) {
rtlsdr.set_tuner_gain_mode(dev, autoGain ? 0 : 1)
if (!autoGain) {
if (findMaxGain) {
// Find the maximum gain available
const gains = new Int32Array(100)
const numgains = rtlsdr.get_tuner_gains(dev, gains)
opts.gain = gains[numgains - 1]
debug('Max available gain is: %d', opts.gain / 10)
gain = gains[numgains - 1]
debug('Max available gain is: %d', gain / 10)
}
rtlsdr.set_tuner_gain(dev, opts.gain)
debug('Setting gain to: %d', opts.gain / 10)
debug('Setting gain to: %d', gain / 10)
rtlsdr.set_tuner_gain(dev, gain)
} else {
debug('Using automatic gain control.')
debug('Using automatic gain control')
}

rtlsdr.set_freq_correction(dev, ppmError)
if (opts.enable_agc) rtlsdr.set_agc_mode(dev, 1)
rtlsdr.set_center_freq(dev, opts.freq)
if (enableAgc) rtlsdr.set_agc_mode(dev, 1)
rtlsdr.set_center_freq(dev, freq)
rtlsdr.set_sample_rate(dev, DEFAULT_RATE)
rtlsdr.reset_buffer(dev)
debug('Gain reported by device: %d', rtlsdr.get_tuner_gain(dev) / 10)
Expand Down Expand Up @@ -169,22 +176,41 @@ function startServer () {
fn(req, res)
})

if (argv.port) listen(argv.port)
const customPort = argv.port || argv.p

if (customPort) listen(customPort)
else getPort({port: 3000}).then(listen)

function listen (port) {
server.listen(port, function () {
const url = 'http:https://localhost:' + port
if (argv.openBrowser) {
if (argv.browser === false) {
console.log('Server running at: %s', url)
} else {
console.log('Opening %s in your favorite browser...', url)
opn(url)
} else {
console.log('Server running at: %s', url)
}
})
}
}

function help () {
console.log('Usage:')
console.log(' airplanejs [options]')
console.log()
console.log('Options:')
console.log(' --help -h Show this help')
console.log(' --version -v Output AirplaneJS version')
console.log(' --device -d <index> Select RTL dongle (default: 0)')
console.log(' --frequency -f <hz> Set custom frequency (default: 2000000)')
console.log(' --gain -g <gain> Set custom tuner gain')
console.log(' --max-gain Set tuner gain to highest possible value (default: on)')
console.log(' --auto-gain Disable manual tuner gain (default: off)')
console.log(' --enable-agc Use Automatic Gain Control (default: off)')
console.log(' --port -p <port> Set custom HTTP server port (default: 3000)')
console.log(' --no-browser Disable automatic opening of default browser')
}

function exit () {
console.log('Closing down RTL-SDR device...')
rtlsdr.cancel_async(dev)
Expand Down

0 comments on commit 4e14638

Please sign in to comment.