Skip to main content

device_envoy/button/
button_watch_generated.rs

1// @generated - manually created as documentation example. Do not auto-generate.
2//! Module containing [`ButtonWatchGenerated`], the sample struct type generated by the
3//! [`button_watch!`](crate::button_watch!) macro.
4//!
5//! This file shows what the macro expansion looks like for documentation purposes.
6
7#[cfg(all(not(doc), not(feature = "host")))]
8use crate::button_watch;
9
10#[cfg(all(not(doc), not(feature = "host")))]
11button_watch! {
12    pub ButtonWatchGenerated {
13        pin: PIN_13,
14    }
15}
16
17#[cfg(doc)]
18/// Sample struct type generated by the [`button_watch!`](crate::button_watch!) macro, showing all methods.
19///
20/// This page serves as the definitive reference for what a generated button watch type
21/// provides. For first-time readers, start with the examples in the [`button_watch!`](crate::button_watch!) macro
22/// documentation, then return here for a complete list of available methods.
23pub struct ButtonWatchGenerated {
24    button_watch: super::ButtonWatch,
25}
26
27#[cfg(doc)]
28impl ButtonWatchGenerated {
29    /// Creates a new button monitor and spawns its background task.
30    ///
31    /// # Parameters
32    ///
33    /// - `pin`: GPIO pin for the button
34    /// - `pressed_to`: How the button is wired ([`PressedTo::Ground`](crate::button::PressedTo::Ground) or [`PressedTo::Voltage`](crate::button::PressedTo::Voltage))
35    /// - `spawner`: Task spawner for background operations
36    ///
37    /// # Errors
38    ///
39    /// Returns an error if the background task cannot be spawned.
40    ///
41    /// # Example
42    ///
43    /// See the [`button_watch!`](crate::button_watch!) macro for usage.
44    pub fn new(
45        pin: impl Into<embassy_rp::Peri<'static, embassy_rp::peripherals::PIN_13>>,
46        pressed_to: crate::button::PressedTo,
47        spawner: embassy_executor::Spawner,
48    ) -> crate::Result<&'static Self> {
49        static INSTANCE: ButtonWatchGenerated = ButtonWatchGenerated {
50            button_watch: super::ButtonWatch {
51                signal: &embassy_sync::signal::Signal::new(),
52            },
53        };
54        let _ = (pin, pressed_to, spawner);
55        Ok(&INSTANCE)
56    }
57
58    /// A device abstraction for buttons that uses a background task to monitor presses.
59    ///
60    /// This is useful for converting a `Button` returned from `WifiAuto::connect()`
61    /// into a `ButtonWatch` for background monitoring.
62    ///
63    /// # Parameters
64    ///
65    /// - `button`: An existing button (e.g., from `WifiAuto::connect()`)
66    /// - `spawner`: Task spawner for background operations
67    ///
68    /// # Errors
69    ///
70    /// Returns an error if the background task cannot be spawned.
71    pub fn from_button(
72        button: crate::button::Button<'static>,
73        spawner: embassy_executor::Spawner,
74    ) -> crate::Result<&'static Self> {
75        static INSTANCE: ButtonWatchGenerated = ButtonWatchGenerated {
76            button_watch: super::ButtonWatch {
77                signal: &embassy_sync::signal::Signal::new(),
78            },
79        };
80        let _ = (button, spawner);
81        Ok(&INSTANCE)
82    }
83}
84
85#[cfg(doc)]
86impl core::ops::Deref for ButtonWatchGenerated {
87    type Target = super::ButtonWatch;
88
89    fn deref(&self) -> &Self::Target {
90        &self.button_watch
91    }
92}