83 lines
2.2 KiB
Lua
83 lines
2.2 KiB
Lua
local drive = peripheral.find("tape_drive")
|
|
if not drive then
|
|
printError("no tape drive found")
|
|
printError("it's kinda required to play tapes, you know?")
|
|
return
|
|
end
|
|
|
|
local running = true
|
|
|
|
local table_of_contents = {}
|
|
|
|
local function read32()
|
|
local v = 0
|
|
for i = 1, 4 do
|
|
local b = drive.read()
|
|
v = bit32.bor(bit32.lshift(v, 8), b)
|
|
end
|
|
return v
|
|
end
|
|
|
|
local function bytes2time(b)
|
|
local s = math.floor(b / 6000)
|
|
if s < 60 then return string.format("%ds", s) end
|
|
return string.format("%dm, %ds", math.floor(s / 60), s % 60)
|
|
end
|
|
|
|
|
|
parallel.waitForAll(
|
|
function()
|
|
while running do
|
|
os.sleep(0.1)
|
|
end
|
|
end,
|
|
function()
|
|
while running do
|
|
local evd = { os.pullEvent() }
|
|
local ev, evd = table.remove(evd, 1), evd
|
|
|
|
if ev == "mouse_click" then
|
|
local x, y = table.unpack(evd, 1)
|
|
elseif ev == "tape_present" then
|
|
table_of_contents = {}
|
|
if evd[1] then
|
|
drive.stop()
|
|
drive.seek(-drive.getSize())
|
|
for i = 1, 48 do
|
|
local offset = read32()
|
|
local length = read32()
|
|
local title = drive.read(117)
|
|
if length > 0 then
|
|
print(string.format("%d: %d + %d %s", i, offset, length, title))
|
|
end
|
|
table.insert(table_of_contents, {
|
|
title = length > 0 and title or nil,
|
|
offset = offset,
|
|
length = length,
|
|
ending = offset + length
|
|
})
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end,
|
|
function()
|
|
local tape_was_present = nil
|
|
local drive_old_state = nil
|
|
while running do
|
|
local tape_present = drive.isReady()
|
|
if tape_present ~= tape_was_present then
|
|
os.queueEvent("tape_present", tape_present)
|
|
tape_was_present = tape_present
|
|
end
|
|
|
|
local drive_state = drive.getState()
|
|
if drive_old_state ~= drive_state then
|
|
os.queueEvent("drive_state", drive_state)
|
|
drive_old_state = drive_state
|
|
end
|
|
|
|
os.sleep(0.25)
|
|
end
|
|
end)
|