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

Basic multitasking (keeping a clock running in the background)

Started by Nothy, 02 March 2016 - 11:19 AM
Nothy #1
Posted 02 March 2016 - 12:19 PM
(solved)
Disclaimer: I'm a bit of a noob when it comes to the parallel api.

Okay, so. Basically what I'm trying to do is keep a clock running in my OS.
I set the clock position based on what "state" the OS is in. As in, if the OS is in "home", then the clock would be in 1,1. If that makes sense.

But if I call the clock to render in the same function as the screen to update where the user clicked, it will pause the clock updating.

And if I use the parallel API, I run into the issue where it just simply does not work.
The screen will allow one click, no more.
The clock will refresh once, no more.

So I'm wondering, what would be a possible solution?
Code
 max_x = 26
min_x = 1
max_y = 20
min_y = 1
locked = true
quit = false
function time()
	  if locked then
		edge.xtime(13,7)
		term.setTextColor(colors.black)
		edge.cprint("Day "..os.day(),8 )
		sleep(0.8)
	  end
	  if not locked then
		edge.xtime(1,1)
		sleep(0.8)
	  end
	  if quit then
		return true
	  end
end
local function home()
  locked = false
  shell.run("clear")

end
local function keydet()
  local event, key = os.pullEvent("key")
  if key == keys.tab and locked == false then
	  locked = true
	  shell.run("clear")
	  parallel.waitForAny(time, lockscreen)
  end
end
local function lockscreen()
  term.setTextColor(colors.black)
  edge.cprint("locked",19)
  local event, button, x, y = os.pullEvent("mouse_click")
	if button == 1 then
	  parallel.waitForAll(home,keydet)
	end
	if quit then
	  os.shutdown()
	end
end

function start()
  --edge.render(1,26,1,20,colors.white,colors.white,"")
  edge.xprint("PockeOS.", 13 - string.len("PockeOS.") / 2, 10,colors.red)
  term.setTextColor(colors.black)
  sleep(5)
  shell.run("clear")
  while(true) do
	parallel.waitForAny(time, lockscreen)
	if quit then
	  break
	end
  end
end

term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
shell.run("clear")
start()
Edited on 03 March 2016 - 06:48 AM
Bomb Bloke #2
Posted 02 March 2016 - 01:14 PM
Don't run parallel.waitForAny() within a while loop; put while loops into your time and lockscreen functions instead.
Nothy #3
Posted 02 March 2016 - 01:21 PM
Don't run parallel.waitForAny() within a while loop; put while loops into your time and lockscreen functions instead.
Alrighty. I'll try. Thank you :)/>
Nothy #4
Posted 02 March 2016 - 08:20 PM
It works very well. Thank you so, so much.