|
|
@ -7,9 +7,11 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Loop is the main receive and transmit handling loop
|
|
|
|
// 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)
|
|
|
|
quit := make(chan int)
|
|
|
|
c := make(chan Data, 5)
|
|
|
|
txChan := make(chan Data, 5)
|
|
|
|
|
|
|
|
rxChan := make(chan Data, 5)
|
|
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
irq := make(chan int)
|
|
|
|
irq := make(chan int)
|
|
|
|
|
|
|
|
|
|
|
@ -27,33 +29,70 @@ func (r *Device) Loop() chan int {
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case dataToTransmit := <-c:
|
|
|
|
case dataToTransmit := <-txChan:
|
|
|
|
// can send?
|
|
|
|
// can send?
|
|
|
|
r.SetMode(RF_OPMODE_STANDBY)
|
|
|
|
err = r.SetMode(RF_OPMODE_STANDBY)
|
|
|
|
r.waitForMode()
|
|
|
|
if err != nil {
|
|
|
|
r.writeFifo(&dataToTransmit)
|
|
|
|
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("transmit")
|
|
|
|
log.Print(dataToTransmit)
|
|
|
|
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
|
|
|
|
<-irq
|
|
|
|
log.Print("transmit done")
|
|
|
|
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:
|
|
|
|
case <-irq:
|
|
|
|
|
|
|
|
err = r.SetMode(RF_OPMODE_STANDBY)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := r.readFifo()
|
|
|
|
data, err := r.readFifo()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Print("receive")
|
|
|
|
log.Print(data)
|
|
|
|
log.Print(data)
|
|
|
|
|
|
|
|
|
|
|
|
if data.ToAddress != 255 && data.RequestAck {
|
|
|
|
if data.ToAddress != 255 && data.RequestAck {
|
|
|
|
resp := Data{
|
|
|
|
resp := Data{
|
|
|
|
FromAddress: r.address,
|
|
|
|
FromAddress: r.address,
|
|
|
|
ToAddress: data.FromAddress,
|
|
|
|
ToAddress: data.FromAddress,
|
|
|
|
SendAck: true,
|
|
|
|
SendAck: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c <- resp
|
|
|
|
txChan <- resp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-quit:
|
|
|
|
case <-quit:
|
|
|
|
quit <- 1
|
|
|
|
quit <- 1
|
|
|
@ -61,5 +100,6 @@ func (r *Device) Loop() chan int {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
return quit
|
|
|
|
|
|
|
|
|
|
|
|
return rxChan, quit
|
|
|
|
}
|
|
|
|
}
|
|
|
|