From 00c809a354f93df292f58344792abdcfa31192fd Mon Sep 17 00:00:00 2001 From: Friedl Ulrich Date: Thu, 26 Feb 2015 22:15:43 +0100 Subject: [PATCH] Refactoring --- device.go | 3 +-- handler.go | 16 +++++++++------- pin.go | 13 +++++++++++++ pin_linux.go | 11 +++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 pin.go create mode 100644 pin_linux.go diff --git a/device.go b/device.go index 8a93887..388d5af 100644 --- a/device.go +++ b/device.go @@ -10,7 +10,6 @@ import ( const ( spiPath = "/dev/spidev0.0" - irqPin = gpio.GPIO25 ) // Device RFM69 Device @@ -42,7 +41,7 @@ type Data struct { // NewDevice creates a new device func NewDevice(nodeID, networkID byte, isRfm69HW bool) (*Device, error) { - pin, err := gpio.OpenPin(irqPin, gpio.ModeInput) + pin, err := getPin() if err != nil { return nil, err } diff --git a/handler.go b/handler.go index b1f0829..69b87c4 100644 --- a/handler.go +++ b/handler.go @@ -7,14 +7,14 @@ import ( ) // Loop is the main receive and transmit handling loop -func (r *Device) Loop() (chan Data, chan int) { - quit := make(chan int) - ch := make(chan Data, 5) +func (r *Device) Loop() (chan *Data, chan bool) { + quit := make(chan bool) + ch := make(chan *Data, 5) go r.loopInternal(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) r.gpio.BeginWatch(gpio.EdgeRising, func() { irq <- 1 @@ -41,7 +41,7 @@ func (r *Device) loopInternal(ch chan Data, quit chan int) { if err != nil { log.Fatal(err) } - err = r.writeFifo(&dataToTransmit) + err = r.writeFifo(dataToTransmit) if err != nil { log.Fatal(err) } @@ -49,7 +49,9 @@ func (r *Device) loopInternal(ch chan Data, quit chan int) { if err != nil { log.Fatal(err) } + <-irq + err = r.SetModeAndWait(RF_OPMODE_STANDBY) if err != nil { log.Fatal(err) @@ -78,13 +80,13 @@ func (r *Device) loopInternal(ch chan Data, quit chan int) { log.Print(err) return } - ch <- data + ch <- &data err = r.SetMode(RF_OPMODE_RECEIVER) if err != nil { log.Fatal(err) } case <-quit: - quit <- 1 + quit <- true return } } diff --git a/pin.go b/pin.go new file mode 100644 index 0000000..6563b54 --- /dev/null +++ b/pin.go @@ -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") +} diff --git a/pin_linux.go b/pin_linux.go new file mode 100644 index 0000000..2f838a8 --- /dev/null +++ b/pin_linux.go @@ -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) +}