Ken Van Hoeylandt 2bbd44a8b5
Unphone improvements (#172)
- Debounce nav button presses: This fixes multiple triggers on a single press to navigate back. Otherwise, more than 1 app would often close at the same time.
- Nav button respond to release instead of push down, because these buttons aren't super reliable in general. (I might change this in the future after more testing)
- Move single buzz earlier in boot phase, so that we can detect silent boot loops.
2025-01-19 21:40:26 +01:00

96 lines
2.6 KiB
C++

#include "TactilityCore.h"
#include "UnPhoneFeatures.h"
#include <esp_sleep.h>
#define TAG "unphone"
extern UnPhoneFeatures unPhoneFeatures;
static std::unique_ptr<tt::Thread> powerThread;
static void updatePowerSwitch() {
static bool last_on_state = true;
if (!unPhoneFeatures.isPowerSwitchOn()) {
if (last_on_state) {
TT_LOG_W(TAG, "Power off");
}
unPhoneFeatures.turnPeripheralsOff();
if (!unPhoneFeatures.isUsbPowerConnected()) { // and usb unplugged we go into shipping mode
if (last_on_state) {
TT_LOG_W(TAG, "Shipping mode until USB connects");
unPhoneFeatures.setShipping(true); // tell BM to stop supplying power until USB connects
}
} else { // power switch off and usb plugged in we sleep
unPhoneFeatures.wakeOnPowerSwitch();
esp_sleep_enable_timer_wakeup(60000000); // ea min: USB? else->shipping
esp_deep_sleep_start(); // deep sleep, wait for wakeup on GPIO
}
last_on_state = false;
} else {
if (!last_on_state) {
TT_LOG_W(TAG, "Power on");
unPhoneFeatures.setShipping(false);
}
last_on_state = true;
}
}
static int32_t powerSwitchMain(void*) { // check power switch every 10th of sec
while (true) {
updatePowerSwitch();
tt::kernel::delayMillis(200);
}
}
static void startPowerSwitchThread() {
powerThread = std::make_unique<tt::Thread>(
"unphone_power_switch",
4096,
powerSwitchMain,
nullptr
);
powerThread->start();
}
static bool unPhonePowerOn() {
if (!unPhoneFeatures.init()) {
TT_LOG_E(TAG, "UnPhoneFeatures init failed");
return false;
}
unPhoneFeatures.printInfo();
// Vibrate once
// Note: Do this before power switching logic, to detect silent boot loops
unPhoneFeatures.setVibePower(true);
tt::kernel::delayMillis(150);
unPhoneFeatures.setVibePower(false);
// Turn off the device if power switch is on off state,
// instead of waiting for the Thread to start and continue booting
updatePowerSwitch();
startPowerSwitchThread();
unPhoneFeatures.setBacklightPower(false);
unPhoneFeatures.setVibePower(false);
unPhoneFeatures.setIrPower(false);
unPhoneFeatures.setExpanderPower(false);
return true;
}
bool unPhoneInitPower() {
ESP_LOGI(TAG, LOG_MESSAGE_POWER_ON_START);
if (!unPhonePowerOn()) {
TT_LOG_E(TAG, LOG_MESSAGE_POWER_ON_FAILED);
return false;
}
return true;
}