Clean up button

This commit is contained in:
Sofia 2026-05-17 19:50:46 +03:00
parent db6270d262
commit 4c79f79638
4 changed files with 51 additions and 35 deletions

View File

@ -25,18 +25,57 @@ pub enum SMSFormat {
TextMode = 1,
}
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct Button {
was_pressed: bool,
is_pressed: bool,
presses: u32,
was_pressed: Rc<Mutex<RefCell<bool>>>,
is_pressed: Rc<Mutex<RefCell<bool>>>,
presses: Rc<Mutex<RefCell<u32>>>,
}
impl Default for Button {
fn default() -> Self {
Self {
was_pressed: Rc::new(Mutex::new(RefCell::new(Default::default()))),
is_pressed: Rc::new(Mutex::new(RefCell::new(Default::default()))),
presses: Rc::new(Mutex::new(RefCell::new(Default::default()))),
}
}
}
impl Button {
pub unsafe fn set_pressed(&self, pressed: bool) {
critical_section::with(|cs| {
let mut was_pressed = self.was_pressed.borrow_ref_mut(cs);
let mut is_pressed = self.is_pressed.borrow_ref_mut(cs);
let mut presses = self.presses.borrow_ref_mut(cs);
*was_pressed = *is_pressed;
*is_pressed = pressed;
if !*was_pressed && *is_pressed {
*presses += 1;
}
})
}
pub fn get_presses(&self) -> u32 {
critical_section::with(|cs| {
let presses = self.presses.borrow_ref(cs);
*presses
})
}
pub fn clear(&self) {
critical_section::with(|cs| {
let mut presses = self.presses.borrow_ref_mut(cs);
*presses = 0;
})
}
}
#[derive(Clone)]
pub struct AsyncIO {
at_command: Rc<Mutex<RefCell<Option<ATCommand>>>>,
at_response: Rc<Mutex<RefCell<Option<String>>>>,
button: Rc<Mutex<RefCell<Button>>>,
pub button: Button,
}
impl Default for AsyncIO {
@ -44,7 +83,7 @@ impl Default for AsyncIO {
Self {
at_command: Rc::new(Mutex::new(RefCell::new(None))),
at_response: Rc::new(Mutex::new(RefCell::new(None))),
button: Rc::new(Mutex::new(RefCell::new(Default::default()))),
button: Default::default(),
}
}
}
@ -95,29 +134,4 @@ impl AsyncIO {
None
})
}
pub unsafe fn set_button_pressed(&self, pressed: bool) {
critical_section::with(|cs| {
let mut button = self.button.borrow_ref_mut(cs);
button.was_pressed = button.is_pressed;
button.is_pressed = pressed;
if !button.was_pressed && button.is_pressed {
button.presses += 1;
}
})
}
pub fn get_button_presses(&self) -> u32 {
critical_section::with(|cs| {
let button = self.button.borrow_ref(cs);
button.presses
})
}
pub fn clear_button_presses(&self) {
critical_section::with(|cs| {
let mut button = self.button.borrow_ref_mut(cs);
button.presses = 0;
})
}
}

View File

@ -237,6 +237,8 @@ fn thread_2_main(async_io: AsyncIO, mut at_commands: ATCommands<'static, 'static
unsafe { async_io.set_at_response(response) };
}
unsafe { async_io.set_button_pressed(button.is_low()) };
unsafe {
async_io.button.set_pressed(button.is_low());
};
}
}

View File

@ -82,7 +82,7 @@ impl<'a> StateManager<'a> {
Some(next_state) => self.curr_state = next_state,
None => {}
}
self.data.io.clear_button_presses();
self.data.io.button.clear();
}
pub fn draw(&mut self) {

View File

@ -111,7 +111,7 @@ pub struct TextState {
impl State for TextState {
fn update(&mut self, data: &mut StateData) -> Option<Box<dyn State>> {
if data.io.get_button_presses() > 0 {
if data.io.button.get_presses() > 0 {
Some(Box::new(ButtonTestState { presses: 0 }))
} else {
None
@ -139,7 +139,7 @@ pub struct ButtonTestState {
impl State for ButtonTestState {
fn update(&mut self, data: &mut StateData) -> Option<Box<dyn State>> {
self.presses += data.io.get_button_presses();
self.presses += data.io.button.get_presses();
None
}