Fixes and improvements (#185)
- unPhone improvements related to power and boot (add boot count logging) - Cleanup of Mutex acquire/release - Removed `tt_assert()` in favour of `assert()` - Fix sim build (likely failed due to migration of GitHub Actions to Ubuntu 24.04)
This commit is contained in:
committed by
GitHub
parent
3be251d8fb
commit
d86dc40472
@@ -1,3 +1,4 @@
|
||||
#include "Preferences.h"
|
||||
#include "TactilityCore.h"
|
||||
#include "UnPhoneFeatures.h"
|
||||
#include <esp_sleep.h>
|
||||
@@ -8,6 +9,55 @@ extern UnPhoneFeatures unPhoneFeatures;
|
||||
|
||||
static std::unique_ptr<tt::Thread> powerThread;
|
||||
|
||||
static const char* bootCountKey = "boot_count";
|
||||
static const char* powerOffCountKey = "power_off_count";
|
||||
static const char* powerSleepKey = "power_sleep_key";
|
||||
|
||||
class DeviceStats {
|
||||
|
||||
private:
|
||||
|
||||
tt::Preferences preferences = tt::Preferences("unphone");
|
||||
|
||||
int32_t getValue(const char* key) {
|
||||
int32_t value = 0;
|
||||
preferences.optInt32(key, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
void setValue(const char* key, int32_t value) {
|
||||
preferences.putInt32(key, value);
|
||||
}
|
||||
|
||||
void increaseValue(const char* key) {
|
||||
int32_t new_value = getValue(key) + 1;
|
||||
setValue(key, new_value);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
void notifyBootStart() {
|
||||
increaseValue(bootCountKey);
|
||||
}
|
||||
|
||||
void notifyPowerOff() {
|
||||
increaseValue(powerOffCountKey);
|
||||
}
|
||||
|
||||
void notifyPowerSleep() {
|
||||
increaseValue(powerSleepKey);
|
||||
}
|
||||
|
||||
void printInfo() {
|
||||
TT_LOG_I("TAG", "Device stats:");
|
||||
TT_LOG_I("TAG", " boot: %ld", getValue(bootCountKey));
|
||||
TT_LOG_I("TAG", " power off: %ld", getValue(powerOffCountKey));
|
||||
TT_LOG_I("TAG", " power sleep: %ld", getValue(powerSleepKey));
|
||||
}
|
||||
};
|
||||
|
||||
DeviceStats bootStats;
|
||||
|
||||
enum class PowerState {
|
||||
Initial,
|
||||
On,
|
||||
@@ -16,6 +66,7 @@ enum class PowerState {
|
||||
|
||||
#define DEBUG_POWER_STATES false
|
||||
|
||||
#if DEBUG_POWER_STATES
|
||||
/** Helper method to use the buzzer to signal the different power stages */
|
||||
static void powerInfoBuzz(uint8_t count) {
|
||||
if (DEBUG_POWER_STATES) {
|
||||
@@ -33,6 +84,7 @@ static void powerInfoBuzz(uint8_t count) {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void updatePowerSwitch() {
|
||||
static PowerState last_state = PowerState::Initial;
|
||||
@@ -46,19 +98,28 @@ static void updatePowerSwitch() {
|
||||
if (!unPhoneFeatures.isUsbPowerConnected()) { // and usb unplugged we go into shipping mode
|
||||
TT_LOG_W(TAG, "Shipping mode until USB connects");
|
||||
|
||||
#if DEBUG_POWER_STATES
|
||||
unPhoneFeatures.setExpanderPower(true);
|
||||
powerInfoBuzz(3);
|
||||
unPhoneFeatures.setExpanderPower(false);
|
||||
#endif
|
||||
|
||||
unPhoneFeatures.turnPeripheralsOff();
|
||||
|
||||
bootStats.notifyPowerOff();
|
||||
|
||||
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");
|
||||
|
||||
#if DEBUG_POWER_STATES
|
||||
powerInfoBuzz(2);
|
||||
#endif
|
||||
|
||||
unPhoneFeatures.turnPeripheralsOff();
|
||||
|
||||
bootStats.notifyPowerSleep();
|
||||
|
||||
// Deep sleep for 1 minute, then awaken to check power state again
|
||||
// GPIO trigger from power switch also awakens the device
|
||||
unPhoneFeatures.wakeOnPowerSwitch();
|
||||
@@ -70,9 +131,9 @@ static void updatePowerSwitch() {
|
||||
last_state = PowerState::On;
|
||||
TT_LOG_W(TAG, "Power on");
|
||||
|
||||
unPhoneFeatures.setShipping(false);
|
||||
unPhoneFeatures.setExpanderPower(true);
|
||||
#if DEBUG_POWER_STATES
|
||||
powerInfoBuzz(1);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,6 +156,11 @@ static void startPowerSwitchThread() {
|
||||
}
|
||||
|
||||
static bool unPhonePowerOn() {
|
||||
// Print early, in case of early crash (info will be from previous boot)
|
||||
bootStats.printInfo();
|
||||
|
||||
bootStats.notifyBootStart();
|
||||
|
||||
if (!unPhoneFeatures.init()) {
|
||||
TT_LOG_E(TAG, "UnPhoneFeatures init failed");
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user