Home AMX User Forum AMX Technical Discussion

Always executing the first CASE statement

I'm trying to use a SWITCH statement in my code, and after running it. It looks like the first CASE statement is always executed regardless of what the value is. My Code:

SWITCH (BUTTON.INPUT.CHANNEL)
{
CASE 1: PULSE [vdvDisplay, 27]
CASE 2: PULSE [vdvDisplay, 28]
CASE 11:
{
nInputSource++
SEND_COMMAND dvDisplay, "'INPUT-',ITOA(nInputSource)"
}
}

I've noticed this happening with multiple switch statements

Comments

  • Is this Switch in a Button_Event or is it in a function?
  • It's in a Button Event

    BUTTON_EVENT [dvTP, 1]
    BUTTON_EVENT [dvTP, 2]
    BUTTON_EVENT [dvTP, 11]
    {

    }
  • you could start by checking the value... ie
    stack_var integer iBtn;
    iBtn = get_last(buttuon.input.channel);
    
    send_string 0, "'button is... ', itoa(iBtn)";
    
    switch(iBtn){
     case 1... 
    }
    

    or if you want some opinion on structuring your code
    BUTTON_1 = [a number];
    BUTTON_2 = [a number];
    BUTTON_3 = [a number];
    BUTTON_4 = [a number];
    
    volatile integer nBtns[] = {
    BUTTON_1,
    BUTTON_2,
    BUTTON_3,
    BUTTON_4
    };
    
    button_event[dvTPs,nBtns]{
    stack_var btn;
    stack var tp;
    btn = get_last(nBtns);
    tp = get_last(dvTPs);
    
    switch(nBtns[btn]){
    
    case BUTTON_1: {
    }
    case BUTTON_2: {
    break;
    }
    default:{
    
    }
    }
    

    hopefully these help in isolating your problem. I personally prefer not to use the button.input.chanel call as I've had weird things happen with no answer as to why.
  • I just tried this and it works with no problems:
    Define_Function Handle_DisplayButtonPresses(Integer btn, Dev Device, Integer InputSource)
    {
    	Switch ( btn )
    	{
    		Case 1:
    		{
    			PULSE[Device, PWR_ON];
    			Break;
    		}
    		Case 2:
    		{
    			PULSE[Device, PWR_OFF];
    			Break;
    		}
    		Case 11:
    		{
    			InputSource++;
    			SEND_COMMAND Device, "'INPUT-', ITOA(InputSource)";
    			Break;
    		}
    	}
    }
    DEFINE_EVENT
    BUTTON_EVENT [dvTP, 1]
    BUTTON_EVENT [dvTP, 2]
    BUTTON_EVENT [dvTP, 11]
    {
    	PUSH:
    	{
    		Handle_DisplayButtonPresses(Button.Input.Channel, dvDisplay, nInputSource);
    	}
    }
    
  • I think you're looking for the [ code ] and [ /code ] to show your code in a box.

    You can absolutely do what you're doing, however I usually use a function for a single, specific task that will be repeated; this way I have a single place to update a command, while many places could potentially call this function/task.

    You should figure out whats causing your problem and fix it instead of jumping to an alternative means of bypassing the problem.
  • mpullinmpullin Posts: 949
    I just tried this and it works with no problems:
    Define_Function Handle_DisplayButtonPresses(Integer btn, Dev Device, Integer InputSource)
    {
    	Switch ( btn )
    	{
    		Case 1:
    		{
    			PULSE[Device, PWR_ON];
    			Break;
    		}
    		Case 2:
    		{
    			PULSE[Device, PWR_OFF];
    			Break;
    		}
    		Case 11:
    		{
    			InputSource++;
    			SEND_COMMAND Device, "'INPUT-', ITOA(InputSource)";
    			Break;
    		}
    	}
    }
    DEFINE_EVENT
    BUTTON_EVENT [dvTP, 1]
    BUTTON_EVENT [dvTP, 2]
    BUTTON_EVENT [dvTP, 11]
    {
    	PUSH:
    	{
    		Handle_DisplayButtonPresses(Button.Input.Channel, dvDisplay, nInputSource);
    	}
    }
    
    Why would you do this? You've got to maintain a list of relevant channel codes in two places. If one insists on SWITCH/CASEing by BUTTON.INPUT.CHANNEL maybe just use BUTTON_EVENT[dvTP, 0] to catch all button presses.

    Regardless, I don't see anything wrong with the OP's code as posted. There is funny business taking place elsewhere.
  • I do not code in this fashion at all.
    Simply, working with the setup as the OP has setup.

    You are right, there was probably nothing wrong with what the OP had;
    the problem most likely lies elsewhere.
  • HedbergHedberg Posts: 671
    I just ran the following code using diagnostics to do the button presses and it worked just as would be expected. I suspect there is something odd going on not related to the code as described.
    BUTTON_EVENT [dvTP, 1]
    BUTTON_EVENT [dvTP, 2]
    BUTTON_EVENT [dvTP, 11]
    {
    	push:
    	{
    		SWITCH (BUTTON.INPUT.CHANNEL)
    		{
    			CASE 1: PULSE [dvDisplay, 27]
    			CASE 2: PULSE [dvDisplay, 28]
    			CASE 11:
    			{
    				nInputSource++
    				SEND_COMMAND dvDisplay, "'INPUT-',ITOA(nInputSource)"
    			}
    		}
    	}
    } 
    
  • mpullinmpullin Posts: 949
    I do not code in this fashion at all.
    Simply, working with the setup as the OP has setup.

    You are right, there was probably nothing wrong with what the OP had;
    the problem most likely lies elsewhere.
    I figured so, I should have written "why would ONE" do this (the global 'one')

    Maybe every button on the TP has channel code 1.
Sign In or Register to comment.