feat: Long press VOL_DOWN button to shutdown/reset device
Signed-off-by: Matthias Blankertz <matthias@blankertz.org>
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
# Copyright (c) 2025 Matthias Blankertz <matthias@blankertz.org>
|
# Copyright (c) 2025 Matthias Blankertz <matthias@blankertz.org>
|
||||||
|
|
||||||
from utils.helpers import safe_callback
|
from utils.helpers import safe_callback
|
||||||
|
from utils.timer import TimerManager
|
||||||
from utils.buttons import Buttons
|
from utils.buttons import Buttons
|
||||||
from utils.config import Configuration
|
from utils.config import Configuration
|
||||||
from utils.leds import LedManager
|
from utils.leds import LedManager
|
||||||
@@ -9,7 +10,6 @@ from utils.mbrpartition import MBRPartition
|
|||||||
from utils.pinindex import get_pin_index
|
from utils.pinindex import get_pin_index
|
||||||
from utils.playlistdb import BTreeDB, BTreeFileManager
|
from utils.playlistdb import BTreeDB, BTreeFileManager
|
||||||
from utils.sdcontext import SDContext
|
from utils.sdcontext import SDContext
|
||||||
from utils.timer import TimerManager
|
|
||||||
|
|
||||||
__all__ = ["BTreeDB", "BTreeFileManager", "Buttons", "Configuration", "get_pin_index", "LedManager", "MBRPartition",
|
__all__ = ["BTreeDB", "BTreeFileManager", "Buttons", "Configuration", "get_pin_index", "LedManager", "MBRPartition",
|
||||||
"safe_callback", "SDContext", "TimerManager"]
|
"safe_callback", "SDContext", "TimerManager"]
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import asyncio
|
|||||||
import machine
|
import machine
|
||||||
import micropython
|
import micropython
|
||||||
import time
|
import time
|
||||||
from utils import safe_callback
|
from utils import safe_callback, TimerManager
|
||||||
try:
|
try:
|
||||||
from typing import TYPE_CHECKING # type: ignore
|
from typing import TYPE_CHECKING # type: ignore
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -32,6 +32,7 @@ class Buttons:
|
|||||||
def __init__(self, cb: "ButtonCallback", config, hwconfig):
|
def __init__(self, cb: "ButtonCallback", config, hwconfig):
|
||||||
self.button_map = config.get_button_map()
|
self.button_map = config.get_button_map()
|
||||||
self.hw_buttons = hwconfig.BUTTONS
|
self.hw_buttons = hwconfig.BUTTONS
|
||||||
|
self.hwconfig = hwconfig
|
||||||
self.cb = cb
|
self.cb = cb
|
||||||
self.buttons = dict()
|
self.buttons = dict()
|
||||||
for key_id, key_name in self.KEYMAP.items():
|
for key_id, key_name in self.KEYMAP.items():
|
||||||
@@ -42,6 +43,7 @@ class Buttons:
|
|||||||
self.int_flag = asyncio.ThreadSafeFlag()
|
self.int_flag = asyncio.ThreadSafeFlag()
|
||||||
self.pressed: list[int] = []
|
self.pressed: list[int] = []
|
||||||
self.last: dict[int, int] = {}
|
self.last: dict[int, int] = {}
|
||||||
|
self.timer_manager = TimerManager()
|
||||||
for button in self.buttons.keys():
|
for button in self.buttons.keys():
|
||||||
button.irq(handler=self._interrupt, trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING)
|
button.irq(handler=self._interrupt, trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING)
|
||||||
asyncio.create_task(self.task())
|
asyncio.create_task(self.task())
|
||||||
@@ -69,6 +71,10 @@ class Buttons:
|
|||||||
# print(f'B{keycode} {now}')
|
# print(f'B{keycode} {now}')
|
||||||
self.pressed.append(keycode)
|
self.pressed.append(keycode)
|
||||||
self.int_flag.set()
|
self.int_flag.set()
|
||||||
|
if keycode == self.VOLDOWN:
|
||||||
|
self.timer_manager.schedule(time.ticks_ms() + 5000, self.long_press_shutdown)
|
||||||
|
if button.value() == 1 and keycode == self.VOLDOWN:
|
||||||
|
self.timer_manager.cancel(self.long_press_shutdown)
|
||||||
|
|
||||||
async def task(self):
|
async def task(self):
|
||||||
while True:
|
while True:
|
||||||
@@ -76,3 +82,9 @@ class Buttons:
|
|||||||
while len(self.pressed) > 0:
|
while len(self.pressed) > 0:
|
||||||
what = self.pressed.pop()
|
what = self.pressed.pop()
|
||||||
safe_callback(lambda: self.cb.onButtonPressed(what), "button callback")
|
safe_callback(lambda: self.cb.onButtonPressed(what), "button callback")
|
||||||
|
|
||||||
|
def long_press_shutdown(self):
|
||||||
|
if self.hwconfig.get_on_battery():
|
||||||
|
self.hwconfig.power_off()
|
||||||
|
else:
|
||||||
|
machine.reset()
|
||||||
|
|||||||
Reference in New Issue
Block a user