diff --git a/src/fdrs_functions.h b/src/fdrs_functions.h index 3d45ae9..93aabd0 100644 --- a/src/fdrs_functions.h +++ b/src/fdrs_functions.h @@ -21,6 +21,7 @@ #include #endif #ifdef USE_LORA +#include #include #endif #ifdef USE_LED @@ -852,7 +853,7 @@ void loopFDRS(){ while (UART_IF.available() || Serial.available()) { getSerial(); } - if (receivedFlag) getLoRa(); +handleLoRa(); #ifdef USE_WIFI if (!client.connected()) { reconnect(1, true); diff --git a/src/fdrs_lora.h b/src/fdrs_lora.h index e66d6f2..b7b91c4 100644 --- a/src/fdrs_lora.h +++ b/src/fdrs_lora.h @@ -1,6 +1,21 @@ RADIOLIB_MODULE radio = new Module(LORA_SS, LORA_DIO0, LORA_RST, LORA_DIO1); -volatile bool receivedFlag = false; -volatile bool enableInterrupt = true; + +bool transmitFlag = false;// flag to indicate transmission or reception state +volatile bool enableInterrupt = true;// disable interrupt when it's not needed +volatile bool operationDone = false;// flag to indicate that a packet was sent or received +#if defined(ESP8266) || defined(ESP32) + ICACHE_RAM_ATTR +#endif +void setFlag(void) { + // check if the interrupt is enabled + if(!enableInterrupt) { + return; + } + + // we sent or received packet, set the flag + operationDone = true; +} + #ifdef USE_LORA void transmitLoRa(uint16_t* destMac, DataReading * packet, uint8_t len) { @@ -44,9 +59,17 @@ void transmitLoRa(uint16_t* destMac, SystemPacket * packet, uint8_t len) { pkt[(len * sizeof(SystemPacket) + 5)] = (calcCRC & 0x00FF); DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC, HEX) + " to LoRa MAC 0x" + String(*destMac, HEX)); //printLoraPacket(pkt,sizeof(pkt)); - LoRa.beginPacket(); - LoRa.write((uint8_t*)&pkt, sizeof(pkt)); - LoRa.endPacket(); + + int state = radio.startTransmit(pkt,sizeof(pkt)); + if (state == RADIOLIB_ERR_NONE) { + DBG(" begun successfully!"); + } else { + DBG(" failed, code " + String(state)); + while (true); + } + // LoRa.beginPacket(); + // LoRa.write((uint8_t*)&pkt, sizeof(pkt)); + // LoRa.endPacket(); } #endif //USE_LORA @@ -98,28 +121,10 @@ void begin_lora() { // #endif // USE_LORA } -#if defined(ESP8266) || defined(ESP32) - ICACHE_RAM_ATTR -#endif -void setFlag(void) { - // check if the interrupt is enabled - if(!enableInterrupt) { - return; - } - // we got a packet, set the flag - receivedFlag = true; -} -void parsePacket(){ - - RADIOLIB_MODULE.getPacketLength(); - int state = radio.readData(str); -} crcResult getLoRa() { #ifdef USE_LORA - enableInterrupt = false; - receivedFlag = false; int packetSize = radio.getPacketLength(); if((((packetSize - 6) % sizeof(DataReading) == 0) || ((packetSize - 6) % sizeof(SystemPacket) == 0)) && packetSize > 0) { // packet size should be 6 bytes plus multiple of size of DataReading @@ -129,7 +134,7 @@ crcResult getLoRa() { uint16_t sourceMAC = 0x0000; uint16_t destMAC = 0x0000; - radio.readBytes((uint8_t *)&packet, packetSize); + radio.readData((uint8_t *)&packet, packetSize); destMAC = (packet[0] << 8) | packet[1]; sourceMAC = (packet[2] << 8) | packet[3]; @@ -334,7 +339,24 @@ void releaseLoRa(uint8_t interface) { } #endif } +void handleLoRa(){ + if(operationDone) { + enableInterrupt = false; + operationDone = false; + if(transmitFlag) { + // the previous operation was transmission, + radio.startReceive(); // return to listen mode + enableInterrupt = true; + transmitFlag = false; + + } else { + // the previous operation was reception + getLoRa(); + enableInterrupt = true; + } + } + } void releaseSerial() { DBG("Releasing Serial."); DynamicJsonDocument doc(24576);