Skip to content

Split toggle2nist component - #4350

Open
Sigma1912 wants to merge 2 commits into
LinuxCNC:masterfrom
Sigma1912:split_toggle2nist_component
Open

Split toggle2nist component#4350
Sigma1912 wants to merge 2 commits into
LinuxCNC:masterfrom
Sigma1912:split_toggle2nist_component

Conversation

@Sigma1912

@Sigma1912 Sigma1912 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Creates separate components for handling toggle buttons and momentary buttons

  • Restores the legacy functionality (as in 2.9) of 'toggle2nist' with added input debounce and a maximum output pulse length to avoid jamming up the component if 'is-on' fails to follow the 'on' / 'off' output signal.
  • Fixes the toggle2nist man page description so it matches actual (legacy) behavior.
  • Adds a new 'momentary2nist' component that does what the old 'toggle2nist' docs described plus the debounce and max puls length described above.

Background

Before #3193 'toggle2nist' component docs described the functionality as converting
momentary inputs to nist outputs but the actual functionality was converting toggled
inputs to nist outputs. This required using a preceding 'toggle' component to match
documented behavior.

The fix in #3193 changed the behavior to match the documentation (and added debouncing
to the inputs). While this fixed the issues reported on the forum it breaks functionality
for hal files that use the preceeding 'toggle' component as a workaround.
Also the name 'toggle2nist' does no longer reflect the actual function.

What is 'nist'

The term 'nist' seems to have been invented by the original author of the 'toggle2nist' component.
From the description:

[...] device that has separate on and off inputs
and an is-on output

Alternative

The 'toggle' and 'momentary' functionality could be combined in a single component.
However, due to the very descriptive component name, this new component should probably not be called 'toggle2nist'. Changing the name would then of course require an update to existing hal files.

See previous discussion: #4223

Tested

Both components have been tested on real hard ware using mechanical switches.

@Sigma1912
Sigma1912 marked this pull request as draft August 7, 2026 14:29
This component does what the legacy 'toggle2nist' man page described plus
added debounce on input pins and a maximum output pulse length to recover if
the 'is-on' signal does not change state.
@Sigma1912
Sigma1912 force-pushed the split_toggle2nist_component branch from 3fd56d3 to 90aeed6 Compare August 7, 2026 15:13
Restores the legacy functionality (as in 2.9) plus added input debounce and
and a maximum output pulse length to recover if the 'is-on' signal does not change state.
Also fixes the description so it matches actual (legacy) behavior.
@Sigma1912
Sigma1912 force-pushed the split_toggle2nist_component branch from 90aeed6 to d6070be Compare August 7, 2026 15:15
@Sigma1912
Sigma1912 marked this pull request as ready for review August 7, 2026 15:29
Comment on lines +48 to +52
if (( debounce < 1 ) || ( debounce > 10000 )) {
debounce_val = 2; // set a sane value
} else {
debounce_val = debounce;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest (reads debounce only once):

rtapi_u32 debounce_val = debounce;
if(debounce_val < 1 || debounce_val > 10000) {
    debounce_val = 2; // set a sane value
}

You may also want to cache is_on and in:

rtapi_bool inval = in;
rtapi_bool isonval = is_on;

and then use the cached values.

Comment on lines +39 to +40
variable int state;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statefollows in, which is a bool. Why isn't state a bool? Is there a specific reason for state to be an integer?

Comment on lines +37 to +38
variable unsigned debounce_val;
variable unsigned pulse_length;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debounce_val is unconditionally overwritten in the function. Having it as as variable here is expensive. Better use a local variable (see also other comment).

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.

2 participants