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

receive rednet messages in background?

Started by 5mCjLRgW2d473dyJtiiTnOr3wu, 11 August 2016 - 05:55 PM
5mCjLRgW2d473dyJtiiTnOr3wu #1
Posted 11 August 2016 - 07:55 PM
I'm writing a Terminal Glasses(OpenPeripheralAddons) script, to convey time, power status and other information to my HUD.

I have a advanced computer with the Terminal Glasses Bridge and a wireless modem attached, and another computer next to my Power Storage. I want to send the data via rednet, but I can't wait and listen via os.pullEvent or rednet.receive. I already tried the parallel API, but it seems like the second function never runs, even when I yield.

To sum it up, I need to receive rednet messages in the background somehow, or stack them(stop the event's removal) and check each cycle.

Code

--// FUNCTIONS
function declareFuncs()
  function glass.message(msg)
	table.insert(msgs,msg)
	if s.enableConsole then
	  term.setTextColor(colors.red)
	  io.write("[msg] ")
	  term.setTextColor(colors.white)
	  print(msg)
	end
  end
  function sys.message(msg)
	if s.enableConsole then
	  term.setTextColor(colors.red)
	  io.write("[sys] ")
	  term.setTextColor(colors.white)
	  print(msg)
	end
  end
  function round(num, idp)
	local mult = 10^(idp or 0)
	return math.floor(num * mult + 0.5) / mult
  end
end
--// CORE FUNCTIONS
function init()
  setvars()
  shell.run("glassconfig.cfg")
  glass=peripheral.wrap(s.bridgeSide)
  declareFuncs()
  sys.message("wrapped Bridge")
  sys.message("loaded config")
  rednet.open(s.modemSide)
  sys.message("opened Rednet")
  rednet.host("MAIN","Main")
  glass.message("Online!")
  rednet.broadcast("RESET","RESET")
  glass.message("Services reset.")
end
function setvars()
  sys={}
  msgs={"","","","",""}
  powerfail=0
end
function msgsystem()

end
function drawAll()
  do
	glass.addBox(0,233,100,30,0xFFFFFF,0.2)
	if power then
	  if s.powerPercentage then
		glass.addText(2,253,"Storage: "..round(power[1]/power[2]*100,2) .."%",0x000000)
	  else
		glass.addText(2,253,"Storage: "..round(power[1]/1000).."kRF/"..round(power[2]/1000).."kRF",0x000000)
	  end
	else
	  glass.addText(2,253,"Storage: Offline",0xFF0000)
	end
  end
  do
  
  end
end
function main()
  while true do
	glass.clear()
	drawAll()
	glass.sync()
	print("hi")
  end
end
function pullrednet()
  while active do
	print("hi2")
	local type,id,msg,protocol=rednet.receive()
	if protocol=="POWER" then
	  power=msg
	elseif protocol=="MSG" then
	  glass.msg(msg)
	else
	  glass.message("received rednet message with unknown protocol")
	end
  end
end
--// PROGRAM START
init()
parallel.waitForAll(main(),pullrednet())
The s table is defined in my config.

Any help is appreciated.
Lyqyd #2
Posted 11 August 2016 - 08:17 PM
You're using the parallel API wrong. You're supposed to pass the functions, not call them and pass the results.
5mCjLRgW2d473dyJtiiTnOr3wu #3
Posted 11 August 2016 - 08:55 PM
You're using the parallel API wrong. You're supposed to pass the functions, not call them and pass the results.
I'm such a spoon. Thanks, It works now.