pimoroni / phat-beat

Python library for PHAT BEAT - A stereo DAC, AMP and VU for the Raspberry Pi
https://shop.pimoroni.com/products/phat-beat
MIT License
99 stars 28 forks source link

Add the sleep timer function #21

Closed pirateradiohack closed 4 years ago

pirateradiohack commented 5 years ago

When holding the power off button for 2 seconds, the pirate radio will shut down with a delay of 20 minutes.

Gadgetoid commented 5 years ago

I love the concept- as a user of sleep timers myself- but I think the implementation could be better.

Blocking the whole script with a really, really long time.sleep() poses all sorts of potential problems and, if I'm not wrong, will block any further control input such as volume, channel changes or play/pause as soon as it's triggered. It also cannot be disarmed.

We should aim for something that:

  1. Can be toggled on and off
  2. Maybe steps through increments of 10, 20 and 30 minutes
  3. Allows other controls to function when it's active

This could be done using a Threading Timer, see - https://docs.python.org/3/library/threading.html#timer-objects

It basically allows an action to be called after a certain period of time, but doesn't block the rest of the script so that the timer can be cancelled and replaced at any time before it's triggered.

pirateradiohack commented 5 years ago

Regarding the Threading Timer, that was exactly my thought. I wanted to try first with the time.sleep() just to see if the button was going to trigger it. And then thought that it would block all the other controls. But oh to my surprise, nothing is blocked at all. I really can't explain why. But yet, a Threading Timer is still the way to go.

And regarding the other comments, to me it smells like a framework to be able to program the buttons in general, not only a specific function of a specific button.

I'll see if I have time for that.

Gadgetoid commented 5 years ago

Aha! I completely forgot that pHAT BEAT silently uses threading to handle button presses by default. This would, at least, prevent other controls from being blocked! It may suffice to just have long-press set or cancel a threading.Timer in that case.

Yeah, it's difficult to balance a feature with scope creep and ending up with a framework to support said feature!

Thanks for looking into it though, feedback, improvements, suggesstions, etc, always welcome!