From e613ab1eb31170d7dc4835cc4221ce2114a6b4e4 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Tue, 31 Jan 2023 00:38:32 +0100 Subject: [PATCH] sx127x: use select for timeout Signed-off-by: deadprogram --- sx127x/sx127x.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/sx127x/sx127x.go b/sx127x/sx127x.go index 9fe6897..9055dc5 100644 --- a/sx127x/sx127x.go +++ b/sx127x/sx127x.go @@ -434,8 +434,6 @@ func (d *Device) Rx(timeoutMs uint32) ([]uint8, error) { d.SetHeaderType(d.loraConf.HeaderType) d.SetAgcAuto(SX127X_AGC_AUTO_ON) - //d.SetRxTimeout(200) - // set the IRQ mapping DIO0=RxDone DIO1=RxTimeout DIO2=NOP d.WriteRegister(SX127X_REG_DIO_MAPPING_1, SX127X_MAP_DIO0_LORA_RXDONE|SX127X_MAP_DIO1_LORA_RXTOUT|SX127X_MAP_DIO2_LORA_NOP) // Clear all radio IRQ Flags @@ -450,18 +448,15 @@ func (d *Device) Rx(timeoutMs uint32) ([]uint8, error) { // Go routine is a workaround to stop the Continuous RX and fire a timeout Event d.SetOpMode(SX127X_OPMODE_RX) - go func() { - time.Sleep(time.Millisecond * time.Duration(timeoutMs)) - d.SetOpMode(SX127X_OPMODE_STANDBY) //Go standby - radioCh <- lora.NewRadioEvent(lora.RadioEventTimeout, 0, nil) - }() - - // Now the channel can receive either a Timeout or a RxDone: - msg := <-radioCh - if msg.EventType == lora.RadioEventTimeout { + var msg lora.RadioEvent + select { + case msg = <-radioCh: + if msg.EventType != lora.RadioEventRxDone { + return nil, errors.New("Unexpected Radio Event while RX " + string(0x30+msg.EventType)) + } + case <-time.After(time.Millisecond * time.Duration(timeoutMs)): + d.SetOpMode(SX127X_OPMODE_STANDBY) return nil, nil - } else if msg.EventType != lora.RadioEventRxDone { - return nil, errors.New("Unexpected Radio Event while RX " + string(0x30+msg.EventType)) } // Get the received payload