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

Parallel API - Updating statuses while also taking input

Started by AadamZ5, 06 August 2015 - 04:12 AM
AadamZ5 #1
Posted 06 August 2015 - 06:12 AM
So, I'm trying to make a control computer for a reactor lab, and what I've been trying to do is take input from the user while also updating the statuses of redstone signals on-screen.

Specifically, I have the redstone signal of my alarm system (A remote thermal monitor)

The problem then becomes, while using the read() function with parallel, My characters are not displayed, or so I have read, they are overwritten, and not redrawn on the screen.

I've searched and looked into similar problems, and have researched methods with using os.startTimer(1) but I cant figure out how I can apply this to my situation, for I am not too advanced in LUA.

My question is,

Is there any better method of doing this that could display similar results?
I still want my redstone signals to be tested and updated every 1 second, but I also want to type, while still seeing the characters.


function clear(sec)
  term.clear()
  term.setCursorPos(1,1)
  if not sec == nil then
	sleep(sec)
  end
end

function getAllInputs()
  clear()
  while true do
	alrm = rs.testBundledInput("bottom", colors.red)
	chmbrdoor = rs.testBundledInput("bottom", colors.orange)
	labdoor = rs.testBundledInput("bottom", colors.yellow)
	local retx,rety = term.getCursorPos()
	term.setCursorPos(1, 1)
	term.clearLine()
	if alrm then
	  term.write("Alarm: ")
	  term.setTextColor(colors.red)
	  term.write("Triggered")
	  term.setTextColor(colors.white)
	else
	  term.write("Alarm: ")
	  term.setTextColor(colors.green)
	  term.write("Ready")
	  term.setTextColor(colors.white)
	end
	term.setCursorPos(retx,rety)
	sleep(0.5)
  end
end

function labDoors()
  print("wip")
end

function cmds()
  while true do
	term.setCursorPos(1,2)
	print("Choose an option from the list to manipulate")
	print("LabDoors  ChamberDoors  Alarm  Reactor")
	inp = read("")
	if inp == "LabDoors" then
	  labDoors()
	else
	  print("Working on it still..")
	  sleep(2)
	end
  end
end

parallel.waitForAll(cmds, getAllInputs)
	  
Edited on 06 August 2015 - 04:13 AM
Bomb Bloke #2
Posted 06 August 2015 - 08:13 AM
Less to do with your use of the Parallel API (there are steps you need to take there, but you're performing them correctly), more to do with you sticking "" as a parameter when you call read(). It prints the value of that parameter when people type stuff, see. If you want it to print what they actually type, use read() instead of read("").

Aside from some missing variable localisation, the only other change I'd suggest would be to your "clear()" function - if a variable is set to false / nil, then simply sticking it into an "if" conditional check on its own'll make the result false. Any other variable is treated as true. You can therefore do:

if sec then sleep(sec) end

But those are minor issues.
AadamZ5 #3
Posted 06 August 2015 - 10:16 AM
Less to do with your use of the Parallel API (there are steps you need to take there, but you're performing them correctly), more to do with you sticking "" as a parameter when you call read(). It prints the value of that parameter when people type stuff, see. If you want it to print what they actually type, use read() instead of read("").

Aside from some missing variable localisation, the only other change I'd suggest would be to your "clear()" function - if a variable is set to false / nil, then simply sticking it into an "if" conditional check on its own'll make the result false. Any other variable is treated as true. You can therefore do:

if sec then sleep(sec) end

But those are minor issues.

Aha, Didn't know quotes weren't needed.
3rd edit, ftw. not reading right.

Problem solved, Not sure how to lock topic.
Edited on 06 August 2015 - 08:19 AM
flaghacker #4
Posted 06 August 2015 - 07:24 PM
Problem solved, Not sure how to lock topic.

You don't. You are encouraged to ask further questions about this program in this topic in the future (should you have any).