summaryrefslogtreecommitdiff
path: root/src/hal/components/updown.comp
blob: 02af5c180db9cbfb8cab9803e1db63611ade57e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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";
param rw bit clamp "If TRUE, then clamp the output to the min and max parameters.";
param rw 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.";
param rw s32 max = 0x7FFFFFFF "If clamp or wrap is set, count will never exceed this number";
param rw s32 min "If clamp or wrap is set, count will never be less than this number";
variable int oldup;
variable int olddown;
variable int first = 1;
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=0;
	}
	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;
}