lorawan/sx12xx : Code cleanup

This commit is contained in:
Olivier Fauchon
2023-01-29 23:14:45 +01:00
committed by Ron Evans
parent d473b9e1dc
commit 02d808a6fb
7 changed files with 116 additions and 43 deletions
+16 -18
View File
@@ -44,6 +44,15 @@ func UseRadio(r lora.Radio) {
ActiveRadio = r
}
func ApplyChannelConfig(ch *region.Channel) {
ActiveRadio.SetFrequency(ch.Frequency)
ActiveRadio.SetBandwidth(ch.Bandwidth)
ActiveRadio.SetCodingRate(ch.CodingRate)
ActiveRadio.SetSpreadingFactor(ch.SpreadingFactor)
ActiveRadio.SetPreambleLength(ch.PreambleLength)
ActiveRadio.SetTxPower(ch.TxPowerDBm)
}
func Join(otaa *Otaa, session *Session) error {
var resp []uint8
@@ -64,25 +73,18 @@ func Join(otaa *Otaa, session *Session) error {
}
// Prepare radio for Join Tx
joinChannel := regionSettings.GetJoinRequestChannel()
ActiveRadio.SetFrequency(joinChannel.Frequency)
ActiveRadio.SetBandwidth(joinChannel.Bandwidth)
ActiveRadio.SetCodingRate(joinChannel.CodingRate)
ActiveRadio.SetSpreadingFactor(joinChannel.SpreadingFactor)
ApplyChannelConfig(regionSettings.JoinRequestChannel())
ActiveRadio.SetCrc(true)
ActiveRadio.SetIqMode(0) // IQ Standard
ActiveRadio.SetIqMode(lora.IQStandard)
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
if err != nil {
return err
}
// Wait for JoinAccept
joinChannel = regionSettings.GetJoinAcceptChannel()
ActiveRadio.SetFrequency(joinChannel.Frequency)
ActiveRadio.SetBandwidth(joinChannel.Bandwidth)
ActiveRadio.SetCodingRate(joinChannel.CodingRate)
ActiveRadio.SetSpreadingFactor(joinChannel.SpreadingFactor)
ActiveRadio.SetIqMode(1) // IQ Inverted
ApplyChannelConfig(regionSettings.JoinAcceptChannel())
ActiveRadio.SetCrc(true)
ActiveRadio.SetIqMode(lora.IQInverted)
for i := 0; i < Retries; i++ {
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
if err != nil {
@@ -115,13 +117,9 @@ func SendUplink(data []uint8, session *Session) error {
return err
}
joinChannel := regionSettings.GetUplinkChannel()
ActiveRadio.SetFrequency(joinChannel.Frequency)
ActiveRadio.SetBandwidth(joinChannel.Bandwidth)
ActiveRadio.SetCodingRate(joinChannel.CodingRate)
ActiveRadio.SetSpreadingFactor(joinChannel.SpreadingFactor)
ApplyChannelConfig(regionSettings.UplinkChannel())
ActiveRadio.SetCrc(true)
ActiveRadio.SetIqMode(0) // IQ Standard
ActiveRadio.SetIqMode(lora.IQInverted)
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
if err != nil {
return err
+34 -7
View File
@@ -2,21 +2,48 @@ package region
import "tinygo.org/x/drivers/lora"
const (
AU915_DEFAULT_PREAMBLE_LEN = 8
AU915_DEFAULT_TX_POWER_DBM = 20
)
type RegionSettingsAU915 struct {
joinRequestChannel *Channel
joinAcceptChannel *Channel
uplinkChannel *Channel
}
func AU915() *RegionSettingsAU915 {
return &RegionSettingsAU915{}
return &RegionSettingsAU915{
joinRequestChannel: &Channel{916800000,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
AU915_DEFAULT_PREAMBLE_LEN,
AU915_DEFAULT_TX_POWER_DBM},
joinAcceptChannel: &Channel{923300000,
lora.Bandwidth_500_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
AU915_DEFAULT_PREAMBLE_LEN,
AU915_DEFAULT_TX_POWER_DBM},
uplinkChannel: &Channel{868500000,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
AU915_DEFAULT_PREAMBLE_LEN,
AU915_DEFAULT_TX_POWER_DBM},
}
}
func (r *RegionSettingsAU915) GetJoinRequestChannel() Channel {
return Channel{916800000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_5}
func (r *RegionSettingsAU915) JoinRequestChannel() *Channel {
return r.joinRequestChannel
}
func (r *RegionSettingsAU915) GetJoinAcceptChannel() Channel {
return Channel{923300000, lora.Bandwidth_500_0, lora.SpreadingFactor9, lora.CodingRate4_5}
func (r *RegionSettingsAU915) JoinAcceptChannel() *Channel {
return r.joinAcceptChannel
}
func (r *RegionSettingsAU915) GetUplinkChannel() Channel {
return Channel{868500000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_5}
func (r *RegionSettingsAU915) UplinkChannel() *Channel {
return r.uplinkChannel
}
+34 -7
View File
@@ -2,21 +2,48 @@ package region
import "tinygo.org/x/drivers/lora"
const (
EU868_DEFAULT_PREAMBLE_LEN = 8
EU868_DEFAULT_TX_POWER_DBM = 20
)
type RegionSettingsEU868 struct {
joinRequestChannel *Channel
joinAcceptChannel *Channel
uplinkChannel *Channel
}
func EU868() *RegionSettingsEU868 {
return &RegionSettingsEU868{}
return &RegionSettingsEU868{
joinRequestChannel: &Channel{868100000,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
EU868_DEFAULT_PREAMBLE_LEN,
EU868_DEFAULT_TX_POWER_DBM},
joinAcceptChannel: &Channel{868100000,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
EU868_DEFAULT_PREAMBLE_LEN,
EU868_DEFAULT_TX_POWER_DBM},
uplinkChannel: &Channel{868100000,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
EU868_DEFAULT_PREAMBLE_LEN,
EU868_DEFAULT_TX_POWER_DBM},
}
}
func (r *RegionSettingsEU868) GetJoinRequestChannel() Channel {
return Channel{868100000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7}
func (r *RegionSettingsEU868) JoinRequestChannel() *Channel {
return r.joinRequestChannel
}
func (r *RegionSettingsEU868) GetJoinAcceptChannel() Channel {
return Channel{868100000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7}
func (r *RegionSettingsEU868) JoinAcceptChannel() *Channel {
return r.joinAcceptChannel
}
func (r *RegionSettingsEU868) GetUplinkChannel() Channel {
return Channel{868100000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7}
func (r *RegionSettingsEU868) UplinkChannel() *Channel {
return r.uplinkChannel
}
+5 -3
View File
@@ -5,10 +5,12 @@ type Channel struct {
Bandwidth uint8
SpreadingFactor uint8
CodingRate uint8
PreambleLength uint16
TxPowerDBm int8
}
type RegionSettings interface {
GetJoinRequestChannel() Channel
GetJoinAcceptChannel() Channel
GetUplinkChannel() Channel
JoinRequestChannel() *Channel
JoinAcceptChannel() *Channel
UplinkChannel() *Channel
}