LuPPC/src/lua/core/computer.lua

186 lines
3.8 KiB
Lua
Raw Normal View History

2016-01-06 08:29:49 +11:00
local computer = {}
2016-01-07 02:58:05 +11:00
local api = {}
computer.api = api
2016-01-05 04:20:40 +11:00
2016-01-14 12:13:00 +11:00
--computer.tmp - set in init.lua
2016-01-06 08:29:49 +11:00
function computer.prepare( ... )
end
2016-01-14 12:13:00 +11:00
function api.address()
return modules.address
end
2016-01-14 11:10:04 +11:00
local signalQueue = {}
2016-01-15 08:44:49 +11:00
computer.signalTransformers = setmetatable({}, {__index = function(t, k)
return function(...)
return ...
end
end})
-----
--TODO: Move this out
local keymap = {
[48] = 0x2,
[49] = 0x3,
[50] = 0x4,
[51] = 0x5,
[52] = 0x6,
[53] = 0x7,
[54] = 0x8,
[55] = 0x9,
[56] = 0xA,
[57] = 0xB,
[0x40 + 0x01] = 0x1E,
[0x40 + 0x02] = 0x30,
[0x40 + 0x04] = 0x2E,
[0x40 + 0x05] = 0x20,
[0x40 + 0x06] = 0x12,
[0x40 + 0x07] = 0x21,
[0x40 + 0x08] = 0x22,
[0x40 + 0x09] = 0x23,
[0x40 + 0x0A] = 0x17,
[0x40 + 0x0B] = 0x24,
[0x40 + 0x0C] = 0x25,
[0x40 + 0x0D] = 0x26,
[0x40 + 0x0E] = 0x32,
[0x40 + 0x0F] = 0x31,
[0x40 + 0x11] = 0x18,
[0x40 + 0x12] = 0x19,
[0x40 + 0x13] = 0x10,
[0x40 + 0x14] = 0x13,
[0x40 + 0x15] = 0x1F,
[0x40 + 0x16] = 0x14,
[0x40 + 0x17] = 0x16,
[0x40 + 0x18] = 0x2F,
[0x40 + 0x19] = 0x11,
[0x40 + 0x1A] = 0x2D,
[0x40 + 0x1B] = 0x15,
[0x40 + 0x1C] = 0x2C,
[0x60 + 0x01] = 0x1E,
[0x60 + 0x02] = 0x30,
[0x60 + 0x04] = 0x2E,
[0x60 + 0x05] = 0x20,
[0x60 + 0x06] = 0x12,
[0x60 + 0x07] = 0x21,
[0x60 + 0x08] = 0x22,
[0x60 + 0x09] = 0x23,
[0x60 + 0x0A] = 0x17,
[0x60 + 0x0B] = 0x24,
[0x60 + 0x0C] = 0x25,
[0x60 + 0x0D] = 0x26,
[0x60 + 0x0E] = 0x32,
[0x60 + 0x0F] = 0x31,
[0x60 + 0x11] = 0x18,
[0x60 + 0x12] = 0x19,
[0x60 + 0x13] = 0x10,
[0x60 + 0x14] = 0x13,
[0x60 + 0x15] = 0x1F,
[0x60 + 0x16] = 0x14,
[0x60 + 0x17] = 0x16,
[0x60 + 0x18] = 0x2F,
[0x60 + 0x19] = 0x11,
[0x60 + 0x1A] = 0x2D,
[0x60 + 0x1B] = 0x15,
[0x60 + 0x1C] = 0x2C,
2016-01-16 05:58:26 +11:00
[13] = 28, --Return key
[127] = 14, --backspace
[9] = 15, --Tab
2016-01-15 08:44:49 +11:00
}
2016-01-16 05:58:26 +11:00
local asciitr = {
[127] = 8,
}
2016-01-15 08:44:49 +11:00
function computer.signalTransformers.key_down(s, a, ascii, key, user)
if key ~= -1 then
return s, a, ascii, key, user
end
return s, a, math.floor(asciitr[ascii] or ascii), keymap[ascii] or key, user
2016-01-15 08:44:49 +11:00
end
2016-01-14 11:10:04 +11:00
2016-01-15 08:44:49 +11:00
function computer.signalTransformers.key_up(s, a, ascii, key, user)
if key ~= -1 then
return s, a, ascii, key, user
end
return s, a, math.floor(asciitr[ascii] or ascii), keymap[ascii] or key, user
2016-01-15 08:44:49 +11:00
end
-----
function api.pushSignal(s, ...)
signalQueue[#signalQueue + 1] = {computer.signalTransformers[s](s, ...)}
2016-01-07 02:58:05 +11:00
end
2016-01-13 11:12:01 +11:00
function api.pullSignal(timeout)
2016-02-10 03:40:51 +11:00
--native.log("pullSignal for " .. (timeout or " infinite") .. " s")
if signalQueue[1] then
native.log("pullSignal direct: " .. signalQueue[1][1])
return table.unpack(table.remove(signalQueue, 1))
end
2016-01-15 08:44:49 +11:00
local timeoutuptime = math.huge
if not timeout then
timeout = -1
else
2016-02-10 03:40:51 +11:00
if timeout < 0 then timeout = 0 end
2016-01-15 08:44:49 +11:00
timeout = timeout * 1000
timeoutuptime = native.uptime() + timeout
2016-01-13 11:12:01 +11:00
end
2016-01-15 08:44:49 +11:00
local nevts = 0
repeat
nevts = native.pull(timeout)
2016-02-27 06:09:38 +11:00
until nevts > 0 or native.uptime() >= timeoutuptime
2016-02-10 03:40:51 +11:00
if signalQueue[1] then
native.log("pullSignal native: " .. signalQueue[1][1])
return table.unpack(table.remove(signalQueue, 1))
end
--native.log("pullSignal timeout")
2016-01-13 11:12:01 +11:00
end
function api.uptime()
2016-01-13 11:15:51 +11:00
return native.uptime() / 1000
2016-01-13 11:12:01 +11:00
end
2016-01-10 05:39:48 +11:00
function api.beep(freq, time)
2016-01-13 11:12:01 +11:00
if not freq then freq = 1000 end
if not time then time = 0.2 end
native.beep(freq, time * 1000)
2016-01-10 05:39:48 +11:00
end
2016-01-14 12:13:00 +11:00
function api.tmpAddress()
return computer.tmp
end
function api.freeMemory()
return native.freeMemory()
end
function api.totalMemory()
return native.totalMemory()
end
2021-05-23 12:12:23 +10:00
function api.shutdown(reboot)
2016-02-10 03:40:51 +11:00
--TODO: Longjmp to init somehow?
print("Running shutdown hooks")
for k, hook in ipairs(deadhooks) do
local state, cause = pcall(hook)
if not state then
print("Shutdown hook with following error:")
print(cause)
end
end
print("Hooks executed: " .. #deadhooks)
2021-05-23 12:12:23 +10:00
native.shutdown(reboot)
2016-01-19 05:42:32 +11:00
os.exit(0)
end
2021-05-23 12:12:23 +10:00
return computer