diff --git a/src/async_io.rs b/src/async_io.rs index 8cb9344..325948e 100644 --- a/src/async_io.rs +++ b/src/async_io.rs @@ -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>>, + is_pressed: Rc>>, + presses: Rc>>, +} + +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>>>, at_response: Rc>>>, - button: Rc>>, + 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; - }) - } } diff --git a/src/main.rs b/src/main.rs index 6a567dd..d505f0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()); + }; } } diff --git a/src/state.rs b/src/state.rs index a12fe3b..a130e1d 100644 --- a/src/state.rs +++ b/src/state.rs @@ -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) { diff --git a/src/states.rs b/src/states.rs index 2e02040..8d72233 100644 --- a/src/states.rs +++ b/src/states.rs @@ -111,7 +111,7 @@ pub struct TextState { impl State for TextState { fn update(&mut self, data: &mut StateData) -> Option> { - 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> { - self.presses += data.io.get_button_presses(); + self.presses += data.io.button.get_presses(); None }