Solved : Need help with calling a function

Dear javascript wizards,
I have this block of code which works perfectly fine:

for (var i = 0; i < PushEncodersA; ++i)  
{
    var displayInsertsParameterTitle=allInsertParameters[i].mOnTitleChange = 
    (function (activeDevice, activeMapping, value, print) {
    var i2=this.i
    var dspl="0x"+(11+i2)
    var row = 0x00
    var columns = 7
    
    
    var dataDisplay = [0xf0, 0x00, 0x00, 0x66, 0x14, dspl, row];
    var dataDisplayReset = [0xf0, 0x00, 0x00, 0x66, 0x14, dspl, row]
    
    for (var j = 0, len = columns; j < len; ++j) {
        dataDisplay.push(print.charCodeAt(j))
        dataDisplayReset.push(0x00)
        }
        dataDisplayReset.push(0xF7)
        dataDisplay.push(0xF7)
        midiOutput.sendMidi(activeDevice, dataDisplayReset)
        midiOutput.sendMidi(activeDevice, dataDisplay)
    }).bind({i})
}

Since I’m using a big part of that code (the function) at other points of my script again and again I tried to put it into a variable and call that. Well, I don’t get en error report but the code like this just does not work:

var printDspl = (function (activeDevice, activeMapping, value, print) {
    var i2=this.i
    var dspl="0x"+(11+i2)
    var row = 0x00
    var columns = 7
    
    
    var dataDisplay = [0xf0, 0x00, 0x00, 0x66, 0x14, dspl, row];
    var dataDisplayReset = [0xf0, 0x00, 0x00, 0x66, 0x14, dspl, row]
    
    for (var j = 0, len = columns; j < len; ++j) {
        dataDisplay.push(print.charCodeAt(j))
        dataDisplayReset.push(0x00)
        }
        dataDisplayReset.push(0xF7)
        dataDisplay.push(0xF7)
        midiOutput.sendMidi(activeDevice, dataDisplayReset)
        midiOutput.sendMidi(activeDevice, dataDisplay)
    }).bind({i})

    

//Display Inserts Parameter Title
for (var i = 0; i < PushEncodersA; ++i)  
{
    var displayInsertsParameterTitle=allInsertParameters[i].mOnTitleChange = printDspl
    
}

Has anybody got a solution? Many thanks in advance!
Emre

Why do you want it to be a variable? You can create a function.

2 Likes

Just as @cubace said.

Anyway, since I recognize using some stuff I’ve post, here’s a (NOT tested) edit:

function printDspl(activeDevice,index,print) {
    var realIndex=11+index 
    var dspl="0x"+realIndex.toString()
    
    var row = 0x00
    var columns = 7
    
    var dataDisplay = [0xf0, 0x00, 0x00, 0x66, 0x14, dspl, row];
    var dataDisplayReset = [0xf0, 0x00, 0x00, 0x66, 0x14, dspl, row]
    
    var printLength=print.length
    if(printLength<columns){
        while(printLength++<columns){
            print=print+" "
        }
    }

    for (var j = 0;j<columns; ++j) {
        dataDisplay.push(print.charCodeAt(j))
        dataDisplayReset.push(0x00)
    }
    dataDisplayReset.push(0xF7)
    dataDisplay.push(0xF7)

    //why resetting the display's positions? Shouldn't just rewrite do what you want?
    midiOutput.sendMidi(activeDevice, dataDisplayReset)
    //-------------------------------------------------
    midiOutput.sendMidi(activeDevice, dataDisplay)
}

    
//Display Inserts Parameter Title
for (var i = 0; i < PushEncodersA; ++i)  
{
    allInsertParameters[i].mOnTitleChange = (function(activeDevice,activeMapping,value,print){
        var index=this.i 
        printDspl(activeDevice,index,print)
    }).bind({i})
    
}

Just because I don’t know better :sweat_smile:
Thanks!

Thanks you so much @m.c , great idea to adjust the resetting also!
Your code works (again).
Is there a way to keep" var = row" undefined in the function and declare it when calling?
I tried this but it just works for “Display Inserts Parameter Title”, not “Display Inserts Parameter Value”:

function printDspl(activeDevice,index,print) {
    var realIndex=11+index 
    var dspl="0x"+realIndex.toString()
    var row
    var columns = 7
    
    var dataDisplay = [0xf0, 0x00, 0x00, 0x66, 0x14, dspl, row];
    var dataDisplayReset = [0xf0, 0x00, 0x00, 0x66, 0x14, dspl, row]
    
    var printLength=print.length
    if(printLength<columns){
        while(printLength++<columns){
            print=print+" "
        }
    }

    for (var j = 0;j<columns; ++j) {
        dataDisplay.push(print.charCodeAt(j))
        dataDisplayReset.push(0x00)
    }
    dataDisplayReset.push(0xF7)
    dataDisplay.push(0xF7)

    //why resetting the display's positions? Shouldn't just rewrite do what you want?
    midiOutput.sendMidi(activeDevice, dataDisplayReset)
    //-------------------------------------------------
    midiOutput.sendMidi(activeDevice, dataDisplay)
}

    
//Display Inserts Parameter Title
for (var i = 0; i < PushEncodersA; ++i)  
{
    allInsertParameters[i].mOnTitleChange = (function(activeDevice,activeMapping,value,print){
        var index=this.i 
        var row = 0x00
        printDspl(activeDevice,index,print)
    }).bind({i})
    
}

//Display Inserts Parameter Value
for (var i = 0; i < PushEncodersA; ++i)  
{
    allInsertParameters[i].mOnDisplayValueChange = (function (activeDevice, activeMapping, print) {
        var index=this.i 
        var row = 0x07

        printDspl(activeDevice,index,print)
    }).bind({i})
}

I really appreciate your help!
All the best, Emre

Sure.

function printDspl(activeDevice,index,print,row)

Remove the “var row” from inside the function’s content, since we define it inside the function’s arguments now.

Call:

printDspl(activeDevice,index,print,row)

Place 0x00 or 0x07 depending on your needs.

This just works perfect, thank you so much!!
Emre