Skip to content

Snippets

  • PUBLISHED: 2024-2-8
  • UPDATED: 2024-2-10

A collection of my code snippets for Touch OSC.

Debugging

Debug Frame
function debugFrame(obj)
  print(obj.name .. ' ( ' .. obj.frame.x .. ', ' .. obj.frame.y .. ', ' .. obj.frame.w .. ', ' .. obj.frame.h .. ')')
end
Debug Lua Table
function debugTableInternal(tableName, table)
  local s = "TABLE: " .. tableName .. " {" 
  for k, v in pairs(table) do
    if(type(v) == "table") then
      local sDeep = debugTable(tostring(k), v)
      if (sDeep ~= nil) then
        s = s .. sDeep 
      end

    end
    s = s .. k .. ": " .. tostring(v) .. ", "
  end
  s = string.sub(s, 1, -3)
  s = s .. "}"
  return s
end
function debugTable(tableName, table)
  print(debugTableInternal(tableName, table))
end

Events

OnReceiveOSC
OSC Callback - Single Value
function onReceiveOSC(message, connections)
  local path = message[1]
  local arguments = message[2]
  if path=="/" then
    local v = arguments[1].value

  end
end

code "Snippet 2"

```Lua title="OSC Callback - Loop Values"
function onReceiveOSC(message, connections)
  local path = message[1]
  local arguments = message[2]
  if path=="/" then
    for i=1,#arguments do
      local tag = arguments[i].tag
      local v = arguments[i].value
    end
  end
end
```

code "Debug"

```Lua
function onReceiveOSC(message, connections)
  print(self.name, 'onReceiveOSC')
  local path = message[1]
  local arguments = message[2]
  print('\t path        =', path)
  for i=1,#arguments do
    print('\t argument    =', arguments[i].tag, arguments[i].value)
  end
  print('\t connections =', table.unpack(connections))
end
```

Math

Map: Remap a value to a different range
function map(value, oldMin, oldMax, newMin, newMax)
  return (((value - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin
end

Tables

Deep Table Clear
function deepClear(t)
  for k, v in pairs(t) do
    if type(v) == "table" then
      deepClear(v)
    end
    t[k] = nil
  end
end

-- Example usage:
myTable = {
  a = {
    b = {
      c = "value"
    }
  },
  d = "another value"
}

deepClear(myTable)

Finding Objects

All Object Based On Tag
local t = self.parent:findAllByProperty('tag', '3/6', true)
print(#t)
for i=1, #t do
  print(t[i].name)
  print(t[i].parent.name)
end
Find All Objects Based on Tag With Full Path
local objs = root:findAllByProperty("tag", "3/5", true)
-- print all object names and their path to root
for i, obj in ipairs(objs) do
  print(obj.name .. " " .. getPathToRoot(obj))
end

function getPathToRoot(obj)
  local path = obj.name
  local parent = obj.parent
  while parent ~= nil do
    path = parent.name .. "/" .. path
    parent = parent.parent
  end
  return path
end
Find Objects via Table of Names
--Deep Search
local obj = {"A", "B","C","D"}
for key, name in ipairs(names) do 
  local obj = self.parent:findByName(name, true)
end
--one level search
local obj = {"A", "B","C","D"}
for key, name in ipairs(names) do 
  local obj = self.parent:findByName(name, false)
end
--Loop Thru objects
local obj = {"A", "B","C","D"}
for key, name in ipairs(names) do 
  local obj = self.parent.children[name]
end

Object Layouts

??? "Object Layout - Even vertical spacing top to bottom

Even Vertical spacing top to bottom

```Lua
function layout()
local spacing = 15
local coiunt = 10
for index=led_count, 1, -1 do
    local box = "box" .. index

    self.children[box].frame.y = index * - spacing + led_count*spacing
  end
end
layout()
```

process() ```

Timers

Control Double-tap

```Lua

local delay = 300 local last = 0

function onValueChanged() if(not self.values.touch) then local now = getMillis() if(now - last < delay) then print('double tap!') last = 0 else last = now end end end ```

Send Periodic Message

```Lua

local delay = 1000 -- every 1000ms = 1s local last = 0

function update() local now = getMillis() if(now - last > delay) then last = now sendOSC('/ping') end end

```

Accelerometer

Get accelerometer Data

```Lua

if(hasAccelerometer()) then update = function() local values = getAccelerometer() sendOSC('/accxyz', table.unpack(values)) end end ```