2026-02-09 16:24:18 -05:00
|
|
|
#![no_std]
|
|
|
|
|
#![no_main]
|
|
|
|
|
|
2026-07-19 20:04:07 -04:00
|
|
|
mod morse;
|
2026-08-19 14:43:07 -04:00
|
|
|
mod si5351;
|
2026-07-19 20:04:07 -04:00
|
|
|
|
2026-09-04 14:59:33 -04:00
|
|
|
use cortex_m::prelude::_embedded_hal_PwmPin;
|
2026-02-09 16:24:18 -05:00
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
use defmt::{println, warn};
|
2026-02-09 16:24:18 -05:00
|
|
|
use defmt_rtt as _;
|
2026-09-04 14:59:33 -04:00
|
|
|
use fixedstr::{str_format, str32, str48};
|
2026-02-09 16:24:18 -05:00
|
|
|
use panic_probe as _;
|
|
|
|
|
|
2026-03-02 20:13:03 -05:00
|
|
|
use rp2040_hal::clocks::ClockSource;
|
2026-02-12 21:23:35 -05:00
|
|
|
use rp2040_hal::pll::PLLConfig;
|
|
|
|
|
use rp2040_hal::pll::common_configs::PLL_USB_48MHZ;
|
2026-09-04 14:52:09 -04:00
|
|
|
use rp2040_hal::uart::{DataBits, StopBits, UartConfig};
|
2026-02-09 16:24:18 -05:00
|
|
|
// Alias for our HAL crate
|
|
|
|
|
use rp2040_hal as hal;
|
|
|
|
|
|
|
|
|
|
use hal::fugit::RateExtU32;
|
|
|
|
|
use hal::pac;
|
|
|
|
|
|
|
|
|
|
use embedded_hal::digital::OutputPin;
|
|
|
|
|
use sh1106_pico_rs::graphics::GraphicsBuf;
|
|
|
|
|
use sh1106_pico_rs::sh1106::SH1106Dev;
|
|
|
|
|
|
2026-07-19 20:04:07 -04:00
|
|
|
use crate::morse::Morse;
|
2026-08-19 14:43:07 -04:00
|
|
|
use crate::si5351::Si5351Dev;
|
2026-07-19 20:04:07 -04:00
|
|
|
|
2026-02-12 21:23:35 -05:00
|
|
|
/// NB if OVERCLOCKING
|
|
|
|
|
/// you MUST use a BOOT_LOADER which can tolerate the OC
|
|
|
|
|
/// the GENERIC_03H one cannot (unsure why) but W25Q080 has been solid for me
|
2026-02-09 16:24:18 -05:00
|
|
|
#[unsafe(link_section = ".boot_loader")]
|
|
|
|
|
#[used]
|
2026-02-12 21:23:35 -05:00
|
|
|
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
|
2026-02-09 16:24:18 -05:00
|
|
|
|
|
|
|
|
/// External high-speed crystal on the Raspberry Pi Pico board is 12 MHz. Adjust
|
|
|
|
|
/// if your board has a different frequency
|
|
|
|
|
const XTAL_FREQ_HZ: u32 = 12_000_000u32;
|
|
|
|
|
|
|
|
|
|
#[rp2040_hal::entry]
|
|
|
|
|
fn main() -> ! {
|
|
|
|
|
// Grab our singleton objects
|
|
|
|
|
let mut pac = pac::Peripherals::take().unwrap();
|
|
|
|
|
let core = pac::CorePeripherals::take().unwrap();
|
|
|
|
|
|
|
|
|
|
// Set up the watchdog driver - needed by the clock setup code
|
2026-02-12 21:23:35 -05:00
|
|
|
// let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);
|
|
|
|
|
|
|
|
|
|
hal::vreg::set_voltage(
|
|
|
|
|
&mut pac.VREG_AND_CHIP_RESET,
|
|
|
|
|
pac::vreg_and_chip_reset::vreg::VSEL_A::VOLTAGE1_30,
|
|
|
|
|
);
|
|
|
|
|
// settle
|
|
|
|
|
// cortex_m::asm::delay(3_000_000);
|
|
|
|
|
|
|
|
|
|
let xosc = hal::xosc::setup_xosc_blocking_custom_delay(pac.XOSC, XTAL_FREQ_HZ.Hz(), 128)
|
|
|
|
|
.map_err(|_x| false)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
// watchdog.enable_tick_generation((XTAL_FREQ_HZ / 1_000_000) as u8);
|
2026-02-09 16:24:18 -05:00
|
|
|
|
|
|
|
|
// Configure the clocks
|
2026-02-12 21:23:35 -05:00
|
|
|
let mut clocks = hal::clocks::ClocksManager::new(pac.CLOCKS);
|
|
|
|
|
|
|
|
|
|
let pll_usb = rp2040_hal::pll::setup_pll_blocking(
|
2026-02-09 16:24:18 -05:00
|
|
|
pac.PLL_USB,
|
2026-02-12 21:23:35 -05:00
|
|
|
xosc.operating_frequency(),
|
|
|
|
|
PLL_USB_48MHZ,
|
|
|
|
|
&mut clocks,
|
|
|
|
|
&mut pac.RESETS,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let pll_sys = hal::pll::setup_pll_blocking(
|
|
|
|
|
pac.PLL_SYS,
|
|
|
|
|
xosc.operating_frequency(),
|
|
|
|
|
PLLConfig {
|
2026-08-19 14:43:07 -04:00
|
|
|
vco_freq: 1200.MHz(),
|
|
|
|
|
refdiv: 1,
|
2026-04-03 17:06:20 -04:00
|
|
|
post_div1: 6,
|
|
|
|
|
post_div2: 1,
|
2026-02-12 21:23:35 -05:00
|
|
|
},
|
|
|
|
|
&mut clocks,
|
2026-02-09 16:24:18 -05:00
|
|
|
&mut pac.RESETS,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2026-02-12 21:23:35 -05:00
|
|
|
clocks.init_default(&xosc, &pll_sys, &pll_usb).unwrap();
|
|
|
|
|
|
|
|
|
|
// let mut delay = cortex_m::delay::Delay::new(core.SYST, 200_000_000);
|
|
|
|
|
|
2026-02-09 16:24:18 -05:00
|
|
|
// The single-cycle I/O block controls our GPIO pins
|
|
|
|
|
let sio = hal::Sio::new(pac.SIO);
|
|
|
|
|
|
|
|
|
|
// Set the pins to their default state
|
|
|
|
|
let pins = hal::gpio::Pins::new(
|
|
|
|
|
pac.IO_BANK0,
|
|
|
|
|
pac.PADS_BANK0,
|
|
|
|
|
sio.gpio_bank0,
|
|
|
|
|
&mut pac.RESETS,
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-02 20:13:03 -05:00
|
|
|
let pll_freq = pll_sys.get_freq();
|
|
|
|
|
|
2026-02-09 16:24:18 -05:00
|
|
|
let mut led_pin = pins.gpio25.into_push_pull_output();
|
|
|
|
|
led_pin.set_high();
|
|
|
|
|
|
|
|
|
|
// Init PWMs
|
2026-09-04 14:59:33 -04:00
|
|
|
// let mut pwm_slices = hal::pwm::Slices::new(pac.PWM, &mut pac.RESETS);
|
2026-02-09 16:24:18 -05:00
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
let test_parse: u32 = "4000".parse().unwrap();
|
|
|
|
|
println!("Parse: {}", test_parse);
|
|
|
|
|
|
2026-02-09 16:24:18 -05:00
|
|
|
// Configure PWM0
|
2026-09-04 14:52:09 -04:00
|
|
|
// 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();
|
2026-07-19 20:04:07 -04:00
|
|
|
// pwm_x.disable();
|
|
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
// 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);
|
2026-02-09 16:24:18 -05:00
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
// let current_tune = 695;
|
2026-02-09 16:24:18 -05:00
|
|
|
|
2026-08-19 14:43:07 -04:00
|
|
|
// const MAX_TUNE: u16 = 65000u16;
|
|
|
|
|
// const MIN_TUNE: u16 = 4u16;
|
2026-02-09 16:24:18 -05:00
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
// set_tune(current_tune, pwm_x);
|
2026-02-09 16:24:18 -05:00
|
|
|
|
|
|
|
|
// Create the I²C drive
|
|
|
|
|
let mut i2c = hal::I2C::i2c0(
|
|
|
|
|
pac.I2C0,
|
|
|
|
|
pins.gpio8.reconfigure(),
|
|
|
|
|
pins.gpio9.reconfigure(),
|
2026-08-19 14:43:07 -04:00
|
|
|
100.kHz(),
|
2026-02-09 16:24:18 -05:00
|
|
|
&mut pac.RESETS,
|
|
|
|
|
&clocks.system_clock,
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-02 20:13:03 -05:00
|
|
|
let mut delay = cortex_m::delay::Delay::new(core.SYST, pll_freq.to_Hz());
|
2026-02-12 21:23:35 -05:00
|
|
|
|
2026-02-09 16:24:18 -05:00
|
|
|
let mut sh1106_dev = SH1106Dev::new(&mut delay, &mut i2c);
|
|
|
|
|
sh1106_dev.set_vertical_flip(&mut i2c, true);
|
|
|
|
|
let mut gfx_buf = GraphicsBuf::new();
|
|
|
|
|
let mut last_gfx_update = 0u32;
|
|
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
let mut freq_str_buf = [0u8; 8];
|
|
|
|
|
let _freq_str_len = u32_into_str(pll_freq.to_kHz(), &mut freq_str_buf);
|
|
|
|
|
let mut boot_msg = str48::from("Booted at ");
|
|
|
|
|
boot_msg.push(str::from_utf8(&freq_str_buf).unwrap());
|
|
|
|
|
boot_msg.push("kHz");
|
|
|
|
|
println!("{}", boot_msg.to_str());
|
|
|
|
|
gfx_buf.sprites[0].x = 108;
|
|
|
|
|
gfx_buf.clear();
|
|
|
|
|
gfx_buf.draw_textfield(10, 10, 107, 20, boot_msg.to_str());
|
|
|
|
|
gfx_buf.draw_textfield(10, 40, 107, 20, "Initializing si5351");
|
|
|
|
|
|
|
|
|
|
sh1106_dev.blit_framebuffer(&mut i2c, &gfx_buf);
|
|
|
|
|
delay.delay_ms(500);
|
|
|
|
|
|
|
|
|
|
let si5351_dev = Si5351Dev::new(&mut delay, &mut i2c);
|
|
|
|
|
|
|
|
|
|
gfx_buf.clear();
|
|
|
|
|
gfx_buf.draw_textfield(10, 10, 107, 20, "si5351 initialized.");
|
|
|
|
|
let mut status_str = str48::new();
|
|
|
|
|
let status = si5351_dev.get_status();
|
|
|
|
|
|
|
|
|
|
if (si5351::DeviceStatusBits::LOL_A as u8 & status) > 0 {
|
|
|
|
|
status_str.push("PLLA Lost Lock\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (si5351::DeviceStatusBits::LOL_B as u8 & status) > 0 {
|
|
|
|
|
status_str.push("PLLB Lost Lock\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if status_str.is_empty() {
|
|
|
|
|
status_str.push("Setup OK!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gfx_buf.draw_textfield(10, 30, 107, 20, &status_str);
|
|
|
|
|
sh1106_dev.blit_framebuffer(&mut i2c, &gfx_buf);
|
|
|
|
|
delay.delay_ms(500);
|
2026-03-02 20:13:03 -05:00
|
|
|
|
2026-08-25 11:07:42 -04:00
|
|
|
let input_mode = InputMode::default();
|
|
|
|
|
let _input_mode_chg_input = pins.gpio12.into_pull_up_input();
|
2026-03-02 20:13:03 -05:00
|
|
|
|
2026-07-19 20:04:07 -04:00
|
|
|
let message = "VA3FWE/B";
|
2026-08-25 11:07:42 -04:00
|
|
|
let _morse_sequence_index = 0usize;
|
|
|
|
|
let _current_code = Morse::from_char('_');
|
2026-04-03 17:06:20 -04:00
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
let uart_pins = (
|
|
|
|
|
// UART TX (characters sent from RP2040) on pin 1 (GPIO0)
|
|
|
|
|
pins.gpio0.into_function(),
|
|
|
|
|
// UART RX (characters received by RP2040) on pin 2 (GPIO1)
|
|
|
|
|
pins.gpio1.into_function(),
|
|
|
|
|
);
|
2026-09-04 14:59:33 -04:00
|
|
|
let uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
2026-09-04 14:52:09 -04:00
|
|
|
.enable(
|
|
|
|
|
UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One),
|
|
|
|
|
pll_freq,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
uart.write_full_blocking("\r\nREADY.\r\n".as_bytes());
|
|
|
|
|
|
|
|
|
|
let mut uart_recv_buf = str48::new();
|
|
|
|
|
|
2026-02-09 16:24:18 -05:00
|
|
|
loop {
|
2026-08-19 14:43:07 -04:00
|
|
|
// let mut char_done = false;
|
|
|
|
|
|
|
|
|
|
// while !char_done {
|
|
|
|
|
// let (on, md) = match current_code.next() {
|
|
|
|
|
// Some((o, d)) => (o, d),
|
|
|
|
|
// None => {
|
|
|
|
|
// char_done = true;
|
|
|
|
|
// (false, 0xFF)
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// match on {
|
|
|
|
|
// true => {
|
|
|
|
|
// led_pin.set_high().unwrap();
|
|
|
|
|
// pwm_x.channel_a.set_enabled(true);
|
|
|
|
|
// }
|
|
|
|
|
// false => {
|
|
|
|
|
// led_pin.set_low().unwrap();
|
|
|
|
|
// pwm_x.channel_a.set_enabled(false);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // calculate next delay.
|
|
|
|
|
// let morse_delay = match md {
|
|
|
|
|
// 0 => 100_000,
|
|
|
|
|
// 1 => 200_000,
|
|
|
|
|
// 2 => 400_000,
|
|
|
|
|
// 0xFF => 1_000_000,
|
|
|
|
|
// _ => 400_000,
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// // println!("delaying {} us", morse_delay);
|
|
|
|
|
// delay.delay_us(morse_delay);
|
|
|
|
|
// }
|
|
|
|
|
// morse_sequence_index = (morse_sequence_index + 1) % message.len();
|
|
|
|
|
|
|
|
|
|
// if morse_sequence_index == 0 {
|
|
|
|
|
// println!("Delay between beacon calls.");
|
|
|
|
|
// delay.delay_ms(5_000);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// let mut message_chars = message.chars();
|
|
|
|
|
// let c = message_chars.nth(morse_sequence_index).unwrap();
|
|
|
|
|
|
|
|
|
|
// println!("next char is {}", c);
|
|
|
|
|
|
|
|
|
|
// current_code = Morse::from_char(c);
|
|
|
|
|
|
|
|
|
|
// current_tune += 1;
|
|
|
|
|
|
|
|
|
|
// if current_tune > 222 {
|
|
|
|
|
// current_tune = 200;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// println!(
|
|
|
|
|
// "next freq is {}kHz",
|
|
|
|
|
// (pll_freq.to_kHz() as f32) / (current_tune as f32)
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
last_gfx_update = last_gfx_update.saturating_add(1);
|
|
|
|
|
|
|
|
|
|
if last_gfx_update < 10_000_000 {
|
|
|
|
|
continue;
|
2026-02-09 16:24:18 -05:00
|
|
|
}
|
2026-08-19 14:43:07 -04:00
|
|
|
last_gfx_update = 0;
|
2026-03-02 20:13:03 -05:00
|
|
|
|
2026-08-19 14:43:07 -04:00
|
|
|
// current_tune -= 1;
|
|
|
|
|
// if current_tune < 421 {
|
|
|
|
|
// current_tune = 695;
|
|
|
|
|
// }
|
2026-03-02 20:13:03 -05:00
|
|
|
|
2026-08-19 14:43:07 -04:00
|
|
|
// set_tune(current_tune, pwm_x);
|
2026-02-09 16:24:18 -05:00
|
|
|
|
2026-07-19 20:04:07 -04:00
|
|
|
gfx_buf.clear();
|
|
|
|
|
gfx_buf.draw_string(20, 14, "Beacon running");
|
2026-03-02 20:13:03 -05:00
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
// let tuned_freq = pll_freq.to_Hz() / (current_tune as u32);
|
|
|
|
|
// let tuned_freq = tuned_freq / 100;
|
|
|
|
|
let tuned_freq_mhz = si5351_dev.get_clk_freq() / 1_000_000;
|
2026-07-19 20:04:07 -04:00
|
|
|
let mut tune_str_buf = [0u8; 5];
|
2026-09-04 14:52:09 -04:00
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
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 = (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);
|
|
|
|
|
let tune_str_khz = str::from_utf8(&tune_str_buf).unwrap();
|
|
|
|
|
tune_str.push(tune_str_khz);
|
|
|
|
|
tune_str.push("MHz");
|
|
|
|
|
|
2026-07-19 20:04:07 -04:00
|
|
|
// info!("val {}, len {}", tuned_freq, len);
|
|
|
|
|
// info!("{}, {}", tune_str, tune_str.len());
|
2026-09-04 14:52:09 -04:00
|
|
|
gfx_buf.draw_string(20, 28, &tune_str);
|
2026-07-19 20:04:07 -04:00
|
|
|
gfx_buf.sprites[0].y = match gfx_buf.sprites[0].y {
|
2026-09-04 14:52:09 -04:00
|
|
|
14 => 15,
|
|
|
|
|
_ => 14,
|
2026-07-19 20:04:07 -04:00
|
|
|
};
|
|
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
// let len = u32_into_str(current_tune as u32, &mut tune_str_buf);
|
|
|
|
|
// let tune_str = str::from_utf8(&tune_str_buf[tune_str_buf.len() - len..]).unwrap();
|
|
|
|
|
// // info!("len {}", len);
|
2026-07-19 20:04:07 -04:00
|
|
|
|
2026-08-25 12:03:28 -04:00
|
|
|
// gfx_buf.draw_string(64 + 6 * (5 - len as u8), 28, tune_str);
|
2026-07-19 20:04:07 -04:00
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
gfx_buf.draw_string(20, 40, message);
|
2026-07-19 20:04:07 -04:00
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
match input_mode {
|
|
|
|
|
InputMode::DisplayOnly => (),
|
|
|
|
|
InputMode::TuneDigit(d) => {
|
|
|
|
|
let x = 64 + 6 * (4 - d) as u8;
|
|
|
|
|
gfx_buf.draw_bresenham_line(x, 36, x + 5, 36);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gfx_buf.redraw();
|
|
|
|
|
sh1106_dev.blit_framebuffer(&mut i2c, &mut gfx_buf);
|
2026-07-19 20:04:07 -04:00
|
|
|
|
2026-09-04 14:52:09 -04:00
|
|
|
// uart.write_full_blocking(b"UART example\r\n");
|
|
|
|
|
|
|
|
|
|
if !uart.uart_is_readable() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut recv_buf = [0u8; 8];
|
|
|
|
|
let len_read = match uart.read_raw(&mut recv_buf) {
|
|
|
|
|
Ok(n) => n,
|
|
|
|
|
_ => 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if len_read < 1 {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
warn!("Uart wasn't valid utf8");
|
|
|
|
|
1
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// println!("UART CMD:\n{}", uart_recv_buf.to_str());
|
|
|
|
|
|
|
|
|
|
if uart_recv_buf.contains(";") {
|
|
|
|
|
let cmd = RadioCmd::parse_cmd(&uart_recv_buf);
|
|
|
|
|
let resp = str_format!(str32, "\r\nACK CMD: {:?}\r\n", cmd);
|
|
|
|
|
println!("{}", resp.to_str());
|
|
|
|
|
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,
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let param = match len {
|
|
|
|
|
..=3 => str48::new(),
|
|
|
|
|
n => cmd.substr(2, len),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// println!(
|
|
|
|
|
// "cmd {:?}|\nlen {}|\nparam {}|\n",
|
|
|
|
|
// cmd.as_bytes(),
|
|
|
|
|
// len,
|
|
|
|
|
// param.to_str()
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
_ => Self::Unknown,
|
|
|
|
|
}
|
2026-02-09 16:24:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 23:08:16 -05:00
|
|
|
fn set_tune(
|
|
|
|
|
tune: u16,
|
2026-07-19 20:04:07 -04:00
|
|
|
pwm: &mut rp2040_hal::pwm::Slice<rp2040_hal::pwm::Pwm1, rp2040_hal::pwm::FreeRunning>,
|
2026-02-13 23:08:16 -05:00
|
|
|
) {
|
2026-08-19 14:43:07 -04:00
|
|
|
let tgt_duty = tune / 3;
|
2026-02-13 23:08:16 -05:00
|
|
|
pwm.set_top(tune - 1);
|
2026-08-19 14:43:07 -04:00
|
|
|
pwm.channel_a.set_duty(tgt_duty.max(1));
|
|
|
|
|
println!("set tune to {} with duty {}", tune, tgt_duty);
|
2026-02-13 23:08:16 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-12 21:23:35 -05:00
|
|
|
// returns the length of the final str removing leading zeroes
|
|
|
|
|
fn u32_into_str(mut value: u32, str_buf: &mut [u8]) -> usize {
|
|
|
|
|
let mut len = str_buf.len();
|
|
|
|
|
|
|
|
|
|
// info!("val {}", value);
|
|
|
|
|
for i in 0..len {
|
|
|
|
|
// info!("val {}, i {}, digit {}", value, i, value % 10);
|
|
|
|
|
str_buf[len - i - 1] = b'0' + (value % 10) as u8;
|
2026-02-09 16:24:18 -05:00
|
|
|
value /= 10;
|
2026-02-12 21:23:35 -05:00
|
|
|
|
|
|
|
|
if value <= 0 {
|
|
|
|
|
// info!("Ran out of digits at {}", i);
|
|
|
|
|
len = i + 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-02-09 16:24:18 -05:00
|
|
|
}
|
2026-02-12 21:23:35 -05:00
|
|
|
// info!("utf8 buf {}, len {}", str_buf, len);
|
|
|
|
|
|
|
|
|
|
len
|
2026-02-09 16:24:18 -05:00
|
|
|
}
|
2026-03-02 20:13:03 -05:00
|
|
|
|
|
|
|
|
#[derive(Default, Debug, PartialEq, Eq, defmt::Format)]
|
|
|
|
|
enum InputMode {
|
|
|
|
|
#[default]
|
|
|
|
|
DisplayOnly,
|
|
|
|
|
TuneDigit(u32),
|
|
|
|
|
}
|