forked from sterling/gd-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnrf-comms.js
123 lines (100 loc) · 2.47 KB
/
nrf-comms.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
let NRF24 = require('nrf');
class NRFComms {
constructor(config) {
this._onresponse = null;
this._ontxerror = null;
this.ondata = null;
this.tx = null;
this.rx = null;
this.txAddr = config.tx;
this.rxAddr = config.rx;
this.nrf = NRF24.connect(config.spiDev, config.ce, config.irq);
this.nrf.channel(config.channel || 0x4c);
this.nrf.transmitPower(config.power || 'PA_MAX');
this.nrf.dataRate(config.rate || '1Mbps');
this.nrf.crcBytes(config.crc || 2);
this.nrf.autoRetransmit(config.retransmit || {count: 15, delay: 15});
}
_rxData(buf) {
buf = buf.reverse();
if (this._onresponse) {
this._onresponse(buf);
this._clearResponseListener();
} else {
this.ondata && this.ondata(buf);
}
}
_txError(e) {
console.warn('.', e);
this._clearResponseListener();
this._ontxerror && this._ontxerror(e);
}
_clearResponseListener() {
this._setResponseListener(null, null);
}
_setResponseListener(fn, efn) {
this._onresponse = fn;
this._ontxerror = efn;
}
_write(buf) {
//console.log('writing', buf)
let write = new Buffer(buf);
write.reverse();
this.tx.write(write);
}
_writeWait(buf) {
return new Promise((resolve, reject) => {
this._write(buf);
setTimeout(() => {
this._clearResponseListener();
reject(new Error('ERESPTIMEOUT'));
}, 300);
this._setResponseListener(data => {
this._clearResponseListener();
resolve(data);
}, err => {
this._clearResponseListener();
reject(err);
});
});
}
_transmit(buf) {
}
onData(fn) {
this.ondata = fn;
}
send(buf) {
this._write(buf);
}
/**
* Sends a message and waits for a reply
* @param {Buffer} buf
* @returns {Buffer}
*/
* request(buf) {
for (let i = 0; i < 5; i++) {
try {
return yield this._writeWait(buf);
} catch (e) {
if (e.message != 'ERESPTIMEOUT') {
throw e;
}
}
}
throw new Error('ERESPTIMEOUTFULL');
}
setup() {
return new Promise(resolve => {
this.nrf.begin(() => {
this.tx = this.nrf.openPipe('tx', this.txAddr);
this.rx = this.nrf.openPipe('rx', this.rxAddr);
//this.nrf.printDetails();
this.rx.on('data', this._rxData.bind(this));
this.tx.on('error', this._txError.bind(this));
resolve(true);
});
});
}
}
module.exports = NRFComms;