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

Non blocking touch monitor

Started by Akxe, 01 May 2015 - 11:56 PM
Akxe #1
Posted 02 May 2015 - 01:56 AM
I wanted to create a program which, would run forever, but had control buttons, to change behavior.


local monitor = peripheral.wrap("front")
local kill = false
term.redirect(monitor)
term.setBackgroundColor(colors.black)
monitor.clear()
term.setCursorPos(3, 6)
print("Blaze  farm")
monitor.setTextScale(0.5)
function killBtn(color)
  paintutils.drawFilledBox(2, 2, 7, 4, color)
  term.setCursorPos(3,3)
  print("kill")
end
killBtn(colors.green)
function nextBtn(color)
  paintutils.drawFilledBox(9, 2, 14, 4, color)
  term.setCursorPos(10,3)
  print("next")
end
nextBtn(colors.green)

function Damage(crusher, dropper)
  redstone.setOutput("top",true)
  term.setCursorPos(3, 9)
  term.setBackgroundColor(colors.black)
  print("Waiting ...")
  sleep(dropper)
  redstone.setOutput("top",false)

  redstone.setOutput("right",true)
  redstone.setOutput("bottom",true)
  update(crusher)
  redstone.setOutput("right",false)
  redstone.setOutput("bottom",false)

  paintutils.drawFilledBox(0, 8, 15, 10, colors.black)
end
function update(count)
  local total = count
  while count > 0 do
	count = count - 0.5
	paintutils.drawFilledBox(0, 8, 15 - count / total * 15, 10, colors.gray)
	sleep(0.5)
  end
end

while true do
  event, side, xPos, yPos = os.pullEvent("monitor_touch")
  if yPos >= 2 and yPos <= 4 then
	if xPos >= 2 and xPos <= 7 then
	  kill = not kill
	  if kill then
		killBtn(colors.red)
		nextBtn(colors.gray)
	  else
		killBtn(colors.green)
		nextBtn(colors.green)
	  end
	  Damage(9, 5)
	elseif (kill == false) and (xPos >= 9 and xPos <= 14) then
	  Damage(8.5, 5)
	end
  end

  if kill == true then
    Damage(9, 5)
  end
end
Edited on 02 May 2015 - 09:58 AM
Square789 #2
Posted 02 May 2015 - 10:38 AM
We don't write programs on request.
Please show us the code you've written so far and then post it here.
Also, what should the program be for?
If you want to use an advanced monitor with buttons you can take a look at a button API.
Edited on 02 May 2015 - 08:39 AM
Akxe #3
Posted 02 May 2015 - 10:50 AM
Well I wanted to find some non blocking os.pullevent() equivalent
flaghacker #4
Posted 02 May 2015 - 11:29 AM
That doesn't exist, and there's no reason it should. The point of os.pullEvent is to wait for an event.

Could you explain why you want this?
Akxe #5
Posted 02 May 2015 - 12:02 PM
I have updated the first post for the code, there is a os.pullEvent("monitor_touch"), that unfortunately stop execution of killing blazes (sending redstone signal to piston crushers).
I want to have possibility to change type of damaging (between 8.5 &amp; 9) even while the code is executing

Images of it working
idle (waiting for touch :(/> )
[attachment=2262:2015-05-02_13.14.48.png]

In action
[attachment=2263:2015-05-02_13.14.57.png]
Edited on 02 May 2015 - 10:17 AM
flaghacker #6
Posted 02 May 2015 - 01:45 PM
You have 2 options, use timers or use the Parallel API.

The parallel API is probably simpler:

kill = true

function runDamageLoop()
  while true do
    if kill then
	  //damage
    end
  end
end

function runTouchLoop()
  while true do
    event, side, x, y = os.pullEvent("monitor_touch")
    //handle event, set kill
  end
end

//main
parallel.waitForAny(runDamageLoop, runTouchLoop)

Basically you make 2 functions each doing one part of the job, and then you simulate them both running at the same time using parallel.waitForAny .
HPWebcamAble #7
Posted 02 May 2015 - 03:17 PM
- snip -

Remember to call os.pullEvent() at some point in all of the functions you use with the parallel API

If you don't outright need os.pullEvent(), just use sleep() somewhere
Edited on 02 May 2015 - 01:17 PM
flaghacker #8
Posted 02 May 2015 - 03:57 PM
- snip -

Remember to call os.pullEvent() at some point in all of the functions you use with the parallel API

If you don't outright need os.pullEvent(), just use sleep() somewhere

This goes up for any code, always call os.pullEvent regularly(sleep does this internally).
Akxe #9
Posted 02 May 2015 - 06:52 PM
Thank you, it works… I have found coroutine, but not parallel
flaghacker #10
Posted 02 May 2015 - 09:14 PM
Thank you, it works… I have found coroutine, but not parallel

Yea, parallel is just an API that handles the coroutines for you in the background.
valithor #11
Posted 03 May 2015 - 06:10 AM
Thank you, it works… I have found coroutine, but not parallel

If you do want to ever use them. A link to the api can be linked here: http://www.computercraft.info/wiki/Category:APIs
A dirrect link to the api does not want to work… It is leaving out part of it when it is clicked.
Edited on 03 May 2015 - 04:14 AM
flaghacker #12
Posted 03 May 2015 - 07:35 AM
Thank you, it works… I have found coroutine, but not parallel

If you do want to ever use them. A link to the api can be linked here: http://www.computercraft.info/wiki/Category:APIs
A dirrect link to the api does not want to work… It is leaving out part of it when it is clicked.
Your link is still corrupt, I recommend linking like this:
Parallel API

[url=http://computercraft.info/wiki/Parallel_(API)]Parallel API[/url]
And why are you linking again? :P/>