This commit is contained in:
cheetah 2021-02-28 03:20:18 +01:00
parent 62fa4e0530
commit 9979a80cf0
2 changed files with 42 additions and 24 deletions

View file

@ -33,6 +33,14 @@ type Device struct {
quit chan bool quit chan bool
_invert bool _invert bool
OnReceive OnReceiveHandler OnReceive OnReceiveHandler
FreqOffset int
TXBaud int
TXFreq int
TXPower int
RXBaud int
RXFreq int
} }
// Global settings // Global settings
@ -61,6 +69,12 @@ func NewDevice(isRfm69HW bool) (*Device, error) {
_invert: false, _invert: false,
tx: make(chan *Data, 5), tx: make(chan *Data, 5),
quit: make(chan bool), quit: make(chan bool),
FreqOffset: 0,
TXBaud: 1200,
TXFreq: 433e6,
RXBaud: 1200,
RXFreq: 433e6,
} }
err = ret.setup() err = ret.setup()
@ -476,28 +490,24 @@ func (r *Device) readFifo() (Data, error) {
data.Data = rx[1:] data.Data = rx[1:]
return data, nil return data, nil
} }
func (r *Device) readFifo1Byte() (byte, error) {
var err error
log.Println("readFifo1Byte()")
tx := new([2]byte) func (r *Device) PrepareTX() (error) {
tx[0] = REG_FIFO & 0x7f if err := r.SetFrequency(r.TXFreq, r.FreqOffset); err != nil {
log.Println(tx) return err
rx, err := r.spiDevice.Xfer(tx[:1])
if err != nil {
return rx[1], err
}
log.Println(rx)
rx, err = r.spiDevice.Xfer(tx[1:])
if err != nil {
return rx[1], err
} }
log.Println(rx) if err := r.SetBaudrate(r.TXBaud); err != nil {
return rx[0], nil return err
}
return nil
} }
func (r *Device) PrepareRX() (error) {
if err := r.SetFrequency(r.RXFreq, r.FreqOffset); err != nil {
return err
}
if err := r.SetBaudrate(r.RXBaud); err != nil {
return err
}
return nil
}

10
loop.go
View file

@ -8,6 +8,9 @@ import (
// Send data // Send data
func (r *Device) Send(d *Data) { func (r *Device) Send(d *Data) {
if r.TXFreq == 0 {
return
}
if (r._invert) { if (r._invert) {
for i := 0; i < len(d.Data); i++ { for i := 0; i < len(d.Data); i++ {
d.Data[i] = 255 - d.Data[i] d.Data[i] = 255 - d.Data[i]
@ -41,6 +44,7 @@ func (r *Device) Loop() {
select { select {
case dataToTransmit := <-r.tx: case dataToTransmit := <-r.tx:
log.Println("going to transmit"); log.Println("going to transmit");
r.PrepareTX();
// 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)
@ -110,15 +114,19 @@ func (r *Device) Loop() {
log.Fatal(err) log.Fatal(err)
} }
log.Println("tx done"); log.Println("tx done");
r.PrepareRX();
break
case <-r.quit: case <-r.quit:
r.quit <- true r.quit <- true
return return
default: default:
if r.RXFreq == 0 {
continue
}
if r.mode != RF_OPMODE_RECEIVER { if r.mode != RF_OPMODE_RECEIVER {
continue continue
} }
flags, err := r.readReg(REG_IRQFLAGS2) flags, err := r.readReg(REG_IRQFLAGS2)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)