Transmit Data, call Functions, Log Information in TouchOsc with MIOS32_MIDI_SendDebugMessage()

– Display MIOS32_MIDI_SendDebugMessage() messages and transmit data/trigger functions
 
– this prints to the Touchosc Log window, as well as to a text element called “text”

local MIOS_MSG = root.children.text – <- this text element must be in root, or simply change this adress

function log(msg)

print(msg)

text = msg.. “\n” .. text

–text = text .. “\n” .. msg

local count = 0

MIOS_msg_lines = {}

for line in text:gmatch(“([^\n]*)\n?”) do

if line == “” then break end

table.insert(MIOS_msg_lines, line)

count = count + 1

if count >= maxLines then break end

end

text = table.concat(MIOS_msg_lines, “\n”)

MIOS_MSG.values.text = text

end
 
– messages in the form of “Start .. ID .. list of numbers seperated by spaces .. End” can be used to transmit data to TouchOSC and call functions

local MIOS_SYSEX_COMMAND_FUNCTIONS =

{

– numbers from mios are 0 based!

MyFunction = function(numbers)

print(#numbers)

end
 
}
 
function Do_MIOS_Sysex_Command(ID, numbers)

local fn = MIOS_SYSEX_COMMAND_FUNCTIONS[ID]

if fn then fn(numbers) end

end

 
– sysex message handling

local sysex_buffer = {}

local last_sysex_time = 0

local sysex_timeout = 200 – ms (adjust if needed)
 
function flush_sysex_buffer()

if #sysex_buffer > 0 then

local msg = table.concat(sysex_buffer)

sysex_buffer = {}

log("MIOS32 (timeout flush): " .. msg)

Log("[TIMEOUT] " .. msg)

end

end
 
function onReceiveMIDI(sysExMessage)

local now = os.clock() * 1000 – ms resolution

– if too much time passed since last byte -> flush old buffer

if now - last_sysex_time > sysex_timeout then

flush_sysex_buffer()

end

last_sysex_time = now
 
local headerLen = 8

for i = headerLen + 1, #sysExMessage do

local b = sysExMessage[i]
 
if b == 0xF7 then

– end of SysEx, flush buffer as a complete message

local msg = table.concat(sysex_buffer)

sysex_buffer = {}
 
local ID = msg:match(“^Start%s+(%a+).-%d+.-End$”)

if ID then

local numbers = {}

for num in msg:gmatch(“(%d+)”) do

table.insert(numbers, tonumber(num))

end

Do_MIOS_Sysex_Command(ID, numbers)

else

log("MIOS32: " .. msg)

end

else

if b ~= 0 then

table.insert(sysex_buffer, string.char(b))

end

end

end

end

 

if you place this in your root script in TouchOSC, the text element will display the last messages from MIOS32.

I included a little parser that can be used to trigger functions and transmit data - like

 

“Start MyFunction 4 35 2334 4 End” that will call MyFunction({4,35,2334,4})

The way i am using this my functions are actually all in a list called MIOS_SYSEX_COMMAND_FUNCTIONS[ID]

 

so in this example :

MIOS_SYSEX_COMMAND_FUNCTIONS = {

MyFunction = function(numbers) print(numbers[1] .. numbers[2]) end,

}