#pragma once #include #include #include // Display SPI pins (custom pins for XteinkX4, not hardware SPI defaults) #define EPD_SCLK 8 // SPI Clock #define EPD_MOSI 10 // SPI MOSI (Master Out Slave In) #define EPD_CS 21 // Chip Select #define EPD_DC 4 // Data/Command #define EPD_RST 5 // Reset #define EPD_BUSY 6 // Busy #define SPI_MISO 7 // SPI MISO, shared between SD card and display (Master In Slave Out) #define BAT_GPIO0 0 // Battery voltage #define UART0_RXD 20 // Used for USB connection detection class HalGPIO { #if CROSSPOINT_EMULATED == 0 InputManager inputMgr; #endif public: enum class DeviceType : uint8_t { X4, X3 }; private: DeviceType _deviceType = DeviceType::X4; int _detectAdcValue = 0; int _batteryPin = BAT_GPIO0; // I2C fuel gauge configuration for X3 battery monitoring bool _batteryUseI2C = false; // Whether to use I2C fuel gauge (X3) vs ADC (X4) uint8_t _batteryI2cAddr = 0; // I2C address of fuel gauge chip uint8_t _batterySocRegister = 0; // Register address for state-of-charge public: HalGPIO() = default; // Inline device type helpers for cleaner downstream checks inline bool deviceIsX3() const { return _deviceType == DeviceType::X3; } inline bool deviceIsX4() const { return _deviceType == DeviceType::X4; } // Start button GPIO and setup SPI for screen and SD card void begin(); // Button input methods void update(); bool isPressed(uint8_t buttonIndex) const; bool wasPressed(uint8_t buttonIndex) const; bool wasAnyPressed() const; bool wasReleased(uint8_t buttonIndex) const; bool wasAnyReleased() const; unsigned long getHeldTime() const; // Setup wake up GPIO and enter deep sleep void startDeepSleep(); // Verify power button was held long enough after wakeup. // If verification fails, enters deep sleep and does not return. // Should only be called when wakeup reason is PowerButton. void verifyPowerButtonWakeup(uint16_t requiredDurationMs, bool shortPressAllowed); // Get battery percentage (range 0-100) int getBatteryPercentage() const; // Check if USB is connected bool isUsbConnected() const; // Device detection helpers DeviceType getDeviceType() const { return _deviceType; } int getDetectAdcValue() const { return _detectAdcValue; } int getBatteryPin() const { return _batteryPin; } enum class WakeupReason { PowerButton, AfterFlash, AfterUSBPower, Other }; WakeupReason getWakeupReason() const; // Button indices static constexpr uint8_t BTN_BACK = 0; static constexpr uint8_t BTN_CONFIRM = 1; static constexpr uint8_t BTN_LEFT = 2; static constexpr uint8_t BTN_RIGHT = 3; static constexpr uint8_t BTN_UP = 4; static constexpr uint8_t BTN_DOWN = 5; static constexpr uint8_t BTN_POWER = 6; }; extern HalGPIO gpio;