kywy/
macros.rs

1// SPDX-FileCopyrightText: 2025 KOINSLOT Inc.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5// ==========================
6// Kywy Macros for Init
7// ==========================
8
9#[macro_export]
10macro_rules! kywy_spi_from {
11    ($peripherals:ident => $valname:ident) => {
12        let $valname = {
13            use embassy_rp::spi::{Async, Config, Spi};
14            use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
15            use embassy_sync::mutex::Mutex;
16            use static_cell::StaticCell;
17
18            // NOTE: we declare a STATIC cell with a different name than the return value
19            static SPI_BUS_STATIC: StaticCell<
20                Mutex<CriticalSectionRawMutex, Spi<'static, embassy_rp::peripherals::SPI0, Async>>,
21            > = StaticCell::new();
22
23            let spi = Spi::new(
24                $peripherals.SPI0,
25                $peripherals.PIN_18,
26                $peripherals.PIN_19,
27                $peripherals.PIN_16,
28                $peripherals.DMA_CH0,
29                $peripherals.DMA_CH1,
30                Config::default(),
31            );
32
33            let mutex = Mutex::new(spi);
34            let $valname = SPI_BUS_STATIC.init(mutex);
35            $valname
36        };
37    };
38}
39
40#[macro_export]
41macro_rules! kywy_display_from {
42    ($spi_bus:ident, $peripherals:ident => $var:ident) => {
43        use embassy_embedded_hal::shared_bus::asynch::spi::SpiDeviceWithConfig;
44        use embassy_rp::gpio::{Level, Output};
45        use embassy_rp::spi::Config;
46        use embassy_rp::spi::Phase;
47        use embassy_rp::spi::Polarity;
48        use inverted_pin::InvertedPin;
49        //initialize pins
50        let cs_disp_pin = InvertedPin::new(Output::new($peripherals.PIN_17, Level::Low));
51        let disp_pin = Output::new($peripherals.PIN_22, Level::Low);
52
53        //initialize SPI device
54        let mut config = Config::default();
55        config.frequency = 1_000_000; // Try overclocking me! some displays can handle higher frequencies (~8_000_000), 2-4 MHz seems mostly stable but the spec says 1 MHz so leaving it there for now.
56        config.polarity = Polarity::IdleLow;
57        config.phase = Phase::CaptureOnFirstTransition;
58        let disp_spi = SpiDeviceWithConfig::new(&$spi_bus, cs_disp_pin, config);
59
60        //create display
61        let mut $var = $crate::display::KywyDisplay::new(
62            disp_spi, disp_pin, // DISP
63        );
64
65        //initialize display
66        $var.initialize().await;
67    };
68}
69
70#[macro_export]
71macro_rules! kywy_button_async_from {
72    ($spawner:expr, $peripherals:ident => $var:ident) => {
73        let mut $var = $crate::button_async::init(
74            $spawner,
75            $peripherals.PIN_12, // Button: Right
76            $peripherals.PIN_2,  // Button: Left
77            $peripherals.PIN_9,  // Button: DUp
78            $peripherals.PIN_3,  // Button: DDown
79            $peripherals.PIN_6,  // Button: DLeft
80            $peripherals.PIN_7,  // Button: DRight
81            $peripherals.PIN_8,  // Button: DCenter
82        );
83    };
84}
85
86#[macro_export]
87macro_rules! kywy_button_poll_from {
88    ($peripherals:ident => $var:ident) => {
89        let $var = $crate::button_poll::ButtonPoller::new(
90            $peripherals.PIN_2,  // Button: Left
91            $peripherals.PIN_12, // Button: Right
92            $peripherals.PIN_9,  // Button: DUp
93            $peripherals.PIN_3,  // Button: DDown
94            $peripherals.PIN_6,  // Button: DLeft
95            $peripherals.PIN_7,  // Button: DRight
96            $peripherals.PIN_8,  // Button: DCenter
97        );
98    };
99}
100
101#[macro_export]
102macro_rules! kywy_battery_from {
103    ($peripherals:ident => $battery_var:ident) => {
104        let mut $battery_var = $crate::battery::BatteryMonitor::new(
105            $peripherals.PIN_26,
106            $peripherals.PIN_10,
107            $peripherals.PIN_11,
108            $peripherals.ADC,
109            embedded_graphics::geometry::Point::new(125, 0), // default battery location
110            embedded_graphics::pixelcolor::BinaryColor::Off,
111        )
112        .await;
113    };
114}