Home AMX User Forum AMX General Discussion
Options

Checking if a device from an array, equals a certain device

I'm trying to do the following:
define_variable
dev touchpanels[]={tp_5200,tp_ipad};
integer navigation[]={
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
}

define_event
button_event{
    PUSH:{
        page_flip(touchpanels[get_last(touchpanels)], get_last(navigation));
    }
}

define_function page_flip(dev tp, integer navigation)
{
    if(tp == touchpanels[1]){
        ;
    }
    else if(tp == touchpanels[2]){
        ;
    }
}

however it won't let me compile and only gives me syntax error. Is there a different way to check this?

Comments

  • Options
    viningvining Posts: 4,368
    maybe it will compile if you change the button event line to this:
    button_event[touchpanels,navigation]  
    
    then add send string 0 with info to see the results in diagnostics window.
  • Options
    jjamesjjames Posts: 2,908
    Yeah - looks like you forgot the proper button_event declaration. :-)

    Happens to the best of us!
  • Options
    Jorde_VJorde_V Posts: 393
    Just bad copy pasting in this case. There wasn't actually anything wrong in my code. Restarting ns fixed the issue.

    AMX support told me it wasn't possible though..

    Glad it's solved now though.

    Jjames, you trying to exclude me there? :p
  • Options
    viningvining Posts: 4,368
    Jorde_V wrote: »
    Just bad copy pasting in this case. There wasn't actually anything wrong in my code. Restarting ns fixed the issue.

    AMX support told me it wasn't possible though..

    Glad it's solved now though.

    Jjames, you trying to exclude me there? :p
    Told you what wasn't possible?

    You can definitely do what I think you want to do which is just see what tp in the array fired the button and then do something depending on which one it was. Personally I wouldn't pass the dev I'd just pass the index number but either way will work.
  • Options
    Jorde_VJorde_V Posts: 393
    vining wrote: »
    Told you what wasn't possible?

    You can definitely do what I think you want to do which is just see what tp in the array fired the button and then do something depending on which one it was. Personally I wouldn't pass the dev I'd just pass the index number but either way will work.

    They told me it wasn't possible to check whether a device equals the device in an array using the method I described above. Works just fine now, though I do wonder why it started working after I restarted NS.

    What would be the benefit of passing the index number rather than use the actual dev?
  • Options
    viningvining Posts: 4,368
    Jorde_V wrote: »
    They told me it wasn't possible to check whether a device equals the device in an array using the method I described above. Works just fine now, though I do wonder why it started working after I restarted NS.

    What would be the benefit of passing the index number rather than use the actual dev?
    a little less work, a little less code and little easier on the eyes. Sometimes you can get lost in all those brackets.

    Here's the code using index instead. Just a little different.
    define_variable
    dev touchpanels[]={10001:1:0,10002:1:0};
    integer navigation[]={
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    }
    
    define_event
    
    button_event[touchpanels,navigation]  
         {
        PUSH:{
            page_flip(get_last(touchpanels), get_last(navigation));
        }
    }
    
    define_function page_flip(integer iIndx, integer navigation)
    {
        if(iIndx == 1){
            
        }
        else if(iIndx == 2){
          
        }
    }
    

    eventually you'll have other arrays that work with your UI array that will tell you what type device it is (iPad, MVP9000i, MET-Ecom, KP, etc..) another to tell you the resolution, another for online status, etc. Using the index numbers is the easiest way sort this out and keep your code flexible so it's a good habit to get into so that you'll have less re-writing to do down the road when you need these other arrays or the next job when you have to re-use this code and the iPad isn't the 2nd device in the UI array.

    The hardest part about writing code is trying to make it re-usable with out going through it line by line to make changes for every job. The idea is to change some initial definitions and quantities and the rest of the code should take care of itself.
  • Options
    jjamesjjames Posts: 2,908
    Jorde_V wrote: »
    They told me it wasn't possible to check whether a device equals the device in an array using the method I described above. Works just fine now, though I do wonder why it started working after I restarted NS.

    What would be the benefit of passing the index number rather than use the actual dev?

    I don't see any advantage except for visually seeing what you're comparing.

    And also - unless I'm totally missing something (it is 3:30 am), comparing a dev to an indexed dev array works just fine.
    button_event[dv_tp,i_tpBtns_transport]	// Source Transport Buttons
    {
    	push:
    	{
    		test_function(dv_tp[get_last(dv_tp)]);
    	}
    }
    
    define_function test_function(dev tp)
    {
    	if(Panel[1].device == tp)
    	{
    		send_string 0, 'Device was dvIPAD_1'
    	}
    	
    	else
    	{
    		send_string 0, 'Device was dvIPAD_2'
    	}
    	
    	// Another way
    	switch(tp)
    	{
    		case dvIPAD_1:
    		{
    			send_string 0, 'Device was dvIPAD_1'
    		}
    		
    		case dvIPAD_2:
    		{
    			send_string 0, 'Device was dvIPAD_2'
    		}
    		
    		default:{send_string 0, "'Oop!'"}
    	}
    }
    
    Line 1 (03:36:50.612):: Device was dvIPAD_1
    Line 2 (03:36:50.614):: Device was dvIPAD_1
    Line 3 (03:36:54.341):: Device was dvIPAD_2
    Line 4 (03:36:54.342):: Device was dvIPAD_2

    Both examples in the function worked just fine.

    I'm doing the same thing as you are and have been working on it for the past few jobs now. I now work with structures rather than arrays, and try to make things as OO as possible. Using a lot of pre-compiler directives as well. Makes things easier to manage.
  • Options
    viningvining Posts: 4,368
    Yes either method will work but eventualy you'll find 1) there's no need to pass a dev structure when simple integer value will suffice and 2) at some point you'll want to compare that index to an online array for example to decide whether or not to send feedback, to UI type array to dertermine what types of feedback you can / should send and when using multiple instances of a device module you need another array to track which module instance a particular UI is controlling. Using the index of the UI that triggered the event is the pointer used to look up values in all the other possible arrays. The get last is returning an index and you're doing more work just to convert it back to a dev and there's really no point, just more work for you and the processor, granted not much more. I can't think of any advantage to passing the dev when working with indexes is a bit more versatile. Sometimes you have to pass the entire dev in order to compare the full DPS but this isn't one of them. It's alright if you do and you won't be arrested by the code police or anything but eventually you'll realize indexes (when possible) are just simpler/better. :)
  • Options
    a_riot42a_riot42 Posts: 1,624
    jjames wrote: »
    I now work with structures rather than arrays, and try to make things as OO as possible.

    I used that method for a while but noticed that using structures slowed any stuctures processing down by a larger than expected margin. As well, since they can't be passed to functions or modules, I went back to using mostly arrays.
    Paul
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    a_riot42 wrote:
    I used that method for a while but noticed that using structures slowed any stuctures processing down by a larger than expected margin.

    Do you have any evidence that using structures slows down processing by a larger than expected margin? I’ve never ran into any issues that would be attributed to that and I use structures everywhere.
    a_riot42 wrote:
    As well, since they can't be passed to functions or modules, I went back to using mostly arrays.
    Structures can be passed directly into functions and you can also declare a structure instance inside a function same as you would an integer or any other intrinsic data type.

    I use structures all the time as I find the code much easier to read, write, organize and maintain vs. using a whole bunch of multi-dimensional arrays.

    To each their own....
  • Options
    a_riot42a_riot42 Posts: 1,624
    Joe Hebert wrote: »
    Do you have any evidence that using structures slows down processing by a larger than expected margin? I’ve never ran into any issues that would be attributed to that and I use structures everywhere.

    Sorry, I meant modules not functions. They can be used in functions like you mention, I just don't do that typically since I went back to mostly arrays. I didn't find much of a performance issue with simple structs, but when I was trying to design my code to be more OO I used nested structs, and this is where I really noticed a difference. I didn't do any formal testing, but I was doing some bulk copying from one nested struct to another, and then sending the result to a list on a touch panel. I was using the nested struct method and found a lot of latency I couldn't account for. After trying all manner of things to get rid of it, I switched from nested structs to arrays and all of a sudden the latency disappeared. It appeared again switching back to nested structs. I would prefer to use nested structs but for this reason I now only use simple structs and only for storage and not for processing. I couldn't figure out why the nested structs were so slow, but arrays work just as well and have the advantage that I can pass them to modules. I prefer multidimensional array notation to struct notation anyway since I don't need to type the name of the struct member, just the array index, and the code completion in NS Studio doesn't seem to work all that well for struct members which is annoying.
    Paul
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    a_riot42 wrote:
    I was doing some bulk copying from one nested struct to another, and then sending the result to a list on a touch panel. I was using the nested struct method and found a lot of latency I couldn't account for
    I would think that’s where structs (nested or otherwise) should shine.

    How were you doing the copying? Did you copy each member of one struct to each member of another struct or did set one struct equal to another with one line of code?

    For example let’s say we have a struct that contains info for a song and we have a nested struct for an album that contains info for an album along an array of 12 songs structs:
    DEFINE_TYPE
    
    STRUCT _sSong {
       
       CHAR 	Name[32]
       INTEGER  	Tracktime
    }
    
    STRUCT _sAlbum {
    
       CHAR		Name[32]
       CHAR 	Artist[32]
       _sSong 	Songs[12]
    
    }
    

    We declare a single album and then a collection of albums
    DEFINE_VARIABLE _
    
    VOLATILE _sAlbum sTempAlbum
    VOLATILE _sAlbum sAlbumCollection[10]
    

    If we wanted to take a single album and copy it as the first album in the collection we could take the long route and copy each member like this:
    sAlbumCollection[1].Name = sTempAlbum.Name
    sAlbumCollection[1].Artist = sTempAlbum.Artist
    FOR (x=1; x<=12; x++) {
       sAlbumCollection[1].Songs[x].Name = sTempAlbum.Songs.Name[x]
    }
    

    Or we can take the single line approach to accomplish the same thing like this:
    sAlbumCollection[1] = sTempAlbum
    

    Or if we wanted to get an editable copy of the 5th album in the collection we need one line of code:
    sTempAlbum = sAlbumCollection[5]
    

    And let’s say we want to prompt the user if he tries to exit before saving the changes but we only want to prompt if the editable copy is different than the saved copy. Unfortunately we can‘t do that with one line of code since we can’t perform Boolean operators on structs. Instead we have to compare each member of one to another.
    a_riot42 wrote:
    I prefer multidimensional array notation to struct notation
    I prefer dot notation myself, it’s easier for my brain to process.
    a_riot42 wrote:
    the code completion in NS Studio doesn't seem to work all that well for struct members which is annoying.
    Incredibly annoying, exacerbating. It really takes the fun out of using structs.
  • Options
    a_riot42a_riot42 Posts: 1,624
    Joe Hebert wrote: »
    I would think that’s where structs (nested or otherwise) should shine.

    Yes one would hope.
    Joe Hebert wrote: »
    How were you doing the copying? Did you copy each member of one struct to each member of another struct or did set one struct equal to another with one line of code?

    Both. You might not see much latency with a small structure like in your example. However, with structs like this one, where a number of members can have a kilobyte or more of storage, and there are nested structs (since removed), things start to bog quite quickly. Fortunately arrays don't seem to exhibit this symptom. I can only guess that the memory management system isn't nearly as efficient with structs as it is with arrays.
    structure _stAVTransport
    {
    	integer  bAlarmIncludeLinkedZones
    	char     sAlarmState[32]
    	integer  iAlarmVolume
    	integer  bEnqueueAsNext	
    	char     sGroupID[8]
    	integer  iInstanceID
    	char     sISO8601Time[16]
    	char     sMemberID[16]
    	char     sMemberList[16]
    	integer  iNumTracks
    	char     sObjectID[8]
    	char     sQueue[8]
    	integer  bResumePlayback
    	char     sSavedQueueTitle[8]	
    	integer  iAbsoluteCounterPosition
    	char     sAbsoluteTimePosition[16]
    	integer  iAlarmIDRunning
    	char     sAlarmLoggedStartTime[16]
    	integer  bAlarmRunning
    	char     sAVTransportURI[128]
    	char     sAVTransportURIMetaData[128]
    	char     sCurrentMediaDuration[16]
    	char     sCurrentPlayMode[16]
    	char     sCurrentRecordQualityMode[16]
    	integer  iCurrentSection
    	integer  iCurrentTrack
    	char     sCurrentTrackDuration[16]
    	integer  iCurrentTrackDuration
    	char     sCurrentTrackMetaData[1024]
    	char     sCurrentTrackURI[256]
    	char     sCurrentTransportActions[16]
    	char     sLastChange[128]
    	char     sNextAVTransportURI[128]
    	char     sNextAVTransportURIMetaData[1024]
    	integer  iNumberOfTracks
    	char     sPlaybackStorageMedium[16]
    	char     sPossiblePlaybackStorageMedia[16]
    	char     sPossibleRecordQualityModes[16]
    	char     sPossibleRecordStorageMedia[16]
    	char     sRecordMediumWriteStatus[16]
    	char     sRecordStorageMedium[16]
    	integer  iRelativeTimePosition
    	char     sRelativeTimePosition[16]
    	integer  bRestartPending
    	integer  iSleepTimerGeneration
    	integer  bSnoozeRunning
    	char     sTransportErrorCondition[128]
    	char     sTransportErrorURI[128]
    	char     sTransportPlaySpeed[1]
    	char     sTransportState[16]
    	char     sTransportStatus[16]
    }
    
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    a_riot42 wrote: »
    I can only guess that the memory management system isn't nearly as efficient with structs as it is with arrays.
    I’m not questioning your results but I’m certainly surprised that the amount of lines of code it takes to copy 50+ arrays is noticeably faster than the one line of code it takes to copy that struct. It wouldn’t have been my first guess.
  • Options
    a_riot42a_riot42 Posts: 1,624
    Joe Hebert wrote: »
    I’m not questioning your results but I’m certainly surprised that the amount of lines of code it takes to copy 50+ arrays is noticeably faster than the one line of code it takes to copy that struct. It wouldn’t have been my first guess.

    It was only the nested structs that seem to cause the issue. I didn't have time to do more testing so I could be off. But with arrays the compiler can do some optimizations, whereas with nested structs, perhaps due to the padding involved, it may not be able to do this. Just a guess.
    Paul
Sign In or Register to comment.