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.

91 lines
1.6 KiB
Go

10 years ago
package rfm69
import (
"log"
"github.com/davecheney/gpio"
)
// Send data
func (r *Device) Send(d *Data) {
r.tx <- d
10 years ago
}
func (r *Device) loop() {
10 years ago
irq := make(chan int)
r.gpio.BeginWatch(gpio.EdgeRising, func() {
irq <- 1
})
defer r.gpio.EndWatch()
err := r.SetMode(RF_OPMODE_RECEIVER)
if err != nil {
log.Fatal(err)
10 years ago
}
defer r.SetMode(RF_OPMODE_STANDBY)
for {
select {
case dataToTransmit := <-r.tx:
10 years ago
// TODO: can send?
r.readWriteReg(REG_PACKETCONFIG2, 0xFB, RF_PACKET2_RXRESTART) // avoid RX deadlocks
err = r.SetModeAndWait(RF_OPMODE_STANDBY)
if err != nil {
log.Fatal(err)
}
err = r.writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00)
if err != nil {
log.Fatal(err)
}
10 years ago
err = r.writeFifo(dataToTransmit)
10 years ago
if err != nil {
log.Fatal(err)
}
err = r.SetMode(RF_OPMODE_TRANSMITTER)
if err != nil {
log.Fatal(err)
}
10 years ago
10 years ago
<-irq
10 years ago
10 years ago
err = r.SetModeAndWait(RF_OPMODE_STANDBY)
if err != nil {
log.Fatal(err)
}
err = r.writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01)
if err != nil {
log.Fatal(err)
}
err = r.SetMode(RF_OPMODE_RECEIVER)
if err != nil {
log.Fatal(err)
}
case <-irq:
if r.mode != RF_OPMODE_RECEIVER {
continue
}
flags, err := r.readReg(REG_IRQFLAGS2)
if err != nil {
log.Fatal(err)
}
10 years ago
if flags&RF_IRQFLAGS2_PAYLOADREADY == 0 {
continue
}
data, err := r.readFifo()
if err != nil {
log.Fatal(err)
10 years ago
}
if r.OnReceive != nil {
go r.OnReceive(&data)
}
10 years ago
err = r.SetMode(RF_OPMODE_RECEIVER)
if err != nil {
log.Fatal(err)
}
case <-r.quit:
r.quit <- true
10 years ago
return
}
10 years ago
}
10 years ago
}