Better error handling

master
Friedl Ulrich 10 years ago
parent 767107c897
commit ec805e2772

@ -35,13 +35,16 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
quit := rfm.Loop() rxChan, quit := rfm.Loop()
sigint := make(chan os.Signal, 1) sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt, os.Kill) signal.Notify(sigint, os.Interrupt, os.Kill)
for { for {
select { select {
case data := <-rxChan:
log.Print("main got data")
log.Print(data)
case <-sigint: case <-sigint:
quit <- 1 quit <- 1
<-quit <-quit

@ -216,14 +216,9 @@ func (r *Device) SetMode(newMode byte) error {
// we are using packet mode, so this check is not really needed // 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 // 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 { if r.mode == RF_OPMODE_SLEEP {
for { err = r.waitForMode()
data, err := r.readReg(REG_IRQFLAGS1) if err != nil {
if err != nil { return err
return err
}
if data&RF_IRQFLAGS1_MODEREADY != 0 {
break
}
} }
} }

@ -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
} }

Loading…
Cancel
Save