Skip to content

Instantly share code, notes, and snippets.

@okyeron
Created February 5, 2019 06:49
Show Gist options
  • Save okyeron/1c77c4e9822903bac135a98156acd735 to your computer and use it in GitHub Desktop.
Save okyeron/1c77c4e9822903bac135a98156acd735 to your computer and use it in GitHub Desktop.
-- hid-terminal.lua
-- requires keycodes lib
-- enter text to the norns screen - upper and lower case text
local keyb = hid.connect()
local keycodes = require 'keycodes'
local wordarray ={}
local keyinput = ""
local keyoutput = ""
local start_y = 10
local shift = false;
function init()
screen.aa(1)
--tab.print(keyb)
tab.print(hid.devices)
--tab.print(hid.devices[keyb.index])
--print(keyb.index .." " ..hid.devices[keyb.index].name)
--tab.print(hid.devices[4].ports)
redraw()
end
function draw_firstscreen()
--screen.clear()
screen.level(15)
screen.line_width(1)
screen.font_face(0)
screen.font_size(8)
screen.move(10,10)
screen.text("NORNS> ")
screen.update()
end
function textwrap(s, w, offset, prefix)
local len = string.len(s)
--print (len)
local strstore = {}
local k = 1
if len == 0 then
screen.text(prefix)
else
while k <= len do
table.insert(strstore, string.sub(s, k, k+w-1))
k = k + w
end
strposition = start_y + offset
for v in pairs(strstore) do
screen.text(prefix .. strstore[v])
screen.move(0, strposition)
strposition = strposition + offset
end
end
end
function get_key(code, val, shift)
--print(shift)
if keycodes.keys[code] ~= nil and val == 1 then
if (shift) then
return(keycodes.keys[code])
else
return(lowercase(keycodes.keys[code]))
end
end
end
function keyb.event(typ, code, val)
--print("hid.event ", typ, code, val)
if (code == hid.codes.KEY_LEFTSHIFT) and (val == 2) then
shift = true;
elseif (code == hid.codes.KEY_LEFTSHIFT) and (val == 0) then
shift = false;
end
keyinput = get_key(code, val, shift)
buildword()
end
function lowercase(str)
return string.lower(str)
end
function buildword()
if keyinput ~= "Enter" then
table.insert(wordarray,keyinput)
keyoutput = table.concat(wordarray )
redraw()
else
redraw()
keyoutput = ""
wordarray = {}
end
end
-- screen redraw function
function redraw()
-- clear screen
screen.clear()
-- set pixel brightness (0-15)
screen.level(15)
screen.line_width(1)
screen.font_face(0)
screen.font_size(8)
screen.move(0,10)
textwrap(keyoutput, 15, 10, "NORNS> ")
screen.update()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment