Skip to content

Commit 68c261b

Browse files
author
L Diaz
committed
upgrade 0.0.0-alpha4
1 parent 7a8b91f commit 68c261b

17 files changed

+153
-72
lines changed

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
The MIT License (MIT)
2+
=====================
3+
4+
Copyright © 2018 Luis Diaz
5+
6+
Permission is hereby granted, free of charge, to any person
7+
obtaining a copy of this software and associated documentation
8+
files (the “Software”), to deal in the Software without
9+
restriction, including without limitation the rights to use,
10+
copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the
12+
Software is furnished to do so, subject to the following
13+
conditions:
14+
15+
The above copyright notice and this permission notice shall be
16+
included in all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
19+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1+
12
# nrf24 - RF24 Radios in the node way
23

3-
This module enable __nodejs__ to manage __nRF24L01(+)__ radios using javascript syntactic sugar manage seamlessly on linux-based one board computers (RaspberryPi,OrangePi,BeagleBone,Edison,CHIP...). Wide support for SBCs is provided by the *RF24 library* supporting Generic Linux devices with SPIDEV, MRAA, RPi native via BCM* chips, WiringPi or using LittleWire.
4+
[![GitHub issues](https://img.shields.io/github/issues/ludiazv/node-nrf24.svg)](https://github.com/ludiazv/node-nrf24/issues)
5+
6+
This module enable __nodejs__ (using javascript syntactic sugar) to manage __nRF24L01(+)__ radios on linux-based one board computers (RaspberryPi,OrangePi,BeagleBone,Edison,CHIP...). Wide support for SBCs is provided by the *RF24 library* supporting Generic Linux devices with SPIDEV, MRAA, RPi native via BCM* chips, WiringPi or using LittleWire.
47

5-
This module is based on the outstanding C++ library optimized by @tmrh20. Please consult this project documentation for **nRF24** [here](http://tmrh20.github.io/RF24/).
8+
This module is based on the outstanding C++ library optimized by @tmrh20. Please consult this project documentation **nRF24** [here](http://tmrh20.github.io/RF24/) for additional details.
9+
10+
This project has a sister project to enable **Node-RED** to use __nRF24L01(+)__ radios in a seamless way with visual flow programming. Check out the repository [here](https://github.com/ludiazv/node-red-contrib-nrf24).
611

7-
This project has a sister project enable Node-RED to use __nRF24L01(+)__ radios in a seamless way. Check out the repository [here](TODO)
812

913
## Overview
1014

11-
This node add-on has the following features:
15+
This nodejs add-on has the following features:
1216

1317
1. Basic __RF24__ radio support.
1418
2. Create a __RF24Mesh__ network as master or Join to a *RF24Mesh* as node.
15-
3. Provide a __RF24Gateway__ directly on nodeJs to provide TCP/IP connectivity for your radios.
19+
3. Provide a __RF24Gateway__ directly on nodeJs to provide TCP/IP connectivity for your radios. *[In development]*
1620

1721

1822
## Installation
1923

2024
### Prerequisites:
2125

26+
#### SPI enabled
27+
In order to communicate with the radio linux kernel need to have SPI enabled. In some distributions SPI interface is disabled by default so it's needed to be enabled.
28+
29+
In Rpi Raspbian it can be done using ``raspi-config`` or modifing `` /boot/config.txt ``. Check [here](https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md) for more details.
30+
For Rpi DietPi (recommended distribution for headless projects) the procedure is similar.
31+
32+
In Rpi alternatives based on armbian such OrangePi, NaoPi,... please consult armbian documentation [here](https://docs.armbian.com/Hardware_Allwinner_overlays/).
33+
34+
For other SBCs or distributions consult who to activate SPI.
35+
36+
If using Spidev driver (see below) the user executing the nodejs application need to have access to the /dev/spidevX.X files. This can be done in typical distribution adding the user to the ``spi`` group.
37+
2238
#### A working nRF24L01(+) radio
2339

2440
A *nRF24L01(+)* radio must be wired to your system and tested for connection. RF24* libs have several testing examples that can be use for for that purpose.
@@ -49,6 +65,9 @@ An example of wiring for an OrangePi Zero(using SPI 1 bus) and Rpi (using SPI 0
4965

5066
Understand your wiring is critical for hardware initialization as there are not hardware discovery mechanism implemented in the library.
5167

68+
__note__: IRQ pins are not used in current version but planned.
69+
70+
5271
#### RF24* libraries installed
5372

5473
RF24* libs must be installed in the system. Please check out [this](http://tmrh20.github.io/RF24/Linux.html) for detailed installation procedure.
@@ -60,7 +79,7 @@ You can use the installation script in this repository:
6079
# ./build_rf24libs.sh
6180
```
6281

63-
it should work on a typical linux environments. nRF24 c++ support different drivers. SPIDEV driver is recommended as this this the standard portable SPI.
82+
it should work on a typical linux environments. nRF24 c++ support different drivers. SPIDEV driver is recommended as this this the standard portable SPI. But in principle all drived provides _should_ work in a similar way. This nodejs package has been tested using Rpi driver and Spidev.
6483

6584
### Install the nrf24 package
6685

@@ -410,7 +429,7 @@ rf24.stop_read();
410429

411430

412431
### useWritePipe(addrs)
413-
432+
Define the output pipe to write to. __nRF24L01__ have only one
414433

415434

416435

@@ -431,15 +450,28 @@ TODO
431450

432451
# TODO
433452

434-
- Test bindings
435-
- Migrate to NAN 2.8 to support queued async msg passing.
453+
- ~~Test bindings~~
454+
- ~~Migrate to NAN >2.8 to support queued async msg passing.~~
455+
- Document Mesh
456+
- Document Gateway.
436457
- Get rid off try_abort hack (pending nRF24 lib merge)
437458
- Implement IRQ management.
438-
459+
- Benchmark IRQ performance.
439460

440461
# Change log
441-
- v0.0.0 alpha versions
442-
- v0.0.1 First version
462+
463+
- v0.0.0-alpha4
464+
- Documentation improvements.
465+
- Basic RF24 additional Testing
466+
- Migrated to NAN 2.10.0: Async workers now are queued so no data loss in nodejs.
467+
- Cleaned RF24Mesh for after testing.
468+
- Test/Example modification to arduino RF24 common pins.
469+
- Added License
470+
471+
- v0.0.0-alpha3
472+
- Include bindings package to resolve package name
443473
- Basic RF24 Working stable.
444474
- RF24Mesh in testing.
445475
- RF24Gateway in development.
476+
477+
- v0.0.0-alpha1 & 2 versions: Initial implementation of Basic Radio.

binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"cflags": [
99
"-std=c++11","-fexceptions"
1010
],
11-
'cflags_cc': [ '-fexceptions',"-std=c++11" ],
11+
'cflags_cc': [ '-fexceptions',"-std=c++11","-Ofast" ],
1212
"include_dirs": [
1313
"<!(node -e \"require('nan')\")",
1414
"<!(node -e \"require('nan-check')\")",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nrf24",
3-
"version": "0.0.0-alpha3",
3+
"version": "0.0.0-alpha4",
44
"description": "nRF24 (nrf24l01/nrfl24l01+) radios in the nodejs way",
55
"main": "index.js",
66
"scripts": {
@@ -37,7 +37,7 @@
3737
"gypfile": true,
3838
"dependencies": {
3939
"bindings": "^1.3.0",
40-
"nan": "^2.7.0",
40+
"nan": "^2.10.0",
4141
"nan-check": "0.0.6"
4242
}
4343
}

rf24.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
#include "rf24.hpp"
55
#include "tryabort.hpp"
66

7+
#ifdef NRF24_DEBUG
8+
79
std::ostream& operator<<(std::ostream& out, RF24_conf_t &h) {
810
return out << "PALevel:" << (int)h.PALevel << " DataRate:" << (int)h.DataRate
911
<< " Channel:" << (int)h.Channel << " CRCLength:" << (int)h.CRCLength
1012
<< " Retries:("<< (int)h.retriesDelay << "," << (int)h.retriesCount << ")"
1113
<< " PayloadSize:" << (int)h.PayloadSize << " AddressWidth:" << (int)h.AddressWidth;
1214
}
1315

16+
#endif
17+
1418
static RF24_conf_t DEFAULT_RF24_CONF={ RF24_PA_MIN , RF24_1MBPS , 76 , RF24_CRC_16 , 5, 15, 32, 5 };
1519

1620
/* Reader */
17-
void nRF24::ReaderWorker::Execute(const Nan::AsyncProgressWorker::ExecutionProgress& progress_) {
21+
void nRF24::ReaderWorker::Execute(const RF24AsyncWorker::ExecutionProgress& progress_) {
1822
char frame[32+1];
1923
uint8_t pipe;
2024
useconds_t half=poll_timeus/4;
@@ -57,7 +61,7 @@ void nRF24::ReaderWorker::HandleProgressCallback(const char *data, size_t size)
5761
Nan::CopyBuffer( ((char*)data)+1,size-1).ToLocalChecked(),
5862
Nan::New(pipe)
5963
};
60-
progress->Call(2, argv);
64+
progress->Call(2, argv,this->async_resource);
6165
}
6266

6367
void nRF24::ReaderWorker::HandleOKCallback() {
@@ -66,7 +70,7 @@ void nRF24::ReaderWorker::HandleOKCallback() {
6670
Nan::New(stopped_),
6771
Nan::New(want_stop),
6872
Nan::New(error_count) };
69-
callback->Call(3,argv);
73+
callback->Call(3,argv,this->async_resource);
7074
}
7175

7276
void nRF24::ReaderWorker::stop() {
@@ -141,15 +145,15 @@ bool nRF24::_write(void *data,size_t r_length){
141145
if(!is_enabled_) return false;
142146
if(worker_!=NULL) worker_->want_write(true);
143147
std::lock_guard<std::mutex> guard2(radio_write_mutex); // write lock
144-
//std::cout << "try to w" << std::endl;
148+
145149
if(_powerUp() && _transmit()) {
146150
std::lock_guard<std::mutex> guard(radio_mutex); // radio lock
147151
try_and_catch_abort([&]() -> void {
148152
res=radio_->write(data,r_length);
149153
});
150154
}
151155
if(worker_!=NULL) worker_->want_write(false);
152-
_listen(); // Back to listen
156+
_listen(); // Back to listen TODO- remove this an test.
153157
return res;
154158
}
155159
bool nRF24::_useWritePipe(uint8_t *pipe_name){
@@ -166,7 +170,7 @@ int32_t nRF24::_addReadPipe(uint8_t *pipe_name,bool auto_ack) {
166170
int i=1;
167171
if(!is_enabled_) return -1;
168172
while(used_pipes_[i] && i<6) i++;
169-
//std::cout << "Free pipe " << i << std::endl;
173+
170174
if(i<6) {
171175
std::lock_guard<std::mutex> guard(radio_mutex); // radio lock
172176
try_and_catch_abort([&]() -> void {

rf24.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,25 @@ typedef struct RF24_conf {
2020

2121
std::ostream& operator<<(std::ostream& out, RF24_conf_t &h); // Prototype
2222

23-
//typedef Nan::AsyncProgressQueueWorker<char> LFG;
2423

2524
class nRF24 : public Nan::ObjectWrap {
2625
public:
2726
// Reader nested class
28-
class ReaderWorker : public Nan::AsyncProgressWorker {
27+
class ReaderWorker : public RF24AsyncWorker {
2928
public:
3029
ReaderWorker(
3130
Nan::Callback *progress
3231
, Nan::Callback *callback
3332
, nRF24& dev)
34-
: Nan::AsyncProgressWorker(callback), progress(progress),device(dev)
33+
: RF24AsyncWorker(callback), progress(progress),device(dev)
3534
, want_stop(false),stopped_(true), want_towrite(false), error_count(0)
3635
, poll_timeus(RF24_DEFAULT_POLLTIME) {
3736
dev.worker_=this;
3837
}
3938
~ReaderWorker() { device.worker_=NULL; if(progress) delete progress; }
4039

4140
// Main loop for pooling the reading
42-
void Execute(const Nan::AsyncProgressWorker::ExecutionProgress& progress);
41+
void Execute(const RF24AsyncWorker::ExecutionProgress& progress);
4342
void HandleProgressCallback(const char *data, size_t size);
4443
void HandleOKCallback();
4544

rf24_config.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include <nan.h>
55
#include <nan-check.h>
66
#include <RF24.h>
7-
#include <RF24Network.h>
7+
/*#include <RF24Network.h>
88
#include <RF24Mesh.h>
99
#include <RF24Gateway.h>
10-
10+
*/
1111

1212
// config
1313
#define RF24_DEFAULT_POLLTIME 50*1000 // 50ms default poll wait for reading
@@ -19,5 +19,10 @@
1919
#define NANCONST(x,v) (Nan::Set(target,Nan::New(x).ToLocalChecked(),Nan::New(v).ToLocalChecked()))
2020
#define NANCONSTI(x,v) (Nan::Set(target,Nan::New(x).ToLocalChecked(),Nan::New(v)))
2121

22+
// Old Nan version 2.7.0
23+
//typedef Nan::AsyncProgressWorker RF24AsyncWorker;
24+
// For Nan version > 2.8.0
25+
typedef Nan::AsyncProgressQueueWorker<char> RF24AsyncWorker;
26+
2227

2328
#endif

rf24_util.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#define _NRF24_NODE_UTIL_HPP_
33
#include<nan.h>
44
// For debug
5+
#ifdef NRF24_DEBUG
56
#include<iostream>
7+
#endif
68

79
bool ObjHas(v8::Local<v8::Object> & obj,const std::string & name);
810
v8::Local<v8::Value> ObjGet(v8::Local<v8::Object> & obj,const std::string & name);

rf24mesh.cc

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
// Main loop for pooling the reading
88

9-
void nRF24Mesh::MeshWorker::Execute(const Nan::AsyncProgressWorker::ExecutionProgress& progress_) {
9+
void nRF24Mesh::MeshWorker::Execute(const RF24AsyncWorker::ExecutionProgress& progress_) {
1010
stopped_=false;
1111
useconds_t half=poll_timeus/4;
1212
nRF24Mesh::MeshFrame frame;
@@ -48,7 +48,7 @@ void nRF24Mesh::MeshWorker::HandleProgressCallback(const char *data, size_t size
4848
Nan::CopyBuffer((char*)f->data,f->size).ToLocalChecked()
4949
};
5050

51-
progress->Call(2, argv);
51+
progress->Call(2, argv,this->async_resource);
5252
}
5353

5454
void nRF24Mesh::MeshWorker::HandleOKCallback() {
@@ -57,7 +57,7 @@ void nRF24Mesh::MeshWorker::HandleOKCallback() {
5757
Nan::New(stopped_),
5858
Nan::New(want_stop),
5959
Nan::New(error_count) };
60-
callback->Call(3,argv);
60+
callback->Call(3,argv,this->async_resource);
6161
}
6262

6363
void nRF24Mesh::MeshWorker::stop() {
@@ -78,9 +78,8 @@ bool nRF24Mesh::_begin(uint8_t nodeID,uint8_t channel, uint8_t data_rate,uint8_t
7878
bool res=false;
7979
std::lock_guard<std::mutex> guard(mesh_mutex);
8080
if(reset_dhcp) std::remove("dhcplist.txt"); // Clean DHCP if required
81-
on_abort(); // Do nothing on abort
81+
//on_abort(); // Do nothing on abort
8282
try_and_catch_abort([&]() -> void {
83-
set_debug_abort("begin");
8483
int ce,cs;
8584
// Disable referenced radio object
8685
nrf24._begin(false);
@@ -116,7 +115,6 @@ int16_t nRF24Mesh::_getNodeID(uint16_t address) {
116115
std::lock_guard<std::mutex> guard(rules_mutex);
117116
std::lock_guard<std::mutex> guard2(mesh_mutex);
118117
try_and_catch_abort([&]() -> void {
119-
set_debug_abort("getID");
120118
res=mesh->getNodeID(address);
121119
});
122120
return res;
@@ -139,7 +137,6 @@ bool nRF24Mesh::_send(void* data, uint8_t msg_type, uint16_t size, uint8_t nodeI
139137
bool res=false;
140138
std::lock_guard<std::mutex> guard(mesh_mutex);
141139
try_and_catch_abort([&]() -> void {
142-
set_debug_abort("write");
143140
res=mesh->write(data,msg_type,size,nodeID);
144141
if(!res && !_isMaster()) {
145142
if(!mesh->checkConnection()) mesh->renewAddress();
@@ -154,28 +151,21 @@ int nRF24Mesh::_loop(nRF24Mesh::MeshFrame &frame) {
154151
std::lock_guard<std::mutex> guard(mesh_mutex);
155152
std::lock_guard<std::mutex> guard2(rules_mutex);
156153
try_and_catch_abort([&]()-> void {
157-
//std::cout << "Thread" << std::this_thread::get_id() << std::endl;
158-
set_debug_abort("update");
159154
mesh->update();
160-
set_debug_abort("dhcp");
161155
if(_isMaster()) mesh->DHCP();
162-
set_debug_abort("available");
163156
if(network->available()) {
164157
RF24NetworkHeader header; // Check the header and try to match rules
165-
set_debug_abort("peek");
166158
network->peek(header);
167159
NRF24DBG(std::cout << "[Loop] type:" << header.type << std::endl);
168160
auto i=rules.begin();
169161
for( ;i != rules.end(); ++i) {
170-
if(i->type == 0 || i->type == header.type) { //
171-
set_debug_abort("read");
162+
if(i->type == 0 || i->type == header.type) { // type match
172163
auto lng=network->read(frame.header,frame.data,i->max_len);
173164
frame.size=lng;
174165
res=1;
175166
break;
176167
}
177168
}
178-
set_debug_abort("read2");
179169
if(i==rules.end()) {network->read(header,0,0); res=0; } // Ignore frame
180170
} else res=0;
181171
});

0 commit comments

Comments
 (0)