-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnrf24Node.cc
169 lines (134 loc) · 4.76 KB
/
nrf24Node.cc
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <nan.h>
#include <v8.h>
#include <RF24.h>
#include <RF24Network.h>
#include <iostream>
#include <ctime>
#include <stdio.h>
#include <time.h>
#include <string>
using namespace Nan;
using namespace v8;
RF24 radio(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ);
RF24Network network(radio);
Nan::Callback *cbPeriodic;
uv_async_t* async;
struct payload_t { // Structure of our payload
char msg[24];
};
struct payload_pi {
uint16_t fromNode;
char msg[24];
};
//--------------------------------------------------------------------------
//Below functions are just replica of RF24Network functions.
//No need to use these functions in you app.
NAN_METHOD(BeginRadio) {
radio.begin();
}
NAN_METHOD(BeginNetwork){
uint16_t channel = info[0]->Uint32Value();
uint16_t thisNode = info[0]->Uint32Value();
network.begin(channel,thisNode);
}
NAN_METHOD(Update) {
network.update();
}
NAN_METHOD(Available) {
v8::Local<v8::Boolean> status = Nan::New(network.available());
info.GetReturnValue().Set(status);
}
NAN_METHOD(Read) {
payload_t payload;
RF24NetworkHeader header;
network.read(header,&payload,sizeof(payload));
info.GetReturnValue().Set(Nan::New(payload.msg).ToLocalChecked());
}
//--------------------------------------------------------------------------------
NAN_METHOD(Begin){
if (info.Length() < 2)
return Nan::ThrowTypeError("Should pass Channel and Node id");
uint16_t channel = info[0]->Uint32Value();
uint16_t thisNode = info[1]->Uint32Value();
radio.begin();
delay(5);
network.begin(channel, thisNode);
}
NAN_METHOD(Write){
if (info.Length() < 2)
return Nan::ThrowTypeError("Should pass Receiver Node Id and Message");
uint16_t otherNode = info[0]->Uint32Value();
v8::String::Utf8Value message(info[1]->ToString());
std::string msg = std::string(*message);
payload_t payload;
strncpy(payload.msg, msg.c_str(),24);
RF24NetworkHeader header(otherNode);
bool ok = network.write(header,&payload, sizeof(payload));
info.GetReturnValue().Set(ok);
}
void keepListen(void *arg) {
while(1)
{
network.update();
while (network.available()) { // Is there anything ready for us?
RF24NetworkHeader header;
payload_t payload;
network.read(header,&payload,sizeof(payload));
payload_pi localPayload;
localPayload.fromNode = header.from_node;
strncpy(localPayload.msg, payload.msg, 24);
async->data = (void *) &localPayload;
uv_async_send(async);
}
delay(2000);
}
}
void doCallback(uv_async_t *handle){
payload_pi* p = (struct payload_pi*)handle->data;
v8::Handle<v8::Value> argv[2] = {
Nan::New(p->fromNode),
Nan::New(p->msg).ToLocalChecked()
};
cbPeriodic->Call(2, argv);
}
NAN_METHOD(ReadAsync){
if (info.Length() <= 0)
return Nan::ThrowTypeError("Should pass a callback function");
if (info.Length() > 0 && !info[0]->IsFunction())
return Nan::ThrowTypeError("Provided callback must be a function");
cbPeriodic = new Nan::Callback(info[0].As<Function>());
async = (uv_async_t*)malloc(sizeof(uv_async_t));
uv_async_init(uv_default_loop(), async, doCallback);
uv_thread_t id;
uv_thread_create(&id, keepListen, NULL);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}
NAN_METHOD(PrintDetails) {
radio.printDetails();
}
NAN_METHOD(Close){
uv_close((uv_handle_t*) &async, NULL);
}
NAN_MODULE_INIT(Init){
Nan::Set(target, New<String>("beginRadio").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(BeginRadio)).ToLocalChecked());
Nan::Set(target, New<String>("beginNetwork").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(BeginNetwork)).ToLocalChecked());
Nan::Set(target, New<String>("update").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(Update)).ToLocalChecked());
Nan::Set(target, New<String>("printDetails").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(PrintDetails)).ToLocalChecked());
Nan::Set(target, New<String>("available").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(Available)).ToLocalChecked());
Nan::Set(target, New<String>("read").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(Read)).ToLocalChecked());
Nan::Set(target, New<String>("readAsync").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(ReadAsync)).ToLocalChecked());
Nan::Set(target, New<String>("write").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(Write)).ToLocalChecked());
Nan::Set(target, New<String>("close").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(Close)).ToLocalChecked());
Nan::Set(target, New<String>("begin").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(Begin)).ToLocalChecked());
}
NODE_MODULE(nrf24Node, Init)