Home AMX User Forum AMX Technical Discussion
Options

Can you have an array of functions?

(i.e Function Subroutines, defined in DEFINE_FUNCTION)

And then refer to them as per other arrays, e.g. within a button event thus:

DEFINE_VARIABLE
dfMyArrayOfFunkyFunctions[] = { ( fnMyfunction() ), ( fnMyFunction2() ) }

DEFINE_FUNCTION fnMyFunction()
{
// do awesome stuff
}

DEFINE_FUNCTION fnMyFunction2()
{
// do more awesome stuff
}

BUTTON_EVENT [ dvMyTP, dcMyButtons ]
{
STACK_VAR nBtnIdx
nBtnIdx = GET_LAST (dcMyButtons)
{

// dofunction ( dfMyArrayOfFunkyFunctions [button.input.channel] )

}

}

or similar.

Could you create something with structures maybe? Or maybe the keyword simply doesn"t exist (e.g. a keyword like DO_FUNCTION, or something)?




Purpose: to simplify code, make it more modular, avoiding multiple SWITCH...CASEs or SELECT...ACTIVEs.

Comments

  • Options
    I guess it wouldnt work anyway as the array being declared is comprised of functions that are defined after the variables at compile time.
  • Options
    Spire_JeffSpire_Jeff Posts: 1,917
    I doubt it will work the way you expect it to in your example. Most likely it will compile with errors because the functions you declared do not have return types. If they did have return types, you would just populate the dfArrayOfFunkyFunctions variable with the return values.

    There is a way you could do what I think you are after tho. You could write one more function that calls the funky functions.
    DEFINE_FUNCTION fnPickAFunction(integer nFunctionRequested){
      switch(nFunctionRequested){
        case 1:    fnMyFunction1();
        case 2:    fnMyFunction2();
        case 3:    fnMyFunction3();
        default:    fnMyFunctionRandom();
      }
    }
    
    
    DEFINE_FUNCTION fnMyFunctionRandom(){
      stack_var integer nFunctionRequested;
      nFunctionRequested = RANDOM_NUMBER(3) +1;
      switch(nFunctionRequested){
        case 1:    fnMyFunction1();
        case 2:    fnMyFunction2();
        case 3:    fnMyFunction3();
        default:    fnMyFunctionRandom();
      }
    }
    
    
    
    DEFINE_EVENT
    button_event[dvTP,nMyButtons]{
      PUSH:{
         fnPickAFunction(button.input.channel);
      }
    }
    
    

    I'm not sure where this might be applicable in a job, but it should work if I am interpreting your requirements properly. (I through the random function in for fun :) )

    Jeff
  • Options
    cool! I'll give it a go.

    it's for a job with two rooms, 2x MET KPs, 14 buttons on each, 1 in each room, same room layout, same same everting, off 1x NI3100, 1 vol box axb-vol3.

    Just wanted to rinse and repeat, reallly! If they added more rooms i could connect them to the same master wouldn't have to change code much, just add one more device to each array (extra screen, extra proj and extra kp, extra vol box) until i ran out of ports or the maths becomes too hard for scaling (for referencing the elements in the array using (button.input.device.number - xx ) and (b.i.c - xx)). I could even declare those devices in the code right off the bat then not have to change anything when/if the time comes to add stuff (providing the added kit is the same).


    anyhooze, too drunk watching murray v roddick!




    cheers
  • Options
    Sample Code

    FYI, here's what I've come up with...
    PROGRAM_NAME='Generic_Code'
    
    
    DEFINE_DEVICE
    dvDISPLAY_01	= 5001:1:0
    dvDISPLAY_2     = 5001:2:0
    dvDISPLAY_03	= 5001:3:0
    dvDISPLAY_04	= 5001:4:0
    
    dvRELAYS	= 5001:8:0 //1,2,3,4 UP ROOMS 1,2,3,4 :-:  5,6,7,8 DOWNS ROOMS 1,2,3,4
    
    dvKP_01 	= 128:1:0
    dvKP_02 	= 129:1:0
    dvKP_03 	= 130:1:0
    dvKP_04 	= 131:1:0
    
    dvPGVOL_01	= 170:1:0 // rooms 1 &2
    dvPGVOL_02	= 171:1:0 // rooms 3 &4
    
    (***********************************************************)
    (*               CONSTANT DEFINITIONS GO BELOW             *)
    (***********************************************************)
    DEFINE_CONSTANT
    INTEGER nON = 1
    INTEGER nOFF = 0
    
    INTEGER nSCREEN_UP = 1
    INTEGER nSCREEN_DOWN = 0
    
    INTEGER srcNONE = 0
    INTEGER srcVID = 1
    INTEGER srcSVID = 2
    INTEGER srcYUV = 3
    INTEGER srcPC = 4
    INTEGER srcPC2 = 5
    INTEGER srcHDMI = 6
    
    (***********************************************************)
    (*              DATA TYPE DEFINITIONS GO BELOW             *)
    (***********************************************************)
    DEFINE_TYPE
    
    (***********************************************************)
    (*               VARIABLE DEFINITIONS GO BELOW             *)
    (***********************************************************)
    DEFINE_VARIABLE
    INTEGER nPROJ_POWER
    INTEGER nSOURCE
    INTEGER nSCREEN
    
    DEV dvDISPLAYS[] = {
    		    dvDISPLAY_01, dvDISPLAY_2, dvDISPLAY_03, dvDISPLAY_04
    		   }
    
    DEV dvKEYPADS[]	= {
    		    dvKP_01, dvKP_02, dvKP_03, dvKP_04
    		  }
    		  
    DEV dvVOLBOXES[]= {
    		    dvPGVOL_01, dvPGVOL_01, dvPGVOL_02, dvPGVOL_02
    		  }
    
    INTEGER dcKP_BUTTONS[] =   {
    			    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
    			 }
    			
    INTEGER nBUTTON_PUSHED
    INTEGER nDEVICE_THAT_BUTTON_WAS_PUSHED_ON
    
    INTEGER nPUSH
    
    
    DEFINE_FUNCTION fnPickAFunction(integer nFunctionRequested)
    {
      switch(nFunctionRequested)
      {
        case 1:     fnMyFunction1();
        case 2:     fnMyFunction2();
        case 3:     fnMyFunction3();
        case 4:     fnMyFunction4();
        case 5:     fnMyFunction5();
        case 6:     fnMyFunction6();
        case 7:     fnMyFunction7();
        case 8:     fnMyFunction8();
        case 9:     fnMyFunction9();
        case 10:    fnMyFunction10();
        case 11:    fnMyFunction11();
        case 12:    fnMyFunction12();
        case 13:    fnMyFunction13();
        case 14:    fnMyFunction14();
        
      }
    }
    
    DEFINE_FUNCTION fnMyFunction1()
        {
        SEND_STRING dvDISPLAYS[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],"'STRING_01'" // ON
        PULSE [dvRELAYS,(nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127)] //SCREEN DOWN
        nPROJ_POWER = 1
        }
        
    DEFINE_FUNCTION fnMyFunction2()
        {
        SEND_STRING dvDISPLAYS[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],"'STRING_02'" // OFF
        PULSE [dvRELAYS,(nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-123)] //SCREEN UP
        nSOURCE = srcNONE
        nPROJ_POWER = 0
        }
        
    DEFINE_FUNCTION fnMyFunction3()
        {
        SEND_STRING dvDISPLAYS[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],"'STRING_03'" // SELECT VID 1
        nSOURCE = srcVID
        }
        
    DEFINE_FUNCTION fnMyFunction4()
        {
        SEND_STRING dvDISPLAYS[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],"'STRING_04'" // SELECT SVID
        nSOURCE = srcSVID
        }
        
    DEFINE_FUNCTION fnMyFunction5()
        {
        SEND_STRING dvDISPLAYS[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],"'STRING_05'" // SELECT YUV
        nSOURCE = srcYUV
        }
        
    DEFINE_FUNCTION fnMyFunction6()
        {
        SEND_STRING dvDISPLAYS[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],"'STRING_06'" // SELECT PC
        nSOURCE = srcPC
        }
        
    DEFINE_FUNCTION fnMyFunction7()
        {
        SEND_STRING dvDISPLAYS[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],"'STRING_07'" // SELECT PC2
        nSOURCE = srcPC2
        }
        
    DEFINE_FUNCTION fnMyFunction8()
        {
        SEND_STRING dvDISPLAYS[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],"'STRING_07'" // SELECT HDMI
        nSOURCE = srcHDMI
        }
        
    DEFINE_FUNCTION fnMyFunction9()
        {
        // YOU KNOW, LIKE, DO STUFF
        }
        
    DEFINE_FUNCTION fnMyFunction10()
        {
        // YOU KNOW, LIKE, DO STUFF
        }
        
    DEFINE_FUNCTION fnMyFunction11()
        {
        // YOU KNOW, LIKE, DO STUFF
        }
        
    DEFINE_FUNCTION fnMyFunction12()
        {
        // YOU KNOW, LIKE, DO STUFF
        }
        
    DEFINE_FUNCTION fnMyFunction13() // should ramp up at defined level until nPUSH = 0, which it is when RELEASE happens
        {
        IF(nPUSH = 1)
    	{
    	IF(nSOURCE<>srcPC2)
    	    {
    	    IF(nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 128 || nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 130)
    		{
    		ON[dvVOLBOXES[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],4]
    		}
    	    ELSE IF(nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 129 || nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 131)
    		{
    		ON [dvVOLBOXES[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],7]
    		}
    	    }
    	}
        IF(nPUSH = 0)
    	{
    	IF(nSOURCE<>srcPC2)
    	    {
    	    IF(nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 128 || nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 130)
    		{
    		OFF [dvVOLBOXES[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],4]
    		}
    	    ELSE IF(nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 129 || nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 131)
    		{
    		OFF [dvVOLBOXES[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],7]
    		}
    	    }
    	}
        }
        
    DEFINE_FUNCTION fnMyFunction14() // same saem, but ramping down   
        {
        IF(nPUSH = 1)
    	{
    	IF(nSOURCE<>srcPC2)
    	    {
    	    IF(nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 128 || nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 130)
    		{
    		ON[dvVOLBOXES[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],5]
    		}
    	    ELSE IF(nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 129 || nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 131)
    		{
    		ON [dvVOLBOXES[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],8]
    		}
    	    }
    	}
        IF(nPUSH = 0)
    	{
    	IF(nSOURCE<>srcPC2)
    	    {
    	    IF(nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 128 || nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 130)
    		{
    		OFF [dvVOLBOXES[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],5]
    		}
    	    ELSE IF(nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 129 || nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = 131)
    		{
    		OFF [dvVOLBOXES[nDEVICE_THAT_BUTTON_WAS_PUSHED_ON-127],8]
    		}
    	    }
    	}
        }
    
    
    
    DEFINE_EVENT
    button_event[dvKEYPADS,dcKP_BUTTONS]
    {
      PUSH:
        {
        nPUSH = 1
        nBUTTON_PUSHED = button.input.channel
        nDEVICE_THAT_BUTTON_WAS_PUSHED_ON = button.input.device.number
        fnPickAFunction(button.input.channel);
        }
      RELEASE:
        {
        nPUSH = 0
        }
    }
    
Sign In or Register to comment.