refactor: make CodingRate a plain u8 denominator (5-8) on the wire

CodingRate was an enum with repr(u8) values that didn't match postcard's
wire encoding (variant index). Instead of papering over the mismatch,
make cr a plain u8 in RadioConfig. Host sends the denominator directly:
5 for CR 4/5, 6 for CR 4/6, etc. No lookup tables, no index confusion.

Remove CodingRate enum, cr_denom() helper, and the Bandwidth import that
was only needed for cr_denom. ErrorCode repr values also fixed to 0-based.
This commit is contained in:
swaits committed 2026-03-29 20:42:57 -06:00
1 parent 920dd7cef8
commit ece7cbc8ab
4 files changed
+22 -33

No files matched your search

+1 -10
View File
@@ -106,7 +106,7 @@ pub fn dashboard(
Bandwidth::Khz250 => "250kHz", Bandwidth::Khz250 => "250kHz",
Bandwidth::Khz500 => "500kHz", Bandwidth::Khz500 => "500kHz",
}; };
let _ = write!(buf, "{} SF{} CR4/{}", bw_str, cfg.sf, cr_denom(cfg.cr)); let _ = write!(buf, "{} SF{} CR4/{}", bw_str, cfg.sf, cfg.cr);
Text::new(&buf, Point::new(0, FONT_H * 2 - 1), style) Text::new(&buf, Point::new(0, FONT_H * 2 - 1), style)
.draw(target) .draw(target)
.ok(); .ok();
@@ -244,12 +244,3 @@ pub fn blank(target: &mut impl DrawTarget<Color = BinaryColor>) {
let _ = target.clear(BinaryColor::Off); let _ = target.clear(BinaryColor::Off);
} }
fn cr_denom(cr: crate::protocol::CodingRate) -> u8 {
use crate::protocol::CodingRate;
match cr {
CodingRate::Cr4_5 => 5,
CodingRate::Cr4_6 => 6,
CodingRate::Cr4_7 => 7,
CodingRate::Cr4_8 => 8,
}
}
+11 -17
View File
@@ -20,23 +20,15 @@ pub enum Bandwidth {
Khz500 = 9, Khz500 = 9,
} }
/// LoRa forward error correction coding rate.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, defmt::Format)]
#[repr(u8)]
pub enum CodingRate {
Cr4_5 = 1,
Cr4_6 = 2,
Cr4_7 = 3,
Cr4_8 = 4,
}
/// Complete LoRa radio configuration. /// Complete LoRa radio configuration.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, defmt::Format)]
pub struct RadioConfig { pub struct RadioConfig {
pub freq_hz: u32, pub freq_hz: u32,
pub bw: Bandwidth, pub bw: Bandwidth,
/// Spreading factor (5-12).
pub sf: u8, pub sf: u8,
pub cr: CodingRate, /// Coding rate denominator (5-8). E.g. 5 = CR 4/5, 8 = CR 4/8.
pub cr: u8,
pub sync_word: u16, pub sync_word: u16,
pub tx_power_dbm: i8, pub tx_power_dbm: i8,
} }
@@ -77,13 +69,15 @@ pub enum Response {
} }
/// Error codes reported to the host. /// Error codes reported to the host.
///
/// Variant indices match postcard wire encoding (0-based).
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, defmt::Format)]
#[repr(u8)] #[repr(u8)]
pub enum ErrorCode { pub enum ErrorCode {
InvalidConfig = 1, InvalidConfig = 0,
RadioBusy = 2, RadioBusy = 1,
TxTimeout = 3, TxTimeout = 2,
CrcError = 4, CrcError = 3,
NotConfigured = 5, NotConfigured = 4,
NoDisplay = 6, NoDisplay = 5,
} }
+9 -5
View File
@@ -206,13 +206,17 @@ fn to_sf(sf: u8) -> lora_phy::mod_params::SpreadingFactor {
} }
} }
fn to_cr(cr: protocol::CodingRate) -> lora_phy::mod_params::CodingRate { fn to_cr(cr: u8) -> lora_phy::mod_params::CodingRate {
use lora_phy::mod_params::CodingRate::*; use lora_phy::mod_params::CodingRate::*;
match cr { match cr {
protocol::CodingRate::Cr4_5 => _4_5, 5 => _4_5,
protocol::CodingRate::Cr4_6 => _4_6, 6 => _4_6,
protocol::CodingRate::Cr4_7 => _4_7, 7 => _4_7,
protocol::CodingRate::Cr4_8 => _4_8, 8 => _4_8,
_ => {
debug_assert!(false, "BUG: unvalidated CR {}", cr);
_4_5
}
} }
} }
+1 -1
View File
@@ -914,7 +914,7 @@ RADIO_CONFIG = {
"freq_hz": 910_525_000, "freq_hz": 910_525_000,
"bw": 6, # Khz62 = variant index 6 "bw": 6, # Khz62 = variant index 6
"sf": 7, "sf": 7,
"cr": 0, # Cr4_5 = variant index 0 "cr": 5, # CR 4/5 — denominator value
"sync_word": 0x3444, "sync_word": 0x3444,
"tx_power_dbm": 14, "tx_power_dbm": 14,
} }