auto_poweroff
This commit is contained in:
37
hardware.py
37
hardware.py
@@ -4,7 +4,7 @@
|
||||
硬件管理器模块
|
||||
提供硬件对象的统一管理和访问
|
||||
"""
|
||||
import threading
|
||||
from maix import time
|
||||
import config
|
||||
from at_client import ATClient
|
||||
|
||||
@@ -28,8 +28,13 @@ class HardwareManager:
|
||||
self._bus = None # I2C总线
|
||||
self._adc_obj = None # ADC对象
|
||||
self._at_client = None # AT客户端
|
||||
|
||||
|
||||
self._last_active_time = 0 # 用于记录用户的最后一次活跃的时间
|
||||
self._stop_timer = False # 用于停止定时器的标志
|
||||
|
||||
self._initialized = True
|
||||
|
||||
|
||||
|
||||
# ==================== 硬件访问(只读属性)====================
|
||||
|
||||
@@ -93,6 +98,34 @@ class HardwareManager:
|
||||
self._at_client.start()
|
||||
return self._at_client
|
||||
|
||||
def power_off(self):
|
||||
"""关闭电源板"""
|
||||
try:
|
||||
# 物理引脚是 A24,对应 GPIO 功能是 GPIOA24
|
||||
# 注意:这里需要先在 config.PIN_MAPPINGS 中配置好 "A24": "GPIOA24"
|
||||
from maix import gpio
|
||||
# 输出高电平关闭
|
||||
gpio.GPIO("GPIOA24", gpio.Mode.OUT).value(1)
|
||||
except Exception as e:
|
||||
print(f"关机失败: {e}")
|
||||
|
||||
def start_idle_timer(self):
|
||||
self._stop_timer = False
|
||||
self._last_active_time = time.time()
|
||||
|
||||
def stop_idle_timer(self):
|
||||
self._stop_timer = True
|
||||
|
||||
def get_idle_time_in_sec(self):
|
||||
if self._stop_timer:
|
||||
return 0
|
||||
diff = time.time() - self._last_active_time
|
||||
if diff < 0:
|
||||
# 时间可能被重置了,重新计时
|
||||
self._last_active_time = time.time()
|
||||
return 0
|
||||
return diff
|
||||
|
||||
|
||||
# 创建全局单例实例
|
||||
hardware_manager = HardwareManager()
|
||||
|
||||
Reference in New Issue
Block a user