Talk:Digiline Jumpdrive controller

From Pandorabox
Revision as of 18:44, 28 April 2022 by Huhhila (talk | contribs) (+syntaxhighlight)
Jump to navigation Jump to search

Draft of modernized version for comments, before making version 1.2 out of it, any comments? (some spaces eaten by copypaste)

-- version 1.1.huhhila.1

mem.factor = mem.factor or 1

-- TODO actually use converted coordinates in table
local function c(v)
return (v + 0)*5/4 + 3/8
end

local function c1(t)
local r = {}
for k, v in pairs(t) do
r[k] = (k == "X" or k == "Y") and c(v) or k == "H" and v*2*15/13*0.35 or v
end
return r
end

local function convert(t)
local r = {}
for _, v in ipairs(t) do
table.insert(r, c1(v))
end
return r
end

local function draw_touch()
digiline_send("touch", convert({
{ command = "set", locked = true, real_coordinates = true, width = (10-1)*5/4+3/4+1, height = (8-1)*5/4+3/4+1 },
{ command = "clear" },

{ command = "add", element="button_exit", name="xplus", label="X+", X=0, Y=0, W=4, H=1 },
{ command = "add", element="button_exit", name="xminus", label="X-", X=0, Y=1, W=4, H=1 },
{ command = "add", element="button_exit", name="yplus", label="Y+", X=0, Y=2, W=4, H=1 },
{ command = "add", element="button_exit", name="yminus", label="Y-", X=0, Y=3, W=4, H=1 },
{ command = "add", element="button_exit", name="zplus", label="Z+", X=0, Y=4, W=4, H=1 },
{ command = "add", element="button_exit", name="zminus", label="Z-", X=0, Y=5, W=4, H=1 },

{ command = "add", element="button", name="factorplus", label="Factor+", X=0, Y=6, W=4, H=1 },
{ command = "add", element="button", name="factorminus", label="Factor-", X=0, Y=7, W=4, H=1 },
{ command = "add", element="label", label="Factor: " .. mem.factor, X=4, Y=6.5 },

{ command = "add", element="button_exit", name="sethome", label="Set home", X=4, Y=0, W=4, H=1 },
{ command = "add", element="button_exit", name="gohome", label="Go home", X=4, Y=1, W=4, H=1 },

{ command = "add", element="button_exit", name="lock", label="Lock", X=4, Y=5, W=4, H=1 }}))
end

local function update_touch()
digiline_send("touch", { command = "replace", index = 9,
element = "label", label = "Factor: " .. mem.factor, X = 5.375, Y = 8.5 })
end

if event.type == "program" then
draw_touch()
end

if event.type == "digiline" and event.channel == "touch" then
mem.touch_event = event.msg
digiline_send("jumpdrive", {command="get"})
end

if event.type == "digiline" and event.channel == "jumpdrive" and event.msg.target then
local jump = false
local increment = ((event.msg.radius*2)+1)*mem.factor
if mem.touch_event.lock then
digiline_send("touch", { command = "clear" })

elseif mem.touch_event.sethome then
mem.home = event.msg.position

elseif mem.touch_event.gohome and mem.home then
digiline_send("jumpdrive", {command="set", {x = mem.home.x, y = mem.home.y, z = mem.home.z}})
jump = true

elseif mem.touch_event.yplus then
digiline_send("jumpdrive", {command="set", key="y", value=event.msg.target.y+increment})
jump = true

elseif mem.touch_event.yminus then
digiline_send("jumpdrive", {command="set", key="y", value=event.msg.target.y-increment})
jump = true

elseif mem.touch_event.xplus then
digiline_send("jumpdrive", {command="set", key="x", value=event.msg.target.x+increment})
jump = true

elseif mem.touch_event.xminus then
digiline_send("jumpdrive", {command="set", key="x", value=event.msg.target.x-increment})
jump = true

elseif mem.touch_event.zplus then
digiline_send("jumpdrive", {command="set", key="z", value=event.msg.target.z+increment})
jump = true

elseif mem.touch_event.zminus then
digiline_send("jumpdrive", {command="set", key="z", value=event.msg.target.z-increment})
jump = true

elseif mem.touch_event.factorplus then
mem.factor = mem.factor + 1
update_touch()

elseif mem.touch_event.factorminus then
mem.factor = mem.factor - 1
if mem.factor < 1 then
mem.factor = 1
end
update_touch()

end

if jump then
digiline_send("jumpdrive", {command="jump"})
end
end