You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派FourLetterPHat温时钟夜间启停及按钮唤醒需求求助

树莓派FourLetterPHat温时钟程序优化

问题描述

  • 夜间时段(21:00-06:00)需自动暂停显示并清屏,当前代码仅能在夜间开始时清屏,无法自动恢复日间运行
  • 夜间按下GPIO16按钮时,需唤醒显示并维持10秒,之后自动停止并清屏

优化后的完整代码

#! /usr/bin/env python3
# Tempclock with FourLetterPHat - 切换时间/温度显示,支持夜间自动休眠+按钮唤醒

import glob
import time
import datetime
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
import fourletterphat as flp

# 温度传感器路径初始化
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

# 时间配置
NIGHT_START = 21    # 夜间开始时间(21点)
NIGHT_END = 6       # 夜间结束时间(6点)
DAY_BRIGHTNESS = 12 # 日间亮度
DIM_BRIGHTNESS = 0  # 傍晚/清晨亮度
WAKE_DURATION = 10  # 夜间唤醒持续时间(秒)

display_mode = 1    # 1=显示时间,2=显示温度
last_button_press = 0
wake_end_time = 0

# 判断当前是否处于夜间时段
def is_night_time():
    current_hour = datetime.datetime.now().hour
    return current_hour >= NIGHT_START or current_hour < NIGHT_END

# 设置显示屏亮度(日间逻辑)
def set_day_brightness():
    current_hour = datetime.datetime.now().hour
    if 7 <= current_hour < 18:
        flp.set_brightness(DAY_BRIGHTNESS)
    else:
        flp.set_brightness(DIM_BRIGHTNESS)

# 读取温度传感器数据
def read_temp_raw():
    with open(device_file, 'r') as f:
        return f.readlines()

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        return temp_c

# 显示时间
def display_time():
    str_time = datetime.datetime.now().strftime("%H%M")
    flp.print_number_str(str_time)
    flp.show()

# 显示温度
def display_temp():
    temp = read_temp()
    flp.print_float(temp, decimal_digits=1, justify_right=True)
    flp.show()

# 清屏
def clear_display():
    flp.clear()
    flp.show()

try:
    while True:
        current_time = time.time()
        
        # 夜间逻辑
        if is_night_time():
            # 检查是否在唤醒时间段内
            if current_time <= wake_end_time:
                # 维持当前显示模式
                if display_mode == 1:
                    display_time()
                else:
                    display_temp()
            else:
                # 休眠状态:清屏,仅监听按钮
                clear_display()
                
                # 检测按钮按下(防抖处理)
                if GPIO.input(16) == GPIO.HIGH and current_time - last_button_press > 0.5:
                    last_button_press = current_time
                    wake_end_time = current_time + WAKE_DURATION
                    # 切换显示模式并唤醒
                    display_mode = 2 if display_mode == 1 else 1
                    if display_mode == 1:
                        display_time()
                    else:
                        display_temp()
        # 日间逻辑
        else:
            set_day_brightness()
            
            # 检测按钮按下(防抖处理)
            if GPIO.input(16) == GPIO.HIGH and current_time - last_button_press > 0.5:
                last_button_press = current_time
                display_mode = 2 if display_mode == 1 else 1
            
            # 正常显示当前模式内容
            if display_mode == 1:
                display_time()
            else:
                display_temp()
        
        time.sleep(0.2)

finally:
    GPIO.cleanup()
    clear_display()

关键修改说明

  • 夜间时段判断逻辑:新增is_night_time()函数明确区分21:00-06:00的夜间时段,替代原代码中模糊的时间判断
  • 夜间唤醒机制:用wake_end_time记录唤醒截止时间,按下按钮后启动10秒倒计时,时间到自动清屏回到休眠状态
  • 防抖处理:添加last_button_press变量避免按钮重复触发,提升操作体验
  • 代码结构优化:拆分功能函数(如clear_display()、is_night_time()),让主循环逻辑更清晰
  • 资源清理:添加finally块确保程序退出时清理GPIO并清屏,避免硬件残留状态
  • 亮度逻辑修正:修复原bright()函数的判断错误,确保日间(7-18点)高亮度,清晨/傍晚低亮度,夜间彻底清屏

内容的提问来源于stack exchange,提问作者Carl-Fredrik von Essen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 17:16:02