Posted 16 April 2015 - 09:38 PM
My problem is kind of insane.
I'm trying to make a base monitoring program that displays the amount of RF in total that my base has, among other things. Of course, I also want to control the system from the monitor or the keyboard. To that end, I tried using coroutines, and for the actual outputs I designed a plugin system. Here is the main file.
One of the plugins monitors the RF of one of the redstone cells in my base. Here's the code for that.
When I run the main program, the thermal expansion total is never given.
The rest of the program works all the time, such as the event handling for keys and so on.
It looks like the problem is that OpenPeripherals ThermalExpansion code uses some kind of event to synchronise when it needs to check the Redstone Cell's energy level. That event gets swept up into the event pulling from the main code and never gets handled, but I am not sure.
I'm trying to make a base monitoring program that displays the amount of RF in total that my base has, among other things. Of course, I also want to control the system from the monitor or the keyboard. To that end, I tried using coroutines, and for the actual outputs I designed a plugin system. Here is the main file.
Spoiler
-- Based
-- A base monitoring and control daemon
os.loadAPI("lib/tcolor")
local args = { ... }
if #args ~= 1 then
print("Usage: based <side>")
return
end
-- Wrap the monitor to that side.
monitor = peripheral.wrap(args[1])
if monitor == nil then
print("Side must exist.")
return
end
-- Main function to handle events.
function event_handler()
while true do
event = {coroutine.yield()}
if event[1] == "key" then
if event[2] == keys.q then
break
end
elseif event[1] == "op_tick_sync" then
os.queueEvent(unpack(event))
end
end
end
-- Show the header of the program.
function display_header()
x, y = monitor.getSize()
for i=1,x do
monitor.setCursorPos(i, 1)
tcolor.write(monitor, " ", colors.blue, colors.blue)
end
monitor.setCursorPos(1, 1)
tcolor.write(monitor, "Base Monitor", colors.black, colors.blue)
end
function display_info()
while true do
coroutine.yield()
monitor.clear()
display_header()
-- Keep from flickering.
sleep(0.2)
end
end
-- Plugin architecture.
local cblist = {}
function add_cb(f)
table.insert(cblist, f)
end
function load_plugins(list)
for c, item in pairs(list) do
print("trying to load plugin "..item)
os.loadAPI("bin/bdplugins/"..item)
_G[item].plugin_setup(cblist)
end
print("plugins loaded")
end
-- Plugins register callbacks to the plugin manager.
function run_plugins()
print("loading plugins")
load_plugins(fs.list("bin/bdplugins"))
while true do
local event = {coroutine.yield()}
print("Plugins running")
for i,item in pairs(cblist) do
item(event, monitor)
end
end
end
local eventRoutine = coroutine.create(event_handler)
local displayRoutine = coroutine.create(display_info)
local pluginRoutine = coroutine.create(run_plugins)
local event = {}
while true do
-- launch the coroutines with specific events
coroutine.resume(eventRoutine, unpack(event))
coroutine.resume(displayRoutine, unpack(event))
coroutine.resume(pluginRoutine, unpack(event))
-- Add an event to keep going
os.queueEvent("idle")
if coroutine.status(eventRoutine) == "dead" then
break
end
event = { os.pullEvent() }
end
monitor.clear()
One of the plugins monitors the RF of one of the redstone cells in my base. Here's the code for that.
Spoiler
function plugin_setup(callbacks)
table.insert(callbacks, display_power)
end
function test(event, monitor)
--print("test!")
tcolor.write(monitor, "Testing", colors.white, colors.black)
end
function display_power(event, monitor)
print("test")
thermxp = "tile_thermalexpansion_cell_reinforced_name_1"
bigr = "BigReactors-Reactor_0"
local cell = peripheral.wrap(thermxp)
if cell == nil then print("cell not found") end
print("getting power")
total = cell.getEnergyStored() -- This does not work.
print(total)
-- total = 5
print("got total")
x, y = monitor.getCursorPos()
monitor.setCursorPos(1, y+1)
tcolor.write(monitor, total, colors.white, colors.black)
end
When I run the main program, the thermal expansion total is never given.
The rest of the program works all the time, such as the event handling for keys and so on.
It looks like the problem is that OpenPeripherals ThermalExpansion code uses some kind of event to synchronise when it needs to check the Redstone Cell's energy level. That event gets swept up into the event pulling from the main code and never gets handled, but I am not sure.