javascript and sliders

118 views
Skip to first unread message

Tom Neary

unread,
Mar 12, 2016, 6:59:16 PM3/12/16
to CommandFusion Software
Hi,

I am in the process of moving all my CF commands from GUI to javascript.  I was able to get all the necessary commands to control my B&K amps via javascript, thanks to Barry Gordon.  I am a little stumped on the use of sliders with javascript.  I've read through other posts to the forums and I understand how the analog join and data variables are passed to the javascript function.  My question, what is the best way to handle multiple sliders with similar functions.  I have 12 audio zones and each zone has 3 sliders, Volume, Bass, and Treble.  In theory I can use a single javascript function, with arguments defining the system, zone, volume|bass|treble, and level.  My thought is to define my analog joins in a method that I can use to parse out for either nested ifs or case and switch.  So the first 2 numbers can be the zone, the third number can be either volume, bass or tremble, then the data sent will set the volume.  Is there a simpler way to have javascript recognize which slider is being accessed?

thank in advance,

Tom

Jarrod Bell

unread,
Mar 12, 2016, 8:00:11 PM3/12/16
to comman...@googlegroups.com
You can use tags on sliders (via the tag editor, open it from the view menu in guiDesigner).
Then you can listen to drag and release events (or just drag events if you prefer) and read the value of the tags array to see which slider is being used.
You can assign multiple tags (separated by spaces) and access them all in the tags array.

Something like this within your script - just have to give your sliders unique analog joins, like 1, 2, 3 in the case below:

CF.watch(CF.ObjectDraggedEvent, ["a1", "a2", "a3"], function (join, value, tokens, tags) {
    var zone, i = 0;
    // Loop through all zone possibilities until finding the zone tag that was assigned to the slider
    for (;i<12; i++) {
        // Check if the tags array contains a zone tag like "zone_1" or "zone_2", etc
        if (tags.indexOf["zone_" + i] !== false) {
            // Zone found, break out of loop
            zone = i;
            break;
        }
    }
    if (!zone) {
        // Zone tag wasn't assigned to the slider, so exit now
        return;
    }

    // Now zone variable contains the zone number being adjusted...

    if (tags.indexOf["bass"] !== false) {
        // Bass adjustment

    } else if (tags.indexOf["treble"] !== false) {
        // Treble adjustment

    } else if (tags.indexOf["volume"] !== false) {
        // volume adjustment
    }
});

Regards,

Jarrod Bell
CommandFusion
www.commandfusion.com


--
You received this message because you are subscribed to the Google Groups "CommandFusion Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to commandfusio...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tom Neary

unread,
Mar 13, 2016, 12:24:12 PM3/13/16
to CommandFusion Software
Hi Jarrod,
I appreciate the quick response and the code example.  I can't seem to get it to work though.  I created 3 test sliders.  Each one with a unique analog join 501-503.  I have 2 tags on each slider zn1-3 for zone number and volume.  The code seems to be breaking without reading a valid tag.  I put a CF.log in the code an I am seeing the correct join, value, and tags, output is below, however I am seeing zn0 for the zone variable, even though no tags have zn0.  It is also going right into the bass if, even though the tag is volume.

"Join= a501 value= 65535 tokens= [object Object] tags= zn1,volume"

It doesn't seem to be acting properly on the zone name or mode in the tags. I cannot find any info on how to use tags, so I ran out of ideas on troubleshooting.

thanks again,
Tom

Tom Neary

unread,
Mar 13, 2016, 3:58:20 PM3/13/16
to CommandFusion Software
Hi Jarrod,
I was able to get it working after testing it more today.  I was able to output the correct indexOf output via CF.log, so I that was working. It seemed to be the square brackets in this line if (tags.indexOf["zone_" + i] !== false) that was causing the issue.  I changed it to parenthesis, as well as change the operator to 0, and it work.  My updated code is listed below in case anyone is interested.

CF.watch(CF.ObjectReleasedEvent, ["a501", "a502", "a503"], function (join, value, tokens, tags) {
    var zone, i = 0;
    CF.log(" ")

    // Loop through all zone possibilities until finding the zone tag that was assigned to the slider
    for (; i<4; i++) {
        // Check if the tags array contains a zone tag like "zn1" or "zn2", etc
        if (tags.indexOf("zn" + i) == 0) {
            // Zone found, break out of loop
            zone = "zn" + i;
            CF.log(" ")
            CF.log("zone tag match = " + zone);
            break;
        }
    }
    if (!zone) {
    CF.log(" ")
    CF.log("no zone tag found")
    CF.log(" ")
        // Zone tag wasn't assigned to the slider, so exit now
        return;
    }

    // Now zone variable contains the zone number being adjusted...

    if (tags.indexOf("bass") == 0) {
        var bass=(Math.round(value/5461)).toString(16);
        CF.log(" ");
        CF.log("Zone - " + zone + " Bass Level- " + bass);
        

    } else if (tags.indexOf("treble") == 0) {
    CF.log(" ")
    var treble=(Math.round(value/5461)).toString(16);
    CF.log("Zone - " + zone + " Bass Level- " + treble);


} else if (tags.indexOf("volume") !== 0) {
CF.log(" ");
    var volume=(Math.round(value/1633)).toString(16);
    CF.log("Zone - " + zone + " Volume Level- " + volume);

    }
});
On Saturday, March 12, 2016 at 6:59:16 PM UTC-5, Tom Neary wrote:

Barry Gordon

unread,
Mar 13, 2016, 4:20:42 PM3/13/16
to comman...@googlegroups.com
Your problem is just JavaScript errors in syntax.  Parens enclose function or method arguments, brackets enclose indicies. Get a good reference book and you will do much better. I use JavaScript The definitive guide by Flanagan

Sent from my iPhone
--

Jarrod Bell

unread,
Mar 13, 2016, 8:59:31 PM3/13/16
to comman...@googlegroups.com
Sorry about my syntax errors! Great that you figured it out.
You should use if (tags.indexOf("zn" + i) !== -1) so that the tags don't have to be in any specific order.
indexOf will report -1 if not found.


Regards,

Jarrod Bell
CommandFusion
www.commandfusion.com


--

Tom Neary

unread,
Mar 13, 2016, 9:13:45 PM3/13/16
to CommandFusion Software, jar...@commandfusion.com
Ok, thanks for the tip. I was writing indexOf out via CF.log, and I saw the 0 and -1. I will update to !==-1.  Thanks again for the help.
Reply all
Reply to author
Forward
0 new messages