Posted 20 April 2014 - 08:29 AM
Hi there,
I've got an elusive bug in my code, and could use some advice from a pro. :)/> This program connects to an ME storage unit and displays its capacity on a monitor.
Problem:
My monitor doesn't work on the first run of the code after initially placing the computer, but does on all subsequent runs. The code doesn't return any errors. Curiously, if I add print(text) inside the while loop, the correct display is repeatedly printed in the terminal until I break - so the ME is wrapped correctly and the code is iterating properly, but the monitor just isn't communicating??
Code
Thanks very much in advance!
I've got an elusive bug in my code, and could use some advice from a pro. :)/> This program connects to an ME storage unit and displays its capacity on a monitor.
Problem:
My monitor doesn't work on the first run of the code after initially placing the computer, but does on all subsequent runs. The code doesn't return any errors. Curiously, if I add print(text) inside the while loop, the correct display is repeatedly printed in the terminal until I break - so the ME is wrapped correctly and the code is iterating properly, but the monitor just isn't communicating??
Code
-- Simple LUA script to watch the ME Storage unit
-- Heimdall116, 4/6/14
--Load adjacent peripherals
local ME = peripheral.wrap("left")
local monitor = peripheral.wrap("top")
--Prep the monitor
monitor.clear()
local mX, mY = monitor.getSize()
monitor.setTextScale(3)
--Start periodic updates
while true do
free = ME.getFreeBytes()
total = ME.getTotalBytes()
local text = (total-free..' / '..total)
--Display it on the monitor
monitor.clear()
local x = math.floor(mX/2 - 4)
local y = mY/2-1
monitor.setTextColor(colors.white)
monitor.setCursorPos(x,y)
monitor.write("ME Usage:")
if free/total < 0.25 or total == 0 then
cCode = colors.red
elseif free/total < 0.5 then
cCode = colors.yellow
else
cCode = colors.green
end
local x = math.floor(mX/2 - string.len(text)/2)
monitor.setCursorPos(x,y+1)
monitor.setTextColor(cCode)
monitor.write(text)
monitor.setTextColor(colors.yellow)
monitor.setCursorPos(1,mY)
monitor.write(' - H116 ')
monitor.setTextColor(colors.blue)
monitor.write(' (Go Blue!)')
os.sleep(3)
end
Thanks very much in advance!