-
Notifications
You must be signed in to change notification settings - Fork 0
/
RF24Services.cpp
80 lines (72 loc) · 1.89 KB
/
RF24Services.cpp
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
/*
RF24Services.cpp - Library for handling callbacs with RF24 library.
Created by Filip Richter, 30.07.2016.
*/
#include "Arduino.h"
#include "RF24Services.h"
RF24Services::RF24Services(RF24 &rf): _rf(rf)
{
_irq_pin = NO_IRQ;
}
RF24Services::RF24Services(RF24 &rf, int8_t irq_pin): _rf(rf)
{
_irq_pin = irq_pin;
pinMode(_irq_pin, INPUT_PULLUP);
}
void RF24Services::addService(void (*callback)(uint8_t *data, uint8_t len), uint8_t sid)
{
service_ids[service_cnt] = sid;
service_ptrs[service_cnt] = callback;
service_cnt++;
}
void RF24Services::doWork()
{
unsigned long t = millis();
if(((t - rxTime) > lostTime) && (connectionState == CONNECTED)){
connectionState = LOST;
if(lostCallback) lostCallback();
}
if(!irqStatus()) return;
bool tx,fail,rx;
uint8_t pipe;
_rf.whatHappened(tx,fail,rx);
if(rx){
rxTime=t;
if(activeCallback) activeCallback();
while ( _rf.available(&pipe) )
{
uint8_t len = _rf.getDynamicPayloadSize();
if(!len) continue;
_rf.read(buffer, len);
handlePacket(buffer, len);
}
if(inactiveCallback) inactiveCallback();
if(connectionState==LOST){
connectionState = CONNECTED;
if(connectCallback) connectCallback();
}
}
}
void RF24Services::handlePacket(uint8_t buffer[], uint8_t len){
for(uint8_t i=0; i<service_cnt; i++){
if(service_ids[i]==buffer[0]){
service_ptrs[i](buffer, len);
return;
}
}
if(defaultService!=0) defaultService(buffer, len);
}
inline bool RF24Services::irqStatus(){
if(_irq_pin==NO_IRQ) return true;
return !digitalRead(_irq_pin);
}
bool RF24Services::send(uint8_t pipe, void *data, uint8_t len){
if(activeCallback) activeCallback();
boolean status = false;
_rf.stopListening();
_rf.openWritingPipe(pipe+baseAddress);
status = _rf.write(data, len);
_rf.startListening();
if(inactiveCallback) inactiveCallback();
return status;
}