add edit step + do recipe in main view, added carousel for recipe selection

This commit is contained in:
2026-03-12 22:57:15 +01:00
parent 90257a62a0
commit d5dacb8fc4
21 changed files with 1052 additions and 279 deletions

View File

@@ -2,4 +2,4 @@ from .recipe_selection import RecipeSelection
from .recipe_manager import RecipeManager
from .edit_recipe import EditRecipe
from .edit_step import EditStep
from .recipe import Recipe
from .recipe import Recipe, StepType, Step

View File

@@ -1,11 +1,38 @@
from typing import Tuple
from PIL import ImageDraw, Image
from copy import deepcopy
from ..base import View
from ..button_interface import ButtonInterface
from ..list_select import ListItem, ListView
from .recipe_manager import RecipeManager
from .recipe import Recipe
from .recipe import Recipe, Step, StepType
def _make_step_item(step):
"""ListItem that renders a recipe step (icon + value)."""
def render(draw, pos, fill, **kw):
step.step_type.render(draw, pos, fill=fill, **kw)
font_size = kw.get('font_size', 10)
draw.text((pos[0] + font_size + 12, pos[1]), step.value_str, fill=fill, **kw)
return ListItem(render)
def _make_add_item():
"""ListItem that renders as a '+' add button."""
return ListItem(lambda draw, pos, fill, **kw: draw.text(pos, '+', fill=fill, **kw))
def _make_cancel_item():
"""ListItem that renders as 'Cancel'."""
return ListItem(lambda draw, pos, fill, **kw: draw.text(pos, 'Cancel', fill=fill, **kw))
def _make_save_item():
"""ListItem that renders as 'Save'."""
return ListItem(lambda draw, pos, fill, **kw: draw.text(pos, 'Save', fill=fill, **kw))
class EditRecipe(View, ButtonInterface):
def __init__(self, parent, im_size, center,
@@ -22,108 +49,144 @@ class EditRecipe(View, ButtonInterface):
self.recipe = Recipe("New", [])
self.recipe_manager.tmp_recipe = self.recipe
else:
self.recipe = recipe_manager.get_recipe(recipe_id)
self.recipe = deepcopy(recipe_manager.get_recipe(recipe_id))
self.confirm_view = False
self.selected_field = 0 # 0: name, 1+: steps
self.move_mode = False
self.item_list = ListView(x_offset=40, max_visible=5, font_size=15)
self._rebuild_items()
super().__init__(parent, im_size, center)
def _get_visual_steps(self):
steps = self.recipe.steps + ['+', 'BACK']
if len(steps) < 4:
return steps, 0
start = max(0, self.selected_field - 2)
end = min(len(steps), start + 4)
steps = steps[start:end]
def _make_name_item(self):
"""ListItem that renders the recipe name field."""
recipe = self.recipe
def render(draw, pos, fill, **kw):
draw.text(pos, f"Name: {recipe.name}", fill=fill, **kw)
return ListItem(render)
return steps, start
def _rebuild_items(self):
"""Rebuild list items from current recipe state."""
old_index = self.item_list.selected_index
items = [self._make_name_item()]
for step in self.recipe.steps:
items.append(_make_step_item(step))
if self.move_mode:
items.append(_make_cancel_item())
else:
items.append(_make_add_item())
items.append(_make_save_item())
self.item_list.items = items
self.item_list.selected_index = min(old_index, len(items) - 1)
def update_weight(self, weight: float) -> Image.Image:
im = self.bkg_im.copy()
draw = ImageDraw.Draw(im)
x = 40
draw.text((x, 10), "Name:", fill='black')
if self.selected_field == 0:
r = 10
offset = 35
for i in range(0, 90, r // 2):
draw.circle((x + i, offset), r, fill='black')
draw.text((x, 30), self.recipe.name, fill='white')
else:
draw.text((x, 30), self.recipe.name, fill='black')
visual_steps, start = self._get_visual_steps()
for idx, step in enumerate(visual_steps):
y_pos = 60 + idx * 20
if start + idx + 1 == self.selected_field:
r = 10
offset = 15
for i in range(0, 90, r // 2):
draw.circle((x + i, y_pos), r, fill='black')
if str(step) == '+':
draw.text((x, y_pos - 5), '+', fill='white')
elif str(step) == 'BACK':
draw.regular_polygon((x + 5, y_pos, 5), 3, fill='white', rotation=90)
else:
step.step_type.render(draw, (x, y_pos - 5), fill='white')
draw.text((x + 30, y_pos - 5), step.value_str, fill='white')
elif str(step) == '+':
draw.text((x, y_pos - 5), '+', fill='black')
elif str(step) == 'BACK':
draw.regular_polygon((x + 5, y_pos, 5), 3, fill='black', rotation=90)
else:
step.step_type.render(draw, (x, y_pos - 5), fill='black')
draw.text((x + 30, y_pos - 5), step.value_str, fill='black')
self._rebuild_items()
self.item_list.render(draw, y_start=10)
if self.move_mode:
draw.text((2, 10), '\u2195', fill='black')
return im
def left_press(self):
self.selected_field = (self.selected_field - 1) % (len(self.recipe.steps) + 3)
def left_long_press(self):
if self.selected_field == len(self.recipe.steps) + 2:
# back
self.deactivate_command()
elif self.selected_field == len(self.recipe.steps) + 1:
# add step
pass
if self.move_mode:
idx = self.item_list.selected_index
num_steps = len(self.recipe.steps)
if 1 < idx <= num_steps: # swap step upward
steps = self.recipe.steps
step_idx = idx - 1
steps[step_idx - 1], steps[step_idx] = steps[step_idx], steps[step_idx - 1]
self.item_list.selected_index -= 1
elif idx == num_steps + 1: # on cancel -> move to last step (clamped, no wrap)
self.item_list.selected_index = num_steps
else:
# edit name
self.edit_step_command(self.recipe_id, self.selected_field)
self.item_list.select_previous()
def left_long_press(self):
if self.move_mode:
idx = self.item_list.selected_index
num_steps = len(self.recipe.steps)
if idx == num_steps + 1: # cancel item
self.move_mode = False
return
# always go back (discard unsaved changes)
self.deactivate_command()
def right_press(self):
self.selected_field = (self.selected_field + 1) % (len(self.recipe.steps) + 3)
if self.move_mode:
idx = self.item_list.selected_index
num_steps = len(self.recipe.steps)
if 1 <= idx < num_steps: # swap step downward
steps = self.recipe.steps
step_idx = idx - 1
steps[step_idx], steps[step_idx + 1] = steps[step_idx + 1], steps[step_idx]
self.item_list.selected_index += 1
elif idx == num_steps: # on last step -> move to cancel (clamped, no wrap)
self.item_list.selected_index = num_steps + 1
# on cancel or name: do nothing
else:
self.item_list.select_next()
def both_long_press(self):
idx = self.item_list.selected_index
num_steps = len(self.recipe.steps)
if self.move_mode:
self.move_mode = False
elif 1 <= idx <= num_steps:
self.move_mode = True
def right_long_press(self):
# save
if self.is_add_form:
self.recipe_manager.add_recipe(self.recipe)
if self.move_mode:
return
idx = self.item_list.selected_index
num_steps = len(self.recipe.steps)
if idx == num_steps + 2:
# save item
if self.is_add_form:
self.recipe_manager.add_recipe(self.recipe)
else:
self.recipe_manager.update_recipe(self.recipe_id, self.recipe)
self.deactivate_command()
elif idx == num_steps + 1:
# add step
new_step = Step(StepType.SECTION, "")
self.recipe.steps.append(new_step)
self.edit_step_command(self.recipe_id, num_steps + 1)
else:
self.recipe_manager.update_recipe(self.recipe_id, self.recipe)
# if view is in edit mode, the recipe is already updated (by reference)
self.deactivate_command()
# edit name (idx=0) or step (idx=1+)
self.edit_step_command(self.recipe_id, idx)
def has_button(self) -> Tuple[bool, bool, bool, bool]:
return True, True, True, True
def render_left_press(self, draw, x, y):
draw.regular_polygon((x, y+2, 5), 3, fill='black')
def render_left_long_press(self, draw, x, y):
draw.text((x, y-5), 'Enter', fill='black')
if self.move_mode:
draw.text((x, y-5), 'Cancel', fill='black')
else:
draw.text((x, y-5), 'Back', fill='black')
def render_right_press(self, draw, x, y):
draw.regular_polygon((x, y+4, 5), 3, fill='black', rotation=180)
def render_right_long_press(self, draw, x, y):
draw.text((x - 20, y-5), 'Save', fill='black')
if self.move_mode:
return
idx = self.item_list.selected_index
num_steps = len(self.recipe.steps)
if idx == num_steps + 2:
draw.text((x - 20, y-5), 'Save', fill='black')
elif idx == num_steps + 1:
draw.text((x - 15, y-5), 'Add', fill='black')
else:
draw.text((x - 15, y-5), 'Edit', fill='black')
def render_both_long_press(self, draw, x, y):
idx = self.item_list.selected_index
num_steps = len(self.recipe.steps)
if self.move_mode:
draw.text((x - 15, y - 5), 'Confirm', fill='black')
elif 1 <= idx <= num_steps:
draw.text((x - 10, y - 5), 'Move', fill='black')

View File

@@ -6,11 +6,51 @@ from MorseCodePy import decode
from ..base import View
from ..button_interface import ButtonInterface
from ..list_select import ListItem, ListView
from .recipe_manager import RecipeManager
from .recipe import Recipe, Step, StepType
# Step types that accept a typed value
_TYPES_WITH_VALUE = {StepType.SECTION, StepType.WEIGH, StepType.WEIGH_WITH_TIMER}
# Step types whose value is numeric (use numbers language for morse)
_NUMERIC_TYPES = {StepType.WEIGH, StepType.WEIGH_WITH_TIMER}
def _make_type_item(step_type: StepType) -> ListItem:
"""ListItem showing a StepType icon + name."""
def render(draw, pos, fill, **kw):
step_type.render(draw, pos, fill=fill)
draw.text((pos[0] + 30, pos[1]), step_type.name, fill=fill, **kw)
return ListItem(render)
def _make_delete_item() -> ListItem:
"""ListItem that renders as a delete/trash label."""
return ListItem(
lambda draw, pos, fill, **kw: draw.text(pos, 'Delete', fill=fill, **kw)
)
class EditStep(View, ButtonInterface):
"""Edit a single recipe step (or the recipe name).
Flow for name editing (step_index == 0):
- Go straight to morse value entry.
Flow for step editing (step_index >= 1):
Phase 0 - select step type via ListView.
left_press : navigate up
right_press : navigate down
left_long_press : confirm type -> advance to phase 1, or save
immediately if the type carries no value
Phase 1 - enter value via morse.
left_press : cancel
left_long_press : save and exit
right_press : morse dot '.'
right_long_press : morse dash '-'
"""
def __init__(self, parent, im_size, center,
recipe_id: int,
step_index: int,
@@ -20,133 +60,260 @@ class EditStep(View, ButtonInterface):
self.recipe_manager = recipe_manager
self.recipe_id = recipe_id
self.step_index = step_index
if step_index == 0:
self.step = recipe_manager.get_recipe(recipe_id).name
self.confirm_view = False
self.edit_step = 0 # 0: type, 1: step/name value
self.new_type = ''
recipe = recipe_manager.get_recipe(recipe_id)
if step_index == 0:
self.step = recipe.name # str -> name editing
else:
self.step = recipe.steps[step_index - 1] # Step
# edit_step: 0 = type selection | 1 = value entry | 2 = goal time entry
# Name editing starts directly in phase 1.
self.edit_step = 1 if step_index == 0 else 0
# Type selection list (used only when editing a Step)
self.type_list = ListView(x_offset=40, max_visible=5)
self.type_list.items = [_make_type_item(t) for t in StepType] + [_make_delete_item()]
if isinstance(self.step, Step):
all_types = list(StepType)
self.type_list.selected_index = all_types.index(self.step.step_type)
self.new_type = self.step.step_type
else:
self.new_type = None
self.new_value = ''
self.new_goal_time = ''
self.value_cursor = 0
self.value_cursor_pulse = 0.0
self.morse_buffer = ''
self.morse_code_language = 'english'
if isinstance(self.step, Step) and \
self.step.step_type in [StepType.START_TIME, StepType.WEIGH]:
self.morse_code_language = 'numbers'
self.morse_code_language = self._morse_language_for(self.new_type)
self.last_input = None
self.letter_timeout = 2.0 # seconds
super().__init__(parent, im_size, center)
def update_weight(self, weight: float) -> Image.Image:
im = self.bkg_im.copy()
draw = ImageDraw.Draw(im)
# ------------------------------------------------------------------
# helpers
# ------------------------------------------------------------------
x = 40
@staticmethod
def _morse_language_for(step_type) -> str:
return 'numbers' if step_type in _NUMERIC_TYPES else 'english'
def _process_morse_timeout(self):
"""Decode pending morse buffer after the letter timeout."""
if self.last_input is not None:
if time() - self.last_input > self.letter_timeout:
if len(self.morse_buffer) < 10:
self.value_cursor += 1
self.new_value += decode(self.morse_buffer, language=self.morse_code_language).upper()
if self.edit_step == 2:
if len(self.morse_buffer) < 10:
decoded = decode(self.morse_buffer, language=self.morse_code_language)
self.new_goal_time += decoded.upper()
self.value_cursor += 1
else:
self.new_goal_time = self.new_goal_time[:-1]
self.value_cursor = max(0, self.value_cursor - 1)
else:
self.value_cursor -= 1
self.value_cursor = max(0, self.value_cursor)
self.new_value = self.new_value[:-1]
# process morse buffer
if len(self.morse_buffer) < 10:
decoded = decode(self.morse_buffer, language=self.morse_code_language)
self.new_value += decoded.upper()
self.value_cursor += 1
else:
self.new_value = self.new_value[:-1]
self.value_cursor = max(0, self.value_cursor - 1)
self.last_input = None
self.morse_buffer = ''
def _render_value_entry(self, draw: ImageDraw.ImageDraw, x: int):
"""Render the morse value-entry screen."""
font_size = 16
y_start = 30
if isinstance(self.step, str):
font_size = 16
y_start = 30
draw.text((x, y_start), "Name:", fill='black')
draw.text((x, y_start + 20), self.new_value, fill='black', font_size=font_size)
if self.value_cursor_pulse > 1.0:
draw.rectangle((x + self.value_cursor * 10,
y_start + 20 - 2,
x + self.value_cursor * 10 + 8,
y_start + 34),
fill='black')
if self.value_cursor_pulse > 2.0:
self.value_cursor_pulse = 0.0
self.value_cursor_pulse += 0.1
draw.line((x, y_start + 37, x + 80, y_start + 37), fill='black')
if self.morse_buffer != '':
for i, suffix in enumerate(['']):#, '.', '-', '.' * 10]):
letter = 'del'
if len(suffix) != 10 and len(self.morse_buffer) + len(suffix) < 10:
letter = decode(self.morse_buffer + suffix, language=self.morse_code_language)
draw.text((x, y_start + 50 + i * 15),
f'{self.morse_buffer + suffix} {letter}',
fill='black', font_size=14)
elif self.edit_step == 0:
pass
label = "Name:"
elif self.edit_step == 2:
label = "Goal time(s):"
else:
pass
label = f"{self.new_type.name}:"
draw.text((x, y_start), label, fill='black')
# visual_steps, start = self._get_visual_steps()
current_value = self.new_goal_time if self.edit_step == 2 else self.new_value
draw.text((x, y_start + 20), current_value, fill='black', font_size=font_size)
# for idx, step in enumerate(visual_steps):
# y_pos = 60 + idx * 20
# if start + idx + 1 == self.selected_field:
# r = 10
# offset = 15
# for i in range(0, 90, r // 2):
# draw.circle((x + i, y_pos), r, fill='black')
if self.value_cursor_pulse > 1.0:
draw.rectangle(
(x + self.value_cursor * 10, y_start + 18,
x + self.value_cursor * 10 + 8, y_start + 34),
fill='black'
)
self.value_cursor_pulse += 0.1
if self.value_cursor_pulse > 2.0:
self.value_cursor_pulse = 0.0
draw.line((x, y_start + 37, x + 80, y_start + 37), fill='black')
# if str(step) == '+':
# draw.text((x, y_pos - 5), '+', fill='white')
# elif str(step) == 'BACK':
# draw.regular_polygon((x + 5, y_pos, 5), 3, fill='white', rotation=90)
# else:
# step.step_type.render(draw, (x, y_pos - 5), fill='white')
# draw.text((x + 30, y_pos - 5), step.value_str, fill='white')
if self.morse_buffer:
letter = decode(self.morse_buffer, language=self.morse_code_language) \
if len(self.morse_buffer) < 10 else 'del'
draw.text((x, y_start + 50), f'{self.morse_buffer} -> {letter}',
fill='black', font_size=14)
# elif str(step) == '+':
# draw.text((x, y_pos - 5), '+', fill='black')
# elif str(step) == 'BACK':
# draw.regular_polygon((x + 5, y_pos, 5), 3, fill='black', rotation=90)
# else:
# step.step_type.render(draw, (x, y_pos - 5), fill='black')
# draw.text((x + 30, y_pos - 5), step.value_str, fill='black')
def _render_type_selection(self, draw: ImageDraw.ImageDraw, x: int):
"""Render the step-type selection screen."""
draw.text((x, 10), "Step type:", fill='black')
self.type_list.render(draw, y_start=30)
# ------------------------------------------------------------------
# View
# ------------------------------------------------------------------
def update_weight(self, weight: float) -> Image.Image:
im = self.bkg_im.copy()
draw = ImageDraw.Draw(im)
x = 40
if isinstance(self.step, str) or self.edit_step >= 1:
self._process_morse_timeout()
self._render_value_entry(draw, x)
else:
self._render_type_selection(draw, x)
return im
# ------------------------------------------------------------------
# ButtonInterface
# ------------------------------------------------------------------
def left_press(self):
self.deactivate_command()
if isinstance(self.step, str) or self.edit_step >= 1:
self.deactivate_command()
else:
self.type_list.select_previous()
def left_long_press(self):
if self.step_index == 0:
# editing name
if isinstance(self.step, str):
if self.new_value:
recipe = self.recipe_manager.get_recipe(self.recipe_id)
recipe.name = self.new_value
self.deactivate_command()
elif self.edit_step == 0:
# always go back from type selection
self.deactivate_command()
elif self.edit_step == 1 and self.new_type == StepType.WEIGH_WITH_TIMER:
# Save weight value and advance to goal time entry
raw = self.new_value
if raw:
try:
self._saved_weight = int(raw)
except ValueError:
self._saved_weight = None
else:
self._saved_weight = None
self.edit_step = 2
self.morse_code_language = 'numbers'
self.new_goal_time = ''
self.value_cursor = 0
self.morse_buffer = ''
self.last_input = None
else:
pass
if self.edit_step == 2:
raw_goal = self.new_goal_time
try:
goal_time = float(raw_goal) if raw_goal else 0.0
except ValueError:
goal_time = 0.0
value = self._saved_weight
else:
raw = self.new_value
if raw and self.new_type in _NUMERIC_TYPES:
try:
value = int(raw)
except ValueError:
value = None
elif raw:
value = raw
else:
value = None
goal_time = 0.0
recipe = self.recipe_manager.get_recipe(self.recipe_id)
step = recipe.steps[self.step_index - 1]
step.step_type = self.new_type
step.value = value
step.goal_time = goal_time
self.deactivate_command()
def right_press(self):
self.last_input = time()
self.morse_buffer += '.'
if isinstance(self.step, str) or self.edit_step >= 1:
self.last_input = time()
self.morse_buffer += '.'
else:
self.type_list.select_next()
def right_long_press(self):
self.last_input = time()
self.morse_buffer += '-'
if isinstance(self.step, str) or self.edit_step >= 1:
self.last_input = time()
self.morse_buffer += '-'
elif self.edit_step == 0:
all_types = list(StepType)
if self.type_list.selected_index == len(all_types):
# delete item selected
recipe = self.recipe_manager.get_recipe(self.recipe_id)
del recipe.steps[self.step_index - 1]
self.deactivate_command()
return
self.new_type = all_types[self.type_list.selected_index]
if self.new_type not in _TYPES_WITH_VALUE:
# No value needed -> save immediately
recipe = self.recipe_manager.get_recipe(self.recipe_id)
recipe.steps[self.step_index - 1].step_type = self.new_type
recipe.steps[self.step_index - 1].value = None
self.deactivate_command()
else:
# Advance to value entry
self.morse_code_language = self._morse_language_for(self.new_type)
self.new_value = ''
self.value_cursor = 0
self.morse_buffer = ''
self.last_input = None
self.edit_step = 1
def has_button(self) -> Tuple[bool, bool, bool, bool]:
return True, True, True, False
return True, True, True, True
# ------------------------------------------------------------------
# Button hint rendering
# ------------------------------------------------------------------
def render_left_press(self, draw, x, y):
draw.text((x, y-5), 'Cancel', fill='black')
if isinstance(self.step, str) or self.edit_step >= 1:
draw.text((x, y - 5), 'Cancel', fill='black')
else:
draw.regular_polygon((x, y + 2, 5), 3, fill='black')
def render_left_long_press(self, draw, x, y):
draw.text((x, y-5), 'Next', fill='black')
if self.edit_step == 0 and not isinstance(self.step, str):
draw.text((x, y - 5), 'Back', fill='black')
elif self.edit_step == 1 and self.new_type == StepType.WEIGH_WITH_TIMER:
draw.text((x, y - 5), 'Next', fill='black')
else:
draw.text((x, y - 5), 'Save', fill='black')
def render_right_press(self, draw, x, y):
draw.text((x - 30, y), 'Morse', fill='black')
if isinstance(self.step, str) or self.edit_step >= 1:
draw.text((x - 5, y - 5), '.', fill='black')
else:
draw.regular_polygon((x, y + 4, 5), 3, fill='black', rotation=180)
def render_right_long_press(self, draw, x, y):
if isinstance(self.step, str) or self.edit_step >= 1:
draw.text((x - 5, y - 5), '-', fill='black')
elif self.edit_step == 0:
all_types = list(StepType)
if self.type_list.selected_index == len(all_types):
draw.text((x - 20, y - 5), 'Delete', fill='black')
else:
draw.text((x - 20, y - 5), 'Select', fill='black')

View File

@@ -8,7 +8,7 @@ from ..draw_utils import draw_clock
class Recipe:
def __init__(self, name: str, steps: list[Step]):
self.name = name
self.steps = steps + [Step(StepType.SECTION, "Enjoy")]
self.steps = steps
def __str__(self):
return self.name
@@ -19,48 +19,50 @@ class Recipe:
class StepType(Enum):
SECTION = 0
WEIGH = 1
START_TIME = 2
WAIT_TIME_FINISHED = 3
TARE = 4
WEIGH_WITH_TIMER = 2
TARE = 3
def render(self, draw, position, fill='black') -> str:
def render(self, draw, position, fill='black', **kwargs) -> str:
font_size = kwargs.get('font_size', 10)
if self == StepType.SECTION:
draw.text(position, "T", fill=fill)
draw.text(position, "T", fill=fill, **kwargs)
elif self == StepType.WEIGH:
draw.text(position, "W", fill=fill)
elif self == StepType.START_TIME or self == StepType.WAIT_TIME_FINISHED:
draw_clock(draw, (position[0] + 3, position[1] + 5), radius=3, color=fill)
draw.text(position, "W", fill=fill, **kwargs)
elif self == StepType.WEIGH_WITH_TIMER:
draw.text(position, "W", fill=fill, **kwargs)
r = max(3, font_size // 4)
draw_clock(draw, (position[0] + font_size + 2, position[1] + font_size // 2), radius=r, color=fill)
elif self == StepType.TARE:
draw.text(position, "0.0g", fill=fill)
draw.text(position, "0.0g", fill=fill, **kwargs)
class Step:
def __init__(self,
step_type: StepType,
value: float | str = None):
value: float | str = None,
goal_time: float = 0.0):
self.step_type = step_type
self.value = value
self.goal_time = goal_time
@property
def value_str(self) -> str:
if self.step_type in [StepType.WEIGH]:
if self.step_type == StepType.WEIGH:
return f"{self.value}g"
elif self.step_type == StepType.START_TIME:
if self.value == -1:
return "Start"
else:
minutes = self.value // 60
seconds = self.value % 60
elif self.step_type == StepType.WEIGH_WITH_TIMER:
s = f"{self.value}g"
if self.goal_time > 0:
minutes = int(self.goal_time) // 60
seconds = int(self.goal_time) % 60
if minutes == 0:
return f"{seconds}s"
s += f" {seconds}s"
else:
return f"{minutes}:{seconds:02d}"
s += f" {minutes}:{seconds:02d}"
return s
elif self.step_type == StepType.SECTION:
return str(self.value)
elif self.step_type == StepType.TARE:
return "Tare"
elif self.step_type == StepType.WAIT_TIME_FINISHED:
return "Wait"
else:
return ""
@@ -75,10 +77,7 @@ V60 = Recipe(
Step(StepType.SECTION, "Brew"),
Step(StepType.TARE),
Step(StepType.START_TIME, 45),
Step(StepType.WEIGH, 50),
Step(StepType.WAIT_TIME_FINISHED),
Step(StepType.START_TIME, -1),
Step(StepType.WEIGH_WITH_TIMER, 50, goal_time=45),
Step(StepType.WEIGH, 250),
]
)
@@ -92,7 +91,6 @@ ESPRESSO = Recipe(
Step(StepType.SECTION, "Brew"),
Step(StepType.TARE),
Step(StepType.START_TIME, -1),
Step(StepType.WEIGH, 40),
Step(StepType.WEIGH_WITH_TIMER, 40, goal_time=0.0),
]
)

View File

@@ -1,4 +1,5 @@
from .recipe import V60, ESPRESSO, Recipe
from typing import List
from .recipe import V60, ESPRESSO, Recipe, Step, StepType
class RecipeManager:
def __init__(self):
@@ -7,6 +8,8 @@ class RecipeManager:
ESPRESSO
]
self.tmp_recipe = None
self.active_recipe: Recipe = None
self.current_recipe_step_id = 0
def add_recipe(self, recipe):
self.recipes.append(recipe)
@@ -21,6 +24,46 @@ class RecipeManager:
return self.recipes.index(recipe)
def get_recipe(self, recipe_id) -> Recipe:
if recipe_id == None:
if recipe_id is None:
return self.tmp_recipe
return self.recipes[recipe_id]
return self.recipes[recipe_id]
def activate_recipe(self, recipe_id):
self.active_recipe = self.get_recipe(recipe_id)
def deactivate_recipe(self):
self.active_recipe = None
self.current_recipe_step_id = 0
def get_current_step(self) -> List[Step]:
steps = [None] * 3
if self.active_recipe is None:
return steps
if self.current_recipe_step_id > len(self.active_recipe.steps):
return steps
if self.current_recipe_step_id > 0:
steps[0] = self.active_recipe.steps[self.current_recipe_step_id - 1]
if self.current_recipe_step_id < len(self.active_recipe.steps):
steps[1] = self.active_recipe.steps[self.current_recipe_step_id]
elif self.current_recipe_step_id == len(self.active_recipe.steps):
steps[1] = Step(StepType.SECTION, "Enjoy")
if self.current_recipe_step_id < len(self.active_recipe.steps) - 1:
steps[2] = self.active_recipe.steps[self.current_recipe_step_id + 1]
return steps
def next_step(self) -> List[Step]:
if self.active_recipe is None:
return [None] * 3
self.current_recipe_step_id = min(self.current_recipe_step_id + 1, len(self.active_recipe.steps) + 1)
return self.get_current_step()
def previous_step(self) -> List[Step]:
if self.active_recipe is None:
return [None] * 3
self.current_recipe_step_id = max(0, self.current_recipe_step_id - 1)
return self.get_current_step()

View File

@@ -2,12 +2,18 @@ from typing import Tuple
from ..base import View
from ..button_interface import ButtonInterface
from ..list_select import ListItem, ListView, CarouselView
from .recipe_manager import RecipeManager
from .recipe import V60, ESPRESSO
from PIL import ImageDraw, Image
def _make_text_item(text):
"""ListItem that renders as plain text (forwards kwargs such as font_size)."""
return ListItem(lambda draw, pos, fill, t=text, **kw: draw.text(pos, str(t), fill=fill, **kw))
class RecipeSelection(View, ButtonInterface):
@property
@@ -17,70 +23,56 @@ class RecipeSelection(View, ButtonInterface):
def __init__(self, parent, im_size, center,
recipe_manager: RecipeManager = None,
edit_recipe_command=None,
run_recipe_command=None,
deactivate_command=None):
self.selected_index = 0
self.deactivate_command = deactivate_command
self.recipe_manager = recipe_manager
self.edit_recipe_command = edit_recipe_command
self.run_recipe_command = run_recipe_command
self.item_list = CarouselView(render_height=124)
# self.item_list = ListView(x_offset=40, max_visible=5)
self._rebuild_items()
super().__init__(parent, im_size, center)
def _get_visual_recipes(self):
recipes = self.recipes + ['+', 'BACK']
if len(recipes) < 5:
return recipes, 0
start = max(0, self.selected_index - 2)
end = min(len(recipes), start + 5)
recipes = recipes[start:end]
return recipes, start
def _rebuild_items(self):
"""Rebuild list items from current recipes."""
old_index = self.item_list.selected_index
items = [_make_text_item(r) for r in self.recipes]
items.append(_make_text_item('+'))
self.item_list.items = items
self.item_list.selected_index = min(old_index, len(items) - 1)
def update_weight(self, weight: float) -> Image.Image:
im = self.bkg_im.copy()
draw = ImageDraw.Draw(im)
recipes, start = self._get_visual_recipes()
for idx, recipe in enumerate(recipes):
if idx + start == self.selected_index:
r = 10
offset = 15
for i in range(0, 90, r // 2):
draw.circle((40 + i, offset + idx * 20), r, fill='black')
if str(recipe) != 'BACK':
draw.text((40, 10 + idx * 20), str(recipe), fill='white')
else:
draw.regular_polygon((40 + 5, 15 + idx * 20, 5), 3, fill='white', rotation=90)
elif str(recipe) == 'BACK':
draw.regular_polygon((40 + 5, 15 + idx * 20, 5), 3, fill='black', rotation=90)
else:
draw.text((40, 10 + idx * 20), str(recipe), fill='black')
self._rebuild_items()
self.item_list.render(draw, y_start=10)
return im
def left_press(self):
self.selected_index = (self.selected_index - 1) % (len(self.recipes) + 2)
self.item_list.select_previous()
def left_long_press(self):
if self.selected_index < len(self.recipes):
self.edit_recipe_command(self.selected_index)
self.item_list.selected_index = 0
self.deactivate_command()
def right_press(self):
self.selected_index = (self.selected_index + 1) % (len(self.recipes) + 2)
self.item_list.select_next()
def right_long_press(self):
if self.selected_index < len(self.recipes):
# activate selected recipe
print(f"Activating recipe: {self.recipes[self.selected_index]}")
elif self.selected_index == len(self.recipes):
idx = self.item_list.selected_index
if idx < len(self.recipes):
# run selected recipe
if self.run_recipe_command:
self.run_recipe_command(idx)
elif idx == len(self.recipes):
# add new recipe
self.edit_recipe_command()
else:
# back
self.selected_index = 0
self.deactivate_command()
def both_long_press(self):
idx = self.item_list.selected_index
if idx < len(self.recipes):
self.edit_recipe_command(idx)
def has_button(self) -> Tuple[bool, bool, bool, bool]:
return True, True, True, True
@@ -89,10 +81,19 @@ class RecipeSelection(View, ButtonInterface):
draw.regular_polygon((x, y+2, 5), 3, fill='black')
def render_left_long_press(self, draw, x, y):
draw.text((x, y-5), 'Edit', fill='black')
draw.text((x, y-5), 'Back', fill='black')
def render_right_press(self, draw, x, y):
draw.regular_polygon((x, y+4, 5), 3, fill='black', rotation=180)
def render_right_long_press(self, draw, x, y):
draw.regular_polygon((x+2, y, 5), 3, fill='black', rotation=270)
idx = self.item_list.selected_index
if idx < len(self.recipes):
draw.text((x - 15, y-5), 'Run', fill='black')
else:
draw.text((x - 15, y-5), 'Add', fill='black')
def render_both_long_press(self, draw, x, y):
idx = self.item_list.selected_index
if idx < len(self.recipes):
draw.text((x - 10, y - 5), 'Edit', fill='black')