private void Dio0Pin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args) { if (args.Edge != GpioPinEdge.RisingEdge) { return; } LoraRegisterIrqFlags.FlagsEnum irqFlags = RegisterManager.Read <LoraRegisterIrqFlags>().Flags; if (irqFlags.HasFlag(LoraRegisterIrqFlags.FlagsEnum.TxDone)) { TransmittedOkCount++; _transmissionCompletedEventSlim.Set(); } if (irqFlags.HasFlag(LoraRegisterIrqFlags.FlagsEnum.RxDone)) { ReceivedMessage message = GetReceivedMessage(); OnMessageReceived?.Invoke(this, new ReceivedMessageEventArgs(message)); } // Clear all IRQ flags RegisterManager.Write(new LoraRegisterIrqFlags(0xFF)); }
private ReceivedMessage GetReceivedMessage() { DateTime timestamp = DateTime.UtcNow; LoraRegisterIrqFlags.FlagsEnum irqFlags = RegisterManager.Read <LoraRegisterIrqFlags>().Flags; bool timeout; if (irqFlags.HasFlag(LoraRegisterIrqFlags.FlagsEnum.RxTimeout)) { timeout = true; ReceivedTimeoutCount++; } else { timeout = false; } bool crcOk; if (irqFlags.HasFlag(LoraRegisterIrqFlags.FlagsEnum.PayloadCrcError)) { crcOk = false; ReceivedBadCrcCount++; } else { crcOk = true; } if (!timeout && crcOk) { ReceivedOkCount++; } lock (_registerFifoLock) { // Get the FIFO-address of the last received packet byte currentFifoAddr = RegisterManager.Read <LoraRegisterFifoRxCurrentAddr>().Addr; // Go to that address RegisterManager.Write(new LoraRegisterFifoAddrPtr(currentFifoAddr)); // Get the number of received bytes byte receivedCount = RegisterManager.Read <LoraRegisterRxNbBytes>().FifoRxBytesNb; // Prepare a buffer to copy to from the FIFO queue var buffer = new byte[receivedCount]; // Copy from the FIFO queue for (int i = 0; i < receivedCount; i++) { buffer[i] = RegisterManager.Read <CommonRegisterFifo>().Value; } // Get the RSSI correction value int rssiCorrection = GetRssiCorrection(); // Get the adjusted RSSI value int rssi = RegisterManager.Read <LoraRegisterRssiValue>().Rssi + rssiCorrection; // Get the adjusted RSSI value of the packet int pktRssi = RegisterManager.Read <LoraRegisterPktRssiValue>().PacketRssi + rssiCorrection; // Get the packet signal-to-noise ratio float pktSnr = (float)Math.Round(RegisterManager.Read <LoraRegisterPktSnrValue>().PacketSnr / 4d, 1); #if DEBUG Debug.WriteLine($"Packet RSSI: {pktRssi}, RSSI: {rssi}, SNR: {pktSnr}, Length: {buffer.Length}"); #endif return(new ReceivedMessage(buffer, rssi, pktRssi, pktSnr, crcOk, timeout, timestamp)); } }