Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Rayos268

Parallel API

Question

Hello all,

i changed from computercraft to opencomputers and now i need the parallel api but in oc is none,

so i need a little bit help to rewrite the programm i have read something about a background process, like windows but i dont understand this so i hope you can tell me what i have to do with my code to abort or cancel the io.read() if my magenta input is on

local component = require("component")
local sides = require("sides")
local colors = require("colors")
local redstone = component.redstone
local computer = require("computer")
oeffner = redstone.getBundledInput(sides.back, colors.magenta)
yellow = redstone.getBundledInput(sides.back, colors.yellow)
lime = redstone.getBundledInput(sides.back, colors.lime)


print("Willkommen im Türkontrollmodus")
if(yellow == 241 and lime == 243) then
print("Tür ist Zurzeit geschlossen")
elseif (yellow < 241 and lime < 243) then
print("Tür ist zurzeit geöffnet")
else
print("Tür steht in einem unbekannten Zustand")
end
print("Bitte warten, die Türsteuerung wird geladen.")
print("")
print("")
os.sleep(2)

print("Willkommen bei der Türsteuerung")
print("Bitte eingabe erfassen zum Tor öffnen(1) oder schließen(2)")
eingabe = io.read()
while not tonumber(eingabe) do
print("keine Nummer")
print("Bitte eine Ziffer von 0-9 Eingeben")
eingabe = io.read()
end

if (eingabe == "1" or oeffner == 230 and yellow == 241 and lime == 243) then
print("Tür wird geöffnet")
redstone.setBundledOutput(sides.back, colors.lightblue, 255)
for i=1,5 do
redstone.setBundledOutput(sides.back, colors.orange, 255)
os.sleep(0.1)
redstone.setBundledOutput(sides.back, colors.orange, 0)
if (i == 5) then
redstone.setBundledOutput(sides.back, colors.lightblue, 0)
end
os.sleep(1)
end

elseif (eingabe == "2" or oeffner == 230 and yellow < 241 and lime < 243) then
print("Tür wird geschlossen")
redstone.setBundledOutput(sides.back, colors.lightblue, 255)
for i=1,5 do
redstone.setBundledOutput(sides.back, colors.white, 255)
os.sleep(0.1)
redstone.setBundledOutput(sides.back, colors.white, 0)
if (i == 5) then
redstone.setBundledOutput(sides.back, colors.lightblue, 0)
end
os.sleep(1)
end

else
print("Eingabe nicht möglich, Nummer nicht verwendet, oder Tor bereits geöffnet/geschlossen")
print("Bitte erneut versuchen")
os.sleep(2)
computer.shutdown(true)
end

 

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

OpenOS 1.6.5 introduces threads: http://ocdoc.cil.li/api:thread

The examples at the end of that wiki page demonstrate how you could cancel io.read due  to some condition: http://ocdoc.cil.li/api:thread#thread_interrupt_handler_example

So for your case:

local thread = require("thread")
local component = require("component")
local sides = require("sides")
local colors = require("colors")
local redstone = component.redstone

local monitor_thread = thread.create(function()
  while true do
    if redstone.getBundledInput(sides.back, colors.magenta) then
      break
    end
    os.sleep(0)
  end
end)

local user_input_thread = thread.create(function()
  io.write("waiting for input: ")
  io.read()
end)

thread.waitForAny({monitor_thread, user_input_thread})
io.write("program closing\n")
os.exit(0) -- kills all remaining threads

I use `os.exit(0)` because threads that are still running block the parent process from closing (keep it running). It is a good idea to please read the wiki page I wrote on threads.

Another option instead of `os.exit(0)` in this case would be to kill the threads:

thread.waitForAny({monitor_thread, user_input_thread})
monitor_thread:kill()
user_input_thread:kill()

 

Link to post
Share on other sites
  • 0

Also, there is a "redstone_changed" event that is fired when the input changes. So...the monitor_thread could be a bit cleaner without the sleep, but instead:

 

local monitor_thread = thread.create(function()
  while true do
    if redstone.getBundledInput(sides.back, colors.magenta) then
      break
    end
    event.pull("redstone_changed")
  end
end)

 

You could also do something quite different with `event.listen`:

 

local thread = require("thread")
local component = require("component")
local sides = require("sides")
local colors = require("colors")
local redstone = component.redstone

local user_input_thread = thread.create(function()
  io.write("waiting for input: ")
  io.read()
end)

event.listen("redstone_changed", function()
  if redstone.getBundledInput(sides.back, colors.magenta) then
    user_input_thread:kill()
    return false -- return false removes this event listener from the event registration pool
  end
end)

-- user_input_thread will block this process from closing
-- but you could optional block on the thread explicitly:
-- user_input_thread:join()

 

I personally like the thread-only solution more than the event.listener solution

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.