From 4922c5550fe015e2d2f09f139fd0806c537cd2e1 Mon Sep 17 00:00:00 2001 From: hkc Date: Sun, 21 Jan 2024 18:46:33 +0300 Subject: [PATCH] Bigterm slideshow, etc --- bigterm.lua | 32 +++++++++++++++++++++++--------- btccshow.lua | 26 ++++++++++++++++++++++++++ btccslideshow.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 btccshow.lua create mode 100644 btccslideshow.lua diff --git a/bigterm.lua b/bigterm.lua index 6ec0fb1..d5cda24 100644 --- a/bigterm.lua +++ b/bigterm.lua @@ -196,17 +196,29 @@ local BigTerm = { } -return function(monitors, args) +local lib = {} +function lib.fromFile(conf) + local fp = assert(io.open(conf, "r")) + local conf = textutils.unserializeJSON(fp:read("a")) + fp:close() + local monitors = {} + for addr, pos in pairs(conf.monitors) do + local p = assert(peripheral.wrap(addr)) + table.insert(monitors, { p = p, x = pos.x, y = pos.y }) + end + return lib.new(monitors, { palette = conf.palette, scale = conf.scale }) +end +function lib.new(monitors, args) args = args or {} local mon = setmetatable({ - _monitors = monitors, - _pos = { x = 1, y = 1 }, - _blink = false, - _colorForeground = colors.white, - _colorBackground = colors.black, - _scale = args.scale or 1.0, - _palette = args.palette or {}, - }, { __index = BigTerm }) + _monitors = monitors, + _pos = { x = 1, y = 1 }, + _blink = false, + _colorForeground = colors.white, + _colorBackground = colors.black, + _scale = args.scale or 1.0, + _palette = args.palette or {}, + }, { __index = BigTerm }) for name, method in pairs(BigTerm) do if type(method) == "function" then mon[name] = function(...) return method(mon, ...) end @@ -215,3 +227,5 @@ return function(monitors, args) mon._reset() return mon end + +return lib diff --git a/btccshow.lua b/btccshow.lua new file mode 100644 index 0000000..43a6349 --- /dev/null +++ b/btccshow.lua @@ -0,0 +1,26 @@ +local args = { ... } +local ccpi = require("ccpi") +local bigterm = require("bigterm").fromFile("/bigterm.json") + +local img, err = ccpi.load(args[1]) +if not img then + printError(err) + return +end + +local ys = {} +for y = 1, img.h do table.insert(ys, y) end + +for i = 1, math.floor((#ys) / 2), 2 do + local a, b = i, (#ys - 1) - i + 1 + ys[a], ys[b] = ys[b], ys[a] +end + +for i, y in ipairs(ys) do + bigterm.setCursorPos(1, y) + bigterm.blit(img.lines[y].s, img.lines[y].fg, img.lines[y].bg) +end + +for i = 1, 16 do + bigterm.setPaletteColor(bit.blshift(1, i - 1), img.palette[i]) +end diff --git a/btccslideshow.lua b/btccslideshow.lua new file mode 100644 index 0000000..e8d37ab --- /dev/null +++ b/btccslideshow.lua @@ -0,0 +1,47 @@ +local args = { ... } +local ccpi = require("ccpi") +local bigterm = require("bigterm") + +local conf_path = "/bigterm.json" +if args[1] == "-c" then + table.remove(args, 1) + conf_path = table.remove(args, 1) +end +local screen = bigterm.fromFile(conf_path) + +local time = 10 +if args[1] == "-t" then + table.remove(args, 1) + time = tonumber(table.remove(args, 1)) +end + +local files = fs.list(args[1]) + +while true do + local img, err = ccpi.load(fs.combine(args[1], files[math.random(1, #files)])) + if not img then + printError(err) + return + else + local ys = {} + for y = 1, img.h do table.insert(ys, y) end + + for i = 1, math.floor((#ys) / 2), 2 do + local a, b = i, (#ys - 1) - i + 1 + ys[a], ys[b] = ys[b], ys[a] + end + + for i, y in ipairs(ys) do + screen.setCursorPos(1, y) + screen.blit(img.lines[y].s, img.lines[y].fg, img.lines[y].bg) + if (i % 10) == 0 then + os.sleep(0) + end + end + + for i = 1, 16 do + screen.setPaletteColor(bit.blshift(1, i - 1), img.palette[i]) + end + end + os.sleep(time) +end