mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-07-26 10:38:41 +00:00
lora/lorawan: refactor shared functionality for ChannelUS, etc into embedded type
named channel, do the same for RegionSettings, and then change RegionSettings to just Settings to avoid the redundant naming. Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -30,11 +30,11 @@ const (
|
||||
var (
|
||||
ActiveRadio lora.Radio
|
||||
Retries = 15
|
||||
regionSettings region.RegionSettings
|
||||
regionSettings region.Settings
|
||||
)
|
||||
|
||||
// UseRegionSettings sets current Lorawan Regional parameters
|
||||
func UseRegionSettings(rs region.RegionSettings) {
|
||||
func UseRegionSettings(rs region.Settings) {
|
||||
regionSettings = rs
|
||||
}
|
||||
|
||||
|
||||
@@ -8,75 +8,40 @@ const (
|
||||
)
|
||||
|
||||
type ChannelAU struct {
|
||||
frequency uint32
|
||||
bandwidth uint8
|
||||
spreadingFactor uint8
|
||||
codingRate uint8
|
||||
preambleLength uint16
|
||||
txPowerDBm int8
|
||||
channel
|
||||
}
|
||||
|
||||
// Getter functions
|
||||
func (c *ChannelAU) Frequency() uint32 { return c.frequency }
|
||||
func (c *ChannelAU) Bandwidth() uint8 { return c.bandwidth }
|
||||
func (c *ChannelAU) SpreadingFactor() uint8 { return c.spreadingFactor }
|
||||
func (c *ChannelAU) CodingRate() uint8 { return c.codingRate }
|
||||
func (c *ChannelAU) PreambleLength() uint16 { return c.preambleLength }
|
||||
func (c *ChannelAU) TxPowerDBm() int8 { return c.txPowerDBm }
|
||||
|
||||
// Set functions
|
||||
func (c *ChannelAU) SetFrequency(v uint32) { c.frequency = v }
|
||||
func (c *ChannelAU) SetBandwidth(v uint8) { c.bandwidth = v }
|
||||
func (c *ChannelAU) SetSpreadingFactor(v uint8) { c.spreadingFactor = v }
|
||||
func (c *ChannelAU) SetCodingRate(v uint8) { c.codingRate = v }
|
||||
func (c *ChannelAU) SetPreambleLength(v uint16) { c.preambleLength = v }
|
||||
func (c *ChannelAU) SetTxPowerDBm(v int8) { c.txPowerDBm = v }
|
||||
|
||||
func (c *ChannelAU) Next() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type RegionSettingsAU915 struct {
|
||||
joinRequestChannel *ChannelAU
|
||||
joinAcceptChannel *ChannelAU
|
||||
uplinkChannel *ChannelAU
|
||||
type SettingsAU915 struct {
|
||||
settings
|
||||
}
|
||||
|
||||
func AU915() *RegionSettingsAU915 {
|
||||
return &RegionSettingsAU915{
|
||||
joinRequestChannel: &ChannelAU{lora.MHz_916_8,
|
||||
func AU915() *SettingsAU915 {
|
||||
return &SettingsAU915{settings: settings{
|
||||
joinRequestChannel: &ChannelAU{channel: channel{lora.MHz_916_8,
|
||||
lora.Bandwidth_125_0,
|
||||
lora.SpreadingFactor9,
|
||||
lora.CodingRate4_5,
|
||||
AU915_DEFAULT_PREAMBLE_LEN,
|
||||
AU915_DEFAULT_TX_POWER_DBM},
|
||||
joinAcceptChannel: &ChannelAU{lora.MHz_923_3,
|
||||
AU915_DEFAULT_TX_POWER_DBM}},
|
||||
joinAcceptChannel: &ChannelAU{channel: channel{lora.MHz_923_3,
|
||||
lora.Bandwidth_500_0,
|
||||
lora.SpreadingFactor9,
|
||||
lora.CodingRate4_5,
|
||||
AU915_DEFAULT_PREAMBLE_LEN,
|
||||
AU915_DEFAULT_TX_POWER_DBM},
|
||||
uplinkChannel: &ChannelAU{lora.MHz_916_8,
|
||||
AU915_DEFAULT_TX_POWER_DBM}},
|
||||
uplinkChannel: &ChannelAU{channel: channel{lora.MHz_916_8,
|
||||
lora.Bandwidth_125_0,
|
||||
lora.SpreadingFactor9,
|
||||
lora.CodingRate4_5,
|
||||
AU915_DEFAULT_PREAMBLE_LEN,
|
||||
AU915_DEFAULT_TX_POWER_DBM},
|
||||
}
|
||||
AU915_DEFAULT_TX_POWER_DBM}},
|
||||
}}
|
||||
}
|
||||
|
||||
func Next(c *ChannelAU) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *RegionSettingsAU915) JoinRequestChannel() Channel {
|
||||
return r.joinRequestChannel
|
||||
}
|
||||
|
||||
func (r *RegionSettingsAU915) JoinAcceptChannel() Channel {
|
||||
return r.joinAcceptChannel
|
||||
}
|
||||
|
||||
func (r *RegionSettingsAU915) UplinkChannel() Channel {
|
||||
return r.uplinkChannel
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package region
|
||||
|
||||
type Channel interface {
|
||||
Next() bool
|
||||
Frequency() uint32
|
||||
Bandwidth() uint8
|
||||
SpreadingFactor() uint8
|
||||
CodingRate() uint8
|
||||
PreambleLength() uint16
|
||||
TxPowerDBm() int8
|
||||
SetFrequency(v uint32)
|
||||
SetBandwidth(v uint8)
|
||||
SetSpreadingFactor(v uint8)
|
||||
SetCodingRate(v uint8)
|
||||
SetPreambleLength(v uint16)
|
||||
SetTxPowerDBm(v int8)
|
||||
}
|
||||
|
||||
type channel struct {
|
||||
frequency uint32
|
||||
bandwidth uint8
|
||||
spreadingFactor uint8
|
||||
codingRate uint8
|
||||
preambleLength uint16
|
||||
txPowerDBm int8
|
||||
}
|
||||
|
||||
// Getter functions
|
||||
func (c *channel) Frequency() uint32 { return c.frequency }
|
||||
func (c *channel) Bandwidth() uint8 { return c.bandwidth }
|
||||
func (c *channel) SpreadingFactor() uint8 { return c.spreadingFactor }
|
||||
func (c *channel) CodingRate() uint8 { return c.codingRate }
|
||||
func (c *channel) PreambleLength() uint16 { return c.preambleLength }
|
||||
func (c *channel) TxPowerDBm() int8 { return c.txPowerDBm }
|
||||
|
||||
// Set functions
|
||||
func (c *channel) SetFrequency(v uint32) { c.frequency = v }
|
||||
func (c *channel) SetBandwidth(v uint8) { c.bandwidth = v }
|
||||
func (c *channel) SetSpreadingFactor(v uint8) { c.spreadingFactor = v }
|
||||
func (c *channel) SetCodingRate(v uint8) { c.codingRate = v }
|
||||
func (c *channel) SetPreambleLength(v uint16) { c.preambleLength = v }
|
||||
func (c *channel) SetTxPowerDBm(v int8) { c.txPowerDBm = v }
|
||||
@@ -8,71 +8,36 @@ const (
|
||||
)
|
||||
|
||||
type ChannelEU struct {
|
||||
frequency uint32
|
||||
bandwidth uint8
|
||||
spreadingFactor uint8
|
||||
codingRate uint8
|
||||
preambleLength uint16
|
||||
txPowerDBm int8
|
||||
channel
|
||||
}
|
||||
|
||||
// Getter functions
|
||||
func (c *ChannelEU) Frequency() uint32 { return c.frequency }
|
||||
func (c *ChannelEU) Bandwidth() uint8 { return c.bandwidth }
|
||||
func (c *ChannelEU) SpreadingFactor() uint8 { return c.spreadingFactor }
|
||||
func (c *ChannelEU) CodingRate() uint8 { return c.codingRate }
|
||||
func (c *ChannelEU) PreambleLength() uint16 { return c.preambleLength }
|
||||
func (c *ChannelEU) TxPowerDBm() int8 { return c.txPowerDBm }
|
||||
|
||||
// Set functions
|
||||
func (c *ChannelEU) SetFrequency(v uint32) { c.frequency = v }
|
||||
func (c *ChannelEU) SetBandwidth(v uint8) { c.bandwidth = v }
|
||||
func (c *ChannelEU) SetSpreadingFactor(v uint8) { c.spreadingFactor = v }
|
||||
func (c *ChannelEU) SetCodingRate(v uint8) { c.codingRate = v }
|
||||
func (c *ChannelEU) SetPreambleLength(v uint16) { c.preambleLength = v }
|
||||
func (c *ChannelEU) SetTxPowerDBm(v int8) { c.txPowerDBm = v }
|
||||
|
||||
func (c *ChannelEU) Next() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type RegionSettingsEU868 struct {
|
||||
joinRequestChannel *ChannelEU
|
||||
joinAcceptChannel *ChannelEU
|
||||
uplinkChannel *ChannelEU
|
||||
type SettingsEU868 struct {
|
||||
settings
|
||||
}
|
||||
|
||||
func EU868() *RegionSettingsEU868 {
|
||||
return &RegionSettingsEU868{
|
||||
joinRequestChannel: &ChannelEU{lora.MHz_868_1,
|
||||
func EU868() *SettingsEU868 {
|
||||
return &SettingsEU868{settings: settings{
|
||||
joinRequestChannel: &ChannelEU{channel: channel{lora.MHz_868_1,
|
||||
lora.Bandwidth_125_0,
|
||||
lora.SpreadingFactor9,
|
||||
lora.CodingRate4_7,
|
||||
EU868_DEFAULT_PREAMBLE_LEN,
|
||||
EU868_DEFAULT_TX_POWER_DBM},
|
||||
joinAcceptChannel: &ChannelEU{lora.MHz_868_1,
|
||||
EU868_DEFAULT_TX_POWER_DBM}},
|
||||
joinAcceptChannel: &ChannelEU{channel: channel{lora.MHz_868_1,
|
||||
lora.Bandwidth_125_0,
|
||||
lora.SpreadingFactor9,
|
||||
lora.CodingRate4_7,
|
||||
EU868_DEFAULT_PREAMBLE_LEN,
|
||||
EU868_DEFAULT_TX_POWER_DBM},
|
||||
uplinkChannel: &ChannelEU{lora.MHz_868_1,
|
||||
EU868_DEFAULT_TX_POWER_DBM}},
|
||||
uplinkChannel: &ChannelEU{channel: channel{lora.MHz_868_1,
|
||||
lora.Bandwidth_125_0,
|
||||
lora.SpreadingFactor9,
|
||||
lora.CodingRate4_7,
|
||||
EU868_DEFAULT_PREAMBLE_LEN,
|
||||
EU868_DEFAULT_TX_POWER_DBM},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RegionSettingsEU868) JoinRequestChannel() Channel {
|
||||
return r.joinRequestChannel
|
||||
}
|
||||
|
||||
func (r *RegionSettingsEU868) JoinAcceptChannel() Channel {
|
||||
return r.joinAcceptChannel
|
||||
}
|
||||
|
||||
func (r *RegionSettingsEU868) UplinkChannel() Channel {
|
||||
return r.uplinkChannel
|
||||
EU868_DEFAULT_TX_POWER_DBM}},
|
||||
}}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package region
|
||||
|
||||
type RegionSettings interface {
|
||||
JoinRequestChannel() Channel
|
||||
JoinAcceptChannel() Channel
|
||||
UplinkChannel() Channel
|
||||
}
|
||||
|
||||
type Channel interface {
|
||||
Next() bool
|
||||
Frequency() uint32
|
||||
Bandwidth() uint8
|
||||
SpreadingFactor() uint8
|
||||
CodingRate() uint8
|
||||
PreambleLength() uint16
|
||||
TxPowerDBm() int8
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package region
|
||||
|
||||
type Settings interface {
|
||||
JoinRequestChannel() Channel
|
||||
JoinAcceptChannel() Channel
|
||||
UplinkChannel() Channel
|
||||
}
|
||||
|
||||
type settings struct {
|
||||
joinRequestChannel Channel
|
||||
joinAcceptChannel Channel
|
||||
uplinkChannel Channel
|
||||
}
|
||||
|
||||
func (r *settings) JoinRequestChannel() Channel {
|
||||
return r.joinRequestChannel
|
||||
}
|
||||
|
||||
func (r *settings) JoinAcceptChannel() Channel {
|
||||
return r.joinAcceptChannel
|
||||
}
|
||||
|
||||
func (r *settings) UplinkChannel() Channel {
|
||||
return r.uplinkChannel
|
||||
}
|
||||
@@ -10,31 +10,9 @@ const (
|
||||
)
|
||||
|
||||
type ChannelUS struct {
|
||||
frequency uint32
|
||||
bandwidth uint8
|
||||
spreadingFactor uint8
|
||||
codingRate uint8
|
||||
preambleLength uint16
|
||||
txPowerDBm int8
|
||||
channel
|
||||
}
|
||||
|
||||
// Getter functions
|
||||
func (c *ChannelUS) Frequency() uint32 { return c.frequency }
|
||||
func (c *ChannelUS) Bandwidth() uint8 { return c.bandwidth }
|
||||
func (c *ChannelUS) SpreadingFactor() uint8 { return c.spreadingFactor }
|
||||
func (c *ChannelUS) CodingRate() uint8 { return c.codingRate }
|
||||
func (c *ChannelUS) PreambleLength() uint16 { return c.preambleLength }
|
||||
func (c *ChannelUS) TxPowerDBm() int8 { return c.txPowerDBm }
|
||||
|
||||
// Set functions
|
||||
// TODO: validate input
|
||||
func (c *ChannelUS) SetFrequency(v uint32) { c.frequency = v }
|
||||
func (c *ChannelUS) SetBandwidth(v uint8) { c.bandwidth = v }
|
||||
func (c *ChannelUS) SetSpreadingFactor(v uint8) { c.spreadingFactor = v }
|
||||
func (c *ChannelUS) SetCodingRate(v uint8) { c.codingRate = v }
|
||||
func (c *ChannelUS) SetPreambleLength(v uint16) { c.preambleLength = v }
|
||||
func (c *ChannelUS) SetTxPowerDBm(v int8) { c.txPowerDBm = v }
|
||||
|
||||
func (c *ChannelUS) Next() bool {
|
||||
switch c.Bandwidth() {
|
||||
case lora.Bandwidth_125_0:
|
||||
@@ -76,43 +54,29 @@ func stepFrequency500(freq uint32) (uint32, bool) {
|
||||
return f, true
|
||||
}
|
||||
|
||||
type RegionSettingsUS915 struct {
|
||||
joinRequestChannel *ChannelUS
|
||||
joinAcceptChannel *ChannelUS
|
||||
uplinkChannel *ChannelUS
|
||||
type SettingsUS915 struct {
|
||||
settings
|
||||
}
|
||||
|
||||
func US915() *RegionSettingsUS915 {
|
||||
return &RegionSettingsUS915{
|
||||
joinRequestChannel: &ChannelUS{lora.MHz_902_3,
|
||||
func US915() *SettingsUS915 {
|
||||
return &SettingsUS915{settings: settings{
|
||||
joinRequestChannel: &ChannelUS{channel: channel{lora.MHz_902_3,
|
||||
lora.Bandwidth_125_0,
|
||||
lora.SpreadingFactor10,
|
||||
lora.CodingRate4_5,
|
||||
US915_DEFAULT_PREAMBLE_LEN,
|
||||
US915_DEFAULT_TX_POWER_DBM},
|
||||
joinAcceptChannel: &ChannelUS{0,
|
||||
US915_DEFAULT_TX_POWER_DBM}},
|
||||
joinAcceptChannel: &ChannelUS{channel: channel{0,
|
||||
lora.Bandwidth_500_0,
|
||||
lora.SpreadingFactor9,
|
||||
lora.CodingRate4_5,
|
||||
US915_DEFAULT_PREAMBLE_LEN,
|
||||
US915_DEFAULT_TX_POWER_DBM},
|
||||
uplinkChannel: &ChannelUS{lora.Mhz_903_0,
|
||||
US915_DEFAULT_TX_POWER_DBM}},
|
||||
uplinkChannel: &ChannelUS{channel: channel{lora.Mhz_903_0,
|
||||
lora.Bandwidth_500_0,
|
||||
lora.SpreadingFactor9,
|
||||
lora.CodingRate4_5,
|
||||
US915_DEFAULT_PREAMBLE_LEN,
|
||||
US915_DEFAULT_TX_POWER_DBM},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RegionSettingsUS915) JoinRequestChannel() Channel {
|
||||
return r.joinRequestChannel
|
||||
}
|
||||
|
||||
func (r *RegionSettingsUS915) JoinAcceptChannel() Channel {
|
||||
return r.joinAcceptChannel
|
||||
}
|
||||
|
||||
func (r *RegionSettingsUS915) UplinkChannel() Channel {
|
||||
return r.uplinkChannel
|
||||
US915_DEFAULT_TX_POWER_DBM}},
|
||||
}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user