diff options
Diffstat (limited to 'ui/code_templates.py')
| -rw-r--r-- | ui/code_templates.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/ui/code_templates.py b/ui/code_templates.py index ffa1662..2a3ff09 100644 --- a/ui/code_templates.py +++ b/ui/code_templates.py @@ -19,6 +19,15 @@ Namespace available in all kinds: math — Python math module vars — shared SignalProcessor._script_vars dict (read/write) state — per-channel persistent dict (survives between evaluations) + +`vars` is the bridge from the Control Panel into Channel math: a button's +"On Action Script" (ui/control_editor.py) and every Channel script here see +the *same* `vars` dict. e.g. a "Zero" button sets + vars['zero'] = channels.get('force_raw', 0) +and a Channel reads it back with `vars.get('zero', 0)` — see the +"custom_script" template below. Same pattern for an operator-entered +constant like specimen length: a Setpoint control's On Action Script sets +`vars['specimen_length_cm'] = value`, and a Channel divides by it. """ BUILTIN_TEMPLATES: dict = { @@ -27,6 +36,14 @@ BUILTIN_TEMPLATES: dict = { result = x[0] """, + "scale_offset": """\ +# scale/offset also settable from a Control Panel button's On Action Script, +# e.g. vars["offset"] = -x[0] to zero the reading on press +scale = vars.get("scale", 1.0) +offset = vars.get("offset", 0.0) +result = x[0] * scale + offset +""", + "derivative": """\ prev_v = state.get("v", x[0]) prev_t = state.get("t", t) @@ -99,11 +116,14 @@ SCRIPT_TEMPLATES: dict = { "expression": "x[0]", "function": """\ +# "offset" can be set here or from a Control Panel button's On Action Script +# (vars is the same dict on both sides) offset = vars.get("offset", 0) return x[0] - offset """, "custom_script": """\ +# Wire a "Zero" button to this: On Action Script -> vars["zero"] = channels.get("<signal_id>", 0) zero = vars.get("zero", 0) result = x[0] - zero """, |
