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"
)
// 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

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

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