Iteration does not work as expected for SysEx message

Dear forum members,
I have this code where the insert parameters are sent as sysex messages to an external display. By iterating “i” I want to address the parameter number and the according column of my display, but somehow all parameters are displayed starting at the same column (at i = 7).

for (var i = 0; i < 8; ++i)  
{
    var displayValue=ChannelInsertParameters[i].mOnDisplayValueChange = function (activeDevice, activeMapping, value) {
    var dataDisplay = [0xf0, 0x00, 0x00, 0x66, 0x14, 0x12, i*0x07];
    var dataDisplayReset = [0xf0, 0x00, 0x00, 0x66, 0x14, 0x12, i*0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7]
    
    for (var j = 0, len = value.length; j < len; ++j)
        {dataDisplay.push(value.charCodeAt(j))}
        dataDisplay.push(0xF7)
        midiOutput.sendMidi(activeDevice, dataDisplayReset)
        midiOutput.sendMidi(activeDevice, dataDisplay)
    }}

On the other hand when iterating “manually” all works fine:

var displayValue=ChannelInsertParameters[0].mOnDisplayValueChange = function (activeDevice, activeMapping, value) {
        var dataDisplay = [0xf0, 0x00, 0x00, 0x66, 0x14, 0x12, 0*0x07];
        var dataDisplayReset = [0xf0, 0x00, 0x00, 0x66, 0x14, 0x12, 0*0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7]
        
        for (var j = 0, len = value.length; j < len; ++j)
            {dataDisplay.push(value.charCodeAt(j))}
            dataDisplay.push(0xF7)
            midiOutput.sendMidi(activeDevice, dataDisplayReset)
            midiOutput.sendMidi(activeDevice, dataDisplay)
        }

    var displayValue=ChannelInsertParameters[1].mOnDisplayValueChange = function (activeDevice, activeMapping, value) {
        var dataDisplay = [0xf0, 0x00, 0x00, 0x66, 0x14, 0x12, 1*0x07];
        var dataDisplayReset = [0xf0, 0x00, 0x00, 0x66, 0x14, 0x12, 1*0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7]
        
        for (var j = 0, len = value.length; j < len; ++j)
            {dataDisplay.push(value.charCodeAt(j))}
            dataDisplay.push(0xF7)
            midiOutput.sendMidi(activeDevice, dataDisplayReset)
            midiOutput.sendMidi(activeDevice, dataDisplay)
        }

Does anybody know what has to be changed in the first code to make it work like the second?
Thanks a lot in advance, Emre

Well, I’m no java guy, (retired C++ pgmr), but that i*07 in var dataDisplay gives me the creeps. It would be really hard for a compiler to treat that dataDisplay as the constant it wants to be using i, which is non-constant. Consider using dataDisplay and the other vars using it up to, but not including i * ??. Send i * ?? individually each time through the transmit loop and you should be good to go.

Hi,

If I understand you right, you mean…

var column = i*0x07;
var dataDisplay = [..., column];

Right?

I would leave that out i*0x07 of dataDisplay altogether and transmit it individually.

BUT…

Taking a second look at the code I (finally) noticed the push member func and so it appears that dataDisplay is indeed modifiable (although the right side of that assignment expression is a constant). What your suggesting should work…if that’s legal code, because it is forced to evaluate column every time.

i * 0x07

otherwise if you assign (push?) that value to the end of dataDisplay in the loop, not as part of it’s declaration, that’ll work too. It’s the declaration that feels wonky to me.

Try:

for (var i = 0; i < 8; ++i)  
{
    var displayValue=ChannelInsertParameters[i].mOnDisplayValueChange = (function (activeDevice, activeMapping, value) {
     var i2=this.i    
var dataDisplay = [0xf0, 0x00, 0x00, 0x66, 0x14, 0x12, i2*0x07];
    var dataDisplayReset = [0xf0, 0x00, 0x00, 0x66, 0x14, 0x12, i2*0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7]
    
    for (var j = 0, len = value.length; j < len; ++j)
        {dataDisplay.push(value.charCodeAt(j))}
        dataDisplay.push(0xF7)
        midiOutput.sendMidi(activeDevice, dataDisplayReset)
        midiOutput.sendMidi(activeDevice, dataDisplay)
    }).bind({i})
}
1 Like

Thanks a million @SuperG , @Martin.Jirsak and @m.c for your support!
I tried your code @m.c and it works perfectly.
Anybody wants coffee?
Have a nice day, Emre

2 Likes