Skip to content

Commit 0724924

Browse files
committed
Fix channel calculation for NARROW_868 region
Correct the `numChannels` and `radioFreq` calculations to align with the firmware logic. This primarily affects the NARROW_868 region by properly accounting for channel spacing. - Remove `regionInfo.spacing` from the `numChannels` numerator. - Add `regionInfo.spacing` to the `radioFreq` calculation. - Add tests to verify `numChannels` and `radioFreq` for the `NARROW_868` region.
1 parent f75edf2 commit 0724924

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

core/model/src/main/kotlin/org/meshtastic/core/model/ChannelOption.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ val LoRaConfig.numChannels: Int
7878
if (bw <= 0f) return 1 // Return 1 if bandwidth is zero or negative
7979

8080
// Calculate number of channels: spacing = gap between channels (0 for continuous spectrum)
81-
// Match firmware: uint32_t numChannels = round((myRegion->freqEnd - myRegion->freqStart + myRegion->spacing) /
82-
// channelSpacing);
81+
// Match firmware: uint32_t numChannels = round((myRegion->freqEnd - myRegion->freqStart) / channelSpacing);
8382
val channelSpacing = regionInfo.spacing + bw
84-
val num = Math.round((regionInfo.freqEnd - regionInfo.freqStart + regionInfo.spacing) / channelSpacing).toInt()
83+
val num = Math.round((regionInfo.freqEnd - regionInfo.freqStart) / channelSpacing).toInt()
8584

8685
// If the regional frequency range is smaller than the bandwidth, the firmware would
8786
// fall back to a default preset. In the app, we return 1 to avoid a crash.
@@ -100,13 +99,14 @@ internal fun LoRaConfig.radioFreq(channelNum: Int): Float {
10099
return if (regionInfo != null) {
101100
val bw = bandwidth(regionInfo)
102101
val channelSpacing = regionInfo.spacing + bw
103-
// Match firmware: float freq = myRegion->freqStart + (bw / 2000) + (channel_num * channelSpacing);
102+
// Match firmware: float freq = myRegion->freqStart + myRegion->spacing + (bw / 2000) + (channel_num *
103+
// channelSpacing);
104104
// Note: firmware channel_num is 0-indexed in the calculation, but the app uses 1-indexed for some reason?
105105
// Let's re-verify the firmware logic.
106106
// Firmware: channel_num = hash(channelName) % numChannels;
107-
// freq = myRegion->freqStart + (bw / 2000) + (channel_num * channelSpacing);
107+
// freq = myRegion->freqStart + myRegion->spacing + (bw / 2000) + (channel_num * channelSpacing);
108108
// The app's channelNum function returns 1..numChannels if channelNum is 0.
109-
(regionInfo.freqStart + bw / 2) + (channelNum - 1) * channelSpacing
109+
(regionInfo.freqStart + regionInfo.spacing + bw / 2) + (channelNum - 1) * channelSpacing
110110
} else {
111111
0f
112112
}

core/model/src/test/kotlin/org/meshtastic/core/model/ChannelOptionTest.kt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2025 Meshtastic LLC
2+
* Copyright (c) 2025-2026 Meshtastic LLC
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -14,13 +14,14 @@
1414
* You should have received a copy of the GNU General Public License
1515
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
*/
17-
1817
package org.meshtastic.core.model
1918

2019
import org.junit.Assert.assertEquals
2120
import org.junit.Assert.assertNotNull
2221
import org.junit.Test
22+
import org.meshtastic.proto.ConfigKt.loRaConfig
2323
import org.meshtastic.proto.ConfigProtos.Config.LoRaConfig.ModemPreset
24+
import org.meshtastic.proto.ConfigProtos.Config.LoRaConfig.RegionCode
2425

2526
class ChannelOptionTest {
2627

@@ -78,4 +79,24 @@ class ChannelOptionTest {
7879
ChannelOption.entries.size,
7980
)
8081
}
82+
83+
@Test
84+
fun `test radioFreq and numChannels for NARROW_868`() {
85+
val loraConfig = loRaConfig {
86+
region = RegionCode.NARROW_868
87+
usePreset = true
88+
modemPreset = ModemPreset.NARROW_FAST
89+
}
90+
91+
// bw = 0.0625, spacing = 0.015, channelSpacing = 0.0775
92+
// Range = 869.65 - 869.4 = 0.25
93+
// numChannels = round(0.25 / 0.0775) = 3
94+
assertEquals(3, loraConfig.numChannels)
95+
96+
// Slot 1: freqStart + spacing + bw/2 = 869.4 + 0.015 + 0.03125 = 869.44625
97+
assertEquals(869.44625f, loraConfig.radioFreq(1), 0.0001f)
98+
99+
// Slot 3: 869.44625 + 2 * 0.0775 = 869.44625 + 0.155 = 869.60125
100+
assertEquals(869.60125f, loraConfig.radioFreq(3), 0.0001f)
101+
}
81102
}

0 commit comments

Comments
 (0)