Simplify startup and remove rx,tx channels

master
Friedl Ulrich 9 years ago
parent a43dfed1b3
commit 9aeb4fb188

@ -12,6 +12,9 @@ const (
spiPath = "/dev/spidev0.0" spiPath = "/dev/spidev0.0"
) )
// OnReceiveHandler is the receive callback
type OnReceiveHandler func(*Data)
// Device RFM69 Device // Device RFM69 Device
type Device struct { type Device struct {
spiDevice *spidev.SPIDevice spiDevice *spidev.SPIDevice
@ -21,6 +24,9 @@ type Device struct {
network byte network byte
isRFM69HW bool isRFM69HW bool
powerLevel byte powerLevel byte
tx chan *Data
quit chan bool
OnReceive OnReceiveHandler
} }
// Global settings // Global settings
@ -48,15 +54,24 @@ func NewDevice(nodeID, networkID byte, isRfm69HW bool) (*Device, error) {
address: nodeID, address: nodeID,
isRFM69HW: isRfm69HW, isRFM69HW: isRfm69HW,
powerLevel: 31, powerLevel: 31,
tx: make(chan *Data),
quit: make(chan bool),
} }
err = ret.setup() err = ret.setup()
if err != nil {
return nil, err
}
go ret.loop()
return ret, err return ret, nil
} }
// Close cleans up // Close cleans up
func (r *Device) Close() error { func (r *Device) Close() error {
r.quit <- true
<-r.quit
err := r.gpio.Close() err := r.gpio.Close()
if err != nil { if err != nil {
return err return err

@ -6,16 +6,12 @@ import (
"github.com/davecheney/gpio" "github.com/davecheney/gpio"
) )
// Loop is the main receive and transmit handling loop // Send data
func (r *Device) Loop() (rx chan *Data, tx chan *Data, quit chan bool) { func (r *Device) Send(d *Data) {
quit = make(chan bool) r.tx <- d
rx = make(chan *Data, 5)
tx = make(chan *Data, 5)
go r.loopInternal(rx, tx, quit)
return
} }
func (r *Device) loopInternal(rx chan *Data, tx chan *Data, quit chan bool) { func (r *Device) loop() {
irq := make(chan int) irq := make(chan int)
r.gpio.BeginWatch(gpio.EdgeRising, func() { r.gpio.BeginWatch(gpio.EdgeRising, func() {
irq <- 1 irq <- 1
@ -31,7 +27,7 @@ func (r *Device) loopInternal(rx chan *Data, tx chan *Data, quit chan bool) {
for { for {
select { select {
case dataToTransmit := <-tx: case dataToTransmit := <-r.tx:
// TODO: can send? // TODO: can send?
r.readWriteReg(REG_PACKETCONFIG2, 0xFB, RF_PACKET2_RXRESTART) // avoid RX deadlocks r.readWriteReg(REG_PACKETCONFIG2, 0xFB, RF_PACKET2_RXRESTART) // avoid RX deadlocks
err = r.SetModeAndWait(RF_OPMODE_STANDBY) 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) log.Print(err)
return return
} }
rx <- &data if r.OnReceive != nil {
go r.OnReceive(&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 <-r.quit:
quit <- true r.quit <- true
return return
} }
} }

@ -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
}
Loading…
Cancel
Save