UART commands for getting dish temperature
This commit is contained in:
@@ -6,6 +6,7 @@ from lib.microwaveScreen import MicrowaveScreen
|
||||
import uasyncio as asyncio
|
||||
from machine import Pin, SoftI2C
|
||||
import ssd1306
|
||||
import ujson
|
||||
|
||||
# Clean memory immediately
|
||||
gc.collect()
|
||||
@@ -32,6 +33,9 @@ uart_device = None
|
||||
magnetron_led = None
|
||||
microwave_screen = None
|
||||
defrost_mode = False
|
||||
current_temp = [None, None]
|
||||
last_temp = [None, None]
|
||||
temperature_asked = False
|
||||
|
||||
|
||||
PING_PAYLOAD = {
|
||||
@@ -100,7 +104,36 @@ def lora_hardware_thread():
|
||||
|
||||
# --- COOKING STATE CALLBACKS ---
|
||||
def cooking_state_temperature_provider():
|
||||
return 22.0, 29.0 # TODO: Replace with real temperature reading
|
||||
global current_temp, last_temp, temperature_asked, uart_device
|
||||
|
||||
def temp_is_none(temp):
|
||||
return temp is None or temp[0] is None or temp[1] is None
|
||||
|
||||
# Return the current temperature if available,
|
||||
# otherwise return the last recorded temperature
|
||||
# and request a new reading from the WiFi board.
|
||||
if temp_is_none(current_temp):
|
||||
if temp_is_none(last_temp):
|
||||
temps = (0.0, 0.0)
|
||||
print("[CookingState] No temperature data available. Returning default (0.0, 0.0).")
|
||||
else:
|
||||
temps = [last_temp[0], last_temp[1]]
|
||||
print(f"[CookingState] Returning last known temperature: {temps}")
|
||||
|
||||
# Asks the esp-wifi the temperature over UART
|
||||
if not temperature_asked:
|
||||
temperature_asked = True
|
||||
uart_device.send_as_command(UARTCommand(UARTCommandType.TEMPERATURE_REQUEST, {}))
|
||||
print("[CookingState] Requested new temperature reading from WiFi board.")
|
||||
else:
|
||||
print(f"[CookingState] Returning current temperature: {current_temp}")
|
||||
temps = [current_temp[0], current_temp[1]]
|
||||
last_temp[0] = current_temp[0]
|
||||
last_temp[1] = current_temp[1]
|
||||
current_temp[0] = None
|
||||
current_temp[1] = None
|
||||
|
||||
return temps
|
||||
|
||||
def cooking_state_on_state_change(state):
|
||||
print(f"[CookingState] State changed to: {state.state}")
|
||||
@@ -134,7 +167,7 @@ def cooking_state_on_pause(state):
|
||||
|
||||
async def uart_polling_task():
|
||||
"""Polls UART for incoming messages from the WiFi board."""
|
||||
global cooking_state
|
||||
global cooking_state, temperature_asked
|
||||
|
||||
while True:
|
||||
if uart_device and uart_device.any():
|
||||
@@ -145,6 +178,8 @@ async def uart_polling_task():
|
||||
params = command.payload
|
||||
print(f"[UART Task] Cooking parameters received: {params}")
|
||||
|
||||
uart_device.send_as_command(UARTCommand(UARTCommandType.TEMPERATURE_REQUEST, {}))
|
||||
|
||||
cooking_state = cookingState.CookingState(
|
||||
cook_time=params["cook_time"],
|
||||
power_level=params["power_level"],
|
||||
@@ -155,8 +190,13 @@ async def uart_polling_task():
|
||||
cooking_state.set_refresh_callback(cooking_state_on_refresh)
|
||||
cooking_state.set_pause_callback(cooking_state_on_pause)
|
||||
|
||||
await asyncio.sleep_ms(20)
|
||||
await asyncio.sleep_ms(200)
|
||||
cooking_state_on_state_change(cooking_state)
|
||||
elif command.command_type == UARTCommandType.TEMPERATURE_RESPONSE:
|
||||
payload = ujson.loads(command.payload)
|
||||
current_temp[0] = payload["dish_temp"]
|
||||
current_temp[1] = payload["ambient_temp"]
|
||||
temperature_asked = False
|
||||
else:
|
||||
print(f"[UART Task] Unknown command type received: {command.command_type}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user