diff --git a/src/main.rs b/src/main.rs index 1dc35c3..18ce326 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use defmt::println; use defmt_rtt as _; use panic_probe as _; +use rp2040_hal::clocks::ClockSource; +use rp2040_hal::pll; use rp2040_hal::pll::PLLConfig; use rp2040_hal::pll::common_configs::PLL_USB_48MHZ; // Alias for our HAL crate @@ -73,7 +75,7 @@ fn main() -> ! { vco_freq: 1500.MHz(), refdiv: 1, post_div1: 5, - post_div2: 1, + post_div2: 4, }, &mut clocks, &mut pac.RESETS, @@ -95,6 +97,8 @@ fn main() -> ! { &mut pac.RESETS, ); + let pll_freq = pll_sys.get_freq(); + let mut led_pin = pins.gpio25.into_push_pull_output(); led_pin.set_high(); @@ -121,11 +125,11 @@ fn main() -> ! { let mut tick = 0u32; let mut last_input = 0u32; - let mut broadcast_on = false; + let mut broadcast_on = true; - let mut current_tune = 10; + let mut current_tune = 65000; - const MAX_TUNE: u16 = 350u16; + const MAX_TUNE: u16 = 65000u16; const MIN_TUNE: u16 = 4u16; set_tune(current_tune, pwm0); @@ -140,7 +144,7 @@ fn main() -> ! { &clocks.system_clock, ); - let mut delay = cortex_m::delay::Delay::new(core.SYST, 200_000); + let mut delay = cortex_m::delay::Delay::new(core.SYST, pll_freq.to_Hz()); let mut sh1106_dev = SH1106Dev::new(&mut delay, &mut i2c); sh1106_dev.set_vertical_flip(&mut i2c, true); @@ -148,14 +152,21 @@ fn main() -> ! { let mut gfx_buf = GraphicsBuf::new(); let mut last_gfx_update = 0u32; + gfx_buf.sprites[0].x = 114; + + let mut input_mode = InputMode::default(); + let mut input_mode_chg_input = pins.gpio12.into_pull_up_input(); + + println!("Booted at {}MHz", pll_freq.to_MHz()); loop { tick = tick.wrapping_add(1); if tick.wrapping_sub(last_gfx_update) > 500_000 { + last_gfx_update = tick; gfx_buf.clear(); gfx_buf.draw_string(20, 14, "Hello Radio!"); - let tuned_freq = 300_000 / (current_tune as u32); + let tuned_freq = pll_freq.to_kHz() / (current_tune as u32); let mut tune_str_buf = [0u8; 5]; let len = u32_into_str(tuned_freq, &mut tune_str_buf); let tune_str = str::from_utf8(&tune_str_buf[tune_str_buf.len() - len..]).unwrap(); @@ -163,30 +174,53 @@ fn main() -> ! { // info!("{}, {}", tune_str, tune_str.len()); gfx_buf.draw_string(20, 28, tune_str); gfx_buf.sprites[0].y = match gfx_buf.sprites[0].y { - 32 => 33, - _ => 32, + 48 => 49, + _ => 48, }; 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); - gfx_buf.draw_string(64, 28, tune_str); + gfx_buf.draw_string(64 + 6 * (5 - len as u8), 28, tune_str); match broadcast_on { true => gfx_buf.draw_string(20, 42, "ON THE AIR"), false => gfx_buf.draw_string(20, 48, "Offline"), } + 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); - last_gfx_update = tick; } - if tick.wrapping_sub(last_input) < 5_000_000 { + if tick.wrapping_sub(last_input) < 500_000 { continue; } + if input_mode_chg_input.is_low().unwrap() { + last_input = tick; + input_mode = match input_mode { + InputMode::DisplayOnly => InputMode::TuneDigit(0), + InputMode::TuneDigit(mut d) => { + d = (d + 1) % 5; + if d == 0 { + InputMode::DisplayOnly + } else { + InputMode::TuneDigit(d) + } + } + }; + info!("Input Mode is now {}", input_mode); + } + if on_off_input.is_low().unwrap() { info!("Broadcast toggle"); last_input = tick; @@ -199,18 +233,38 @@ fn main() -> ! { continue; } + if input_mode == InputMode::DisplayOnly { + continue; + } + if tune_up_input.is_low().unwrap() { - info!("Tune DOWN"); + info!("Tune UP"); last_input = tick; - current_tune = MAX_TUNE.min(current_tune + 2); + + match input_mode { + InputMode::TuneDigit(d) => { + let pow = 10u16.pow(d); + current_tune = MAX_TUNE.min(current_tune.saturating_add(pow)); + info!("Adding {}", pow); + } + InputMode::DisplayOnly => unreachable!(), + } set_tune(current_tune, pwm0); continue; } if tune_dn_input.is_low().unwrap() { - info!("Tune UP"); + info!("Tune DOWN"); last_input = tick; - current_tune = MIN_TUNE.max(current_tune - 2); + + match input_mode { + InputMode::TuneDigit(d) => { + let pow = 10u16.pow(d); + current_tune = MIN_TUNE.max(current_tune.saturating_sub(pow)); + info!("Subtracting {}", pow); + } + InputMode::DisplayOnly => unreachable!(), + } set_tune(current_tune, pwm0); } } @@ -244,3 +298,10 @@ fn u32_into_str(mut value: u32, str_buf: &mut [u8]) -> usize { len } + +#[derive(Default, Debug, PartialEq, Eq, defmt::Format)] +enum InputMode { + #[default] + DisplayOnly, + TuneDigit(u32), +}