/
index.js
92 lines (74 loc) · 2.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
if (process.env.HACKRF_DEBUG) require('segfault-handler').registerHandler()
var hackrf = require('node-gyp-build')(__dirname)
module.exports = function () {
var devices = hackrf.devices()
devices.open = function (i) {
var device = devices._open(i)
return new API(device)
}
return devices
}
var API = function (device) {
this.device = device
}
API.prototype.getVersion = function () {
return this.device.getVersion()
}
API.prototype.setFrequency = function (n, cb) {
if (typeof n !== 'number') throw new Error('Frequency should be a number')
this.device.setFrequency(n, cb || noop)
}
API.prototype.setBandwidth = function (n, cb) {
if (typeof n !== 'number') throw new Error('Bandwidth should be a number')
this.device.setBandwidth(n, cb || noop)
}
API.prototype.setSampleRate = function (n, cb) {
if (typeof n !== 'number') throw new Error('Sample rate should be a number')
this.device.setSampleRate(n, cb || noop)
}
API.prototype.setLNAGain = function (n) {
if (typeof n !== 'number') throw new Error('LNA gain should be a number')
this.device.setLNAGain(n)
}
API.prototype.setAmpEnable = function (n) {
this.device.setAmpEnable(n)
}
API.prototype.setAntennaEnable = function (n) {
this.device.setAntennaEnable(n)
}
API.prototype.setVGAGain = function (n) {
if (typeof n !== 'number') throw new Error('VGA gain should be a number')
this.device.setVGAGain(n)
}
API.prototype.setTxGain = function (n) {
if (typeof n !== 'number') throw new Error('Tx gain should be a number')
this.device.setTxGain(n)
}
API.prototype.startRx = function (cb) {
var self = this
this.device.startRx(function (data) {
cb(data, function () {
self.device.endRx()
})
})
}
API.prototype.stopRx = function (cb) {
this.device.stopRx(cb || noop)
}
API.prototype.startTx = function (cb) {
var self = this
var buf = new Buffer(0)
this.device.startTx(function (max) {
if (max > buf.length) buf = new Buffer(max)
cb(max !== buf.length ? buf.slice(0, max) : buf, function () {
self.device.endTx(buf)
})
})
}
API.prototype.stopTx = function (cb) {
this.device.stopTx(cb || noop)
}
API.prototype.close = function (cb) {
this.device.close(cb || noop)
}
function noop () {}