Setting a patch name in the status bar

I was asked (by email) how one can display a name of a patch received in sysex dump.

I will share this as it might come in handy to others too…

-- Define an array for the patch name using ASCII codes
local patchNameBytes = {32, 32, 32, 32, 32, 32, 32, 32, 32, 32}

-- Function to set individual ASCII codes at specific indices
function setAsciiCodeAtIndex(asciiArray, index, code)
    if index >= 1 and index <= #asciiArray then
        asciiArray[index] = code
    end
end

-- Function to convert ASCII codes to a text string
function asciiArrayToString(asciiArray)
    local charArray = {}
    for i, code in ipairs(asciiArray) do
        table.insert(charArray, string.char(code))
    end
    return table.concat(charArray)
end

-- Set individual ASCII codes to create the string "kawai K1"
setAsciiCodeAtIndex(patchNameBytes, 1, 107)  -- 'k'
setAsciiCodeAtIndex(patchNameBytes, 2, 97)   -- 'a'
setAsciiCodeAtIndex(patchNameBytes, 3, 119)  -- 'w'
setAsciiCodeAtIndex(patchNameBytes, 4, 97)   -- 'a'
setAsciiCodeAtIndex(patchNameBytes, 5, 105)  -- 'i'
setAsciiCodeAtIndex(patchNameBytes, 6, 32)   -- Space
setAsciiCodeAtIndex(patchNameBytes, 7, 75)   -- 'K'
setAsciiCodeAtIndex(patchNameBytes, 8, 49)   -- '1'

-- Convert ASCII codes to a text string
local patchName = asciiArrayToString(patchNameBytes)

print(patchName)  -- writes "kawai K1" to the debugger log

info.setText(patchName) -- Sets "kawai K1" text in the bottom status bar

Of course, in real world preset you will use received sysex bytes when calling the setAsciiCodeAtIndex() function.

If the sysex bytes expressing the patch name were not ascii codes, a conversion to ascii must be done first.

2 Likes

Wow that is pretty cool to have!
Thanks for sharing this.