diff --git a/RFM12B.cpp b/RFM12B.cpp index 9aca8e4..81810ca 100644 --- a/RFM12B.cpp +++ b/RFM12B.cpp @@ -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) { @@ -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; } @@ -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; @@ -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 } } @@ -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); @@ -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 diff --git a/RFM12B.h b/RFM12B.h index 424a45e..4a8c7f1 100644 --- a/RFM12B.h +++ b/RFM12B.h @@ -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); @@ -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); diff --git a/keywords.txt b/keywords.txt index a430a0f..d27e91d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -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 @@ -26,6 +27,7 @@ LowBattery KEYWORD2 CryptFunction KEYWORD2 Encrypt KEYWORD2 CRCPass KEYWORD2 +ReceiveCallBack KEYWORD2 ####################################### # Instances (KEYWORD2)