flipperzero-firmware icon indicating copy to clipboard operation
flipperzero-firmware copied to clipboard

Automatic shutdown on idle

Open SHxKenzuto opened this issue 3 years ago • 45 comments

What's new

  • Added an option under [Settings->Power] that allows Flipper to be automatically turned off after a certain amount of time has passed without input from user ( the options are 15min, 30min, 1h, 2h, 6h, 12h, 24h, 48h).

Verification

  • A submenu named [Shutdown on Idle] has been added under [Settings->Power]. Under that submenu is possible to set how long Flipper will be idle before shutting off on his own and to turn off this feature.

Checklist (For Reviewer)

  • [ ] PR has description of feature/bug or link to Confluence/Jira task
  • [ ] Description contains actions to verify feature/bugfix
  • [ ] I've built this code, uploaded it to the device and verified feature/bugfix

SHxKenzuto avatar Aug 23 '22 20:08 SHxKenzuto

@hedger

Looks good to me, but introducing a new file in /int will take up at least 4Kb of internal storage - and we're running short on it for debug builds. We'll discuss this feature internally and may merge it after releasing v.0.65.

Good to know this. I've been looking at adding a setting to the Power application for optionally limiting the battery charging voltage, and that would've required creating a new /int file as well.

If this idle shutdown PR gets merged, I can base my battery charge limit configuration efforts atop this so there will be two settings stored in /int/.power.settings, not just one. That might make the 4 Kb minimum filesystem cost more palatable?

digitalcircuit avatar Aug 24 '22 09:08 digitalcircuit

Hi flipper team, how about to merge this? We are at 0.66 already.

azaitsev avatar Sep 07 '22 14:09 azaitsev

Hi @azaitsev, will be included in next release. Sorry for delay.

skotopes avatar Sep 07 '22 14:09 skotopes

@SHxKenzuto May I ask you to merge dev?

skotopes avatar Sep 19 '22 16:09 skotopes

@SHxKenzuto May I ask you to merge dev?

Ok, doing it right now

SHxKenzuto avatar Sep 19 '22 17:09 SHxKenzuto

@skotopes merge completed

SHxKenzuto avatar Sep 19 '22 19:09 SHxKenzuto

Thanks. This RC is already packed with more features than our QA can test, so I'll merge it after this release.

skotopes avatar Sep 20 '22 05:09 skotopes

@skotopes could you merge for the next release?

azaitsev avatar Sep 29 '22 10:09 azaitsev

Most likely after: reading file every second doesn't looks good. I'd rather provide some API on power service level.

skotopes avatar Sep 29 '22 10:09 skotopes

Most likely after: reading file every second doesn't looks good. I'd rather provide some API on power service level.

@skotopes Maybe I can create a custom event that is fired when the setting is modified and activates a callback that updates the timer in the data structure.

SHxKenzuto avatar Sep 29 '22 12:09 SHxKenzuto

Best option is to implement it in a same way as notification service do

skotopes avatar Sep 29 '22 12:09 skotopes

Please let mw know if you need any help.

skotopes avatar Oct 08 '22 18:10 skotopes

@skotopes could you review the changes, please?

SHxKenzuto avatar Oct 21 '22 21:10 SHxKenzuto

Hi @SHxKenzuto, I built the custom firmware that included your patch and used it for a while. Seems flipper shutdowns after no key is pressed by idle time regardless of running any application or not. Could you please add the check to prevent shutdown if any application is running? Or add the option to keep the flipper on for exact list of applications.

UPD: It would be good to add 2 hours interval. I added it, and seems it's has been the optimal compromise.

askoriy avatar Oct 27 '22 11:10 askoriy

Hi @SHxKenzuto, I built the custom firmware that included your patch and used it for a while. Seems flipper shutdowns after no key is pressed by idle time regardless of running any application or not. Could you please add the check to prevent shutdown if any application is running? Or add the option to keep the flipper on for exact list of applications.

UPD: It would be good to add 2 hours interval. I added it, and seems it's has been the optimal compromise.

Hi, in order to do what you're asking there should be a system to make my service aware of running tasks and stop the timer in such cases. I don't really know if there's already such system or if it has to be designed. In the latter case, it wouldn't be so simple. A simpler way would be checking if the current view is the home with flipper's sprite and activate the timer only when the device is in that state. Right now I'll add the 2 hours interval. What do you think?

SHxKenzuto avatar Nov 04 '22 19:11 SHxKenzuto

Would it make sense to be consistent with auto lock feature, auto shutdown in the same conditions when auto lock happens?

max-baz avatar Nov 04 '22 21:11 max-baz

Would it make sense to be consistent with auto lock feature, auto shutdown in the same conditions when auto lock happens?

That's a good idea, I'll check it out

SHxKenzuto avatar Nov 04 '22 21:11 SHxKenzuto

@SHxKenzuto can I help you somehow on this PR? The idea is awesome, would be great to implement it.

azaitsev avatar Nov 22 '22 22:11 azaitsev

@SHxKenzuto can I help you somehow on this PR? The idea is awesome, would be great to implement it.

Hi, there's a lot going on right now in my life, so a little help would be very appreciated. Thank you!

SHxKenzuto avatar Nov 23 '22 19:11 SHxKenzuto

From an initial look, suspending the auto-shutdown timer when inside an app requires hooking into DesktopGlobalBeforeAppStarted and DesktopGlobalAfterAppFinished from applications/services/desktop/desktop.c.

I'm unsure if the global events broadcast by desktop.c in desktop_events.h can be subscribed to from other services. If not, my next guess would be mimicking how the dolphin deed/level-up system works since that can receive events from any app/service.

digitalcircuit avatar Dec 01 '22 19:12 digitalcircuit

With commit 400558acfd4e6c7f62339b59646ea7af1bd8b582 I've added a hook to LoaderEventTypeApplicationStarted and LoaderEventTypeApplicationStopped events. That should stop the shutdown timer when an application is running. Thanks to @digitalcircuit for the tip. @skotopes or @hedger I'd be glad if you could review again the changes.

SHxKenzuto avatar Dec 09 '22 19:12 SHxKenzuto

Un-draft when ready.

skotopes avatar Dec 14 '22 08:12 skotopes

I've built from the most recent set of commits (2022-12-18) and tested it. Overall, this is a nice feature!

Testing - ⚠️ potential issues

This pull request functions as is. However, there are two changes I'd recommend making, noted in tests B and F.

Setup

  1. Compile and install firmware
  2. Set SettingsPowerShutdown on IdleSet Time to 15min
  3. Return to desktop
  4. Unplug Flipper from USB connection

A: Unplugged, auto-shutoff at desktop when idle - ✅ pass

  1. Make sure the Flipper desktop is showing
  2. Wait 15 or so minutes
  3. Ensure the Flipper has powered off

B: Plugged in, auto-shutoff at desktop when idle - ⚠️ potential issues

  1. Plug Flipper into USB charger and/or computer
  2. Make sure the Flipper desktop is showing
  3. Wait 15 or so minutes
  4. Ensure the Flipper remains turned on
  5. Unplug the Flipper from the USB charger
  6. Ensure the Flipper remains turned on
  7. Wait 15 or so minutes
  8. Ensure the Flipper has powered off

Current situation

The Flipper will immediately shut off without warning when unplugged from USB.

While this could be intentional, it feels unintuitive to me.

ℹ Recommended changes

  • When connected to USB power, pause the auto-shutdown timer
  • When disconnected from USB power, re-enable and reset the auto-shutdown timer if it is configured
    • This needs to avoid conflicting with the app launch/quit event handling so if either an app is open or USB is connected it won't auto-shutdown
    • Consider how charging is inhibited for the battery charging chip - every request to pause increments a counter, request to resume decrements it, and only when the counter is at zero does charging turn back on
    • Test: plug into USB, launch an app, then unplug USB - auto-shutdown should still be paused

This might mimic the app launch/quit event handling.

C: Remains on in internal app - ✅ pass

  1. Open SubGhz app
  2. Wait 15 or so minutes
  3. Ensure the Flipper remains turned on

D: Remains on in external app - ✅ pass

  1. Navigate to PluginsTools and launch Weather Station
  2. Wait 15 or so minutes
  3. Ensure the Flipper remains turned on

E: Unlocked, auto-shutoff timer reset on desktop key press - ✅ pass

  1. Make sure auto-lock is disabled
  2. Navigate to the Flipper desktop
  3. Wait 10 minutes
  4. Press the Back button once
  5. Wait 10 more minutes
  6. Ensure the Flipper remains on
  7. Wait 5 more minutes (15 total minutes since last button press)
  8. Ensure the Flipper has powered off

F: Locked, must unlock to reset auto-shutoff timer - ❌ fail

  1. Navigate to the Flipper desktop
  2. Lock the Flipper with Up button, Lock (or use auto-lock)
  3. Wait 10 minutes
  4. Press the Back button only once, do not unlock the Flipper
  5. Wait 5 more minutes (15 total minutes since last Flipper unlock)
  6. Ensure the Flipper has powered off
    • Currently, the Flipper remains on at this point
  7. Power on the Flipper by holding the Back button
  8. Lock the Flipper with Up button, Lock (or use auto-lock)
  9. Wait 10 minutes
  10. Press the Back button three times to unlock the Flipper
  11. Wait 10 more minutes
  12. Ensure the Flipper remains powered on
  13. Wait 5 more minutes (15 total minutes since last Flipper unlock)
  14. Ensure the Flipper has powered off

Current situation

The Flipper will not shut off at step 6 (after pressing Back once without actually unlocking the Flipper).

ℹ Recommended changes

Don't reset the idle shutdown timer via key events when the desktop is locked.

  • When handling key events, check if the desktop is locked and ignore the event if so
  • Listen for the unlock event and reset the idle shutdown timer at that point

This ensures the Flipper will auto-shutdown even if in a backpack or such with occasional button presses.

digitalcircuit avatar Dec 22 '22 05:12 digitalcircuit

We'll include this PR in next release(0.78).

skotopes avatar Feb 09 '23 05:02 skotopes

I need more time with this code, moving to next release.

skotopes avatar Feb 27 '23 06:02 skotopes

I think the next priority it making sure the butthurt_max xp system works off of what the clock app runs off of. Ive had issues with the butthurt_max xp not resetting properly if you turn off the flipper fully instead of locking it. If we do that, it would make this feature much more favorable in terms of preserving power by shutting it off after some time.

Ethanol0001 avatar Mar 10 '23 20:03 Ethanol0001

Otherwise I think this option in the settings to auto-shutoff would be very useful if the butthurt thing resets when the battery isnt under regular load

Ethanol0001 avatar Mar 10 '23 21:03 Ethanol0001

Considering the recent success of a deeper power-saving mode for the Flipper for 1 month of idle time, I think the selectable auto-shutdown delays could be adjusted.

Instead of… {"OFF", "15min", "30min", "1h", "2h", "6h", "12h", "24h", "48h"}

Perhaps… {"OFF", "1h", "2h", "6h", "12h", "24h", "48h", "1wk", "2wk"}, or similar.

Alternatively, if auto-off is no longer desired, I'd still like to make use of the Power app's settings code to add the configurable battery charge voltage limit - which with longer idle time reduces the risk of someone overdischarging.

digitalcircuit avatar Apr 28 '23 19:04 digitalcircuit

Seeing the recent dev merge commits, I'm happy to help with retesting or other things to get this refined and merged.

I'm still both interested in auto-shutdown (especially if increasing the upper bound, so the Flipper shuts off after a week of not interacting with it), and in the Power application having a settings file (to build charge voltage limit settings on top of).

digitalcircuit avatar Sep 07 '23 23:09 digitalcircuit

@digitalcircuit I'm currently refactoring power service, I'll update this PR and then merge it.

skotopes avatar Sep 08 '23 03:09 skotopes