diff --git a/rfm69d/rfm69d.go b/rfm69d/rfm69d.go index eaa9852..0741762 100644 --- a/rfm69d/rfm69d.go +++ b/rfm69d/rfm69d.go @@ -35,13 +35,16 @@ func main() { log.Fatal(err) } - quit := rfm.Loop() + rxChan, quit := rfm.Loop() sigint := make(chan os.Signal, 1) signal.Notify(sigint, os.Interrupt, os.Kill) for { select { + case data := <-rxChan: + log.Print("main got data") + log.Print(data) case <-sigint: quit <- 1 <-quit diff --git a/rfm69device.go b/rfm69device.go index fe20202..e5ef3ce 100644 --- a/rfm69device.go +++ b/rfm69device.go @@ -216,14 +216,9 @@ func (r *Device) SetMode(newMode byte) error { // we are using packet mode, so this check is not really needed // but waiting for mode ready is necessary when going from sleep because the FIFO may not be immediately available from previous mode if r.mode == RF_OPMODE_SLEEP { - for { - data, err := r.readReg(REG_IRQFLAGS1) - if err != nil { - return err - } - if data&RF_IRQFLAGS1_MODEREADY != 0 { - break - } + err = r.waitForMode() + if err != nil { + return err } } diff --git a/rfm69handler.go b/rfm69handler.go index 3a7c9bb..4ff558e 100644 --- a/rfm69handler.go +++ b/rfm69handler.go @@ -7,9 +7,11 @@ import ( ) // Loop is the main receive and transmit handling loop -func (r *Device) Loop() chan int { +func (r *Device) Loop() (chan Data, chan int) { quit := make(chan int) - c := make(chan Data, 5) + txChan := make(chan Data, 5) + rxChan := make(chan Data, 5) + go func() { irq := make(chan int) @@ -27,33 +29,70 @@ func (r *Device) Loop() chan int { for { select { - case dataToTransmit := <-c: + case dataToTransmit := <-txChan: // can send? - r.SetMode(RF_OPMODE_STANDBY) - r.waitForMode() - r.writeFifo(&dataToTransmit) + err = r.SetMode(RF_OPMODE_STANDBY) + if err != nil { + log.Fatal(err) + } + + err = r.waitForMode() + if err != nil { + log.Fatal(err) + } + + err = r.writeFifo(&dataToTransmit) + if err != nil { + log.Fatal(err) + } + log.Print("transmit") log.Print(dataToTransmit) - r.SetMode(RF_OPMODE_TRANSMITTER) - r.writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00) + + err = r.SetMode(RF_OPMODE_TRANSMITTER) + if err != nil { + log.Fatal(err) + } + + err = r.writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00) + if err != nil { + log.Fatal(err) + } + <-irq log.Print("transmit done") - r.SetMode(RF_OPMODE_RECEIVER) - r.writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01) + + err = r.SetMode(RF_OPMODE_RECEIVER) + if err != nil { + log.Fatal(err) + } + + err = r.writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01) + if err != nil { + log.Fatal(err) + } case <-irq: + err = r.SetMode(RF_OPMODE_STANDBY) + if err != nil { + log.Fatal(err) + } + data, err := r.readFifo() if err != nil { log.Print(err) return } + + log.Print("receive") log.Print(data) + if data.ToAddress != 255 && data.RequestAck { resp := Data{ FromAddress: r.address, ToAddress: data.FromAddress, SendAck: true, } - c <- resp + txChan <- resp } case <-quit: quit <- 1 @@ -61,5 +100,6 @@ func (r *Device) Loop() chan int { } } }() - return quit + + return rxChan, quit }