Woyten | 30fc95b | 2019-01-10 21:09:46 +0100 | [diff] [blame] | 1 | use crate::callback::CallbackSubscription; |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 2 | use crate::callback::Consumer; |
| 3 | use crate::result::OtherError; |
Woyten | e22bc2b | 2020-01-17 14:12:47 +0100 | [diff] [blame] | 4 | use crate::result::OutOfRangeError; |
Woyten | 30fc95b | 2019-01-10 21:09:46 +0100 | [diff] [blame] | 5 | use crate::result::TockResult; |
Woyten | 30fc95b | 2019-01-10 21:09:46 +0100 | [diff] [blame] | 6 | use crate::syscalls; |
Woyten | 6cf210a | 2019-11-15 21:18:35 +0100 | [diff] [blame] | 7 | use core::marker::PhantomData; |
Woyten | 7037cf7 | 2018-01-05 12:49:17 +0100 | [diff] [blame] | 8 | |
Woyten | 9567d79 | 2018-02-04 00:11:39 +0100 | [diff] [blame] | 9 | const DRIVER_NUMBER: usize = 0x00003; |
Woyten | 7037cf7 | 2018-01-05 12:49:17 +0100 | [diff] [blame] | 10 | |
| 11 | mod command_nr { |
Woyten | 9567d79 | 2018-02-04 00:11:39 +0100 | [diff] [blame] | 12 | pub const COUNT: usize = 0; |
| 13 | pub const ENABLE_INTERRUPT: usize = 1; |
| 14 | pub const DISABLE_INTERRUPT: usize = 2; |
| 15 | pub const READ: usize = 3; |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | mod subscribe_nr { |
Woyten | 9567d79 | 2018-02-04 00:11:39 +0100 | [diff] [blame] | 19 | pub const SUBSCRIBE_CALLBACK: usize = 0; |
| 20 | } |
| 21 | |
torfmaster | 97c5e72 | 2020-01-02 21:30:16 +0100 | [diff] [blame] | 22 | #[non_exhaustive] |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 23 | pub struct ButtonsDriverFactory; |
torfmaster | d721a44 | 2019-12-27 17:42:42 +0100 | [diff] [blame] | 24 | |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 25 | impl ButtonsDriverFactory { |
| 26 | pub fn init_driver(&mut self) -> TockResult<ButtonsDriver> { |
| 27 | let buttons_driver = ButtonsDriver { |
| 28 | num_buttons: syscalls::command(DRIVER_NUMBER, command_nr::COUNT, 0, 0)?, |
| 29 | lifetime: PhantomData, |
Woyten | 22bc254 | 2019-11-15 23:18:40 +0100 | [diff] [blame] | 30 | }; |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 31 | Ok(buttons_driver) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub struct ButtonsDriver<'a> { |
| 36 | num_buttons: usize, |
| 37 | lifetime: PhantomData<&'a ()>, |
| 38 | } |
| 39 | |
| 40 | impl<'a> ButtonsDriver<'a> { |
| 41 | pub fn num_buttons(&self) -> usize { |
| 42 | self.num_buttons |
| 43 | } |
| 44 | |
Woyten | e22bc2b | 2020-01-17 14:12:47 +0100 | [diff] [blame] | 45 | /// Returns the button at 0-based index `button_num` |
| 46 | pub fn get(&self, button_num: usize) -> Result<Button, OutOfRangeError> { |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 47 | if button_num < self.num_buttons { |
Woyten | e22bc2b | 2020-01-17 14:12:47 +0100 | [diff] [blame] | 48 | Ok(Button { |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 49 | button_num, |
| 50 | lifetime: PhantomData, |
| 51 | }) |
| 52 | } else { |
Woyten | e22bc2b | 2020-01-17 14:12:47 +0100 | [diff] [blame] | 53 | Err(OutOfRangeError) |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | pub fn buttons(&self) -> Buttons { |
| 58 | Buttons { |
| 59 | num_buttons: self.num_buttons, |
| 60 | curr_button: 0, |
| 61 | lifetime: PhantomData, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub fn subscribe<CB: Fn(usize, ButtonState)>( |
| 66 | &self, |
| 67 | callback: &'a mut CB, |
| 68 | ) -> TockResult<CallbackSubscription> { |
| 69 | syscalls::subscribe::<ButtonsEventConsumer, _>( |
| 70 | DRIVER_NUMBER, |
| 71 | subscribe_nr::SUBSCRIBE_CALLBACK, |
| 72 | callback, |
| 73 | ) |
| 74 | .map_err(Into::into) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | struct ButtonsEventConsumer; |
| 79 | |
| 80 | impl<CB: Fn(usize, ButtonState)> Consumer<CB> for ButtonsEventConsumer { |
| 81 | fn consume(callback: &mut CB, button_num: usize, button_state: usize, _: usize) { |
| 82 | let button_state = match button_state { |
| 83 | 0 => ButtonState::Released, |
| 84 | 1 => ButtonState::Pressed, |
| 85 | _ => return, |
| 86 | }; |
| 87 | callback(button_num, button_state); |
Woyten | 9567d79 | 2018-02-04 00:11:39 +0100 | [diff] [blame] | 88 | } |
Woyten | 7037cf7 | 2018-01-05 12:49:17 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Woyten | 0833a04 | 2018-04-03 19:29:34 +0200 | [diff] [blame] | 91 | pub struct Buttons<'a> { |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 92 | num_buttons: usize, |
| 93 | curr_button: usize, |
| 94 | lifetime: PhantomData<&'a ()>, |
Philipp Vollmer | 617c080 | 2018-01-07 12:09:31 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 97 | impl<'a> Iterator for Buttons<'a> { |
| 98 | type Item = Button<'a>; |
| 99 | |
| 100 | fn next(&mut self) -> Option<Self::Item> { |
| 101 | if self.curr_button < self.num_buttons { |
| 102 | let item = Button { |
| 103 | button_num: self.curr_button, |
| 104 | lifetime: PhantomData, |
| 105 | }; |
| 106 | self.curr_button += 1; |
| 107 | Some(item) |
| 108 | } else { |
| 109 | None |
Woyten | d81a0c6 | 2018-01-19 21:44:38 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 112 | } |
Philipp Vollmer | 617c080 | 2018-01-07 12:09:31 +0100 | [diff] [blame] | 113 | |
Woyten | ee5de05 | 2019-11-12 14:15:22 +0100 | [diff] [blame] | 114 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 115 | pub enum ButtonState { |
| 116 | Pressed, |
| 117 | Released, |
| 118 | } |
| 119 | |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 120 | impl From<ButtonState> for bool { |
| 121 | fn from(button_state: ButtonState) -> Self { |
| 122 | match button_state { |
| 123 | ButtonState::Released => false, |
| 124 | ButtonState::Pressed => true, |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 129 | pub struct Button<'a> { |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 130 | button_num: usize, |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 131 | lifetime: PhantomData<&'a ()>, |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 134 | impl<'a> Button<'a> { |
| 135 | pub fn button_num(&self) -> usize { |
| 136 | self.button_num |
| 137 | } |
| 138 | |
| 139 | pub fn read(&self) -> TockResult<ButtonState> { |
| 140 | let button_state = syscalls::command(DRIVER_NUMBER, command_nr::READ, self.button_num, 0)?; |
| 141 | match button_state { |
| 142 | 0 => Ok(ButtonState::Released), |
| 143 | 1 => Ok(ButtonState::Pressed), |
| 144 | _ => Err(OtherError::ButtonsDriverInvalidState.into()), |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | pub fn enable_interrupt(&self) -> TockResult<()> { |
Woyten | 22bc254 | 2019-11-15 23:18:40 +0100 | [diff] [blame] | 149 | syscalls::command( |
Woyten | 4f5bc41 | 2019-11-21 23:36:58 +0100 | [diff] [blame] | 150 | DRIVER_NUMBER, |
| 151 | command_nr::ENABLE_INTERRUPT, |
| 152 | self.button_num, |
| 153 | 0, |
Woyten | 22bc254 | 2019-11-15 23:18:40 +0100 | [diff] [blame] | 154 | )?; |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 155 | Ok(()) |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 158 | pub fn disable_interrupt(&self) -> TockResult<()> { |
Woyten | 22bc254 | 2019-11-15 23:18:40 +0100 | [diff] [blame] | 159 | syscalls::command( |
Woyten | 4f5bc41 | 2019-11-21 23:36:58 +0100 | [diff] [blame] | 160 | DRIVER_NUMBER, |
| 161 | command_nr::DISABLE_INTERRUPT, |
| 162 | self.button_num, |
| 163 | 0, |
Woyten | 22bc254 | 2019-11-15 23:18:40 +0100 | [diff] [blame] | 164 | )?; |
| 165 | Ok(()) |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 166 | } |
| 167 | } |