Some refactoring, and full functional TX/RX switching

This commit is contained in:
2026-09-08 12:57:50 -04:00
parent 16547d4ebf
commit 4d2c2ebdcd
4 changed files with 355 additions and 227 deletions

169
Cargo.lock generated
View File

@@ -4,9 +4,9 @@ version = 4
[[package]]
name = "arrayvec"
version = "0.7.6"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56"
[[package]]
name = "bare-metal"
@@ -43,42 +43,55 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cortex-m"
version = "0.7.7"
version = "0.7.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ec610d8f49840a5b376c69663b6369e71f4b34484b9b2eb29fb918d92516cb9"
checksum = "844b9697e922c99847eed515c6eb6d101e7ce62ff556fcaec243798291427ee8"
dependencies = [
"bare-metal",
"bitfield 0.13.2",
"cortex-m-macros",
"critical-section",
"embedded-hal 0.2.7",
"embedded-hal 1.0.0",
"volatile-register",
]
[[package]]
name = "cortex-m-rt"
version = "0.7.5"
name = "cortex-m-macros"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "801d4dec46b34c299ccf6b036717ae0fce602faa4f4fe816d9013b9a7c9f5ba6"
checksum = "6d1922be58519ad40368fc4ca595a2cefa51a7abf947be3b0c90586dc7dbd0e2"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "cortex-m-rt"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c439b23bf18154d8a634543caf1ee73cceb723f2902f7ea243634159a00fd47"
dependencies = [
"cortex-m-rt-macros",
]
[[package]]
name = "cortex-m-rt-macros"
version = "0.7.5"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e37549a379a9e0e6e576fd208ee60394ccb8be963889eebba3ffe0980364f472"
checksum = "061a75f3e7f3f01d649668d68e02d0ef92c7041a9c97b8fe6bc354ddbf40822d"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.114",
"syn 2.0.119",
]
[[package]]
name = "crc-any"
version = "2.5.0"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a62ec9ff5f7965e4d7280bd5482acd20aadb50d632cf6c1d74493856b011fa73"
checksum = "46db9f663dfb869b80fcf59e32d7a80fc6c464a4f6328f3f06a00f5e36d05f8c"
dependencies = [
"debug-helper",
]
@@ -91,15 +104,15 @@ checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
[[package]]
name = "debug-helper"
version = "0.3.13"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f578e8e2c440e7297e008bb5486a3a8a194775224bbc23729b0dbdfaeebf162e"
checksum = "80a4af69c60438a1a82af89d362f4729fd38db7b73f305a237636fad31ceb2bf"
[[package]]
name = "defmt"
version = "1.0.1"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "548d977b6da32fa1d1fda2876453da1e7df63ad0304c8b3dae4dbe7b96f39b78"
checksum = "e2953bfe4f93bbd20cc71198842756f77d161884c99ebbabc41d80231ded88d1"
dependencies = [
"bitflags",
"defmt-macros",
@@ -107,15 +120,14 @@ dependencies = [
[[package]]
name = "defmt-macros"
version = "1.0.1"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d4fc12a85bcf441cfe44344c4b72d58493178ce635338a3f3b78943aceb258e"
checksum = "bad9c72e7ca2137e0dc3813245a0d282fd6daad32fd800af018306a9169b5fe8"
dependencies = [
"defmt-parser",
"proc-macro-error2",
"proc-macro2",
"quote",
"syn 2.0.114",
"syn 2.0.119",
]
[[package]]
@@ -129,9 +141,9 @@ dependencies = [
[[package]]
name = "defmt-rtt"
version = "1.1.0"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93d5a25c99d89c40f5676bec8cefe0614f17f0f40e916f98e345dae941807f9e"
checksum = "05144944c117db54127a8ac17a3c9a28d55e7047bfddb950bcd20b4a94c79b10"
dependencies = [
"critical-section",
"defmt",
@@ -139,9 +151,9 @@ dependencies = [
[[package]]
name = "either"
version = "1.15.0"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34"
[[package]]
name = "embedded-dma"
@@ -205,7 +217,7 @@ version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28aef0f9aa070bce60767c12ba9cb41efeaf1a2bc6427f87b7d83f11239a16d7"
dependencies = [
"frunk_core",
"frunk_core 0.4.4",
"frunk_derives",
]
@@ -215,6 +227,12 @@ version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "476eeaa382e3462b84da5d6ba3da97b5786823c2d0d3a0d04ef088d073da225c"
[[package]]
name = "frunk_core"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bd3c9ba2e323e8b19e77f15873f60974a7d82f89b80e50c53be44b8b92927c1"
[[package]]
name = "frunk_derives"
version = "0.4.4"
@@ -223,19 +241,19 @@ checksum = "a0b4095fc99e1d858e5b8c7125d2638372ec85aa0fe6c807105cf10b0265ca6c"
dependencies = [
"frunk_proc_macro_helpers",
"quote",
"syn 2.0.114",
"syn 2.0.119",
]
[[package]]
name = "frunk_proc_macro_helpers"
version = "0.1.4"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1952b802269f2db12ab7c0bd328d0ae8feaabf19f352a7b0af7bb0c5693abfce"
checksum = "b70229a1347a20d4af9c06116cc452acef34f798668c6b69e97dd5c8a88052bd"
dependencies = [
"frunk_core",
"frunk_core 0.5.0",
"proc-macro2",
"quote",
"syn 2.0.114",
"syn 2.0.119",
]
[[package]]
@@ -272,6 +290,12 @@ dependencies = [
"stable_deref_trait",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "itertools"
version = "0.10.5"
@@ -346,6 +370,7 @@ dependencies = [
"rp2040-boot2",
"rp2040-hal",
"sh1106-pico-rs",
"strum",
]
[[package]]
@@ -361,46 +386,24 @@ dependencies = [
[[package]]
name = "portable-atomic"
version = "1.13.1"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
[[package]]
name = "proc-macro-error-attr2"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "proc-macro-error2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802"
dependencies = [
"proc-macro-error-attr2",
"proc-macro2",
"quote",
"syn 2.0.114",
]
checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
[[package]]
name = "proc-macro2"
version = "1.0.106"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.44"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
"proc-macro2",
]
@@ -543,6 +546,27 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "strum"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9628de9b8791db39ceda2b119bbe13134770b56c138ec1d3af810d045c04f9bd"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab85eea0270ee17587ed4156089e10b9e6880ee688791d45a905f5b1ca36f664"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "syn"
version = "1.0.109"
@@ -556,9 +580,20 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.114"
version = "2.0.119"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a"
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "3.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12df2e0110f65b775f769bb17ef989067a1d931b2eb822bd4346631eeada89f9"
dependencies = [
"proc-macro2",
"quote",
@@ -567,22 +602,22 @@ dependencies = [
[[package]]
name = "thiserror"
version = "2.0.18"
version = "2.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4"
checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "2.0.18"
version = "2.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5"
checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.114",
"syn 3.0.5",
]
[[package]]
@@ -593,9 +628,9 @@ checksum = "e87a2ed6b42ec5e28cc3b94c09982969e9227600b2e3dcbc1db927a84c06bd69"
[[package]]
name = "unicode-ident"
version = "1.0.23"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "537dd038a89878be9b64dd4bd1b260315c1bb94f4d784956b81e27a088d9a09e"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "usb-device"

View File

@@ -9,11 +9,12 @@ cortex-m-rt = "0.7.5"
defmt = "1.0.1"
defmt-rtt = "1.1.0"
embedded-hal = "1.0.0"
fixedstr = { version = "0.5.11", features = ["no-alloc"] }
fixedstr = { version = "0.5.11", features = ["no-alloc", "circular-str"] }
panic-probe = { version = "1.0.0", features = [ "print-rtt" ]}
rp2040-boot2 = "0.3.0"
rp2040-hal = "0.11.0"
sh1106-pico-rs = { path = "../sh1106-pico-rs" }
strum = { version = "0.28.0", default-features = false, features = ["derive"] }
[features]
logging = []

View File

@@ -2,16 +2,16 @@
#![no_main]
mod morse;
mod radio;
mod si5351;
use cortex_m::prelude::_embedded_hal_PwmPin;
use defmt::{println, warn};
use defmt_rtt as _;
use fixedstr::{str_format, str48};
use fixedstr::{cstr64, str48};
use panic_probe as _;
use rp2040_hal::clocks::ClockSource;
use rp2040_hal::gpio::PinId;
use rp2040_hal::pac::UART0;
use rp2040_hal::pll::PLLConfig;
use rp2040_hal::pll::common_configs::PLL_USB_48MHZ;
@@ -27,6 +27,7 @@ use sh1106_pico_rs::graphics::GraphicsBuf;
use sh1106_pico_rs::sh1106::SH1106Dev;
use crate::morse::Morse;
use crate::radio::*;
use crate::si5351::Si5351Dev;
/// NB if OVERCLOCKING
@@ -108,33 +109,12 @@ fn main() -> ! {
let mut led_pin = pins.gpio25.into_push_pull_output();
led_pin.set_high();
// Init PWMs
// let mut pwm_slices = hal::pwm::Slices::new(pac.PWM, &mut pac.RESETS);
let mut relay_pin = pins.gpio21.into_push_pull_output();
relay_pin.set_low();
let test_parse: u32 = "4000".parse().unwrap();
println!("Parse: {}", test_parse);
// Configure PWM0
// let pwm_x = &mut pwm_slices.pwm1;
// pwm_x.clr_ph_correct();
// pwm_x.set_div_int(1);
// pwm_x.set_div_frac(0);
// pwm_x.clear_interrupt();
// pwm_x.enable();
// pwm_x.disable();
// let mut pwm_pin = pins.gpio18;
// pwm_pin.set_drive_strength(rp2040_hal::gpio::OutputDriveStrength::TwelveMilliAmps);
// pwm_x.channel_a.output_to(pwm_pin);
// pwm_x.channel_a.set_enabled(true);
// let current_tune = 695;
// const MAX_TUNE: u16 = 65000u16;
// const MIN_TUNE: u16 = 4u16;
// set_tune(current_tune, pwm_x);
// Create the I²C drive
let mut i2c = hal::I2C::i2c0(
pac.I2C0,
@@ -166,7 +146,7 @@ fn main() -> ! {
sh1106_dev.blit_framebuffer(&mut i2c, &gfx_buf);
delay.delay_ms(500);
let mut si5351_dev = Si5351Dev::new(&mut delay, &mut i2c);
let si5351_dev = Si5351Dev::new(&mut delay, &mut i2c);
gfx_buf.clear();
gfx_buf.draw_textfield(10, 10, 107, 20, "si5351 initialized.");
@@ -211,22 +191,31 @@ fn main() -> ! {
uart.write_full_blocking("\r\nREADY.\r\n".as_bytes());
let mut uart_recv_buf = str48::new();
let mut uart_recv_buf = cstr64::new();
let mut radio = Radio::new(si5351_dev, relay_pin);
loop {
run_uart_stuff(&mut i2c, &mut uart, &mut si5351_dev, &mut uart_recv_buf);
run_uart_stuff(&mut i2c, &mut uart, &mut radio, &mut uart_recv_buf);
last_gfx_update = last_gfx_update.saturating_add(1);
if last_gfx_update < 100_000 {
if last_gfx_update < 10_000 {
continue;
}
last_gfx_update = 0;
gfx_buf.clear();
gfx_buf.draw_string(20, 14, "Beacon running");
gfx_buf.draw_string(
20,
14,
match radio.get_mode() {
Mode::RX => "Receive",
Mode::TX => "TRANSMIT",
},
);
let tuned_freq_mhz = si5351_dev.get_clk_freq() / 1_000_000;
let tuned_freq_mhz = radio.get_vfo_freq() / 1_000_000;
let mut tune_str_buf = [0u8; 5];
let len = u32_into_str(tuned_freq_mhz, &mut tune_str_buf);
let tune_str_mhz = str::from_utf8(&tune_str_buf[tune_str_buf.len() - len..]).unwrap();
@@ -234,7 +223,7 @@ fn main() -> ! {
let mut tune_str = str48::from(tune_str_mhz);
tune_str.push(".");
let tuned_freq_khz = si5351_dev.get_clk_freq() - tuned_freq_mhz * 1_000_000;
let tuned_freq_khz = radio.get_vfo_freq() - tuned_freq_mhz * 1_000_000;
let tuned_freq_khz = (tuned_freq_khz / 1_000).min(999);
let mut tune_str_buf = ['0' as u8; 3];
let _len = u32_into_str(tuned_freq_khz, &mut tune_str_buf);
@@ -273,144 +262,58 @@ fn main() -> ! {
}
}
fn run_uart_stuff<P>(
mut i2c: &mut dyn embedded_hal::i2c::I2c<Error = rp2040_hal::i2c::Error>,
fn run_uart_stuff<P, I>(
i2c: &mut dyn embedded_hal::i2c::I2c<Error = rp2040_hal::i2c::Error>,
uart: &mut UartPeripheral<Enabled, UART0, P>,
si5351_dev: &mut Si5351Dev,
uart_recv_buf: &mut str48,
radio: &mut Radio<I>,
uart_recv_buf: &mut cstr64,
) where
P: ValidUartPinout<UART0>,
I: PinId,
{
if !uart.uart_is_readable() {
return;
}
if uart.uart_is_readable() {
let mut recv_buf = [0u8; 8];
let len_read = match uart.read_raw(&mut recv_buf) {
Ok(n) => n,
_ => 0,
};
if len_read < 1 {
return;
}
if len_read > 0 {
let unreadable_len = match str::from_utf8(&recv_buf[0..len_read]) {
Ok(utf8str) => {
// println!("Read a string of len {} from uart", utf8str.len());
uart_recv_buf.push(utf8str).len()
uart_recv_buf.push_str(utf8str).len()
}
_ => {
warn!("Uart wasn't valid utf8");
1
}
};
if unreadable_len > 0 {
warn!("UART buffer overflow, resetting");
uart_recv_buf.clear();
}
}
}
// println!("UART CMD:\n{}", uart_recv_buf.to_str());
if uart_recv_buf.contains(";") {
let cmd = RadioCmd::parse_cmd(&uart_recv_buf);
// let ack = str_format!(str32, "ACK CMD: {:?}", cmd);
// println!("{}", ack.to_str());
if let Some(cmd_len) = uart_recv_buf.find_substr(";") {
let cmd = uart_recv_buf.substr(0, cmd_len + 1);
println!(
"process cmd {}, from {}, len {}",
cmd.force_str(),
uart_recv_buf.to_strs(),
cmd_len,
);
let resp = match cmd {
RadioCmd::VFOA(f) | RadioCmd::VFOB(f) => {
if f != 0 {
si5351_dev.set_clk_freq(&mut i2c, f);
}
str_format!(str48, "FA{:011};", si5351_dev.get_clk_freq())
}
RadioCmd::TX => str48::from("TX0;"),
RadioCmd::RX => str48::from("RX0;"),
RadioCmd::PS(_on_off) => str48::from("PS1;"),
RadioCmd::IF => {
str_format!(str48, "IF{:011}{:023} ;", si5351_dev.get_clk_freq(), 0)
}
RadioCmd::ID => str48::from("ID020;"),
RadioCmd::MD => str48::from("MD2;"),
RadioCmd::FW => str_format!(str48, "FW{:04};", 0),
RadioCmd::AI => str48::from("AI0;"),
RadioCmd::Unknown => str48::from("?;"),
};
uart.write_full_blocking(resp.as_bytes());
uart_recv_buf.clear();
} else if unreadable_len > 0 {
println!("Input buffer overflow without terminator, clearing");
uart_recv_buf.clear();
}
}
#[derive(Debug)]
enum RadioCmd {
VFOA(u32),
VFOB(u32),
TX,
RX,
PS(u8),
IF,
ID,
MD,
FW,
AI,
Unknown,
}
impl RadioCmd {
fn parse_cmd(cmd: &str48) -> RadioCmd {
let cmd = cmd.to_ascii_upper();
let cmd = str48::from(cmd.trim());
// println!("{}\n\n", cmd.to_str());
let len = match cmd.to_str().find(";") {
Some(n) => n,
None => return Self::Unknown,
match radio.process_cmd(i2c, str48::from(cmd.force_str())) {
Some(resp) => uart.write_full_blocking(resp.as_bytes()),
None => (),
};
if len < 2 {
return Self::Unknown;
uart_recv_buf.truncate_left(cmd_len + 1);
}
let param = match len {
..=3 => str48::new(),
n => cmd.substr(2, len),
};
match &cmd[0..2] {
"FA" => match len {
..=3 => Self::VFOA(0),
_n => {
let freq_str = param.trim();
// println!("freq_str: {}. len {}", freq_str, freq_str.len());
let frequ32: u32 = freq_str.parse().unwrap();
Self::VFOA(frequ32)
}
},
"FB" => Self::VFOB(0),
"TX" => Self::TX,
"RX" => Self::RX,
"PS" => Self::PS(1),
"IF" => Self::IF,
"ID" => Self::ID,
"MD" => Self::MD,
"FW" => Self::FW,
"AI" => Self::AI,
_ => {
println!("Unknown cmd: {}", &cmd[0..2]);
Self::Unknown
}
}
}
}
fn set_tune(
tune: u16,
pwm: &mut rp2040_hal::pwm::Slice<rp2040_hal::pwm::Pwm1, rp2040_hal::pwm::FreeRunning>,
) {
let tgt_duty = tune / 3;
pwm.set_top(tune - 1);
pwm.channel_a.set_duty(tgt_duty.max(1));
println!("set tune to {} with duty {}", tune, tgt_duty);
}
// returns the length of the final str removing leading zeroes

189
src/radio.rs Normal file
View File

@@ -0,0 +1,189 @@
use core::str::FromStr;
use embedded_hal::{digital::OutputPin, i2c::I2c};
use rp2040_hal::{
gpio::{FunctionSio, Pin, PinId, PullDown, SioOutput},
i2c::Error,
};
use defmt::warn;
use fixedstr::{str_format, str48};
use strum::EnumString;
use crate::si5351::Si5351Dev;
pub struct Radio<I: PinId> {
mode: Mode,
freq: u32,
keyer_speed: u8,
si5351_dev: Si5351Dev,
relay_pin: Pin<I, FunctionSio<SioOutput>, PullDown>,
}
#[derive(Default, Clone, Copy)]
pub enum Mode {
#[default]
RX,
TX,
}
impl<I: PinId> Radio<I> {
pub fn new(si5351_dev: Si5351Dev, relay_pin: Pin<I, FunctionSio<SioOutput>, PullDown>) -> Self {
Self {
mode: Mode::RX,
freq: 14_074_000,
keyer_speed: 10,
si5351_dev,
relay_pin,
}
}
pub fn get_mode(&self) -> Mode {
self.mode
}
pub fn get_vfo_freq(&self) -> u32 {
self.freq
}
pub fn process_cmd(&mut self, i2c: &mut dyn I2c<Error = Error>, buff: str48) -> Option<str48> {
let cmd = Cmd::from(&buff);
match cmd.mode {
CmdMode::Set => {
self.process_set_cmd(i2c, cmd);
None
}
CmdMode::Read => Some(self.process_get_cmd(i2c, cmd)),
}
}
fn process_get_cmd(&mut self, _i2c: &mut dyn I2c<Error = Error>, cmd: Cmd) -> str48 {
match cmd.ty {
CmdType::FA | CmdType::FB => {
str_format!(str48, "FA{:011};", self.freq)
}
CmdType::TX => str48::from("TX0;"),
CmdType::RX => str48::from("RX0;"),
CmdType::PS => str48::from("PS1;"),
CmdType::IF => {
str_format!(str48, "IF{:011}{:023} ;", self.freq, 0)
}
CmdType::ID => str48::from("ID020;"),
CmdType::MD => str48::from("MD2;"),
CmdType::FW => str_format!(str48, "FW{:04};", 0),
CmdType::AI => str48::from("AI0;"),
CmdType::KS => str_format!(str48, "KS{:03}", self.keyer_speed),
CmdType::Unknown => str48::from("?;"),
}
}
fn process_set_cmd(&mut self, i2c: &mut dyn I2c<Error = Error>, cmd: Cmd) {
match cmd.ty {
CmdType::FA | CmdType::FB => {
let Some(freq_str) = cmd.params else {
warn!("Set VFO without params.");
return;
};
let f: u32 = freq_str.parse().unwrap_or(0);
if f != 0 {
self.freq = f;
self.si5351_dev.set_clk_freq(i2c, f);
}
}
CmdType::TX => {
self.mode = Mode::TX;
self.relay_pin.set_high();
}
CmdType::RX => {
self.mode = Mode::RX;
self.relay_pin.set_low();
}
CmdType::KS => {
let Some(ks) = cmd.params else {
warn!("Set VFO without params.");
return;
};
let f: u8 = ks.parse().unwrap_or(0);
self.keyer_speed = f;
}
uk => warn!("Unsupported SET cmd: {:?}", uk),
}
}
}
#[derive(Debug, defmt::Format, EnumString)]
enum CmdType {
FA,
FB,
TX,
RX,
PS,
IF,
ID,
MD,
FW,
AI,
KS,
Unknown,
}
enum CmdMode {
Set,
Read,
}
struct Cmd {
ty: CmdType,
mode: CmdMode,
params: Option<str48>,
}
impl Default for Cmd {
fn default() -> Self {
Self {
ty: CmdType::Unknown,
mode: CmdMode::Read,
params: None,
}
}
}
impl Cmd {
pub fn from(cmd: &str48) -> Cmd {
let cmd = cmd.to_ascii_upper();
let cmd = str48::from(cmd.trim());
// defmt::println!("{:?}\n\n", cmd.as_bytes());
let len = match cmd.to_str().find(";") {
Some(n) => n,
None => {
return Self::default();
}
};
if len < 2 {
return Self::default();
}
let ty = CmdType::from_str(&cmd[0..2]).unwrap_or(CmdType::Unknown);
let mode = match (&ty, len) {
(CmdType::TX | CmdType::RX, _) => CmdMode::Set,
(_, ..=3) => CmdMode::Read,
_ => CmdMode::Set,
};
Self {
ty,
mode,
params: match len {
..=3 => None,
_n => Some(cmd.substr(2, len)),
},
}
}
}