Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions RFM12B.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ void RFM12B::SPIInit() {
#endif
pinMode(RFM_IRQ, INPUT);
digitalWrite(RFM_IRQ, 1); // pull-up

receivedata = NULL; // receivedata callback function set to NULL
}

uint8_t RFM12B::Byte(uint8_t out) {
Expand Down Expand Up @@ -225,7 +227,8 @@ void RFM12B::InterruptHandler() {
case TXSYN2: out = networkID; rxstate = -(3 + rf12_len); break;
case TXCRC1: out = rf12_crc; break;
case TXCRC2: out = rf12_crc >> 8; break;
case TXDONE: XFER(RF_IDLE_MODE); // fall through
case TXDONE: XFER(RF_IDLE_MODE); out = 0xAA; break; // fall through
case TXIDLE: ReceiveStart(); return; break; // Restart Receive hardware when done
default: out = 0xAA;
}

Expand Down Expand Up @@ -263,6 +266,12 @@ void RFM12B::ReceiveStart() {
XFER(RF_RECEIVER_ON);
}

void RFM12B::ReceiveCallBack(void (*receivefunction)(void)) {

receivedata = receivefunction;
}


bool RFM12B::ReceiveComplete() {
if (rxstate == TXRECV && (rxfill >= rf12_len + 6 || rxfill >= RF_MAX)) {
rxstate = TXIDLE;
Expand All @@ -273,6 +282,10 @@ bool RFM12B::ReceiveComplete() {
crypter(false);
else
rf12_seq = -1;

if (receivedata !=NULL) // Callback function if defined
receivedata();

return true; // it's a broadcast packet or it's addressed to this node
}
}
Expand All @@ -295,6 +308,16 @@ bool RFM12B::CanSend() {
return false;
}

bool RFM12B::ClearToSend() {
// no need to test with interrupts disabled: state TXRECV is only reached
// outside of ISR and we don't care if rxfill jumps from 0 to 1 here
if (rxstate == TXRECV && rxfill == 0 && (Byte(0x00) & (RF_RSSI_BIT >> 8)) == 0) {
return true;
}
return false;
}


void RFM12B::SendStart(uint8_t toNodeID, bool requestACK, bool sendACK) {
rf12_hdr1 = toNodeID | (sendACK ? RF12_HDR_ACKCTLMASK : 0);
rf12_hdr2 = nodeID | (requestACK ? RF12_HDR_ACKCTLMASK : 0);
Expand All @@ -309,7 +332,8 @@ void RFM12B::SendStart(uint8_t toNodeID, const void* sendBuf, uint8_t sendLen, b
rf12_len = sendLen;
memcpy((void*) rf12_data, sendBuf, sendLen);
SendStart(toNodeID, requestACK, sendACK);
SendWait(waitMode);
if (waitMode)
SendWait(waitMode);
}

/// Should be called immediately after reception in case sender wants ACK
Expand Down
5 changes: 4 additions & 1 deletion RFM12B.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class RFM12B
static long rf12_seq; // seq number of encrypted packet (or -1)
static uint8_t cs_pin; // chip select pin
void (*crypter)(bool); // does en-/decryption (null if disabled)
void (*receivedata)(void); // Receive callback function
static uint8_t Byte(uint8_t out);
static uint16_t XFERSlow(uint16_t cmd);
static void XFER(uint16_t cmd);
Expand All @@ -191,10 +192,12 @@ class RFM12B
//Defaults: Group: 0xAA=170, transmit power: 0(max), KBPS: 38.3Kbps (air transmission baud - has to be same on all radios in same group)
void Initialize(uint8_t nodeid, uint8_t freqBand, uint8_t groupid=0xAA, uint8_t txPower=0, uint8_t airKbps=0x08, uint8_t lowVoltageThreshold=RF12_2v75);
void SetCS(uint8_t pin);
void ReceiveStart();
static void ReceiveStart();
bool ReceiveComplete();
bool CanSend();
bool ClearToSend();
uint16_t Control(uint16_t cmd);
void ReceiveCallBack(void (*receivefunction)(void));

void SendStart(uint8_t toNodeId, bool requestACK=false, bool sendACK=false);
void SendStart(uint8_t toNodeId, const void* sendBuf, uint8_t sendLen, bool requestACK=false, bool sendACK=false, uint8_t waitMode=SLEEP_MODE_STANDBY);
Expand Down
4 changes: 3 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
# Methods and Functions (KEYWORD2)
#######################################
Initialize KEYWORD2
SetCS KEYWORD2
SetCS KEYWORD2
Control KEYWORD2
ReceiveStart KEYWORD2
ReceiveComplete KEYWORD2
CanSend KEYWORD2
ClearToSend KEYWORD2
SendStart KEYWORD2
SendACK KEYWORD2
SendWait KEYWORD2
Expand All @@ -26,6 +27,7 @@ LowBattery KEYWORD2
CryptFunction KEYWORD2
Encrypt KEYWORD2
CRCPass KEYWORD2
ReceiveCallBack KEYWORD2

#######################################
# Instances (KEYWORD2)
Expand Down