You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.2 KiB
Go

10 years ago
package rfm69
import (
"log"
"github.com/davecheney/gpio"
)
10 years ago
// Loop is the main receive and transmit handling loop
func (r *Device) Loop() chan int {
quit := make(chan int)
10 years ago
c := make(chan Data, 5)
go func() {
irq := make(chan int)
10 years ago
r.gpio.BeginWatch(gpio.EdgeRising, func() {
10 years ago
log.Print("irq")
irq <- 1
})
err := r.SetMode(RF_OPMODE_RECEIVER)
if err != nil {
log.Print(err)
return
}
defer r.SetMode(RF_OPMODE_STANDBY)
for {
select {
case dataToTransmit := <-c:
// can send?
r.SetMode(RF_OPMODE_STANDBY)
10 years ago
r.waitForMode()
r.writeFifo(&dataToTransmit)
10 years ago
log.Print("transmit")
10 years ago
log.Print(dataToTransmit)
r.SetMode(RF_OPMODE_TRANSMITTER)
10 years ago
r.writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00)
<-irq
10 years ago
log.Print("transmit done")
r.SetMode(RF_OPMODE_RECEIVER)
r.writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01)
case <-irq:
data, err := r.readFifo()
if err != nil {
log.Print(err)
return
}
log.Print(data)
10 years ago
if data.ToAddress != 255 && data.RequestAck {
resp := Data{
FromAddress: r.address,
ToAddress: data.FromAddress,
SendAck: true,
}
c <- resp
}
case <-quit:
quit <- 1
return
}
}
}()
return quit
10 years ago
}