From e1ea7ade49ad3f8d7a8667245f96db4773246f4f Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 18 Dec 2020 11:50:43 +0100 Subject: Simplify buttons input --- kicad/control.sch | 2 +- sw/picardy/src/main.rs | 4 +- sw/picardy/src/ui.rs | 117 ++++++++++--------------------------------------- sw/pio.txt | 6 +-- 4 files changed, 28 insertions(+), 101 deletions(-) diff --git a/kicad/control.sch b/kicad/control.sch index e4c2a32..e6f453a 100644 --- a/kicad/control.sch +++ b/kicad/control.sch @@ -80,7 +80,7 @@ Wire Wire Line Text HLabel 6500 7100 2 50 Output ~ 0 CW_TONE Text Notes 650 2600 0 50 ~ 0 -LCD: use a 5V character display, and Adafruit 292 backpack with 3V3 I2C.\nButtons 0 and 1: floating when open, connect to GND through resistors\nwhen pressed for multiplexing several buttons.\nButtons 2 and 3: floating when open, connect to GND when pressed\nCoder: connect as in datasheet PEC12R +LCD: use a 5V character display, and Adafruit 292 backpack with 3V3 I2C.\n\nButtons: floating when open, connect to GND when pressed\n(Buttons 0 and 1 were previously floating when open, connect to GND\nthrough resistors when pressed for multiplexing several buttons, but that\nmakes it impossible to use interrupts, and I didn't need more than 4\nbuttons in the end.)\n\nCoder: connect as in datasheet PEC12R $Comp L mpb:CUI-SJ-3524-SMT-TR J202 U 1 1 5EF19C71 diff --git a/sw/picardy/src/main.rs b/sw/picardy/src/main.rs index ba2452d..8fe0ada 100644 --- a/sw/picardy/src/main.rs +++ b/sw/picardy/src/main.rs @@ -83,8 +83,8 @@ fn main() -> ! { // Buttons as analog inputs (multi-level) let mic_sw1 = gpioa.pa3.into_analog(&mut gpioa.crl); let mic_sw2 = gpioa.pa4.into_analog(&mut gpioa.crl); - let pb0 = gpiob.pb0.into_analog(&mut gpiob.crl); // BTN1 Buttons B D C - let pb1 = gpiob.pb1.into_analog(&mut gpiob.crl); // BTN0 Buttons A F + let pb0 = gpiob.pb0.into_floating_input(&mut gpiob.crl); // BTN1 Button B, has external pullup + let pb1 = gpiob.pb1.into_floating_input(&mut gpiob.crl); // BTN0 Button A, has external pullup let pb12 = gpiob.pb12.into_pull_up_input(&mut gpiob.crh); // BTN2 Button E let pb13 = gpiob.pb13.into_pull_up_input(&mut gpiob.crh); // BTN3 Button G let pc15 = gpioc.pc15.into_pull_up_input(&mut gpioc.crh); diff --git a/sw/picardy/src/ui.rs b/sw/picardy/src/ui.rs index e3f172e..0d42f88 100644 --- a/sw/picardy/src/ui.rs +++ b/sw/picardy/src/ui.rs @@ -36,34 +36,18 @@ use stm32f1xx_hal::{ gpio::gpioa::*, gpio::gpiob::*, gpio::gpioc::*, - gpio::{Analog, Input, PullUp}, + gpio::{Analog, Input, PullUp, Floating}, }; use embedded_hal::digital::v2::InputPin; use hd44780_driver::HD44780; -#[derive(PartialEq, Eq, Clone, Copy)] -enum Btn0Buttons { - B, - C, - D, -} - -#[derive(PartialEq, Eq, Clone, Copy)] -enum Btn1Buttons { - A, - F, -} - #[derive(PartialEq, Eq, Clone, Copy)] struct ButtonState { pub a : bool, pub b : bool, pub c : bool, pub d : bool, - pub e : bool, - pub f : bool, - pub g : bool, pub enc : bool, pub ptt : bool, } @@ -75,9 +59,6 @@ impl ButtonState { b : !old_state.b && self.b, c : !old_state.c && self.c, d : !old_state.d && self.d, - e : !old_state.e && self.e, - f : !old_state.f && self.f, - g : !old_state.g && self.g, enc : !old_state.enc && self.enc, ptt : self.ptt, // Don't do edge detection for PTT! } @@ -91,9 +72,6 @@ impl Default for ButtonState { b : false, c : false, d : false, - e : false, - f : false, - g : false, enc : false, ptt : false, } @@ -102,14 +80,11 @@ impl Default for ButtonState { impl fmt::Display for ButtonState { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}{}{}{}{}{}{}{}{}", + write!(f, "{}{}{}{}{}{}", if self.a { "A" } else { "a" }, if self.b { "B" } else { "b" }, if self.c { "C" } else { "c" }, if self.d { "D" } else { "d" }, - if self.e { "E" } else { "e" }, - if self.f { "F" } else { "f" }, - if self.g { "G" } else { "g" }, if self.enc { "X" } else { "x" }, if self.ptt { "P" } else { "p" }) } @@ -123,11 +98,8 @@ pub struct ButtonResult { } pub struct UI { - btn0_hist : [u16; 3], - btn1_hist : [u16; 3], - - btn0_ch : PB1, - btn1_ch : PB0, + btn0 : PB1>, + btn1 : PB0>, mic_sw1 : PA3, _mic_sw2 : PA4, @@ -143,15 +115,23 @@ pub struct UI { } impl UI { - pub fn new(mic_sw1: PA3, mic_sw2: PA4, pb0: PB0, pb1: PB1, adc1: ADC1, mut apb2: &mut APB2, clocks: &Clocks, pb12: PB12>, pb13: PB13>, pc15 : PC15>) -> UI { + pub fn new( + mic_sw1: PA3, + mic_sw2: PA4, + pb0: PB0>, + pb1: PB1>, + adc1: ADC1, + mut apb2: &mut APB2, + clocks: &Clocks, + pb12: PB12>, + pb13: PB13>, + pc15 : PC15>) -> UI { let adc1 = adc::Adc::adc1(adc1, &mut apb2, *clocks); UI { - btn0_hist : [4095; 3], - btn1_hist : [4095; 3], - btn0_ch : pb1, - btn1_ch : pb0, + btn0 : pb1, + btn1 : pb0, btn2 : pb12, btn3 : pb13, btn_enc : pc15, @@ -165,75 +145,24 @@ impl UI { fn read_buttons(&mut self) -> ButtonState { let mut buttons = ButtonState::default(); - // Debounce BTN0 and BTN1 - let btn0_value: u16 = self.adc.read(&mut self.btn0_ch).unwrap(); - self.btn0_hist[2] = self.btn0_hist[1]; - self.btn0_hist[1] = self.btn0_hist[0]; - self.btn0_hist[0] = btn0_value; - - let btn1_value: u16 = self.adc.read(&mut self.btn1_ch).unwrap(); - self.btn1_hist[2] = self.btn1_hist[1]; - self.btn1_hist[1] = self.btn1_hist[0]; - self.btn1_hist[0] = btn1_value; - let mic_sw1_value: u16 = self.adc.read(&mut self.mic_sw1).unwrap(); //let mic_sw2_value: u16 = self.adc.read(&mut self.mic_sw2).unwrap(); buttons.ptt = mic_sw1_value < 500; - let mut btn0 = [None; 3]; - for (i, &v) in self.btn0_hist.iter().enumerate() { - btn0[i] = - if v > 3050 { - None - } - else if v > 675 { - Some(Btn1Buttons::F) - } - else { - Some(Btn1Buttons::A) - }; - } - - if btn0.iter().all(|&v| v != None && v == btn0[0]) { - match btn0[0] { - None => {}, - Some(Btn1Buttons::A) => buttons.a = true, - Some(Btn1Buttons::F) => buttons.f = true, - } - } - - let mut btn1 = [None; 3]; - for (i, &v) in self.btn1_hist.iter().enumerate() { - btn1[i] = - if v > 3050 { - None - } - else if v > 1650 { - Some(Btn0Buttons::B) - } - else if v > 675 { - Some(Btn0Buttons::C) - } - else { - Some(Btn0Buttons::D) - }; + if self.btn0.is_low().unwrap() { + buttons.a = true; } - if btn1.iter().all(|&v| v != None && v == btn1[0]) { - match btn1[0] { - None => {}, - Some(Btn0Buttons::B) => buttons.b = true, - Some(Btn0Buttons::C) => buttons.c = true, - Some(Btn0Buttons::D) => buttons.d = true, - } + if self.btn1.is_low().unwrap() { + buttons.b = true; } if self.btn2.is_low().unwrap() { - buttons.e = true; + buttons.c = true; } if self.btn3.is_low().unwrap() { - buttons.g = true; + buttons.d = true; } if self.btn_enc.is_low().unwrap() { diff --git a/sw/pio.txt b/sw/pio.txt index 1ca2f18..626404a 100644 --- a/sw/pio.txt +++ b/sw/pio.txt @@ -4,14 +4,12 @@ Pin mapping: see datasheet Table 5 ## GPIO inputs -Analog multi-level button inputs - * BTN0 PB1 ADC12_IN9 - * BTN1 PB0 ADC12_IN8 - Analog RX AGC measurement (S-Meter) * PA5 Digital buttons + * BTN0 PB1 + * BTN1 PB0 * BTN2 PB12 * BTN3 PB13 -- cgit v1.2.3