From 9aeb4fb18806ed86621d6500c6f362f60c9f7d58 Mon Sep 17 00:00:00 2001 From: Friedl Ulrich Date: Fri, 4 Mar 2016 21:01:06 +0100 Subject: [PATCH] Simplify startup and remove rx,tx channels --- device.go | 17 ++++++++++++++++- handler.go => loop.go | 22 ++++++++++------------ payload/payload.go | 20 -------------------- 3 files changed, 26 insertions(+), 33 deletions(-) rename handler.go => loop.go (78%) delete mode 100644 payload/payload.go diff --git a/device.go b/device.go index 2b9bfbf..36a1507 100644 --- a/device.go +++ b/device.go @@ -12,6 +12,9 @@ const ( spiPath = "/dev/spidev0.0" ) +// OnReceiveHandler is the receive callback +type OnReceiveHandler func(*Data) + // Device RFM69 Device type Device struct { spiDevice *spidev.SPIDevice @@ -21,6 +24,9 @@ type Device struct { network byte isRFM69HW bool powerLevel byte + tx chan *Data + quit chan bool + OnReceive OnReceiveHandler } // Global settings @@ -48,15 +54,24 @@ func NewDevice(nodeID, networkID byte, isRfm69HW bool) (*Device, error) { address: nodeID, isRFM69HW: isRfm69HW, powerLevel: 31, + tx: make(chan *Data), + quit: make(chan bool), } err = ret.setup() + if err != nil { + return nil, err + } + + go ret.loop() - return ret, err + return ret, nil } // Close cleans up func (r *Device) Close() error { + r.quit <- true + <-r.quit err := r.gpio.Close() if err != nil { return err diff --git a/handler.go b/loop.go similarity index 78% rename from handler.go rename to loop.go index 468c109..f56271f 100644 --- a/handler.go +++ b/loop.go @@ -6,16 +6,12 @@ import ( "github.com/davecheney/gpio" ) -// Loop is the main receive and transmit handling loop -func (r *Device) Loop() (rx chan *Data, tx chan *Data, quit chan bool) { - quit = make(chan bool) - rx = make(chan *Data, 5) - tx = make(chan *Data, 5) - go r.loopInternal(rx, tx, quit) - return +// Send data +func (r *Device) Send(d *Data) { + r.tx <- d } -func (r *Device) loopInternal(rx chan *Data, tx chan *Data, quit chan bool) { +func (r *Device) loop() { irq := make(chan int) r.gpio.BeginWatch(gpio.EdgeRising, func() { irq <- 1 @@ -31,7 +27,7 @@ func (r *Device) loopInternal(rx chan *Data, tx chan *Data, quit chan bool) { for { select { - case dataToTransmit := <-tx: + case dataToTransmit := <-r.tx: // TODO: can send? r.readWriteReg(REG_PACKETCONFIG2, 0xFB, RF_PACKET2_RXRESTART) // avoid RX deadlocks err = r.SetModeAndWait(RF_OPMODE_STANDBY) @@ -81,13 +77,15 @@ func (r *Device) loopInternal(rx chan *Data, tx chan *Data, quit chan bool) { log.Print(err) return } - rx <- &data + if r.OnReceive != nil { + go r.OnReceive(&data) + } err = r.SetMode(RF_OPMODE_RECEIVER) if err != nil { log.Fatal(err) } - case <-quit: - quit <- true + case <-r.quit: + r.quit <- true return } } diff --git a/payload/payload.go b/payload/payload.go deleted file mode 100644 index cfae339..0000000 --- a/payload/payload.go +++ /dev/null @@ -1,20 +0,0 @@ -package payload - -// Payload is the first part -type Payload struct { - Type int16 // sensor type (2, 3, 4, 5) - Uptime uint32 // uptime in ms -} - -// Payload1 is for DHT22-nodes -type Payload1 struct { - Temperature float32 // Temp - Humidity float32 // Humidity - VBat float32 // V Battery -} - -// Payload2 is for switches -type Payload2 struct { - Pin byte // Pin number - State byte // 1=On 0=Off -}