You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
reudsh3/main.py

169 lines
4.0 KiB

import time
from machine import Pin, SPI
from time import sleep
dht_pin = Pin(1, Pin.IN)
ds18b20_pin = Pin(3, Pin.IN)
# from sensor_dh11 import Sensor
from sensor_ds18x20 import Sensor
# sensor = Sensor(dht_pin)
sensor = Sensor(ds18b20_pin)
print('sensor ready')
import ssd1306
spi = SPI(2, sck=Pin(12), mosi=Pin(11), miso=Pin(13))
lcd = ssd1306.init_spi(width=128, height=64, spi=spi, dc=Pin(5), res=Pin(8), cs=Pin(10))
print('lcd ready')
import font
lcd.set_font(font.font16)
print('font ready')
curr_temp = 28.0
max_temp = 30
min_temp = 10
import keyboard
keyboard.init_keys()
print('keyboard init ready')
FRAME_TIME = const(41)
FOCUS_ITEM_MIN_TEMP = 0
FOCUS_ITEM_MAX_TEMP = 1
editing = False
focus_item = FOCUS_ITEM_MIN_TEMP
editing_min_temp = min_temp
editing_max_temp = max_temp
def render_frame():
global curr_temp
lcd.font_text(f'当前{curr_temp:>4.1f}', 0, 0)
if editing:
if focus_item == FOCUS_ITEM_MIN_TEMP:
lcd.font_text(f'最小▶{editing_min_temp:>3}', 0, 16)
lcd.font_text(f'最大 {editing_max_temp:>3}', 0, 32)
else:
lcd.font_text(f'最小 {editing_min_temp:>3}', 0, 16)
lcd.font_text(f'最大▶{editing_max_temp:>3}', 0, 32)
if curr_temp >= editing_max_temp or curr_temp <= editing_min_temp:
lcd.font_text(f'警告●', 0, 48)
else:
lcd.font_text(f'警告○', 0, 48)
else:
lcd.font_text(f'最小 {min_temp:>3}', 0, 16)
lcd.font_text(f'最大 {max_temp:>3}', 0, 32)
if curr_temp >= editing_max_temp or curr_temp <= editing_min_temp:
lcd.font_text(f'警告●', 0, 48)
else:
lcd.font_text(f'警告○', 0, 48)
lcd.show()
def edit_value(digit):
if not editing:
return
global focus_item
def edit_min_temp():
global editing_min_temp
val = editing_min_temp * 10 + digit
if val > 99:
val = digit
editing_min_temp = val
def edit_max_temp():
global editing_max_temp
val = editing_max_temp * 10 + digit
if val > 99:
val = digit
editing_max_temp = val
focus_item_cases = {
FOCUS_ITEM_MIN_TEMP: edit_min_temp,
FOCUS_ITEM_MAX_TEMP: edit_max_temp,
}
focus_item_cases.get(focus_item, lambda: None)()
def cancel():
global editing, editing_min_temp, editing_max_temp, min_temp, max_temp
editing = False
editing_min_temp = min_temp
editing_max_temp = max_temp
def switch_focus_item(direction):
if not editing:
return
global focus_item
if direction == -1:
focus_item = FOCUS_ITEM_MIN_TEMP
elif direction == 1:
focus_item = FOCUS_ITEM_MAX_TEMP
def handle_key_event(key):
global editing, focus_item
def ok():
global editing, focus_item, editing_min_temp, editing_max_temp, min_temp, max_temp
if editing:
min_temp = editing_min_temp
max_temp = editing_max_temp
editing = not editing
actions = {
keyboard.KEY_OK: lambda: ok(),
keyboard.KEY_CANCEL: lambda: cancel(),
keyboard.NUM0: lambda: edit_value(0),
keyboard.NUM1: lambda: edit_value(1),
keyboard.NUM2: lambda: edit_value(2),
keyboard.NUM3: lambda: edit_value(3),
keyboard.NUM4: lambda: edit_value(4),
keyboard.NUM5: lambda: edit_value(5),
keyboard.NUM6: lambda: edit_value(6),
keyboard.NUM7: lambda: edit_value(7),
keyboard.NUM8: lambda: edit_value(8),
keyboard.NUM9: lambda: edit_value(9),
keyboard.KEY_UP: lambda: switch_focus_item(-1),
keyboard.KEY_DOWN: lambda: switch_focus_item(1),
keyboard.KEY_L: lambda: switch_focus_item(-1),
keyboard.KEY_R: lambda: switch_focus_item(1)
}
actions.get(key, lambda: None)()
def main():
while True:
prev_frame_start = time.ticks_ms()
global curr_temp
curr_temp = sensor.read()
key = keyboard.read_key_on_released()
if key != keyboard.NO_KEY:
print('key', key, 'released')
handle_key_event(key)
render_frame()
prev_frame_time_cost = time.ticks_diff(time.ticks_ms(), prev_frame_start)
if FRAME_TIME > prev_frame_time_cost:
sleep((FRAME_TIME - prev_frame_time_cost) * 0.001)
if __name__ == "__main__":
main()