Skip to content

Simplify template parameters to a single per-element buffer - #403

Draft
SukuWc wants to merge 4 commits into
mainfrom
SUKU-template-buffer-simplify
Draft

Simplify template parameters to a single per-element buffer#403
SukuWc wants to merge 4 commits into
mainfrom
SUKU-template-buffer-simplify

Conversation

@SukuWc

@SukuWc SukuWc commented Jul 16, 2026

Copy link
Copy Markdown
Member

Summary

Replaces the per-page linked list of template buffers with a single buffer per element, reinitialized to defaults on page change — instead of allocating one buffer per page and swapping the live template_parameter_list pointer on page load.

Changes

  • Remove struct grid_ui_template_buffer and the buffer list (template_buffer_list_head, _create/_find/_list_length).
  • Add grid_ui_element_template_parameter_reset(): lazily allocates the element's single buffer, then runs the initializer to load defaults.
  • Retype template_init_t and all *_template_parameter_init functions (including the two VSN screen variants) to take struct grid_ui_element* instead of the removed buffer type.
  • Drop the now-redundant page_change_cb mechanism entirely — potmeter was its only user, and its value refresh is already performed by the initializer that runs on each page load.
  • Simplify grid_ui_page_clear_template_parameters (drop the unused page arg) and update its grid_decode.c call sites.
  • Fix common/test/grid_ui_encoder_test.c to use the new API.

Net: 19 files changed, +57 / −190. Firmware binary shrinks ~128 bytes.

Behavior

Stored config is unaffected: grid_ui_page_read re-runs the config scripts after the reset, so configured min/max/mode are re-established — only transient runtime-only gtv values reset on page change.

Concurrency note (reviewed, benign)

The reset now mutates the live buffer in place (main task) while the ADC ISR reads template_parameter_list[MODE]. This is safe: all slots are 4-byte-aligned int32_t (atomic on Xtensa-LX7 / Cortex-M4), the buffer pointer is stable during a reset, the ISR's array index comes from ele->index (not the buffer), and any transient value self-heals on the next ADC sample. Worst case is one sample binned at the default resolution during the brief page-load window — already the intended reset-to-defaults behavior.

Testing

  • ✅ Host unit tests: 1/1 pass
  • ✅ ESP32-S3 firmware builds clean (lua_buildpico_buildesp_build)

🤖 Generated with Claude Code

Replace the per-page linked list of template buffers with a single
buffer per element that is reinitialized to defaults on every page
change, instead of allocating one buffer per page and swapping the
live pointer on page load.

- Remove struct grid_ui_template_buffer and the buffer list
  (template_buffer_list_head, _create/_find/_list_length).
- Add grid_ui_element_template_parameter_reset(): lazily allocates the
  element's single buffer, then runs the initializer to load defaults.
- Retype template_init_t and all *_template_parameter_init functions to
  take struct grid_ui_element* instead of the removed buffer type.
- Drop the now-redundant page_change_cb mechanism entirely (potmeter was
  its only user, and its value refresh is already done by the
  initializer that runs on each page load).
- Update grid_ui_page_clear_template_parameters (drop unused page arg)
  and its decode.c call sites.

Stored config is unaffected: grid_ui_page_read re-runs the config
scripts after the reset, so only transient runtime-only gtv values reset
on page change.

Verified: host tests pass (1/1), ESP32 firmware builds clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@SukuWc
SukuWc marked this pull request as draft July 16, 2026 19:12
SukuWc and others added 3 commits August 10, 2026 08:54
…eset

Split the single-buffer lifecycle into an explicit allocate-at-init step
and a reset step, removing the lazy allocation and a now-redundant helper:

- Add grid_ui_element_template_parameter_init(): allocates the element's
  template parameter buffer once (sized to template_parameter_list_length,
  no phantom min-1 slot) and seeds it with defaults. Called from the tail
  of each element type-init, where the length is known.
- Fold the template-parameter reset into grid_ui_element_reset() with two
  guard clauses, and drop the standalone
  grid_ui_element_template_parameter_reset() — grid_ui_element_reset is now
  the single per-element runtime reset (timer, name, template parameters).
- Simplify grid_ui_bulk_page_load to a single grid_ui_element_reset() call
  per element (drops the redundant timer clear).
- Remove grid_ui_page_clear_template_parameters() and its two decode.c call
  sites: after a store/clear the following bulk_page_load already resets
  every element to defaults, so the synchronous clear was redundant.
- Update the encoder unit test to rely on encoder_init's allocate+seed.

Allocation now happens once at boot before any read, matching the original
seed-at-creation behavior; page load reseeds via grid_ui_element_reset.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
grid_ui_event_get_script passes the address of a stack buffer to the Lua
"gsg" helper as an integer, which writes the script source back into it.
Both ends cast through uint32_t, which truncates the pointer on 64-bit
builds (the host unit-test build warns with -Wpointer-to-int-cast). On the
32-bit target pointers fit, so this was silent there but is a latent bug
off-target.

Route the pointer through uintptr_t on both sides and format it as
unsigned long long (%llu), which matches Lua's 64-bit lua_Integer. Warning-
free and correct on both 32- and 64-bit; unchanged behavior on the target.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant