diff options
Diffstat (limited to 'sw/picardy/src/main.rs')
-rw-r--r-- | sw/picardy/src/main.rs | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/sw/picardy/src/main.rs b/sw/picardy/src/main.rs index a6a3e4a..22b894b 100644 --- a/sw/picardy/src/main.rs +++ b/sw/picardy/src/main.rs @@ -292,14 +292,8 @@ fn main() -> ! { } previous_vfo = vfo; - let s_meter_value: u16 = adc2.read(&mut s_meter).unwrap(); - - // Avoid 0 because of log10 - let s_meter_value = f32::from(if s_meter_value == 0 { 1 } else { s_meter_value }); - - // ADC is 12-bit, convert to dB full-scale - let s_meter_value = 10f32 * log10f::log10f(f32::from(s_meter_value) / 4092f32); - let s_meter_value = s_meter_value as i32; + let s_meter_adc_value: u16 = adc2.read(&mut s_meter).unwrap(); + let s_meter_value = s_meter_from_adc(s_meter_adc_value); let t_now = ticks_now(); if last_s_meter_update_time + 10 < t_now { update_disp_required = s_meter_value != last_s_meter_value; @@ -322,6 +316,33 @@ fn main() -> ! { } } +fn s_meter_from_adc(adc : u16) -> u8 { + // Avoid 0 because of log10 + let adc = f32::from(if adc == 0 { 1 } else { adc }); + // ADC is 12-bit, convert to dB full-scale + let adc_db = 10f32 * log10f::log10f(adc / 4092f32); + + /* Hand-calibrated lookup table */ + if adc_db <= -35f32 { + 1 + } + else if adc_db <= -20f32 { + 4 + } + else if adc_db <= -12f32 { + 5 + } + else if adc_db <= -8f32 { + 6 + } + else if adc_db <= -7f32 { + 7 + } + else { + 9 + } +} + #[interrupt] fn TIM2() { let timer = unsafe { &mut *CLOCK_TIMER.as_mut_ptr() }; |