LuPPC/src/lua/core/computer.lua

155 lines
3.1 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,
[13] = 28,
}
function computer.signalTransformers.key_down(s, a, ascii, key, user)
if key ~= -1 then
return s, a, ascii, key, user
end
return s, a, ascii, keymap[ascii] or key, user
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, ascii, keymap[ascii] or key, user
end
-----
function api.pushSignal(s, ...)
io.stderr:write("pushSignal "..s.."\n")
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-01-15 08:44:49 +11:00
if signalQueue[1] then return table.unpack(table.remove(signalQueue, 1)) end
local timeoutuptime = math.huge
if not timeout then
timeout = -1
else
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)
until nevts > 0 or native.uptime() > timeoutuptime
if signalQueue[1] then return table.unpack(table.remove(signalQueue, 1)) end
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
2016-01-06 08:29:49 +11:00
return computer