Jump to content




Non blocking touch monitor

computer

11 replies to this topic

#1 Akxe

  • Members
  • 6 posts

Posted 02 May 2015 - 12: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 by Akxe, 02 May 2015 - 10:58 AM.


#2 Square789

  • Members
  • 39 posts
  • LocationUniverse:C:/MilkyWay/Sol/Earth/Europe/Germany

Posted 02 May 2015 - 09: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 by Square789, 02 May 2015 - 09:39 AM.


#3 Akxe

  • Members
  • 6 posts

Posted 02 May 2015 - 09:50 AM

Well I wanted to find some non blocking os.pullevent() equivalent

#4 flaghacker

  • Members
  • 655 posts

Posted 02 May 2015 - 10: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?

#5 Akxe

  • Members
  • 6 posts

Posted 02 May 2015 - 11:02 AM

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 & 9) even while the code is executing

Images of it working
idle (waiting for touch :( )
Attached Image: 2015-05-02_13.14.48.png

In action
Attached Image: 2015-05-02_13.14.57.png

Edited by Akxe, 02 May 2015 - 11:17 AM.


#6 flaghacker

  • Members
  • 655 posts

Posted 02 May 2015 - 12: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 .

#7 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 02 May 2015 - 02:17 PM

View Postflaghacker, on 02 May 2015 - 12:45 PM, said:

- 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 by HPWebcamAble, 02 May 2015 - 02:17 PM.


#8 flaghacker

  • Members
  • 655 posts

Posted 02 May 2015 - 02:57 PM

View PostHPWebcamAble, on 02 May 2015 - 02:17 PM, said:

View Postflaghacker, on 02 May 2015 - 12:45 PM, said:

- 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).

#9 Akxe

  • Members
  • 6 posts

Posted 02 May 2015 - 05:52 PM

Thank you, it works... I have found coroutine, but not parallel

#10 flaghacker

  • Members
  • 655 posts

Posted 02 May 2015 - 08:14 PM

View PostAkxe, on 02 May 2015 - 05:52 PM, said:

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.

#11 valithor

  • Members
  • 1,053 posts

Posted 03 May 2015 - 05:10 AM

View PostAkxe, on 02 May 2015 - 05:52 PM, said:

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 by valithor, 03 May 2015 - 05:14 AM.


#12 flaghacker

  • Members
  • 655 posts

Posted 03 May 2015 - 06:35 AM

View Postvalithor, on 03 May 2015 - 05:10 AM, said:

View PostAkxe, on 02 May 2015 - 05:52 PM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users