diff options
Diffstat (limited to 'sw/picardy/src/main.rs')
-rw-r--r-- | sw/picardy/src/main.rs | 196 |
1 files changed, 19 insertions, 177 deletions
diff --git a/sw/picardy/src/main.rs b/sw/picardy/src/main.rs index dc8d0ef..ed9d799 100644 --- a/sw/picardy/src/main.rs +++ b/sw/picardy/src/main.rs @@ -26,7 +26,6 @@ #![no_std] use core::mem::MaybeUninit; -use core::fmt::Write; use cortex_m_rt::ExceptionFrame; use cortex_m_semihosting::hprintln; use panic_semihosting as _; @@ -42,147 +41,22 @@ use stm32f1xx_hal::{ qei::QeiOptions, }; -use embedded_hal::digital::v2::{OutputPin}; +use embedded_hal::digital::v2::OutputPin; use hd44780_driver::{Cursor, CursorBlink, Display, DisplayMode, HD44780}; pub mod ui; +pub mod state; pub mod si_clock; +use state::*; + static mut TIMER1: MaybeUninit<CountDownTimer<pac::TIM1>> = MaybeUninit::uninit(); static mut DECISECONDS_COUNTER: MaybeUninit<usize> = MaybeUninit::uninit(); -enum Mode { - VFO, - BFO, -} - -enum TuneSpeed { - Slow, - Mid, - Fast -} - -enum FilterShift { - LSB, - USB, - Custom(u32), -} - -const VHF_BAND_EDGE : u32 = 144_000_000; -const VHF_LO : u32 = 114_286_000; -const BFO_LSB : u32 = 4_915_940; -const BFO_USB : u32 = 4_914_910; - -#[derive(PartialEq, Eq)] -enum SequenceState { - Rx, - ToTxStep1, - ToTxStep2, - Tx, - ToRxStep1, - ToRxStep2, -} - -struct State { - mode : Mode, - filter_shift : FilterShift, - bfo_tune_fail : bool, - vhf_qrg : u32, - tune_speed : TuneSpeed, - - sequence_state : SequenceState, - last_sequence_state_change : usize, -} - -impl State { - fn bfo(&self) -> u32 { - match self.filter_shift { - FilterShift::LSB => BFO_LSB, - FilterShift::USB => BFO_USB, - FilterShift::Custom(fs) => fs, - } - } - - fn if_qrg(&self) -> u32 { - self.vhf_qrg - VHF_LO - } - - fn vfo(&self) -> u32 { - self.if_qrg() - self.bfo() - } - - fn vfo_incr(&self) -> i32 { - match self.tune_speed { - TuneSpeed::Slow => 10, - TuneSpeed::Mid => 200, - TuneSpeed::Fast => 1000, - } - } - - fn bfo_incr(&self) -> i32 { - match self.tune_speed { - TuneSpeed::Slow => 10, - TuneSpeed::Mid => 50, - TuneSpeed::Fast => 100, - } - } -} - -fn update_disp<T: hd44780_driver::bus::DataBus>(lcd: &mut HD44780<T>, state: &State, delay: &mut Delay) -{ - let mut string = arrayvec::ArrayString::<[_; 16]>::new(); - - match (state.bfo_tune_fail, &state.mode) { - (true, _) => write!(string, "!").unwrap(), - (false, Mode::BFO) => write!(string, ">").unwrap(), - (false, Mode::VFO) => write!(string, " ").unwrap(), - } - write!(string, "{:<10}", state.bfo()).unwrap(); - - match state.filter_shift { - FilterShift::USB => write!(string, "U").unwrap(), - FilterShift::LSB => write!(string, "L").unwrap(), - FilterShift::Custom(_) => write!(string, " ").unwrap(), - } - - match state.sequence_state { - SequenceState::Rx => write!(string, " R").unwrap(), - SequenceState::Tx => write!(string, " T").unwrap(), - _ => write!(string, " X").unwrap(), - } - - lcd.set_cursor_pos(0, delay).unwrap(); - lcd.write_str(&string, delay).unwrap(); - - string.clear(); - /* Shorten the QRG to avoid using three digits for nothing */ - let disp_freq = (state.vhf_qrg as i32) - (VHF_BAND_EDGE as i32); - match state.mode { - Mode::BFO => write!(string, " .{:<06} ", disp_freq).unwrap(), - Mode::VFO => write!(string, ">.{:<06} ", disp_freq).unwrap(), - } - - match state.tune_speed { - TuneSpeed::Slow => write!(string, "S").unwrap(), - TuneSpeed::Mid => write!(string, "M").unwrap(), - TuneSpeed::Fast => write!(string, "F").unwrap(), - } - - lcd.set_cursor_pos(40, delay).unwrap(); - lcd.write_str(&string, delay).unwrap(); -} #[cortex_m_rt::entry] fn main() -> ! { - let mut state = State { - mode : Mode::VFO, - filter_shift : FilterShift::USB, - bfo_tune_fail : false, - vhf_qrg : VHF_BAND_EDGE, - tune_speed : TuneSpeed::Mid, - sequence_state : SequenceState::Rx, - last_sequence_state_change : 0, - }; + let mut state = State::new(); let cp = cortex_m::Peripherals::take().unwrap(); let dp = pac::Peripherals::take().unwrap(); @@ -267,8 +141,8 @@ fn main() -> ! { }, &mut delay).unwrap(); lcd.set_cursor_pos(0, &mut delay).unwrap(); - lcd.write_str("Hello, world!", &mut delay).unwrap(); - + lcd.write_str("HB9EGM", &mut delay).unwrap(); + delay.delay_ms(200u8); // Configure I2C1 to be used for Si5351 let scl = gpiob.pb6.into_alternate_open_drain(&mut gpiob.crl); @@ -292,7 +166,7 @@ fn main() -> ! { let mut siclock = si_clock::SiClock::new(i2c_busmanager.acquire_i2c(), state.bfo(), state.vfo()); - update_disp(&mut lcd, &state, &mut delay); + ui::update_disp(&mut lcd, &state, &mut delay); let mut last_encoder_count = qei.count(); @@ -309,62 +183,30 @@ fn main() -> ! { loop { let mut update_disp_required = false; - let encoder_count = qei.count(); + let encoder_count : u16 = qei.count(); if encoder_count != last_encoder_count { let delta = encoder_count.wrapping_sub(last_encoder_count); let delta = if delta > 0x7FFF { delta as i32 - 0x10000 } else { delta as i32 }; - - match state.mode { - Mode::VFO => { - state.vhf_qrg = (state.vhf_qrg as i32 + delta * state.vfo_incr()) as u32; - }, - Mode::BFO => { - let new_bfo = (state.bfo() as i32 + delta * state.bfo_incr()) as u32; - state.filter_shift = FilterShift::Custom(new_bfo); - state.bfo_tune_fail = !siclock.set_bfo(state.bfo()).is_ok(); - }, + let require_bfo_update = ui.update_encoder(&mut state, delta); + if require_bfo_update { + state.bfo_tune_fail = !siclock.set_bfo(state.bfo()).is_ok(); } - siclock.set_vfo(state.vfo()); - update_disp_required = true; } - let button_state = ui.read_buttons(); - - if button_state.a { - state.mode = Mode::BFO; - update_disp_required = true; - } - else if button_state.b { - state.mode = Mode::VFO; - update_disp_required = true; - } + let button_result = ui.handle_buttons(&mut state); - if button_state.f { - state.filter_shift = match state.filter_shift { - FilterShift::USB => FilterShift::LSB, - FilterShift::LSB => FilterShift::USB, - FilterShift::Custom(_) => FilterShift::USB, - }; + if button_result.bfo_update { state.bfo_tune_fail = !siclock.set_bfo(state.bfo()).is_ok(); - update_disp_required = true; } - - if button_state.g { - state.tune_speed = match state.tune_speed { - TuneSpeed::Slow => TuneSpeed::Mid, - TuneSpeed::Mid => TuneSpeed::Fast, - TuneSpeed::Fast => TuneSpeed::Slow, - }; - update_disp_required = true; - } + update_disp_required |= button_result.display_update; let next_state = match state.sequence_state { SequenceState::Rx => { led.set_high().unwrap(); - if button_state.ptt { + if button_result.ptt { mute_spkr.set_high().unwrap(); mute_micn.set_high().unwrap(); seq2_switch.set_high().unwrap(); @@ -388,7 +230,7 @@ fn main() -> ! { }, SequenceState::Tx => { led.set_low().unwrap(); - if button_state.ptt { + if button_result.ptt { SequenceState::Tx } else { @@ -417,8 +259,8 @@ fn main() -> ! { } if update_disp_required { - update_disp(&mut lcd, &state, &mut delay) - }; + ui::update_disp(&mut lcd, &state, &mut delay); + } last_encoder_count = encoder_count; |