From f89e07539ab329ea82eb3934919c5220d882c211 Mon Sep 17 00:00:00 2001 From: Friedl Ulrich Date: Sat, 26 Mar 2016 21:56:16 +0100 Subject: [PATCH] timeout in waitForMode --- device.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/device.go b/device.go index b7dfa17..e228e70 100644 --- a/device.go +++ b/device.go @@ -2,7 +2,9 @@ package rfm69 import ( + "errors" "log" + "time" "github.com/davecheney/gpio" "github.com/fulr/spidev" @@ -175,16 +177,24 @@ func (r *Device) setup() error { } func (r *Device) waitForMode() error { - for { - reg, err := r.readReg(REG_IRQFLAGS1) - if err != nil { - return err - } - if reg&RF_IRQFLAGS1_MODEREADY != 0 { - break + errChan := make(chan error) + go func() { + for { + reg, err := r.readReg(REG_IRQFLAGS1) + if err != nil { + errChan <- err + break + } + if reg&RF_IRQFLAGS1_MODEREADY != 0 { + errChan <- nil + break + } } - } - return nil + }() + time.AfterFunc(5*time.Second, func() { + errChan <- errors.New("timeout") + }) + return <-errChan } // Encrypt sets the encryption key and enables AES encryption