multiswitch.comp
1 /******************************************************************************* 2 3 EMC2 HAL component to implement Multistate toggle switch 4 Authors ArcEye 15122011 schooner30@tiscali.co.uk / Andy Pugh andy@bodgesoc.org 5 License GPL 6 Copyright 2011 7 8 example Hal linkages required:- 9 ################################ 10 loadrt multiswitch cfg=4,6,8 11 addf multiswitch.0 servo-thread 12 ... 13 net toggle-switch multiswitch.0.toggle <= parport.N.pin-nn-out 14 net state1 multiswitch.0.state1 => parport.N.pin-nn-in 15 net state1 multiswitch.0.state2 => parport.N.pin-nn-in 16 net state1 multiswitch.0.state3 => parport.N.pin-nn-in 17 18 If you require an "all off" state, then make the component one bit oversize and 19 don't connect the extra pin. 20 21 *******************************************************************************/ 22 23 component multiswitch """This component toggles between a specified number of output bits"""; 24 25 pin in bit up = false "Receives signal to toggle up"; 26 pin in bit down = false "Receives signal to toggle down"; 27 28 param rw unsigned top-position "Number of positions"; 29 param rw signed position "Current state (may be set in the HAL)"; 30 31 pin out bit bit-##[32:personality] = false "Output bits"; 32 33 modparam dummy cfg """cfg should be a comma-separated list of sizes 34 for example cfg=2,4,6 would create 3 instances of 2, 4 and 6 bits respectively. 35 Ignore the "personality" parameter, that is auto-generated"""; 36 37 function _ ; 38 option extra_setup yes; 39 option count_function yes; 40 41 variable int old_up = 0; 42 variable int old_down = 0; 43 44 author "ArcEye schooner30@tiscali.co.uk / Andy Pugh andy@bodgesoc.org"; 45 license "GPL"; 46 ;; 47 48 #define MAX_COUNT 32 49 int cfg[MAX_COUNT]; 50 RTAPI_MP_ARRAY_INT(cfg, MAX_COUNT, "array of function sizes"); 51 52 FUNCTION(_) { 53 int i; 54 55 // debounce 56 if (up && !old_up) { position++; } 57 if (down && !old_down) { position--;} 58 old_up = up; 59 old_down = down; 60 61 if (position < 0) position = top_position; 62 if (position > top_position) position = 0; 63 64 for (i = 0 ; i < personality; i++){ 65 bit(i) = (i == position); 66 } 67 68 } 69 70 EXTRA_SETUP(){ 71 personality = cfg[extra_arg]; 72 top_position = personality - 1; 73 return 0; 74 } 75 76 int get_count(void){ 77 int i; 78 for (i=0; cfg[i] != 0 && i < MAX_COUNT; i++){} 79 if (i == 0){ 80 cfg[0] = 4; 81 i = 1; 82 } 83 return i; 84 } 85