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

os.pullEvent() with timeout?

Started by Dan211, 15 March 2013 - 12:44 AM
Dan211 #1
Posted 15 March 2013 - 01:44 AM
How do i make it so i can use os.pullEvent(monitor_touch) but have it bypassed every second? Its for a touchscreen control panel and I want it to display time, currently it works but i have to click onto the screen to refresh it. thanks.
Lyqyd #2
Posted 15 March 2013 - 05:34 AM
Split into new topic.


local timeout = os.startTimer(1)
while true do
  event = {os.pullEvent()}
  if event[1] == "monitor_touch" then
    --handle monitor touches
    break
  elseif event[1] == "timer" and event[2] == timeout then
    break
  end
end
theoriginalbit #3
Posted 15 March 2013 - 05:39 AM
to expand on Lyqyd's solution…

chances are (based on the use case) you will want to replace break with timeout = os.startTimer(1) that way the program continues to run and refresh every 1 second.
Lyqyd #4
Posted 15 March 2013 - 05:49 AM
to expand on Lyqyd's solution…

chances are (based on the use case) you will want to replace break with timeout = os.startTimer(1) that way the program continues to run and refresh every 1 second.

You are right, though as a naïve replacement for a filtered pullEvent call, this is pretty close to a drop-in replacement, with the additional step of handling the monitor touches internally. Removing the break after the monitor touch handling portion may or may not be desired if it is used this way.

I agree that a simple single event handling loop is probably best here, though.
Dan211 #5
Posted 15 March 2013 - 11:04 PM
Thanks for your help, i might post my project up later.
Dan211 #6
Posted 15 March 2013 - 11:25 PM
I tried it, but I get an error on line 74 that it cannot compare nil with number, I assume this is because the monitor_touch event is firing without an actual touch. What is wrong? heres my code http://pastebin.com/Kd8bkDUC its mostly direwolf20's code tweaked to work for me. Thanks again.
theoriginalbit #7
Posted 15 March 2013 - 11:30 PM
you need to put line 111 into the "monitor_touch" if statement. atm it is trying to call it each time that an event fires, including the "timer" event, which only has 1 parameter, not two… really its a good thing too, or else you would have been getting some weird results that you would have been confused about and would have been a little harder to track down.
Dan211 #8
Posted 15 March 2013 - 11:36 PM
you need to put line 111 into the "monitor_touch" if statement. atm it is trying to call it each time that an event fires, including the "timer" event, which only has 1 parameter, not two… really its a good thing too, or else you would have been getting some weird results that you would have been confused about and would have been a little harder to track down.
Wow, I see what the problem was now, now how do i round the decimal points in the time? I also found another problem, when i click on a button the time stops and I need to click on the screen for it to continue, the buttons also require me to press them twice tor them to actually work.
Dan211 #9
Posted 15 March 2013 - 11:47 PM
you need to put line 111 into the "monitor_touch" if statement. atm it is trying to call it each time that an event fires, including the "timer" event, which only has 1 parameter, not two… really its a good thing too, or else you would have been getting some weird results that you would have been confused about and would have been a little harder to track down.
Wow, I see what the problem was now, now how do i round the decimal points in the time? I also found another problem, when i click on a button the time stops and I need to click on the screen for it to continue, the buttons also require me to press them twice tor them to actually work.
figured it out, i changed line 105 from
local e, side, x, y = os.eventPull(monitor_touch)
to
event[3] = x
event[4] = y
theoriginalbit #10
Posted 15 March 2013 - 11:49 PM
ok…

problem 1… use textutils.formatTime to make the time a nice string that we can all recognise.

problem 2…

change your loop to this

while true do
   mon.clear()
   heading("Dan211's Main Control Panel")
   info("UU made", 18, 7, 1000, " ")
   info("EU", 18, 8, 100, "%")
   info("Time",18, 4, os.time(), "")
   screen()
   local event = {os.pullEvent()}
   if event[1] == "monitor_touch" then
     checkxy(event[3], event[4])
     print(event[4]..":"..event[3])
   elseif event[1] == "timer" and event[2] == timeout then
	 timeout = os.startTimer(1)
   end
end
Dan211 #11
Posted 16 March 2013 - 03:16 AM
I got another question, if I add rednet.open("back") anywhere in the program it gives me an error on line one: attempt to index ? (a nil value)
what am i doing wrong?
heres my code http://pastebin.com/vHrJ3gKW
Dan211 #12
Posted 16 March 2013 - 03:21 AM
I got another question, if I add rednet.open("back") anywhere in the program it gives me an error on line one: attempt to index ? (a nil value)
what am i doing wrong?
heres my code http://pastebin.com/vHrJ3gKW

wow, nevermind this. I misspelled peripheral lol.