Skip to main content

device_envoy_esp/cyd/
text.rs

1//! Convenience text rendering for [`CydFrameEsp`].
2//!
3//! This mirrors the device-envoy `led2d` text helpers: the device owns a single
4//! default style (background, foreground, font) and [`CydFrameEsp::write_text`]
5//! drops a line of text into a frame using that default, without repeating the
6//! [`Text`] / [`MonoTextStyle`] / [`Baseline`] boilerplate each time. Combined
7//! with per-rectangle frames (see [`super::CydDisplay::frame_mut`]), this lets each status or time
8//! message own its own area and be drawn in one call.
9//!
10//! There is intentionally exactly one convenience method. For a different font,
11//! color, alignment, or baseline, draw with embedded-graphics directly against
12//! the frame — that is the escape hatch.
13
14use embedded_graphics::{
15    Drawable,
16    mono_font::{MonoFont, MonoTextStyle, ascii::FONT_9X15_BOLD},
17    text::{Baseline, Text},
18};
19use embedded_hal::spi::SpiDevice;
20
21use super::CydFrameEsp;
22
23/// The default font accepted by [`CydDisplayEsp::new`](super::CydDisplayEsp::new).
24/// See that method's compiled display-only constructor example.
25pub const DEFAULT_FONT: MonoFont<'static> = FONT_9X15_BOLD;
26
27impl<D: SpiDevice<u8>> CydFrameEsp<'_, D> {
28    /// Draw `text` at the frame rectangle's top-left using the device default
29    /// font and foreground color.
30    ///
31    /// For any other font, color, alignment, or baseline, draw with
32    /// embedded-graphics directly against this frame.
33    ///
34    /// See the portable
35    /// [`CydFrame::write_text`](https://docs.rs/device-envoy-core/latest/device_envoy_core/cyd/display/trait.CydFrame.html#tymethod.write_text)
36    /// documentation.
37    pub fn write_text(&mut self, text: &str) -> &mut Self {
38        Text::with_baseline(
39            text,
40            self.rectangle.top_left,
41            MonoTextStyle::new(self.font, self.foreground565),
42            Baseline::Top,
43        )
44        .draw(self)
45        .expect("drawing text to an Infallible CYD frame cannot fail");
46        self
47    }
48}