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

Confusion Using Coroutines to Multi Thread a Program.

Started by Edgelessjet, 07 August 2015 - 03:26 AM
Edgelessjet #1
Posted 07 August 2015 - 05:26 AM
Edit: Thanks for the nearly instant support and example code. I`ll re-post the code in the comments if I can clean up the coroutines using the parallel API. :D/>


This is my first post, so sorry in advance if I make a ridiculous mistake. ;)/>

What I basically want to do is have a main computer taking the EU level of three different storage devices, in this case MFSU`s, and printing them individually then adding them together to find the total EU stored. I also want the user to have the ability to turn charging on or off, via EU splitter cable between the MFSU`s and the power source, all with a refresh rate of somewhere from .25 to .1 seconds. The problem I`m having is that it`s throwing me this error every time I try to run it and I can`t figure out why:

bios:339: [string "eu"]:7: '(' expected

I believe it has to do with the creation of the coroutine, but I don`t know what I`m doing wrong.
P.S. Sorry if the bit about coroutine yielding and resuming makes you want to facepalm. I`m also using Minecraft 1.6.4 on FTB Tech World 2.

Thanks in advance.

p = peripheral.wrap("mfsu_0")
o = peripheral.wrap("mfsu_1")
i = peripheral.wrap("mfsu_2")
rs.setOutput("left", true)
q = true
eu = coroutine.create(function eu ()
  while true do
	term.setCursorPos(1,1)
	term.clear()
	z = p.getEUStored()
	x = o.getEUStored()
	c = i.getEUStored()
	term.setCursorPos(1,1)
	term.clear()
	print("MFSU 1 EU:".. z)
	print("MFSU 2 EU;".. x)
	print("MFSU 3 EU;".. i.getEUStored())
	print("Total EU stored:".. z + x + c)
	print()
	--os.sleep(.1)
	coroutine.yield()
  end
end
end
listen = coroutine.create( function listen())
  while true do
	--os,sleep(.1)
	term.setCursorPos(1,6)
	term.clear()
	if q == false then
	  local h = true
	  print("Change charging setting, t/f? The setting is ".. tostring(h))
	elseif q == true then
	  local h = false
	  print("Change charging setting, t/f? The setting is ".. tostring(h))
	end
	y = read()
	if y == "f" then
	  q = true
	  rs.setOutput("left", false)
	elseif y == "t" then
	  q = false
	  rs.setOutput("left", true)
	end
  end
end
end
while true do
  os.sleep(.1)
  coroutine.resume(eu)
  os.sleep(.1)
  coroutine.resume(eu)
  os.sleep(.1)
  coroutine.resume(listen)
  os.sleep(.1)
  coroutine.yield(listen)
end
	
Edited on 07 August 2015 - 04:17 AM
KingofGamesYami #2
Posted 07 August 2015 - 05:46 AM
Well, the error is because you can't give the function a name if you're immediately going to pass it as an argument.


eu = coroutine.create( function()
 --#etc.

Also: You are grossly mishandling pretty much everything as far as coroutines are concerned. Take a look at the parallel API, it's pretty much a minimal coroutine manager - it passes events, manages the event filters, etc.
HPWebcamAble #3
Posted 07 August 2015 - 05:48 AM
Edit: Ninjad :ph34r:/> But its still relevant :)/>

Using coroutines here is a little overboard.
Also, as you assumed, you are Doing It Wrong (TM Lyqyd) :P/>
That's ok though. When you have a chance, look at the API 'parallel' that comes with CC, as it is a good example of a coroutine manager.

You can do this with a simple os.pullEvent() anyway:

--# Define variables, like the monitor
local lastID = os.startTimer(1) --# Starts a timer for 1 second, you don't need to update that quickly

while true do
  local event, p1, p2, p3, p4 = os.pullEvent() --# Program pauses here, waiting for an event, like a timer ending, or the monitor being clicked

  if event == "monitor_touch" then
	--# Do what you need to here
  elseif event == "timer" then
	if p1 == lastID then
	  --# Its been a second, update the display

	  lastID = os.startTimer(1)
	end
  end
end
Edited on 07 August 2015 - 05:19 PM
Edgelessjet #4
Posted 07 August 2015 - 05:53 AM
Thanks alot for the immediate feedback. I`ll try to clean it up according to your instructions and possibly post the code if I can get it running correctly. :)/>