summaryrefslogtreecommitdiff
path: root/src/hal/components/multiswitch.comp
blob: a7383a21b4040369a47a8882b95cd8b1f2b137bb (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*******************************************************************************

EMC2 HAL component to implement Multistate toggle switch
Authors ArcEye 15122011 schooner30@tiscali.co.uk / Andy Pugh andy@bodgesoc.org
License GPL
Copyright 2011 

example Hal linkages required:-
################################
loadrt multiswitch cfg=4,6,8
addf multiswitch.0 servo-thread
...
net toggle-switch multiswitch.0.toggle <= parport.N.pin-nn-out
net state1 multiswitch.0.state1 => parport.N.pin-nn-in
net state1 multiswitch.0.state2 => parport.N.pin-nn-in
net state1 multiswitch.0.state3 => parport.N.pin-nn-in

If you require an "all off" state, then make the component one bit oversize and
don't connect the extra pin. 

*******************************************************************************/

component multiswitch           """This component toggles between a specified number of output bits""";

pin in bit up = false           "Receives signal to toggle up";
pin in bit down = false         "Receives signal to toggle down";

param rw unsigned top-position  "Number of positions";
param rw signed position      "Current state (may be set in the HAL)";

pin out bit bit-##[32:personality] = false       "Output bits";

modparam dummy cfg              """cfg should be a comma-separated list of sizes
for example cfg=2,4,6 would create 3 instances of 2, 4 and 6 bits respectively.
 Ignore the "personality" parameter, that is auto-generated""";

function _ ;
option extra_setup yes;
option count_function yes;

variable int old_up = 0;
variable int old_down = 0;

author "ArcEye schooner30@tiscali.co.uk / Andy Pugh andy@bodgesoc.org";
license "GPL";
;;

#define MAX_COUNT 32
int cfg[MAX_COUNT];
RTAPI_MP_ARRAY_INT(cfg, MAX_COUNT, "array of function sizes");

FUNCTION(_) {
    int i;
    
    // debounce
    if (up && !old_up) { position++; }
    if (down && !old_down) { position--;}
    old_up = up;
    old_down = down;
    
    if (position < 0) position = top_position;
    if (position > top_position) position = 0;
    
    for (i = 0 ; i < personality; i++){
        bit(i) = (i == position);
    }

}

EXTRA_SETUP(){
    personality = cfg[extra_arg];
    top_position = personality - 1;
    return 0;
}

int get_count(void){
    int i;
    for (i=0; cfg[i] != 0 && i < MAX_COUNT; i++){}
    if (i == 0){
        cfg[0] = 4;
        i = 1;
    }
    return i;
}