mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 04:43:25 +00:00
sx127x: use select for timeout
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
+8
-13
@@ -434,8 +434,6 @@ func (d *Device) Rx(timeoutMs uint32) ([]uint8, error) {
|
|||||||
d.SetHeaderType(d.loraConf.HeaderType)
|
d.SetHeaderType(d.loraConf.HeaderType)
|
||||||
d.SetAgcAuto(SX127X_AGC_AUTO_ON)
|
d.SetAgcAuto(SX127X_AGC_AUTO_ON)
|
||||||
|
|
||||||
//d.SetRxTimeout(200)
|
|
||||||
|
|
||||||
// set the IRQ mapping DIO0=RxDone DIO1=RxTimeout DIO2=NOP
|
// 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)
|
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
|
// 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
|
// Go routine is a workaround to stop the Continuous RX and fire a timeout Event
|
||||||
d.SetOpMode(SX127X_OPMODE_RX)
|
d.SetOpMode(SX127X_OPMODE_RX)
|
||||||
|
|
||||||
go func() {
|
var msg lora.RadioEvent
|
||||||
time.Sleep(time.Millisecond * time.Duration(timeoutMs))
|
select {
|
||||||
d.SetOpMode(SX127X_OPMODE_STANDBY) //Go standby
|
case msg = <-radioCh:
|
||||||
radioCh <- lora.NewRadioEvent(lora.RadioEventTimeout, 0, nil)
|
if msg.EventType != lora.RadioEventRxDone {
|
||||||
}()
|
return nil, errors.New("Unexpected Radio Event while RX " + string(0x30+msg.EventType))
|
||||||
|
}
|
||||||
// Now the channel can receive either a Timeout or a RxDone:
|
case <-time.After(time.Millisecond * time.Duration(timeoutMs)):
|
||||||
msg := <-radioCh
|
d.SetOpMode(SX127X_OPMODE_STANDBY)
|
||||||
if msg.EventType == lora.RadioEventTimeout {
|
|
||||||
return nil, nil
|
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
|
// Get the received payload
|
||||||
|
|||||||
Reference in New Issue
Block a user