This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
os.pullEvent() with timeout?
Started by Dan211, 15 March 2013 - 12:44 AMPosted 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.
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
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.
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.
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.
Posted 15 March 2013 - 11:04 PM
Thanks for your help, i might post my project up later.
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.
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.
Posted 15 March 2013 - 11:36 PM
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.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.
Posted 15 March 2013 - 11:47 PM
figured it out, i changed line 105 fromWow, 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.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.
local e, side, x, y = os.eventPull(monitor_touch)
to
event[3] = x
event[4] = y
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
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
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
what am i doing wrong?
heres my code http://pastebin.com/vHrJ3gKW
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.