-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LoRa WAN US915 Support #603
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
package region | ||
|
||
type Channel struct { | ||
Frequency uint32 | ||
Bandwidth uint8 | ||
SpreadingFactor uint8 | ||
CodingRate uint8 | ||
PreambleLength uint16 | ||
TxPowerDBm int8 | ||
type RegionSettings interface { | ||
JoinRequestChannel() Channel | ||
JoinAcceptChannel() Channel | ||
UplinkChannel() Channel | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package region | ||
|
||
import "tinygo.org/x/drivers/lora" | ||
|
||
const ( | ||
US915_DEFAULT_PREAMBLE_LEN = 8 | ||
US915_DEFAULT_TX_POWER_DBM = 20 | ||
US915_FREQUENCY_INCREMENT_DR_0 = 200000 // only for 125 kHz Bandwidth | ||
US915_FREQUENCY_INCREMENT_DR_4 = 1600000 // only for 500 kHz Bandwidth | ||
) | ||
|
||
type ChannelUS struct { | ||
frequency uint32 | ||
bandwidth uint8 | ||
spreadingFactor uint8 | ||
codingRate uint8 | ||
preambleLength uint16 | ||
txPowerDBm int8 | ||
} | ||
|
||
// 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: | ||
freq, ok := stepFrequency125(c.frequency) | ||
if ok { | ||
c.frequency = freq | ||
} else { | ||
c.frequency = lora.Mhz_903_0 | ||
c.bandwidth = lora.Bandwidth_500_0 | ||
} | ||
case lora.Bandwidth_500_0: | ||
freq, ok := stepFrequency500(c.frequency) | ||
if ok { | ||
c.frequency = freq | ||
} else { | ||
// there are no more frequencies to check after sweeping all 8 500 kHz channels | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func stepFrequency125(freq uint32) (uint32, bool) { | ||
f := freq + US915_FREQUENCY_INCREMENT_DR_0 | ||
if f >= lora.MHZ_915_0 { | ||
return 0, false | ||
} | ||
|
||
return f, true | ||
} | ||
|
||
func stepFrequency500(freq uint32) (uint32, bool) { | ||
f := freq + US915_FREQUENCY_INCREMENT_DR_4 | ||
if f >= lora.MHZ_915_0 { | ||
return 0, false | ||
} | ||
|
||
return f, true | ||
} | ||
|
||
type RegionSettingsUS915 struct { | ||
joinRequestChannel *ChannelUS | ||
joinAcceptChannel *ChannelUS | ||
uplinkChannel *ChannelUS | ||
} | ||
|
||
func US915() *RegionSettingsUS915 { | ||
return &RegionSettingsUS915{ | ||
joinRequestChannel: &ChannelUS{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, | ||
lora.Bandwidth_500_0, | ||
lora.SpreadingFactor9, | ||
lora.CodingRate4_5, | ||
US915_DEFAULT_PREAMBLE_LEN, | ||
US915_DEFAULT_TX_POWER_DBM}, | ||
uplinkChannel: &ChannelUS{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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like identical code for all ChannelXX types-
Should we have a base
channel
type and compose these "higher" level types with it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ie:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@soypat that sounds like an excellent refactoring for a subsequent PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will submit a PR that does exactly this later on...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #611