Skip to content

Commit

Permalink
Move all RTLSDR logic into sub-module
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Dec 24, 2017
1 parent 7f088de commit 3439b96
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 83 deletions.
96 changes: 96 additions & 0 deletions lib/rtlsdr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict'

const rtlsdr = require('rtl-sdr')
const Demodulator = require('mode-s-demodulator')
const debug = require('debug')('airplanejs')
const store = require('./store')

const DEFAULT_RATE = 2000000
const DEFAULT_FREQ = 1090000000
const ASYNC_BUF_NUMBER = 12
const DATA_LEN = 16 * 16384 // 256k

const noop = function () {}
const demodulator = new Demodulator()

let dev

exports.start = function (argv) {
if (dev) throw new Error('Cannot start rtlsdr more than once')

// Connect to RTLSDR device
dev = setupDevice(argv)

// Start reading data from RTLSDR device
rtlsdr.read_async(dev, onData, noop, ASYNC_BUF_NUMBER, DATA_LEN)
}

exports.stop = function () {
rtlsdr.cancel_async(dev)
rtlsdr.close(dev)
}

function onData (data, len) {
demodulator.process(data, len, onMsg)
}

function onMsg (msg) {
store.addMessage(msg)
}

function setupDevice (argv) {
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)
const product = Buffer.alloc(256)
const serial = Buffer.alloc(256)

const deviceCount = rtlsdr.get_device_count()
if (!deviceCount) {
console.error('No supported RTLSDR devices found.')
process.exit(1)
}

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 === devIndex ? '(currently selected)' : '')
}

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, 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)
gain = gains[numgains - 1]
debug('Max available gain is: %d', gain / 10)
}
debug('Setting gain to: %d', gain / 10)
rtlsdr.set_tuner_gain(dev, gain)
} else {
debug('Using automatic gain control')
}

rtlsdr.set_freq_correction(dev, ppmError)
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)

return dev
}
87 changes: 4 additions & 83 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,9 @@ const debug = require('debug')('airplanejs')
const getPort = require('get-port')
const opn = require('opn')
const patterns = require('patterns')()
const rtlsdr = require('rtl-sdr')
const Demodulator = require('mode-s-demodulator')
const store = require('./lib/store')
const rtlsdr = require('./lib/rtlsdr')
const routes = require('./lib/routes')

const DEFAULT_RATE = 2000000
const DEFAULT_FREQ = 1090000000
const ASYNC_BUF_NUMBER = 12
const DATA_LEN = 16 * 16384 // 256k

process.on('SIGINT', exit)

const argv = require('minimist')(process.argv.slice(2))
Expand All @@ -30,83 +23,12 @@ if (argv.version || argv.v) {
process.exit()
}

const noop = function () {}
const demodulator = new Demodulator()

// Connect to RTLSDR device
const dev = setupDevice()

// Start reading data from RTLSDR device
rtlsdr.read_async(dev, onData, noop, ASYNC_BUF_NUMBER, DATA_LEN)
// Start radio
rtlsdr.start(argv)

// Start HTTP server
startServer()

function onData (data, len) {
demodulator.process(data, len, onMsg)
}

function onMsg (msg) {
store.addMessage(msg)
}

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)
const product = Buffer.alloc(256)
const serial = Buffer.alloc(256)

const deviceCount = rtlsdr.get_device_count()
if (!deviceCount) {
console.error('No supported RTLSDR devices found.')
process.exit(1)
}

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 === devIndex ? '(currently selected)' : '')
}

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, 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)
gain = gains[numgains - 1]
debug('Max available gain is: %d', gain / 10)
}
debug('Setting gain to: %d', gain / 10)
rtlsdr.set_tuner_gain(dev, gain)
} else {
debug('Using automatic gain control')
}

rtlsdr.set_freq_correction(dev, ppmError)
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)

return dev
}

function startServer () {
patterns.add('GET /', routes.index)
patterns.add('GET /assets/{file}', routes.assets)
Expand Down Expand Up @@ -168,7 +90,6 @@ function help () {

function exit () {
console.log('Closing down RTL-SDR device...')
rtlsdr.cancel_async(dev)
rtlsdr.close(dev)
rtlsdr.stop()
process.exit()
}

0 comments on commit 3439b96

Please sign in to comment.