component toggle "'push-on, push-off' from momentary pushbuttons";
pin in bit in "button input";
pin io bit out "on/off output";
pin io u32 debounce = 2 "debounce delay in periods";

variable hal_u32_t debounce_cntr = 0;
variable hal_bit_t debounced = false;

function _ nofp;
license "GPL";
;;

FUNCTION(_)
{

    if (( debounce < 1 ) || ( debounce > 10000 )) {
	/* set a sane value, we don't want 2 million second delays */
	debounce = 2;
    }
    if ( in ) {
	/* pressed */
	debounce_cntr++;
	if ( debounce_cntr >= debounce ) {
	    debounce_cntr = debounce;
	    if (!debounced ) {
		/* toggle output */
		out = !out;
	    }
	    debounced = true;
	}
    } else {
	/* not pressed */
	debounce_cntr--;
	if ( debounce_cntr <= 0 ) {
	    debounce_cntr = 0;
	    debounced = false;
	}
    }

return 0;
}
