Various fixes and improvements (#182)

- Fix for `logMutex` bug where the `Mutex` constructor is not called when doing early boot logging (with `DEBUG` level logging) . The only way to make it work is to explicitly call the constructor in the logging wrapper function.
- Fix for unPhone power states
This commit is contained in:
Ken Van Hoeylandt
2025-01-23 21:17:33 +01:00
committed by GitHub
parent 9fb8d45b2e
commit bb7e79886f
6 changed files with 163 additions and 61 deletions
+60 -29
View File
@@ -8,35 +8,72 @@ extern UnPhoneFeatures unPhoneFeatures;
static std::unique_ptr<tt::Thread> powerThread;
enum class PowerState {
Initial,
On,
Off
};
#define DEBUG_POWER_STATES false
/** Helper method to use the buzzer to signal the different power stages */
static void powerInfoBuzz(uint8_t count) {
if (DEBUG_POWER_STATES) {
uint8_t index = 0;
while (index < count) {
unPhoneFeatures.setVibePower(true);
tt::kernel::delayMillis(50);
unPhoneFeatures.setVibePower(false);
index++;
if (index < count) {
tt::kernel::delayMillis(100);
}
}
}
}
static void updatePowerSwitch() {
static bool last_on_state = true;
static PowerState last_state = PowerState::Initial;
if (!unPhoneFeatures.isPowerSwitchOn()) {
if (last_on_state) {
if (last_state != PowerState::Off) {
last_state = PowerState::Off;
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();
// Using UINT64_MAX leads to boot loops because of a bug in esp_sleep_start() converting it to int64_t before sleeping
esp_sleep_enable_timer_wakeup(UINT64_MAX / 2); // ea min: USB? else->shipping
esp_deep_sleep_start(); // deep sleep, wait for wakeup on GPIO
}
TT_LOG_W(TAG, "Shipping mode until USB connects");
last_on_state = false;
} else {
if (!last_on_state) {
TT_LOG_W(TAG, "Power on");
unPhoneFeatures.setShipping(false);
unPhoneFeatures.setExpanderPower(true);
powerInfoBuzz(3);
unPhoneFeatures.setExpanderPower(false);
unPhoneFeatures.turnPeripheralsOff();
unPhoneFeatures.setShipping(true); // tell BM to stop supplying power until USB connects
} else { // When power switch is off, but USB is plugged in, we wait (deep sleep) until USB is unplugged.
TT_LOG_W(TAG, "Waiting for USB disconnect to power off");
powerInfoBuzz(2);
unPhoneFeatures.turnPeripheralsOff();
// Deep sleep for 1 minute, then awaken to check power state again
// GPIO trigger from power switch also awakens the device
unPhoneFeatures.wakeOnPowerSwitch();
esp_sleep_enable_timer_wakeup(60000000);
esp_deep_sleep_start();
}
} else {
if (last_state != PowerState::On) {
last_state = PowerState::On;
TT_LOG_W(TAG, "Power on");
unPhoneFeatures.setShipping(false);
unPhoneFeatures.setExpanderPower(true);
powerInfoBuzz(1);
}
last_on_state = true;
}
}
@@ -65,22 +102,16 @@ static bool unPhonePowerOn() {
unPhoneFeatures.printInfo();
// Vibrate once
// Note: Do this before power switching logic, to detect silent boot loops
unPhoneFeatures.setVibePower(true);
tt::kernel::delayMillis(150);
unPhoneFeatures.setBacklightPower(false);
unPhoneFeatures.setVibePower(false);
unPhoneFeatures.setIrPower(false);
unPhoneFeatures.setExpanderPower(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;
}