Skip to content

Instantly share code, notes, and snippets.

@cheapie
Created April 9, 2017 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheapie/7642faed7465829adf81913b2261b3da to your computer and use it in GitHub Desktop.
Save cheapie/7642faed7465829adf81913b2261b3da to your computer and use it in GitHub Desktop.
--TSLib Test
--A product of Advanced Mesecons Devices, a Cheapie Systems company
--This is free and unencumbered software released into the public domain.
--See http://unlicense.org/ for more information
local tslib = {
_mt = {
setChannel = function(self,channel)
self._channel = channel
end,
getChannel = function(self)
return(self._channel)
end,
draw = function(self)
digiline_send(self._channel,self._commands)
end,
clear = function(self)
self._commands = {{command="clear"}}
end,
setLock = function(self,lock)
if lock then
table.insert(self._commands,{command="lock"})
else
table.insert(self._commands,{command="unlock"})
end
end,
addLabel = function(self,x,y,label,vertical)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
if type(label) ~= "string" then
label = tostring(label)
end
local cmd = {
command = "addlabel",
X = x,
Y = y,
label = label,
}
if vertical then cmd.command = "addvertlabel" end
table.insert(self._commands,cmd)
end,
addImage = function(self,x,y,w,h,tex)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if type(tex) ~= "string" then
tex = tostring(tex)
end
local cmd = {
command = "addimage",
X = x,
Y = y,
W = w,
H = h,
texture_name = tex,
}
table.insert(self._commands,cmd)
end,
addButton = function(self,x,y,w,h,name,label,exit)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if type(name) ~= "string" then
name = tostring(name)
end
if type(label) ~= "string" then
label = tostring(label)
end
local cmd = {
command = "addbutton",
X = x,
Y = y,
W = w,
H = h,
name = name,
label = label,
}
if exit then cmd.command = "addbutton_exit" end
table.insert(self._commands,cmd)
end,
addImageButton = function(self,x,y,w,h,name,label,tex,exit)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if type(name) ~= "string" then
name = tostring(name)
end
if type(label) ~= "string" then
label = tostring(label)
end
if type(tex) ~= "string" then
tex = tostring(tex)
end
local cmd = {
command = "addimage_button",
X = x,
Y = y,
W = w,
H = h,
name = name,
label = label,
image = tex,
}
if exit then cmd.command = "addimage_button_exit" end
table.insert(self._commands,cmd)
end,
addField = function(self,x,y,w,h,name,label,default,password)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if type(name) ~= "string" then
name = tostring(name)
end
if type(label) ~= "string" then
label = tostring(label)
end
if type(default) ~= "string" then
default = tostring(default)
end
local cmd = {
command = "addfield",
X = x,
Y = y,
W = w,
H = h,
name = name,
label = label,
default = default,
}
if password then cmd.command = "addpwdfield" end
table.insert(self._commands,cmd)
end,
addTextArea = function(self,x,y,w,h,name,label,default)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if type(name) ~= "string" then
name = tostring(name)
end
if type(label) ~= "string" then
label = tostring(label)
end
if type(default) ~= "string" then
default = tostring(default)
end
local cmd = {
command = "addtextarea",
X = x,
Y = y,
W = w,
H = h,
name = name,
label = label,
default = default,
}
table.insert(self._commands,cmd)
end,
addDropdown = function(self,x,y,w,h,name,choices,selected)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if not selected then selected = 1 end
assert((type(selected))=="number","Invalid selection index")
if type(name) ~= "string" then
name = tostring(name)
end
assert((type(choices) == "table" and #choices >= 1),"Invalid choices list")
local cmd = {
command = "adddropdown",
X = x,
Y = y,
W = w,
H = h,
name = name,
choices = choices,
selected_id = selected,
}
table.insert(self._commands,cmd)
end,
},
new = function(self,channel)
local ret = {}
for k,v in pairs(self._mt) do
ret[k] = v
end
ret._channel = channel
ret._commands = {{command="clear"}}
return ret
end,
}
local disp = tslib:new()
disp:setChannel("touchscreen")
disp:addLabel(0,0,"TSLib Demo",false)
disp:addLabel(3,0,"Hello, world!",true)
disp:addImage(0,0.5,1,1,"default_dirt.png")
disp:addButton(0,1.5,1.5,1,"button","Button")
disp:addButton(0,2.5,2,1,"button2","Exit Button",true)
disp:addImageButton(0,3.5,1.5,1,"button3","Image Button","default_stone.png")
disp:addImageButton(0,4.5,2,1,"button4","Image Exit Button","default_tree.png",true)
disp:addField(0.3,6,2,1,"field","Field","TSLib")
disp:addField(0.3,7,2,1,"field2","Password Field","",true)
disp:addTextArea(4,3,2,3,"textarea","Text Area","This is a test of TSLib.")
disp:addDropdown(4,2,2,1,"dropdown",{"Apple","Orange","Banana"},2)
disp:draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment