Multiswitch component, one button multiple outputs

18 Dec 2011 10:59 - 20 Dec 2011 09:46 #15803 by ArcEye

This component just demonstrates a one button toggling switch as discussed in this thread.
Can be expanded for more outputs if required.


(see post 15809 for updated version)
Last Edit: 20 Dec 2011 09:46 by ArcEye.
18 Dec 2011 11:31 #15805 by BigJohnT

That is pretty neat! Should that comp be added to master?

18 Dec 2011 12:30 #15806 by Rick G

Cool, I will have to try that, I am running out of buttons on my joypad.

Rick G
18 Dec 2011 14:18 #15808 by andypugh
ArcEye wrote:

This component just demonstrates a one button toggling switch as discussed in this thread.
Can be expanded for more outputs if required.

Can we make it take a modparam for number of outputs?

Give me an hour, I will make a version which does it that way for evaluation.
18 Dec 2011 15:24 - 20 Dec 2011 09:45 #15809 by ArcEye
Hi John

Should that comp be added to master?

I have tested in simulation, setting the pins with halcmd and verifying it cycles and sets flags as it should.

It could do with a real world trial, if Rick is thinking of trying it, I can wait for any feedback first.

I will also wait to see what Andy comes up with, don't know if it is worth a modparam argv / argc type interface when there is only one settable param, but it would make it easier to use and more consistent with other components interfaces I suppose.


Updated version of my offering with toggle set back to input here

File Attachment:

File Name: multiswitc...1dd.comp
File Size:3 KB
Last Edit: 20 Dec 2011 09:45 by ArcEye.
18 Dec 2011 17:25 #15812 by andypugh
andypugh wrote:

[Give me an hour.

Make that three. I got distracted.
The only real advantage to doing it this way is that it supports up to 32 output bits.
It probably needs to support "names=" to be well-behaved component.

EMC2 HAL component to implement Multistate toggle switch
(c) ArcEye 15122011

example Hal linkages required:-
loadrt multiswitch cfg=4,6,8
addf multiswitch.0 servo-thread
net toggle-switch multiswitch.0.toggle <=
net state1 multiswitch.0.state1 =>
net state1 multiswitch.0.state2 =>
net state1 multiswitch.0.state3 =>

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";
license "GPL";

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

    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);


    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;
19 Dec 2011 08:45 #15832 by ArcEye
Hi Andy,

Personalities and modparams are not something I have played with yet in .comp programming.
The components I have written have been either, only intended for use as singletons, or I have manually set params etc.

It didn't make complete sense until I expanded the macros and generated a C file.

You polish it and publish as your own, there is nothing in there of mine, save the concept, which is hardly unique.

19 Dec 2011 10:50 #15834 by andypugh
ArcEye wrote:

It didn't make complete sense until I expanded the macros and generated a C file.

Indeed. I am using at least two undocumented features there.
It is a pity that there is no way to omit "personality" from the auto-generated docs in cases where you are not using it (actually, it would also be nice to be able to alias "personality" to a modparam name of your choosing.
The following user(s) said Thank You: garylogan
20 Dec 2011 05:14 #15879 by fabworx
where can i find other .comp files i need or2 and probably others not sure yet till i get hal to load past the or2 that i dont have.
20 Dec 2011 08:11 #15882 by Rick G
You just need to load them with loadrt in your configuration file.

For example

loadrt or2 count=2

loads 2 or2 components.

Some good reading...

Rick G
Time to create page: 0.171 seconds
Powered by Kunena Forum