10
10
*/
11
11
12
12
#include " MySensor.h"
13
- #include " utility/LowPower.h"
14
- #include " utility/RF24.h"
15
- #include " utility/RF24_config.h"
16
13
17
14
#define DISTANCE_INVALID (0xFF )
18
15
@@ -36,12 +33,12 @@ static inline bool isValidDistance( const uint8_t distance ) {
36
33
return distance != DISTANCE_INVALID;
37
34
}
38
35
39
-
40
- MySensor::MySensor ( uint8_t _cepin, uint8_t _cspin) : RF24(_cepin, _cspin) {
41
- }
36
+ MySensor::MySensor () {
37
+ driver = (MyDriver*) new MyDriverClass ();
38
+ }
42
39
43
40
44
- void MySensor::begin (void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId, rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate ) {
41
+ void MySensor::begin (void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId) {
45
42
Serial.begin (BAUD_RATE);
46
43
repeaterMode = _repeaterMode;
47
44
msgCallback = _msgCallback;
@@ -52,7 +49,7 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
52
49
if (repeaterMode) {
53
50
setupRepeaterMode ();
54
51
}
55
- setupRadio (paLevel, channel, dataRate );
52
+ setupRadio ();
56
53
57
54
// Read settings from EEPROM
58
55
eeprom_read_block ((void *)&nc, (void *)EEPROM_NODE_ID_ADDRESS, sizeof (NodeConfig));
@@ -92,8 +89,7 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
92
89
}
93
90
94
91
// Open reading pipe for messages directed to this node (set write pipe to same)
95
- RF24::openReadingPipe (WRITE_PIPE, TO_ADDR (nc.nodeId ));
96
- RF24::openReadingPipe (CURRENT_NODE_PIPE, TO_ADDR (nc.nodeId ));
92
+ driver->setAddress (nc.nodeId );
97
93
98
94
// Send presentation for this radio node (attach
99
95
present (NODE_SENSOR_ID, repeaterMode? S_ARDUINO_REPEATER_NODE : S_ARDUINO_NODE);
@@ -107,26 +103,9 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
107
103
waitForReply ();
108
104
}
109
105
110
- void MySensor::setupRadio (rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) {
111
- // Start up the radio library
112
- RF24::begin ();
113
-
114
- if (!RF24::isPVariant ()) {
115
- debug (PSTR (" check wires\n " ));
116
- while (1 );
117
- }
118
- RF24::setAutoAck (1 );
119
- RF24::setAutoAck (BROADCAST_PIPE,false ); // Turn off auto ack for broadcast
120
- RF24::setChannel (channel);
121
- RF24::setPALevel (paLevel);
122
- RF24::setDataRate (dataRate);
123
- RF24::setRetries (5 ,15 );
124
- RF24::setCRCLength (RF24_CRC_16);
125
- RF24::enableDynamicPayloads ();
126
- RF24::enableDynamicAck (); // Required to disable ack-sending for broadcast messages
127
-
128
- // All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages)
129
- RF24::openReadingPipe (BROADCAST_PIPE, TO_ADDR (BROADCAST_ADDRESS));
106
+ void MySensor::setupRadio () {
107
+ failedTransmissions = 0 ;
108
+ driver->init ();
130
109
}
131
110
132
111
void MySensor::setupRepeaterMode (){
@@ -145,7 +124,7 @@ ControllerConfig MySensor::getConfig() {
145
124
146
125
void MySensor::requestNodeId () {
147
126
debug (PSTR (" req node id\n " ));
148
- RF24::openReadingPipe (CURRENT_NODE_PIPE, TO_ADDR ( nc.nodeId ) );
127
+ driver-> setAddress ( nc.nodeId );
149
128
sendRoute (build (msg, nc.nodeId , GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_ID_REQUEST, false ).set (" " ));
150
129
waitForReply ();
151
130
}
@@ -223,13 +202,7 @@ boolean MySensor::sendWrite(uint8_t next, MyMessage &message, const bool allowFi
223
202
uint8_t length = mGetLength (message);
224
203
message.last = nc.nodeId ;
225
204
mSetVersion (message, PROTOCOL_VERSION);
226
- // Make sure radio has powered up
227
- RF24::powerUp ();
228
- RF24::stopListening ();
229
- RF24::openWritingPipe (TO_ADDR (next));
230
- // Send message. Disable auto-ack for broadcasts.
231
- ok = RF24::write (&message, min (MAX_MESSAGE_LENGTH, HEADER_SIZE + length), broadcast);
232
- RF24::startListening ();
205
+ bool ok = driver->send (next, &message, min (MAX_MESSAGE_LENGTH, HEADER_SIZE + length));
233
206
234
207
debug (PSTR (" send: %d-%d-%d-%d s=%d,c=%d,t=%d,pt=%d,l=%d,st=%s:%s\n " ),
235
208
message.sender ,message.last , next, message.destination , message.sensor , mGetCommand (message), message.type ,
@@ -290,14 +263,11 @@ void MySensor::requestTime(void (* _timeCallback)(unsigned long)) {
290
263
291
264
292
265
boolean MySensor::process () {
293
- uint8_t pipe;
294
- boolean available = RF24::available (&pipe);
295
-
296
- if (!available || pipe>6 )
266
+ uint8_t to = 0 ;
267
+ if (!driver->available (&to))
297
268
return false ;
298
269
299
- uint8_t len = RF24::getDynamicPayloadSize ();
300
- RF24::read (&msg, len);
270
+ uint8_t len = driver->receive ((uint8_t *)&msg);
301
271
302
272
// Add string termination, good if we later would want to print it.
303
273
msg.data [mGetLength (msg)] = ' \0 ' ;
@@ -380,7 +350,7 @@ boolean MySensor::process() {
380
350
debug (PSTR (" full\n " ));
381
351
while (1 ); // Wait here. Nothing else we can do...
382
352
} else {
383
- RF24::openReadingPipe (CURRENT_NODE_PIPE, TO_ADDR ( nc.nodeId ) );
353
+ driver-> setAddress ( nc.nodeId );
384
354
eeprom_write_byte ((uint8_t *)EEPROM_NODE_ID_ADDRESS, nc.nodeId );
385
355
}
386
356
debug (PSTR (" id=%d\n " ), nc.nodeId );
@@ -412,7 +382,7 @@ boolean MySensor::process() {
412
382
}
413
383
// Return true if message was addressed for this node...
414
384
return true ;
415
- } else if (repeaterMode && pipe == CURRENT_NODE_PIPE ) {
385
+ } else if (repeaterMode && to == nc. nodeId ) {
416
386
// We should try to relay this message to another node
417
387
418
388
uint8_t route = getChildRoute (msg.destination );
@@ -511,7 +481,7 @@ void MySensor::internalSleep(unsigned long ms) {
511
481
void MySensor::sleep (unsigned long ms) {
512
482
// Let serial prints finish (debug, log etc)
513
483
Serial.flush ();
514
- RF24:: powerDown ();
484
+ driver-> powerDown ();
515
485
pinIntTrigger = 0 ;
516
486
internalSleep (ms);
517
487
}
@@ -520,7 +490,7 @@ bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
520
490
// Let serial prints finish (debug, log etc)
521
491
bool pinTriggeredWakeup = true ;
522
492
Serial.flush ();
523
- RF24:: powerDown ();
493
+ driver-> powerDown ();
524
494
attachInterrupt (interrupt, wakeUp, mode);
525
495
if (ms>0 ) {
526
496
pinIntTrigger = 0 ;
@@ -539,7 +509,7 @@ bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
539
509
int8_t MySensor::sleep (uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms) {
540
510
int8_t retVal = 1 ;
541
511
Serial.flush (); // Let serial prints finish (debug, log etc)
542
- RF24:: powerDown ();
512
+ driver-> powerDown ();
543
513
attachInterrupt (interrupt1, wakeUp, mode1);
544
514
attachInterrupt (interrupt2, wakeUp2, mode2);
545
515
if (ms>0 ) {
0 commit comments