This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
austin9898's profile picture

Displaying Data ( Not slapping a monitor on the side of the Computer)

Started by austin9898, 05 July 2014 - 05:53 AM
austin9898 #1
Posted 05 July 2014 - 07:53 AM
Alright, so i've got *SOME* experience with computer craft, enough to make little programs. Recently i found a program that displays data from a reactor, the only problem that is in the script the computer has to be connected directly to a block on the reactor. I don't like that, so is there anyway that i can bring the computer away from the reactor? or transmit the displayed data to a monitor, or another computer without writing or editing the program? Or does the program needed to be edited, and if so, how? Ill put the program below, and it is for BigReactors
– Max energy in a reactor's internal cell
local emax=10000000


-- wrap everything in an exception handler
local ok,msg=pcall(function ()
local r
local m
local redirected=false
local p

function findDev (dType)
  local d
  for _,d in pairs(peripheral.getNames()) do
	if (peripheral.getType(d) == dType) then
	  return peripheral.wrap(d)
	end
  end
  return nil, dType..": not found"
end

function setupDevs()	
  r=assert(findDev("BigReactors-Reactor"))
  if (not r.getConnected()) then
	return nil, "Computer port not connected to a valid reactor"
  end
  --if (r.getNumberOfControlRods() <1) then
  --  return nil, "Reactor seems invalid"
  --end
  r.getEnergyPercent = function ()
	return math.floor(1000 * r.getEnergyStored() / emax)/10
  end
  if r.nativeEPLT then
	r.getEnergyProducedLastTick = r.nativeEPLT
  end
  r.nativeEPLT = r.getEnergyProducedLastTick
  r.getEnergyProducedLastTick = function ()
	return math.floor(r.nativeEPLT()*1000)/1000
  end

  if redirected then
	term.restore()
	redirected = false
  end
  m=findDev("monitor")
  if m then
	m.setTextScale(0.5)
	term.clear()
	term.setCursorPos(1,1)
	print("Redirecting to attached monitor")
	term.redirect(m)
	redirected = true
  end

  term.setCursorBlink(false)
  p=findDev("printer")
end

function ft ()
  local d=os.day()
  local t=os.time()
  local h=math.floor(t)
  local m=math.floor((t-h)*60)
  return string.format("Day %d, %02d:%02d",d,h,m)
end

function log (msg)
  local stamp=ft()
  print (stamp..": "..msg)
end

function tableWidth(t)
  local width=0
  for _,v in pairs(t) do
	if #v>width then width=#v end
  end
  return width
end

function ljust(s,w)
  local pad=w-#s
  return s .. string.rep(" ",pad)
end

function rjust(s,w)
  local pad=w-#s
  return string.rep(" ",pad) .. s
end

function display()
  term.clear()
  term.setCursorPos(1,1)
  print("Reactor Status")
  print(ft())
  print("")
  local funcs={"Connected","Active","NumberOfControlRods","EnergyStored","EnergyPercent","Temperature","FuelAmount","WasteAmount","FuelAmountMax","EnergyProducedLastTick"}
  local units={"","","","RF","%","C","mB","mB","mB","RF/t"}
  local values={}
  for _,v in pairs(funcs) do
	values[#values+1] = tostring(r["get"..v]())
  end
  local funcW=tableWidth(funcs)
  local valW=tableWidth(values)
  for i,v in pairs(funcs) do
	print(rjust(v,funcW)..": "..rjust(values[i],valW).." "..units[i])
  end  
end

log("Starting")
setupDevs()
while true do
  local e=r.getEnergyStored()
  local p=math.floor(100*e/emax)
  local a=p<100
  local elt=r.getEnergyProducedLastTick()
  display()
  r.setAllControlRodLevels(p)
  r.setActive(a)
  os.startTimer(0.8333334)
  local event,p1,p2,p3,p4,p5 = os.pullEvent()
  if event == "key" then
	break
  elseif event == "peripheral_detach" or event == "peripheral" or event == "monitor_resize" then
	setupDevs()
  elseif not (event == "timer" or event=="disk" or event=="disk_eject") then
	error("received "..event)
  end
end

end)
term.restore()
error(msg)
Bomb Bloke #2
Posted 05 July 2014 - 08:41 AM
At a glance, it appears this will operate if you attach wired modems to your computer and reactor block, then connect them via network cable. The modem attached to the reactor would need to be enabled (generally by right clicking it).