| 87 | #endif |
| 88 | |
| 89 | static void updatePowerSwitch() { |
| 90 | static PowerState last_state = PowerState::Initial; |
| 91 | |
| 92 | if (!unPhoneFeatures->isPowerSwitchOn()) { |
| 93 | if (last_state != PowerState::Off) { |
| 94 | last_state = PowerState::Off; |
| 95 | LOGGER.warn("Power off"); |
| 96 | } |
| 97 | |
| 98 | if (!unPhoneFeatures->isUsbPowerConnected()) { // and usb unplugged we go into shipping mode |
| 99 | LOGGER.warn("Shipping mode until USB connects"); |
| 100 | |
| 101 | #if DEBUG_POWER_STATES |
| 102 | unPhoneFeatures.setExpanderPower(true); |
| 103 | powerInfoBuzz(3); |
| 104 | unPhoneFeatures.setExpanderPower(false); |
| 105 | #endif |
| 106 | |
| 107 | unPhoneFeatures->turnPeripheralsOff(); |
| 108 | |
| 109 | bootStats.notifyPowerOff(); |
| 110 | |
| 111 | unPhoneFeatures->setShipping(true); // tell BM to stop supplying power until USB connects |
| 112 | } else { // When power switch is off, but USB is plugged in, we wait (deep sleep) until USB is unplugged. |
| 113 | LOGGER.warn("Waiting for USB disconnect to power off"); |
| 114 | |
| 115 | #if DEBUG_POWER_STATES |
| 116 | powerInfoBuzz(2); |
| 117 | #endif |
| 118 | |
| 119 | unPhoneFeatures->turnPeripheralsOff(); |
| 120 | |
| 121 | bootStats.notifyPowerSleep(); |
| 122 | |
| 123 | // Deep sleep for 1 minute, then awaken to check power state again |
| 124 | // GPIO trigger from power switch also awakens the device |
| 125 | unPhoneFeatures->wakeOnPowerSwitch(); |
| 126 | esp_sleep_enable_timer_wakeup(60000000); |
| 127 | esp_deep_sleep_start(); |
| 128 | } |
| 129 | } else { |
| 130 | if (last_state != PowerState::On) { |
| 131 | last_state = PowerState::On; |
| 132 | LOGGER.warn("Power on"); |
| 133 | |
| 134 | #if DEBUG_POWER_STATES |
| 135 | powerInfoBuzz(1); |
| 136 | #endif |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static int32_t powerSwitchMain() { // check power switch every 10th of sec |
| 142 | while (true) { |
no test coverage detected