Home AMX User Forum AMX Technical Discussion

how to change multi-state button from code

Hello,

a newbie question:
it is the first time that i'm using multi-state buttons, and i need to change its states from code. I know how to do it with a bi-state button (on[dvTp, nButton] or [dvTp, nButton] = 1), but how do you set a state of a multi-state button from code?

Thanks!

Comments

  • ericmedleyericmedley Posts: 4,177
    I might be wrong about this.. But I don't think you can do what you're wanting with a multi-state button. (something like "Go to State 8 or something) The multi-state button is more for making animated buttons that have several frames between the off and on state. I use them for animated buttons. I have several animations I use for things like projectors warming up. It just involves turning on the button and having it play the states up and down or whatnot.

    To have a button that has multiple "States" so-to-speak, you might make a normal button (just on and off states) but is also a multi-state bar graph. Make the bar graph part "Display Only" Then make the range from low to high however many states you want. (ex: 10 states = low-1 high-10) Load in your graphics or whatnot for each state of the level. To effect change of state on the touch panel you just send the level. The graphic will change just fine. and, as a button, you just program the button event and don't have to turn the button feedback on or off, just end the level.
  • viningvining Posts: 4,368
    I think the ani^ command will allow you to do that with a multi-state button but it's way too much trouble compared to using a multi-state bargragh like E described and then just sending your level corresponding to the state you want.
  • MorgoZMorgoZ Posts: 116
    Ok, i'll try eric's answer.

    Thank you very much!
  • PhreaKPhreaK Posts: 966
    vining almost had it, the caret comes first - command is
    '^ANI-<address>,<start state (0 for current)>,<end state>,<time (1/10th's of a second)>'
    
    You can simply pass 0 as the time parameter to have it directly jump to a state. The AMX modero library will also wrap this up all neatly for you.
  • viningvining Posts: 4,368
    PhreaK wrote: »
    vining almost had it, the caret comes first - command is
    '^ANI-<address>,<start state (0 for current)>,<end state>,<time (1/10th's of a second)>'
    
    You can simply pass 0 as the time parameter to have it directly jump to a state. The AMX modero library will also wrap this up all neatly for you.
    Another reason for using send_level instead of ani^ :) is that the master tracks levels and channels and doesn't resend the level to the UI if that is already set to that level so in a simple panel feedback routine where you have channel = var you can also run send_level x, var and for something running repeatedly in a time_line or the define_program it won't actuallly resend to the panel each pass becuase those states are tracked, send_command ^ani doesn't get tracked and will be sent to the panel over and over again which is the main reason I stopped using it long ago.
  • ericmedleyericmedley Posts: 4,177
    vining wrote: »
    Another reason for using send_level instead of ani^ :) is that the master tracks levels and channels and doesn't resend the level to the UI if that is already set to that level so in a simple panel feedback routine where you have channel = var you can also run send_level x, var and for something running repeatedly in a time_line or the define_program it won't actuallly resend to the panel each pass becuase those states are tracked, send_command ^ani doesn't get tracked and will be sent to the panel over and over again which is the main reason I stopped using it long ago.


    I'd also say that it's probably a lot easier to migrate code from G4 panels to G5s. Not all commands that work on the G4 work on the G5. But, levels are still sticking around.
  • a_riot42a_riot42 Posts: 1,624
    I've always used ANI since I can send ranges and do all the other things you can typically do with those commands. I also use levels on occasion with a bargraph. There are pros/cons to both depending on what you are doing. If you have a button that already has a level you will have to use the ANI to animate it.
    Paul
  • MorgoZMorgoZ Posts: 116
    One more question:

    I'm trying Eric's answer, but since in my bargraph i still have "on" and "off" states, how do i set the graphics for all the states?
    I want to implement a 4 state button (in this case, bargraph). It is simple, i just need a button that chages its color at each state (red, yellow, green and grey... some kind of semaphore), i had set the button to "bargraph" type and the "level function" to "display only", but now i don't know how to set the state's colors.

    Any advice?

    Thanks!
  • viningvining Posts: 4,368
    Select your button in TPD and under the properties general tab "Type" select multi-state bargraph, then change the "State Count" to 4, now go to the Programming tab set you port, channel and level port/channels to what they need to be, "Level Function" to display only, "Range Low" to 1, "Range High" to 4. Then go to the States tag and you should see +All, +1,+2,+3,+4 if you expand any one you can set that state's properties.


  • ericmedleyericmedley Posts: 4,177
    MorgoZ wrote: »
    One more question:

    I'm trying Eric's answer, but since in my bargraph i still have "on" and "off" states, how do i set the graphics for all the states?
    I want to implement a 4 state button (in this case, bargraph). It is simple, i just need a button that chages its color at each state (red, yellow, green and grey... some kind of semaphore), i had set the button to "bargraph" type and the "level function" to "display only", but now i don't know how to set the state's colors.

    Any advice?

    Thanks!
    MorgoZ,
    I have attached a TP4 example of a button with 4 states (Apple, Orange, Peach, Pineapple) The button number is 101, the level number is 5, the text address is 20.

    here is some code to illustrate how you'd send feedback and handle the button push. You do not need to turn the button ON or OFF, you just send the level instead. Level=state.
    DEFINE_CONSTANT
    integer Apple=1;
    integer Orange=2;
    integer Peach=3;
    integer Pineapple=4;
    
    integer Button_101_Level_id=5
    
    define_function fn_Update_button_101_State_fb(integer state){
        if(state<=4 and state>0){ // state must be 1-4
            send_level dvTP,Button_101_Level_id,state;
            }
        }
    
    DEFINE_EVENT
    
    button_event[dvTP,101]{
        push:{
            // the fruit button was pushed.
            // do stuff
            }
        }
    
    data_event[MyDevice]{
        string:{
            select{
                active(find_string(data.text,'State=Apple',1)):{
                    MyState=Apple;
                    }
                active(find_string(data.text,'State=Orange',1)):{
                    MyState=Orange;
                    }
                active(find_string(data.text,'State=Peach',1)):{
                    MyState=Peach;
                    }
                active(find_string(data.text,'State=Pineapple',1)):{
                    MyState=Pineapple
                    }
                } // select
            fn_Update_button_101_fb(MyState);
            }
        }
    

    In this example, we have an imaginary device that is returning a string to indicate its current state (4 states) When we get the string to tell us what state it is in we populate a variable called MyState and then update the feedback to the button. the function sends the appropriate level.
  • MorgoZMorgoZ Posts: 116
    Thank you both!

    It was only that i was using bargraph and not multi-state bargraph.

    Thanks also for the complete example, Eric.

    Salutes!!
Sign In or Register to comment.