Refactoring

master
Friedl Ulrich 10 years ago
parent 4a9335b6d0
commit 00c809a354

@ -10,7 +10,6 @@ import (
const ( const (
spiPath = "/dev/spidev0.0" spiPath = "/dev/spidev0.0"
irqPin = gpio.GPIO25
) )
// Device RFM69 Device // Device RFM69 Device
@ -42,7 +41,7 @@ type Data struct {
// NewDevice creates a new device // NewDevice creates a new device
func NewDevice(nodeID, networkID byte, isRfm69HW bool) (*Device, error) { func NewDevice(nodeID, networkID byte, isRfm69HW bool) (*Device, error) {
pin, err := gpio.OpenPin(irqPin, gpio.ModeInput) pin, err := getPin()
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -7,14 +7,14 @@ import (
) )
// Loop is the main receive and transmit handling loop // Loop is the main receive and transmit handling loop
func (r *Device) Loop() (chan Data, chan int) { func (r *Device) Loop() (chan *Data, chan bool) {
quit := make(chan int) quit := make(chan bool)
ch := make(chan Data, 5) ch := make(chan *Data, 5)
go r.loopInternal(ch, quit) go r.loopInternal(ch, quit)
return ch, quit return ch, quit
} }
func (r *Device) loopInternal(ch chan Data, quit chan int) { func (r *Device) loopInternal(ch chan *Data, quit chan bool) {
irq := make(chan int) irq := make(chan int)
r.gpio.BeginWatch(gpio.EdgeRising, func() { r.gpio.BeginWatch(gpio.EdgeRising, func() {
irq <- 1 irq <- 1
@ -41,7 +41,7 @@ func (r *Device) loopInternal(ch chan Data, quit chan int) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = r.writeFifo(&dataToTransmit) err = r.writeFifo(dataToTransmit)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -49,7 +49,9 @@ func (r *Device) loopInternal(ch chan Data, quit chan int) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
<-irq <-irq
err = r.SetModeAndWait(RF_OPMODE_STANDBY) err = r.SetModeAndWait(RF_OPMODE_STANDBY)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -78,13 +80,13 @@ func (r *Device) loopInternal(ch chan Data, quit chan int) {
log.Print(err) log.Print(err)
return return
} }
ch <- data ch <- &data
err = r.SetMode(RF_OPMODE_RECEIVER) err = r.SetMode(RF_OPMODE_RECEIVER)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
case <-quit: case <-quit:
quit <- 1 quit <- true
return return
} }
} }

@ -0,0 +1,13 @@
// +build !linux
package rfm69
import (
"errors"
"github.com/davecheney/gpio"
)
func getPin() (gpio.Pin, error) {
return nil, errors.New("not implemented")
}

@ -0,0 +1,11 @@
package rfm69
import "github.com/davecheney/gpio"
const (
irqPin = gpio.GPIO25
)
func getPin() (gpio.Pin, error) {
return gpio.OpenPin(irqPin, gpio.ModeInput)
}
Loading…
Cancel
Save