Brick_LuaLogic/bricks/inputs/iobrick-update.lua
2024-06-22 18:17:21 -06:00

50 lines
953 B
Lua

local function outputByte(gate, v)
local p = 256
for i = 1, 8 do
p = p/2
local port = 17 - i
if v>=p then
v = v - p
Gate.setportstate(gate, port, 1)
else
Gate.setportstate(gate, port, 0)
end
end
end
local function inputByte(gate)
local v = 0
local p = 256
for i = 1, 8 do
p = p/2
local port = 9 - i
v = v + p*Gate.getportstate(gate, port)
end
return v
end
return function(gate)
-- input
if #gate.queueIn > 0 and gate.outputToggle then
--if #gate.queueIn > 0 then
local v = table.remove(gate.queueIn, #gate.queueIn)
outputByte(gate, v)
Gate.setportstate(gate, 18, 1)
gate.outputToggle = false
Gate.queue(gate, 1)
else
outputByte(gate, 0)
Gate.setportstate(gate, 18, 0)
gate.outputToggle = true
if #gate.queueIn > 0 then
Gate.queue(gate, 1)
end
end
-- output
if Gate.getportisrising(gate, 17) then
local v = inputByte(gate)
Gate.cbQueue(gate, string.format("%02X", v), 1024)
end
end