mirror of
https://github.com/crosspoint-reader/crosspoint-reader.git
synced 2026-04-29 10:26:52 -07:00
76 lines
2.7 KiB
C++
76 lines
2.7 KiB
C++
#include <HalGPIO.h>
|
|
#include <SPI.h>
|
|
#include <esp_sleep.h>
|
|
|
|
void HalGPIO::begin() {
|
|
inputMgr.begin();
|
|
SPI.begin(EPD_SCLK, SPI_MISO, EPD_MOSI, EPD_CS);
|
|
|
|
// X3 boards bias GPIO4 (EPD DC) around ~700 ADC counts at boot in our setup.
|
|
// X4 boards do not, and use GPIO0 for battery ADC.
|
|
_detectAdcValue = analogRead(4);
|
|
_deviceType = (_detectAdcValue > 500 && _detectAdcValue < 1200) ? DeviceType::X3 : DeviceType::X4;
|
|
_batteryPin = (_deviceType == DeviceType::X3) ? 4 : BAT_GPIO0;
|
|
|
|
pinMode(_batteryPin, INPUT);
|
|
pinMode(UART0_RXD, INPUT);
|
|
}
|
|
|
|
void HalGPIO::update() { inputMgr.update(); }
|
|
|
|
bool HalGPIO::isPressed(uint8_t buttonIndex) const { return inputMgr.isPressed(buttonIndex); }
|
|
|
|
bool HalGPIO::wasPressed(uint8_t buttonIndex) const { return inputMgr.wasPressed(buttonIndex); }
|
|
|
|
bool HalGPIO::wasAnyPressed() const { return inputMgr.wasAnyPressed(); }
|
|
|
|
bool HalGPIO::wasReleased(uint8_t buttonIndex) const { return inputMgr.wasReleased(buttonIndex); }
|
|
|
|
bool HalGPIO::wasAnyReleased() const { return inputMgr.wasAnyReleased(); }
|
|
|
|
unsigned long HalGPIO::getHeldTime() const { return inputMgr.getHeldTime(); }
|
|
|
|
void HalGPIO::startDeepSleep() {
|
|
// Ensure that the power button has been released to avoid immediately turning back on if you're holding it
|
|
while (inputMgr.isPressed(BTN_POWER)) {
|
|
delay(50);
|
|
inputMgr.update();
|
|
}
|
|
// Arm the wakeup trigger *after* the button is released
|
|
esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
|
|
// Enter Deep Sleep
|
|
esp_deep_sleep_start();
|
|
}
|
|
|
|
int HalGPIO::getBatteryPercentage() const {
|
|
if (_deviceType == DeviceType::X3) {
|
|
// X3 battery telemetry is not on ADC in stock fw; avoid fighting EPD DC on GPIO4.
|
|
return 0;
|
|
}
|
|
static const BatteryMonitor battery = BatteryMonitor(BAT_GPIO0);
|
|
return battery.readPercentage();
|
|
}
|
|
|
|
bool HalGPIO::isUsbConnected() const {
|
|
// U0RXD/GPIO20 reads HIGH when USB is connected
|
|
return digitalRead(UART0_RXD) == HIGH;
|
|
}
|
|
|
|
HalGPIO::WakeupReason HalGPIO::getWakeupReason() const {
|
|
const bool usbConnected = isUsbConnected();
|
|
const auto wakeupCause = esp_sleep_get_wakeup_cause();
|
|
const auto resetReason = esp_reset_reason();
|
|
|
|
if ((wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_POWERON && !usbConnected) ||
|
|
(wakeupCause == ESP_SLEEP_WAKEUP_GPIO && resetReason == ESP_RST_DEEPSLEEP && usbConnected)) {
|
|
return WakeupReason::PowerButton;
|
|
}
|
|
if (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_UNKNOWN && usbConnected) {
|
|
return WakeupReason::AfterFlash;
|
|
}
|
|
if (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_POWERON && usbConnected) {
|
|
return WakeupReason::AfterUSBPower;
|
|
}
|
|
return WakeupReason::Other;
|
|
}
|