diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-04-27 18:23:36 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-04-27 18:23:36 +0000 |
| commit | c19de2984751ba6fa2972ee66cfa2a6310d5f0c1 (patch) | |
| tree | 9ba8fa01994be9ef43af404cb4399854e8c6de22 /src/events.rs | |
| parent | f4bfda345d3d18232926a87b10333a2e624f4d04 (diff) | |
| parent | 9e96655757180d7fe32ebff1ed93a35a4c3cff28 (diff) | |
Merge pull request #63 from kbleeke/generalize-events
rework event handling to allow sending data to `Control`
Diffstat (limited to 'src/events.rs')
| -rw-r--r-- | src/events.rs | 111 |
1 files changed, 106 insertions, 5 deletions
diff --git a/src/events.rs b/src/events.rs index 87f6c01a3..d6f114ed9 100644 --- a/src/events.rs +++ b/src/events.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #![allow(unused)] | 1 | #![allow(unused)] |
| 2 | #![allow(non_camel_case_types)] | 2 | #![allow(non_camel_case_types)] |
| 3 | 3 | ||
| 4 | use core::num; | 4 | use core::cell::RefCell; |
| 5 | 5 | ||
| 6 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; | 6 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; |
| 7 | use embassy_sync::pubsub::{PubSubChannel, Publisher, Subscriber}; | 7 | use embassy_sync::pubsub::{PubSubChannel, Publisher, Subscriber}; |
| @@ -284,13 +284,114 @@ pub enum Event { | |||
| 284 | LAST = 190, | 284 | LAST = 190, |
| 285 | } | 285 | } |
| 286 | 286 | ||
| 287 | pub type EventQueue = PubSubChannel<NoopRawMutex, EventStatus, 2, 1, 1>; | 287 | // TODO this PubSub can probably be replaced with shared memory to make it a bit more efficient. |
| 288 | pub type EventPublisher<'a> = Publisher<'a, NoopRawMutex, EventStatus, 2, 1, 1>; | 288 | pub type EventQueue = PubSubChannel<NoopRawMutex, Message, 2, 1, 1>; |
| 289 | pub type EventSubscriber<'a> = Subscriber<'a, NoopRawMutex, EventStatus, 2, 1, 1>; | 289 | pub type EventPublisher<'a> = Publisher<'a, NoopRawMutex, Message, 2, 1, 1>; |
| 290 | pub type EventSubscriber<'a> = Subscriber<'a, NoopRawMutex, Message, 2, 1, 1>; | ||
| 291 | |||
| 292 | pub struct Events { | ||
| 293 | pub queue: EventQueue, | ||
| 294 | pub mask: SharedEventMask, | ||
| 295 | } | ||
| 296 | |||
| 297 | impl Events { | ||
| 298 | pub fn new() -> Self { | ||
| 299 | Self { | ||
| 300 | queue: EventQueue::new(), | ||
| 301 | mask: SharedEventMask::default(), | ||
| 302 | } | ||
| 303 | } | ||
| 304 | } | ||
| 290 | 305 | ||
| 291 | #[derive(Clone, Copy)] | 306 | #[derive(Clone, Copy)] |
| 292 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 307 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 293 | pub struct EventStatus { | 308 | pub struct Status { |
| 294 | pub event_type: Event, | 309 | pub event_type: Event, |
| 295 | pub status: u32, | 310 | pub status: u32, |
| 296 | } | 311 | } |
| 312 | |||
| 313 | #[derive(Clone, Copy)] | ||
| 314 | pub enum Payload { | ||
| 315 | None, | ||
| 316 | } | ||
| 317 | |||
| 318 | #[derive(Clone, Copy)] | ||
| 319 | |||
| 320 | pub struct Message { | ||
| 321 | pub header: Status, | ||
| 322 | pub payload: Payload, | ||
| 323 | } | ||
| 324 | |||
| 325 | impl Message { | ||
| 326 | pub fn new(status: Status, payload: Payload) -> Self { | ||
| 327 | Self { | ||
| 328 | header: status, | ||
| 329 | payload, | ||
| 330 | } | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | #[derive(Default)] | ||
| 335 | struct EventMask { | ||
| 336 | mask: [u32; Self::WORD_COUNT], | ||
| 337 | } | ||
| 338 | |||
| 339 | impl EventMask { | ||
| 340 | const WORD_COUNT: usize = ((Event::LAST as u32 + (u32::BITS - 1)) / u32::BITS) as usize; | ||
| 341 | |||
| 342 | fn enable(&mut self, event: Event) { | ||
| 343 | let n = event as u32; | ||
| 344 | let word = n / u32::BITS; | ||
| 345 | let bit = n % u32::BITS; | ||
| 346 | |||
| 347 | self.mask[word as usize] |= (1 << bit); | ||
| 348 | } | ||
| 349 | |||
| 350 | fn disable(&mut self, event: Event) { | ||
| 351 | let n = event as u32; | ||
| 352 | let word = n / u32::BITS; | ||
| 353 | let bit = n % u32::BITS; | ||
| 354 | |||
| 355 | self.mask[word as usize] &= !(1 << bit); | ||
| 356 | } | ||
| 357 | |||
| 358 | fn is_enabled(&self, event: Event) -> bool { | ||
| 359 | let n = event as u32; | ||
| 360 | let word = n / u32::BITS; | ||
| 361 | let bit = n % u32::BITS; | ||
| 362 | |||
| 363 | self.mask[word as usize] & (1 << bit) > 0 | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | #[derive(Default)] | ||
| 368 | |||
| 369 | pub struct SharedEventMask { | ||
| 370 | mask: RefCell<EventMask>, | ||
| 371 | } | ||
| 372 | |||
| 373 | impl SharedEventMask { | ||
| 374 | pub fn enable(&self, events: &[Event]) { | ||
| 375 | let mut mask = self.mask.borrow_mut(); | ||
| 376 | for event in events { | ||
| 377 | mask.enable(*event); | ||
| 378 | } | ||
| 379 | } | ||
| 380 | |||
| 381 | pub fn disable(&self, events: &[Event]) { | ||
| 382 | let mut mask = self.mask.borrow_mut(); | ||
| 383 | for event in events { | ||
| 384 | mask.disable(*event); | ||
| 385 | } | ||
| 386 | } | ||
| 387 | |||
| 388 | pub fn disable_all(&self) { | ||
| 389 | let mut mask = self.mask.borrow_mut(); | ||
| 390 | mask.mask = Default::default(); | ||
| 391 | } | ||
| 392 | |||
| 393 | pub fn is_enabled(&self, event: Event) -> bool { | ||
| 394 | let mask = self.mask.borrow(); | ||
| 395 | mask.is_enabled(event) | ||
| 396 | } | ||
| 397 | } | ||
