component mux16 "Select from one of sixteen input values"; pin in bit use_graycode"""\ This signifies the input will use Gray code instead of binary. Gray code is a good choice when using physical switches because for each increment only one select input changes at a time. """; pin in bit suppress_no_input"""\ This suppresses changing the output if all select lines are false. This stops unwanted jumps in output between transitions of input. but make in00 unavailable. """; pin in float debounce_time"""\ sets debounce time in seconds. eg. .10 = a tenth of a second input must be stable this long before outputs changes. This helps to ignore 'noisy' switches. """; pin in bit sel#[4] """\ Together, these determine which **in**__N__ value is copied to *out*. """; pin out float out_f; pin out s32 out_s """\ Follows the value of one of the **in**__N__ values according to the four *sel* values and whether use-graycode is active. The s32 value will be trunuated and limited to the max and min values of signed values. [cols="^1,^1,^1,^1,1"] |=== ^h|sel3 ^h|sel2 ^h|sel1 ^h|sel0 ^h|out follows |0|0|0|0|*in00* |0|0|0|1|*in01* |0|0|1|0|*in02* |0|0|1|1|*in03* |0|1|0|0|*in04* |0|1|0|1|*in05* |0|1|1|0|*in06* |0|1|1|1|*in07* |1|0|0|0|*in08* |1|0|0|1|*in09* |1|0|1|0|*in10* |1|0|1|1|*in11* |1|1|0|0|*in12* |1|1|0|1|*in13* |1|1|1|0|*in14* |1|1|1|1|*in15* |=== """; param r float elapsed "Current value of the internal debounce timer for debugging."; param r s32 selected "Current value of the internal selection variable after conversion for debugging"; pin in float in##[16] "array of selectable outputs"; variable double delaytime; variable int lastnum; variable int running; function _; license "GPL"; author "Chris S Morley"; see_also "mux2(9), mux4(9), mux8(9), mux_generic(9)."; ;; FUNCTION(_) { int i,num = 0; int internal[4]; if(suppress_no_input) { if (sel(0) + sel(1) + sel(2) + sel(3) == 0) { return; } } if (use_graycode) { internal[0] = sel(3); internal[1] = sel(2); internal[2] = sel(1); internal[3] = sel(0); for(i = 1; i < 4; i++){ internal[i] = internal[i] ^ internal[i - 1]; } selected = num = internal[3]+(internal[2]*2) + (internal[1]*4) + (internal[0]*8); }else{ selected = num = (sel(0))+(sel(1)*2) + (sel(2)*4) + (sel(3)*8); } if(debounce_time) { if (num != lastnum) { if (!running) { running = 1; delaytime = 0; } if (delaytime < debounce_time) { delaytime += fperiod; elapsed = delaytime; return; }else{ running = 0; lastnum = num; out_s = out_f = in(num); return; } } } /* select the output */ out_s = out_f = in(num); }