component updown "Counts up or down, with optional limits and wraparound behavior";
pin in bit countup "Increment count when this pin goes from 0 to 1";
pin in bit countdown "Decrement count when this pin goes from 0 to 1";
pin in bit reset "Reset count when this pin goes from 0 to 1";
pin out s32 count "The current count";
pin io bit clamp "If true, then clamp the output to the min and max parameters.";
pin io bit wrap "If true, then wrap around when the count goes above or below the min and max parameters.  Note that wrap implies (and overrides) clamp.";
pin io s32 max = 0x7FFFFFFF "If clamp or wrap is set, count will never exceed this number";
pin io s32 min "If clamp or wrap is set, count will never be less than this number";
variable hal_bit_t oldup = false;
variable hal_bit_t olddown = false;
variable hal_bit_t first = true;
function _ nofp "Process inputs and update count if necessary";
license "GPL";

;;

FUNCTION(_)
{
	hal_s32_t temp_count = count;
	if (first) {
		oldup=countup;
		olddown=countdown;
		first = false;
	}
	if ((!oldup) && (countup)) { // positive edge, count up
		temp_count++;
	}
	if ((!olddown) && (countdown)) { // positive edge, count down
		temp_count--;
	}
	if (reset) {
		temp_count=0;
	}
	if (wrap) {
		if (temp_count > max)
			temp_count = min;
		else if (temp_count < min)
			temp_count = max;
	} else if (clamp) {
		if (temp_count > max)
			temp_count = max;
		else if (temp_count < min)
			temp_count = min;
	}
	count = temp_count;
	oldup = countup;
	olddown = countdown;

return 0;
}
