jrp Posted 3 hours ago Report Posted 3 hours ago -- 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
jrp Posted 3 hours ago Author Report Posted 3 hours ago 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})
jrp Posted 3 hours ago Author Report Posted 3 hours ago 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, }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now