Home AMX User Forum AMX General Discussion

Arrays vs DEVCHANs?

How are these different when it comes to using them for button presses? They seem to look almost identical in the declarations:
DEFINE_VARIABLE
DEVCHAN dcNums[] = {
{dvTP,71},
{dvTP,72},
{dvTP,73},
{dvTP,74},
{dvTP,75}
and then
NON_VOLATILE INTEGER nInputSelected[] = {41,42,43,44,45,46,47,48}
What is the advantage to using a devchan in this situation, or is there an advantage at all?

Comments

  • the8thstthe8thst Posts: 470
    vegastech wrote: »
    How are these different when it comes to using them for button presses? They seem to look almost identical in the declarations:
    DEFINE_VARIABLE
    DEVCHAN dcNums[] = {
    {dvTP,71},
    {dvTP,72},
    {dvTP,73},
    {dvTP,74},
    {dvTP,75}
    
    and then
    NON_VOLATILE INTEGER nInputSelected[] = {41,42,43,44,45,46,47,48}
    
    What is the advantage to using a devchan in this situation, or is there an advantage at all?

    The main difference is that the DEVCHAN in your example is a single device while you can combine device arrays with channel arrays for more flexible code.
    NON_VOLATILE INTEGER nInputSelected[] = {41,42,43,44,45,46,47,48}
    NON_VOLATILE DEV dvTPs[] = {dvTP1,dvTP2,dvTP3,dvTP4}
    
    BUTTON_EVENT[dvTPs,nInputSelected]
    {
          PUSH:
          {
                 STACK_VAR INTEGER tpIndex
                 STACK_VAR INTEGER nSource
    
                 tpIndex = get_last(dvTPs)
                 nSource = get_last(nInputSelected)
           
                 // do something based on which touchpanel or which source was selected
           }
    }
    
  • PhreaKPhreaK Posts: 966
    Devchan's are a structured data type, whereas arrays themselves are not a type, they are a grouping a variables of one type. Each devchan, or 'slot' within an array of devchan contains two pieces of data - a device reference and a channel (just in case that wasn't obvious from the name).

    The core concept behind there use is to help in the abstraction of interfaces (whether that be with a user via the TP, or via IO port to sync sensors etc) from core program logic. On a basic level say you were to have a series of devchans that you had mapped to transport controls for a bluray player via button events.
    DEFINE_VARIABLE
    DEVCHAN dcPlay[] {
        {dvTP, 1},
        {dvAnotherTP, 54}
    }
    
    DEFINE_EVENT
    BUTTON_EVENT[dcPlay] {
        PUSH: {
            // Do stuff here
        }
    }
    
    Now say an addition interface device was added. To map these controls to it as well the only thing you, or whoever else may be modifying your code in the future has to do is add an additional entry to the dcPlay devchan array.
    DEVCHAN dcPlay[] {
        {dvTP, 1},
        {dvAnotherTP, 1},
        {dvNowSomeonesJustCompensating, 1}
    }
    
    If all has been written nicely anyone changing the code does need to have any knowledge of what is going on outside of this. The button will function, it's feedback will update etc.

    Basically a devchan allow you to explicity list a selection of device and channel pairs you want associated with a particular behavior. On the flip side there are some situations where your code may just need to say right... I want any of these channels on any of these devices to do this, this is when you way want to look into a combination of a device array and button array. Both options are there.
Sign In or Register to comment.