Home AMX User Forum AMX Technical Discussion

Convice this array script to work

DEFINE_CONSTANT
INTEGER test1[] = {1,2}
INTEGER test2[] = {3,4}
INTEGER testa[2][2] = { test1, test2 }

DEFINE_VARIABLE
VOLATILE INTEGER testi = 1
VOLATILE INTEGER testb[2]

DEFINE_START
for(testi=1;testi<=2;testi++)
{
	testb[testi] = testa[testi][1]
}

I think the part that dies is INTEGER testa[2][2] = { test1, test2 }
It shows up blank in the debug window.

Comments

  • I could be wrong here but if this is all of the code in your system you need to have at least 1 non-volatile variable to get any code to work correctly.

    Change your variable

    VOLATILE INTEGER testi = 1

    to

    INTEGER testi = 1

    or

    NON_VOLATILE INTEGER testi = 1

    I once was trying to test out a very basic chunk of code and I could not get it to work. the cause was the couple variables I was using were all VOLATILE and the master likes to have at least 1 non-volatile variable in the system.

    I hope this helps
  • Can you refer to a constant when defining a constant? I don't think I've ever done that before. I wonder if you moved INTEGER testa[2][2] = { test1, test2 } to the define_start section if that would make it work.

    --John
  • jweatherjweather Posts: 320
    I could be wrong here but if this is all of the code in your system you need to have at least 1 non-volatile variable to get any code to work correctly.

    This is only true for debugging the code. If you don't have a non-volatile variable, the debugger complains about the source code not matching what's on the processor. It still runs fine.
  • travistravis Posts: 180
    Can you refer to a constant when defining a constant? I don't think I've ever done that before. I wonder if you moved INTEGER testa[2][2] = { test1, test2 } to the define_start section if that would make it work.

    --John

    Nope, no initializers allowed in define_start


    The example at the top is not my whole program, just snippets to get the point across
  • Spire_JeffSpire_Jeff Posts: 1,917
    maybe try declaring the first two as test1[2] and test2[2] and see if that works???

    If that doesn't work, try moving testa to the define_variable section. You might also have to do a define_start population of the array to get it initialized.

    Jeff
  • I didn't mean to literally cut and paste it into the define_start section as is, but rather if you moved the filling in of the testa[][] array to the define_start section.

    I just defined a constant using a constant and it worked for an integer (learn something new every day). I agree that the problem is in the way the testa array is initialized though.

    --John
  • travistravis Posts: 180
    Now it's turning into so much awkwardness to not be worth it I guess. I wanted to do something like this :

    HBO = {300, 15} //first number is satellite channel #, second the number of the button
    ABC = {290, 66}

    All_the_networks = { HBO, ABC, etc...}

    All_the_channel_numbers = function to pull first item out of each network array, out of all_the_networks [300, 290]
    All_the_buttons = function for second item [15,66]

    button_event[tp, all_the_buttons]
    get_last[all_the_buttons]
    etc...

    don't blame the wonky button number => tv channel mapping on me. I'm just updating (rewriting) some axcess code (written by a mean bad person)
  • viningvining Posts: 4,368
    These work fine:
    DEFINE_CONSTANT
    
    ONE 		= 1 ;
    TWO 		= 2 ;
    THREE 		= 3 ;
    FOUR 		= 4 ;
    NUMBERS[]	= {ONE,TWO,THREE}
    NUMBERS_[][]	=   {
    		    {ONE,TWO},
    		    {TWO,THREE},
    		    {THREE,FOUR}
    		    }
    
    INTEGER test1[] 	= {ONE,TWO}
    INTEGER test2[] 	= {THREE,FOUR}
    INTEGER testa[][] 	= { 
    			  {ONE,TWO}, 
    			  {THREE,FOUR}
    			  }
    
    But this does not:
    INTEGER testa_[][] 	= { 
    			  {test1}, 
    			  {test2}
    			  }
    
    I guess you can't initialize an array with an array?

    I guess you need change it to a variable and initialize somewhere else.
  • jjamesjjames Posts: 2,908
    travis wrote: »
    Now it's turning into so much awkwardness to not be worth it I guess. I wanted to do something like this :

    HBO = {300, 15} //first number is satellite channel #, second the number of the button
    ABC = {290, 66}

    All_the_networks = { HBO, ABC, etc...}

    All_the_channel_numbers = function to pull first item out of each network array, out of all_the_networks [300, 290]
    All_the_buttons = function for second item [15,66]

    button_event[tp, all_the_buttons]
    get_last[all_the_buttons]
    etc...

    don't blame the wonky button number => tv channel mapping on me. I'm just updating (rewriting) some axcess code (written by a mean bad person)

    I hate using them, but would this not be a great time to use a STRUCTURE? Can you even use a structure's value for button events? I've never tried....
  • viningvining Posts: 4,368
    jjames wrote:
    I hate using them, but would this not be a great time to use a STRUCTURE? Can you even use a structure's value for button events? I've never tried....
    Why hate? I would use a structure and have each index positoin hold the channel name, number and icon slot or image name.

    You cant really use a structure in a get_last but if you make the TP channels number the index position of the structure and have the structuer hold the actual TV channel you could something like this:
    BUTTON_EVENT[dvTP_CATV,BUTTON.INPUT.CHANNEL] //
    
         {
         PUSH:
    	  {
    	  LOCAL_VAR INTEGER nBtn ;
    	  LOCAL_VAR INTEGER nCh_Number ;
    	  
    	  
    	  nBtn = BUTTON.INPUT.CHANNEL ;
    	  nCh_Number = sCATV_CH[nBtn].CH_NUM) ;
    	  SEND_STRING 0, "'CATV nBTN = ',ITOA(nBtn)" ;
    	  SEND_STRING 0, "'CATV Ch Number = ',ITOA(nCh_Number)" ;
    	  }
         }
    
  • jjamesjjames Posts: 2,908
    vining wrote: »
    jYou cant really use a structure in a get_last ...
    Why not? Aren't DEVCHANs structures?
    A data type (structure) containing fields used to represent a specific device number, port, system and channel. For example:

    STRUCTURE DEVCHAN

    {

    DEV Device

    INTEGER Channel

    }

    DEVCHAN has everything a BUTTON_EVENT requires: a device (DEV) and a channel (INTEGER). I'm just confused as to why it wouldn't work. If you made a structure like so:

    STRUCTURE ChanLineup
    {
    INTEGER ChannelNumber
    INTEGER ButtonNumber
    }

    BUTTON_EVENT[dvTP,ChanLineup.ButtonNumber] - or however you do it.

    Wouldn't that work?
  • Joe HebertJoe Hebert Posts: 2,159
    Structures rock and they work fine with get_last and button events.
  • viningvining Posts: 4,368
    Joe Hebert wrote:
    Structures rock and they work fine with get_last and button events.
    I tried that last night before I made my comment and I couldn't get it to work. Of course I only spent a minute or two trying.

    Would you show an example of how you implimented the get_last por favor.
  • Joe HebertJoe Hebert Posts: 2,159
    vining wrote: »
    Would you show an example of how you implimented the get_last por favor.
    Sure thing. I normally use simple button arrays for button events but here is an example of how to do it with a structure.
    DEFINE_DEVICE
    
    dvTP = 10001:1:0
    
    DEFINE_TYPE
    
    STRUCT _sSource {
    
       INTEGER Buttons[4]
    }
    
    DEFINE_VARIABLE _
    
    _sSource sSource
    
    DEFINE_START
    
    sSource.Buttons[1] = 2
    sSource.Buttons[2] = 4
    sSource.Buttons[3] = 6
    sSource.Buttons[4] = 8
    
    SET_LENGTH_ARRAY(sSource.Buttons,4)
    
    REBUILD_EVENT()
    
    DEFINE_EVENT
    
    BUTTON_EVENT[dvTP,sSource.Buttons] {
       PUSH: {
    
          SEND_STRING 0,"'GET_LAST(sSource.Buttons) = ',ITOA(GET_LAST(sSource.Buttons))"   
          SEND_STRING 0,"'BUTTON.INPUT.CHANNEL = ',ITOA(BUTTON.INPUT.CHANNEL)"
       }
    
    }
    

    Pushing buttons 2,4,6,8 produces the following:
    Line 1 :: GET_LAST(sSource.Buttons) = 1 - 08:13:07
    Line 2 :: BUTTON.INPUT.CHANNEL = 2 - 08:13:07
    Line 3 :: GET_LAST(sSource.Buttons) = 2 - 08:13:10
    Line 4 :: BUTTON.INPUT.CHANNEL = 4 - 08:13:10
    Line 5 :: GET_LAST(sSource.Buttons) = 3 - 08:13:13
    Line 6 :: BUTTON.INPUT.CHANNEL = 6 - 08:13:13
    Line 7 :: GET_LAST(sSource.Buttons) = 4 - 08:13:16
    Line 8 :: BUTTON.INPUT.CHANNEL = 8 - 08:13:16
  • viningvining Posts: 4,368
    Still can't seem to get this to work?
    DEFINE_CONSTANT
    
    NUM_CATV_CHANNELS	= 100 ;
    
    DEFINE_TYPE //catv channels structure     
    
    STRUCTURE _sCATV
         
         {
         CHAR CH_NAME[12] ;
         CHAR CH_ICON[12] ;
         INTEGER CH_NUM ;
         }
    
    DEFINE_VARIABLE   
    
    VOLATILE _sCATV sCATV[100] ;
    
    DEFINE_START//initialize structure
         
         {
         STACK_VAR INTEGER i ;
         
         for(i = 1 ; i <= NUM_CATV_CHANNELS ; i ++)
    	  {
    	  sCATV[i].CH_NUM = i ;
    	  sCATV[i].CH_ICON = "'CH ICON ',itoa(i)"
    	  sCATV[i].CH_NAME = "'CH NAME ',itoa(i)"
    	  }
    	
         SET_LENGTH_ARRAY(sCATV,NUM_CATV_CHANNELS) ;
         SET_LENGTH_ARRAY(sCATV.CH_NUM,NUM_CATV_CHANNELS) ;
         SET_LENGTH_ARRAY(sCATV.CH_ICON,NUM_CATV_CHANNELS) ;
         SET_LENGTH_ARRAY(sCATV.CH_NAME,NUM_CATV_CHANNELS) ;
         
         REBUILD_EVENT() ;
         }
    
    BUTTON_EVENT[dvTP_CATV,sCATV.CH_NUM] //
    
         {
         PUSH:
    	  {
    	  STACK_VAR INTEGER nBtn ;
    	  STACK_VAR INTEGER nGetLast_Btn ;
    	  STACK_VAR INTEGER nCh_Number ;
    	  
    	  
    	  nBtn = BUTTON.INPUT.CHANNEL ;
    	  nGetLast_Btn = GET_LAST(sCATV.CH_NUM) ;
    	  nCh_Number = sCATV[nBtn].CH_NUM ;
    	  SEND_STRING 0, "'CATV nBTN = ',ITOA(nBtn)" ;
    	  SEND_STRING 0, "'CATV Get_Last = ',ITOA(nGetLast_Btn)" ;
    	  SEND_STRING 0, "'CATV Ch Number = ',ITOA(nCh_Number)" ;
    	  }
         }
    
    Excuse all the set_length_array, just checking the possibilities.

    Results:
    Line      1 (18:26:19):: GetStructItem - Error  Tk=0x1033
    Line      2 (18:26:19):: CATV nBTN = 1
    Line      3 (18:26:19):: CATV Get_Last = 0
    Line      4 (18:26:19):: CATV Ch Number = 1
    Line      5 (18:26:22):: GetStructItem - Error  Tk=0x1033
    Line      6 (18:26:22):: CATV nBTN = 2
    Line      7 (18:26:22):: CATV Get_Last = 0
    Line      8 (18:26:22):: CATV Ch Number = 2
    Line      9 (18:26:25):: GetStructItem - Error  Tk=0x1033
    Line     10 (18:26:25):: CATV nBTN = 3
    Line     11 (18:26:25):: CATV Get_Last = 0
    Line     12 (18:26:25):: CATV Ch Number = 3
    Line     13 (18:26:27):: GetStructItem - Error  Tk=0x1033
    Line     14 (18:26:27):: CATV nBTN = 4
    Line     15 (18:26:27):: CATV Get_Last = 0
    Line     16 (18:26:27):: CATV Ch Number = 4
    

    Maybe it only works when the structure is equivelent to a 1 dimension array?

    I decided to restructure the structure similar to Joe's structure and what do ya know this works but the above way doesn't. Either way the structure is formed would work for my needs, although I'm not actually using any of this code but typically I would format the structure as I originally did above and it wouldn't even had occurred to me to structure the structure the way I did below.

    How would other folks normally create this type of structure if not using it as in this code with a get_last? As I did above or as I did below?
    DEFINE_TYPE //catv channels structure     
    
    STRUCTURE _sCATV_B
         
         {
         CHAR CH_NAME[NUM_CATV_CHANNELS][12] ;
         CHAR CH_ICON[NUM_CATV_CHANNELS][12] ;
         INTEGER CH_NUM[NUM_CATV_CHANNELS] ;
         }
        
    DEFINE_VARIABLE   
    
    VOLATILE _sCATV_B sCATV_B ;
    
    
    DEFINE_START//initialize structure
         
         {
         STACK_VAR INTEGER i ;
         
         for(i = 1 ; i <= NUM_CATV_CHANNELS ; i ++)
    	  {
    	  sCATV_B.CH_NUM[i]= i ;
    	  sCATV_B.CH_ICON[i] = "'CH ICON ',itoa(i)"
    	  sCATV_B.CH_NAME[i] = "'CH NAME ',itoa(i)"
    	  }           
    	
         SET_LENGTH_ARRAY(sCATV_B,NUM_CATV_CHANNELS) ;
         SET_LENGTH_ARRAY(sCATV_B.CH_NUM,NUM_CATV_CHANNELS) ;
         SET_LENGTH_ARRAY(sCATV_B.CH_ICON,NUM_CATV_CHANNELS) ;
         SET_LENGTH_ARRAY(sCATV_B.CH_NAME,NUM_CATV_CHANNELS) ;
         
         REBUILD_EVENT() ;
         }
    
    BUTTON_EVENT[dvTP_CATV_B,sCATV_B.CH_NUM] //
    
         {
         PUSH:
    	  {
    	  STACK_VAR INTEGER nBtn ;
    	  STACK_VAR INTEGER nGetLast_Btn ;
    	  STACK_VAR INTEGER nCh_Number ;
    	  
    	  
    	  nBtn = BUTTON.INPUT.CHANNEL ;
    	  nGetLast_Btn = GET_LAST(sCATV_B.CH_NUM) ;
    	  nCh_Number = sCATV_B.CH_NUM[nBtn] ;
    	  SEND_STRING 0, "'CATV nBTN = ',ITOA(nBtn)" ;
    	  SEND_STRING 0, "'CATV Get_Last = ',ITOA(nGetLast_Btn)" ;
    	  SEND_STRING 0, "'CATV Ch Number = ',ITOA(nCh_Number)" ;
    	  }
         }
    
    Again forgive all the set_length_array.

    results:
    Line      1 (18:40:20):: CATV nBTN = 1
    Line      2 (18:40:20):: CATV Get_Last = 1
    Line      3 (18:40:20):: CATV Ch Number = 1
    Line      4 (18:40:22):: CATV nBTN = 2
    Line      5 (18:40:22):: CATV Get_Last = 2
    Line      6 (18:40:22):: CATV Ch Number = 2
    Line      7 (18:40:24):: CATV nBTN = 3
    Line      8 (18:40:24):: CATV Get_Last = 3
    Line      9 (18:40:24):: CATV Ch Number = 3
    Line     10 (18:40:26):: CATV nBTN = 4
    Line     11 (18:40:26):: CATV Get_Last = 4
    Line     12 (18:40:26):: CATV Ch Number = 4
    
  • dchristodchristo Posts: 177
    Joe,

    Does this also work if the structure is an array?
    DEFINE_VARIABLE _
    
    _sSource sSource[4]
    

    --D
  • Joe HebertJoe Hebert Posts: 2,159
    vining wrote: »
    Still can't seem to get this to work?
    DEFINE_CONSTANT
    
    NUM_CATV_CHANNELS	= 100 ;
    
    DEFINE_TYPE //catv channels structure     
    
    STRUCTURE _sCATV
         
         {
         CHAR CH_NAME[12] ;
         CHAR CH_ICON[12] ;
         INTEGER CH_NUM ;
         }
    
    DEFINE_VARIABLE   
    
    VOLATILE _sCATV sCATV[100] ;
    
    DEFINE_START//initialize structure
         
         {
         STACK_VAR INTEGER i ;
         
         for(i = 1 ; i <= NUM_CATV_CHANNELS ; i ++)
    	  {
    	  sCATV[i].CH_NUM = i ;
    	  sCATV[i].CH_ICON = "'CH ICON ',itoa(i)"
    	  sCATV[i].CH_NAME = "'CH NAME ',itoa(i)"
    	  }
    	
         SET_LENGTH_ARRAY(sCATV,NUM_CATV_CHANNELS) ;
         SET_LENGTH_ARRAY(sCATV.CH_NUM,NUM_CATV_CHANNELS) ;
         SET_LENGTH_ARRAY(sCATV.CH_ICON,NUM_CATV_CHANNELS) ;
         SET_LENGTH_ARRAY(sCATV.CH_NAME,NUM_CATV_CHANNELS) ;
         
         REBUILD_EVENT() ;
         }
    
    BUTTON_EVENT[dvTP_CATV,sCATV.CH_NUM] //
    
         {
         PUSH:
    	  {
    	  STACK_VAR INTEGER nBtn ;
    	  STACK_VAR INTEGER nGetLast_Btn ;
    	  STACK_VAR INTEGER nCh_Number ;
    	  
    	  
    	  nBtn = BUTTON.INPUT.CHANNEL ;
    	  nGetLast_Btn = GET_LAST(sCATV.CH_NUM) ;
    	  nCh_Number = sCATV[nBtn].CH_NUM ;
    	  SEND_STRING 0, "'CATV nBTN = ',ITOA(nBtn)" ;
    	  SEND_STRING 0, "'CATV Get_Last = ',ITOA(nGetLast_Btn)" ;
    	  SEND_STRING 0, "'CATV Ch Number = ',ITOA(nCh_Number)" ;
    	  }
         }
    
    
    SET_LENGTH_ARRAY(sCATV,NUM_CATV_CHANNELS) 
    
    The above line is fine but the next three SET_LENGTH_ARRAYs aren?t.

    The last three SET_LENGTHs aren?t legal for a couple of different reasons. First you need to address an element in the sCATV array before you can set the length of (or peek or poke) any of the members inside the structure. So you need to start off with something like this:
    sCATV[1].something
    

    The second set length
    SET_LENGTH_ARRAY(sCATV.CH_NUM,NUM_CATV_CHANNELS) ;
    
    is trying to set the length of CH_NUM to 100 which doesn?t fly since CH_NUM is a straight INTEGER. So SET_LENGTH_ARRAY need not apply.

    The line:
    SET_LENGTH_ARRAY(sCATV.CH_ICON,NUM_CATV_CHANNELS) 
    
    is not legal since it needs to start with something like this:
    sCATV[1].CH_ICON
    
    and the number can?t be greater than 12 since that?s the max length that was defined.

    Same logic goes for the last SET_LENGTH_ARRAY.

    The last two SET_LENGTHs aren?t needed because the length is getting set by the direct assignment in the FOR loop. But if you did want to manually set the length of the members inside the array then you would have to address an individual element in the sCATV array as stated above.
    vining wrote:
    How would other folks normally create this type of structure if not using it as in this code with a get_last? As I did above or as I did below?
    When I create s STRUCT the prototype is for a unit of 1 so I would go with your first example also.

    I hope I didn?t muddy the waters.
  • Joe HebertJoe Hebert Posts: 2,159
    dchristo wrote: »
    Joe,

    Does this also work if the structure is an array?
    DEFINE_VARIABLE _
    
    _sSource sSource[4]
    

    --D
    Dave,

    It does but as you probably guessed you would have to setup the event like this.
    BUTTON_EVENT[dvTP,sSource[1].Buttons] {PUSH: {}}
    

    And then another button event like this:
    BUTTON_EVENT[dvTP,sSource[2].Buttons] {PUSH: {}}
    

    You can’t put an array of arrays inside an event, struct or not.

    I highly recommend structures but as I stated in an earlier post, I normally use simple button arrays for button events.

    You can use a struct inside a button event and get_last will work but I don’t know how practical it is.
  • travistravis Posts: 180
    Joe Hebert wrote: »

    I highly recommend structures but as I stated in an earlier post, I normally use simple button arrays for button events.

    I'm going with this.

    I made a spreadsheet that has all the channels and button numbers, and then made another column that combines them that I can just paste into netlinx (after stripping out all the carriage returns and adding a } to the end).
  • jjamesjjames Posts: 2,908
    travis wrote: »
    I'm going with this.

    I made a spreadsheet that has all the channels and button numbers, and then made another column that combines them that I can just paste into netlinx (after stripping out all the carriage returns and adding a } to the end).

    Using the spreadsheet, will you be converting it to CSV, and then uploading it somewhere for the processor to download and parse? That would be helpful in the event a channel changes! ;)
  • travistravis Posts: 180
    jjames wrote: »
    Using the spreadsheet, will you be converting it to CSV, and then uploading it somewhere for the processor to download and parse? That would be helpful in the event a channel changes! ;)

    Maybe I could make a program where I drop all the IR files on it, and it does everything for me except draw the buttons!
  • Spire_JeffSpire_Jeff Posts: 1,917
    travis wrote: »
    Maybe I could make a program where I drop all the IR files on it, and it does everything for me except draw the buttons!

    Why not have it "draw" the buttons too? You could create 100 blank buttons on a page and have the processor "draw" them as needed :)

    Jeff
  • jjamesjjames Posts: 2,908
    Travis, should I assume you think my suggestion is not feasible?

    It's very doable - in fact taught in P3.
  • viningvining Posts: 4,368
    I find the best way to do this is to put the formatted file on your companies website and have the individual jobs check in daily and check the date/time stamp of the file. If the file on the website's server is newer that the one each job last used it will retirieve the updated file, parse it and then update the structure that holds those values. This way you update your master file and the jobs take care of themselves.

    Joe Hebert wrote:
    I hope I didn’t muddy the waters.
    I actually like Muddy Waters, James Cotton ......
Sign In or Register to comment.