1
0
mirror of https://github.com/20kdc/OC-KittenOS.git synced 2024-11-23 10:58:06 +11:00

app-tapedeck: Copy/Erase buttons, operation cancelling

This commit is contained in:
20kdc 2018-10-13 10:36:51 +01:00
parent 916d127337
commit 3d3517bc53
2 changed files with 82 additions and 27 deletions

View File

@ -202,7 +202,7 @@ return {
}, },
["app-tapedeck"] = { ["app-tapedeck"] = {
desc = "Computronics Tape Drive interface", desc = "Computronics Tape Drive interface",
v = 1, v = 2,
deps = { deps = {
"neo", "neo",
"zzz-license-pd" "zzz-license-pd"

View File

@ -5,13 +5,10 @@
-- Added note: Computerized record discs aren't available, so it can't be called vinylscratch. -- Added note: Computerized record discs aren't available, so it can't be called vinylscratch.
-- Authors: 20kdc -- Authors: 20kdc
local tapes = {}
for v in neo.requireAccess("c.tape_drive", "tapedrives").list() do
table.insert(tapes, v)
end
local tapeRate = 4096 local tapeRate = 4096
local tapeAccess = neo.requireAccess("c.tape_drive", "tapedrives")
local event = require("event")(neo) local event = require("event")(neo)
local neoux = require("neoux")(event, neo) local neoux = require("neoux")(event, neo)
@ -35,10 +32,11 @@ local updateTick
local downloadCancelled = false local downloadCancelled = false
local genPlayer -- used to return to player local genPlayer, genList -- used to return to player
local function genDownloading(inst) local function genDownloading(downloadText, inst)
local lclLabelText = {"downloading..."} downloadCancelled = false
local lclLabelText = {downloadText}
local lclLabel = neoux.tcrawview(1, 1, lclLabelText) local lclLabel = neoux.tcrawview(1, 1, lclLabelText)
local thr = { local thr = {
"/", "/",
@ -48,7 +46,7 @@ local function genDownloading(inst)
} }
local thri = 0 local thri = 0
updateTick = function () updateTick = function ()
lclLabelText[1] = "downloading... " .. (inst.getPosition() / (1024 * 1024)) .. "MB " .. thr[(thri % #thr) + 1] lclLabelText[1] = downloadText .. " " .. (inst.getPosition() / (1024 * 1024)) .. "MB " .. thr[(thri % #thr) + 1]
thri = thri + 1 thri = thri + 1
lclLabel.update(window) lclLabel.update(window)
end end
@ -59,14 +57,18 @@ local function genDownloading(inst)
end, 0xFFFFFF, 0) end, 0xFFFFFF, 0)
end end
local function maybeSleep()
if math.random() > 0.98 then
event.sleepTo(os.uptime() + 0.05)
end
end
local function doINetThing(inet, url, inst) local function doINetThing(inet, url, inst)
inet = inet.list()() inet = inet.list()()
assert(inet, "No available card") assert(inet, "No available card")
inst.stop() inst.stop()
inst.seek(-inst.getSize()) inst.seek(-inst.getSize())
downloadCancelled = false window.reset(genDownloading("downloading...", inst))
downloadPercent = 0
window.reset(genDownloading(inst))
local req = assert(inet.request(url)) local req = assert(inet.request(url))
req.finishConnect() req.finishConnect()
local tapePos = 0 local tapePos = 0
@ -130,16 +132,15 @@ genPlayer = function (inst)
window.reset(genPlayer(inst)) window.reset(genPlayer(inst))
end end
-- Common code for reading/writing tapes. -- Common code for reading/writing tapes.
-- Note that it tries to allow playback to resume later.
local function rwButton(mode) local function rwButton(mode)
local fh = neoux.fileDialog(mode) local fh = neoux.fileDialog(mode)
if not fh then return end if not fh then return end
inst.stop() inst.stop()
local sp = inst.getPosition()
local tapeSize = inst.getSize() local tapeSize = inst.getSize()
inst.seek(-tapeSize) inst.seek(-tapeSize)
local tapePos = 0 local tapePos = 0
while tapePos < tapeSize do window.reset(genDownloading("working...", inst))
while tapePos < tapeSize and not downloadCancelled do
if mode then if mode then
local data = inst.read(neo.readBufSize) local data = inst.read(neo.readBufSize)
if not data then break end if not data then break end
@ -155,15 +156,15 @@ genPlayer = function (inst)
tapePos = tapePos + #data tapePos = tapePos + #data
inst.write(data) inst.write(data)
end end
maybeSleep()
end end
inst.seek(-tapeSize) inst.seek(-tapeSize)
inst.seek(sp)
fh.close() fh.close()
window.reset(genPlayer(inst))
end end
local elems = { local elems = {
neoux.tcrawview(1, 1, { neoux.tcrawview(1, 1, {
"Label:", "Label:"
"Contents:"
}), }),
neoux.tcfield(7, 1, 34, function (tx) neoux.tcfield(7, 1, 34, function (tx)
if tx then if tx then
@ -245,14 +246,55 @@ genPlayer = function (inst)
pausePlay() pausePlay()
end), end),
-- R/W buttons -- R/W buttons
neoux.tcbutton(11, 2, "Read", function (w) neoux.tcbutton(1, 2, "Read", function (w)
rwButton(true) rwButton(true)
end), end),
neoux.tcbutton(17, 2, "Write", function (w) neoux.tcbutton(7, 2, "Write", function (w)
rwButton(false) rwButton(false)
end), end),
neoux.tcbutton(24, 2, "Write From Web", function (w) neoux.tcbutton(14, 2, "Write Web", function (w)
w.reset(genWeb(inst)) w.reset(genWeb(inst))
end),
neoux.tcbutton(25, 2, "Copy", function (w)
w.reset(genList(function (inst2)
local ts1 = inst.getSize()
inst.stop()
inst.seek(-ts1)
local ts2 = inst2.getSize()
inst2.stop()
inst2.seek(-ts2)
if ts1 < ts2 then
w.reset(genDownloading("copying...", inst))
else
w.reset(genDownloading("copying...", inst2))
end
local pos = 0
while pos < ts1 and pos < ts2 and not downloadCancelled do
local dat = inst.read(neo.readBufSize)
inst2.write(dat)
pos = pos + #dat
maybeSleep()
end
inst.seek(-ts1)
inst2.seek(-ts2)
inst2.setLabel((inst.getLabel() or "") .. " Copy")
w.reset(genPlayer(inst))
end))
end),
neoux.tcbutton(31, 2, "Erase", function (w)
local ts1 = inst.getSize()
inst.stop()
inst.seek(-ts1)
w.reset(genDownloading("erasing...", inst))
local blank = ("\x00"):rep(neo.readBufSize)
local pos = 0
while pos < ts1 and not downloadCancelled do
inst.write(blank)
pos = pos + #blank
maybeSleep()
end
inst.seek(-ts1)
w.reset(genPlayer(inst))
end) end)
} }
updateTick = function () updateTick = function ()
@ -277,14 +319,25 @@ genPlayer = function (inst)
return n(a, ...) return n(a, ...)
end end
end end
local function genList() genList = function(callback)
updateTick = nil
local elems = {} local elems = {}
local tapes = {}
for v in tapeAccess.list() do
table.insert(tapes, v)
end
for k, v in ipairs(tapes) do for k, v in ipairs(tapes) do
elems[k] = neoux.tcbutton(1, k, v.address:sub(1, 38), function (w) -- There's 38 chars available...
window.reset(genPlayer(v)) local desc1 = neoux.pad(v.address, 13, false, true)
if v.isReady() then
desc1 = desc1 .. ": " .. neoux.pad(v.getLabel() or "", 23, false, true)
else
desc1 = desc1 .. " (no tape)"
end
elems[k] = neoux.tcbutton(1, k, desc1, function (w)
callback(v)
end) end)
end end
tapes = nil
return 40, #elems, nil, neoux.tcwindow(40, #elems, elems, function (w) return 40, #elems, nil, neoux.tcwindow(40, #elems, elems, function (w)
running = false running = false
w.close() w.close()
@ -292,7 +345,9 @@ local function genList()
end end
window = neoux.create(genList()) window = neoux.create(genList(function (v)
window.reset(genPlayer(v))
end))
-- Timer for time update -- Timer for time update
local function tick() local function tick()