Problem with running Funktions parallel

Started by CreativePlayer, Jan 25, 2020, 11:36 PM

Previous topic - Next topic

CreativePlayer

Hello,
i wrote a function, to controll a monitor. The function is able to accept touches and to give a redstone output in a bundled cable. At the end of the function, the function starts itself again. So my question is: Is there a way to run many of these functions on one computer?

I know the parallel API, but it doesn't work in my case. When i try to use parallel.waitForAll(f1, f2), it starts only the first function of the call.

Thanks for any help

SquidDev

Would you be able to post the code? Just so we can get an idea of why parallel doesn't work for your use case.
GitHub | CC:Tweaked: A ComputerCraft fork | Plethora: A peripheral mod

CreativePlayer

#2
Okay, here is my whole code:

001 --Wellcome in the HomeOS
002 --pereferie declaration
003 mon1 = peripheral.wrap("monitor_14")
004 mon2 = peripheral.wrap("monitor_15")
005 mon3 = peripheral.wrap("monitor_10")
006 mon4 = peripheral.wrap("monitor_11")
007 mon5 = peripheral.wrap("monitor_12")
008 mon6 = peripheral.wrap("monitor_14")
009 mon7 = peripheral.wrap("monitor_13")
010 mon8 = peripheral.wrap("monitor_19")
011 mon9 = peripheral.wrap("monitor_16")
012 mon10 = peripheral.wrap("monitor_17")
013 mon11 = peripheral.wrap("monitor_18")
014 test = peripheral.wrap("monitor_20")
015
016 --functions:
017 --Background formatting of small monitors (1x1)
018
019 function H1(a)
020 a.setTextScale(0.5)
021 a.setCursorPos(1,1)
022 a.setBackgroundColor(colors.gray)
023 a.clear()
024 for i=1, 2 do
025 a.setCursorPos(1,i)
026 a.setBackgroundColor(colors.black)
027 a.clearLine()
028 end
029 a.setCursorPos(1,1)
030 a.write("HomeOS")
031 a.setCursorPos(1, 2)
032 a.write("---------------")
033 a.setCursorPos(1,3)
034 end
035
036--Small monitor with pin-lock
037 function P(a, b)
038 while true do
039 local p = ""
040
041 H1(a)
042 a.setBackgroundColor(colors.gray)
043 a.write("Pin eingeben:")
044 a.setCursorPos(1, 5)
045 a.write("  [        ]  ")
046 a.setCursorPos(1,7)
047 a.write("  [7] [8] [9]  ")
048 a.setCursorPos(1,8)
049 a.write("  [4] [5] [6]  ")
050 a.setCursorPos(1,9)
051 a.write("  [1] [2] [3]  ")
052 a.setCursorPos(1,10)
053 a.write("      [0]      ")
054 a.setCursorPos(4,5)
055
056 while true do
057 if (#p == 4) and (p == "1234") then
058 a.setCursorPos(4, 5)
059 a.setTextColor(colors.green)
060 a.write(p)
061 sleep(1.5)
062 a.setCursorPos(4, 5)
063 a.write("---------")
064 a.setTextColor(colors.white)
065 rs.setBundledOutput("right", colors[b])
066 sleep(20)
067 rs.setBundledOutput("right", colors.subtract(rs.getBundledOutput("right"), colors[b]))
068 break
069 end
070
071 if (#p == 4) and (p ~= "1234") then
072 a.setCursorPos(4,5)
073 a.setTextColor(colors.red)
074 a.write(p)
075 sleep(1.5)
076 a.setTextColor(colors.white)
077 break
078 end
079
080 event, side, x, y = os.pullEvent()
081    if event == "monitor_touch" then
082        if y == 7 then
083            if (x >= 3) and (x <= 5) then
084            a.write("7")
085            p = p.."7"
086            end
087            if (x >= 7) and (x <= 9) then 
088            a.write("8")
089            p = p.."8"
090            end
091            if (x >= 11) and (x <= 13) then
092            a.write("9")
093            p = p.."9"
094            end
095        end           
096        if y == 8 then         
097            if (x >= 3) and (x <= 5) then
098            a.write("4")
099            p = p.."4"
100            end
101            if (x >= 7) and (x <= 9) then
102            a.write("5")
103            p = p.."5"
104            end
105            if (x >= 11) and (x <= 13) then
106            a.write("6")
107            p = p.."6"
108            end
109        end         
110        if y == 9 then     
111            if (x >= 3) and (x <= 5) then
112            a.write("1")
113            p = p.."1"
114            end
115            if (x >= 7) and (x <= 9) then
116            a.write("2")
117            p = p.."2"
118            end
119            if (x >= 11) and (x <= 13) then
120            a.write("3")
121            p = p.."3"
122            end
123        end 
124        if (y == 10) and (x >= 7) and (x <= 9) then
125        a.write("0")
126        p = p.."0"
127        end     
128    end
129 end
130 P(a, b)
131 end
132
133 end
134
135 --Programmstart:
136
137 parallel.waitForAll(P(mon9, "orange"), P(mon11, "white"))

Only the function P(mon9, "orange") was executet.
I hope this helps...

Lupus590

parallel takes function pointers, you are calling the functions and passing it their results

replace line 137 with this (also please use [.code][./code] tags next time, just remove the dots)

parallel.waitForAll(function() P(mon9,"orange") end, function() P(mon11, "white") end)

what this does it wrap your function calls in an anonymous function the pointer of which gets passed to parallel

parallel actually can't run functions with arguments so using these anonymous functions is a workaround
[/code]

CreativePlayer

#4
Thanks for your help.
well I changed line 137 to:

parallel.waitForAll(function() P(mon9,"orange") end, function() P(mon11, "white") end)

Now the two monitors are kind of mirrored. When i type in a number, each of my monitors show it on their screen.
No matter which monitor I use, when i type in 1234, only the white redstone cable gives an output.

I changed the code in line 137 to:

parallel.waitForAll(function() P(mon11,"white") end, function() P(mon9, "orange") end)

Now the monitors are still mirrored but the orange redstone cable gives an output
I want to use each of the monitors separately.

Lupus590

you might need to use the side argument of the event to separate the events for each of the monitors

you also might want to change how you filter events, you can pass the event type to os.pullEvent and it will only return those events

also P doesn't need to call itself by the looks of it

and please use local variables

CreativePlayer

#6
sorry, but I don't really understand.

Should I use

event, side, x, y = os.pullevent("monitor_touch")
istead of
event, side, x, y = os.pullevent()
    if event == "monitor_touch" then
    (...)
?

How should I change the program?

Actually it looks like this:

001 --Wellcome in the HomeOS
002 --pereferie declaration
003 local mon1 = peripheral.wrap("monitor_14")
004 local mon2 = peripheral.wrap("monitor_15")
005 local mon3 = peripheral.wrap("monitor_10")
006 local mon4 = peripheral.wrap("monitor_11")
007 local mon5 = peripheral.wrap("monitor_12")
008 local mon6 = peripheral.wrap("monitor_14")
009 local mon7 = peripheral.wrap("monitor_13")
010 local mon8 = peripheral.wrap("monitor_19")
011 local mon9 = peripheral.wrap("monitor_16")
012 local mon10 = peripheral.wrap("monitor_17")
013 local mon11 = peripheral.wrap("monitor_18")
014 local test = peripheral.wrap("monitor_20")
015
016 --functions:
017 --Background formatting of small monitors (1x1)
018
019 local function H1(a)
020 a.setTextScale(0.5)
021 a.setCursorPos(1,1)
022 a.setBackgroundColor(colors.gray)
023 a.clear()
024 for i=1, 2 do
025 a.setCursorPos(1,i)
026 a.setBackgroundColor(colors.black)
027 a.clearLine()
028 end
029 a.setCursorPos(1,1)
030 a.write("HomeOS")
031 a.setCursorPos(1, 2)
032 a.write("---------------")
033 a.setCursorPos(1,3)
034 end
035
036--Small monitor with pin-lock
037 local function P(a, b)
038 while true do
039 local p = ""
040
041 H1(a)
042 a.setBackgroundColor(colors.gray)
043 a.write("Pin eingeben:")
044 a.setCursorPos(1, 5)
045 a.write("  [        ]  ")
046 a.setCursorPos(1,7)
047 a.write("  [7] [8] [9]  ")
048 a.setCursorPos(1,8)
049 a.write("  [4] [5] [6]  ")
050 a.setCursorPos(1,9)
051 a.write("  [1] [2] [3]  ")
052 a.setCursorPos(1,10)
053 a.write("      [0]      ")
054 a.setCursorPos(4,5)
055
056 while true do
057 if (#p == 4) and (p == "1234") then
058 a.setCursorPos(4, 5)
059 a.setTextColor(colors.green)
060 a.write(p)
061 sleep(1.5)
062 a.setCursorPos(4, 5)
063 a.write("---------")
064 a.setTextColor(colors.white)
065 rs.setBundledOutput("right", colors[b])
066 sleep(20)
067 rs.setBundledOutput("right", colors.subtract(rs.getBundledOutput("right"), colors[b]))
068 break
069 end
070
071 if (#p == 4) and (p ~= "1234") then
072 a.setCursorPos(4,5)
073 a.setTextColor(colors.red)
074 a.write(p)
075 sleep(1.5)
076 a.setTextColor(colors.white)
077 break
078 end
079
080 event, side, x, y = os.pullEvent("monitor_touch")      --What do you mean at this Point?
081    if event == "monitor_touch" then
082        if y == 7 then
083            if (x >= 3) and (x <= 5) then
084            a.write("7")
085            p = p.."7"
086            end
087            if (x >= 7) and (x <= 9) then
088            a.write("8")
089            p = p.."8"
090            end
091            if (x >= 11) and (x <= 13) then
092            a.write("9")
093            p = p.."9"
094            end
095        end         
096        if y == 8 then       
097            if (x >= 3) and (x <= 5) then
098            a.write("4")
099            p = p.."4"
100            end
101            if (x >= 7) and (x <= 9) then
102            a.write("5")
103            p = p.."5"
104            end
105            if (x >= 11) and (x <= 13) then
106            a.write("6")
107            p = p.."6"
108            end
109        end       
110        if y == 9 then   
111            if (x >= 3) and (x <= 5) then
112            a.write("1")
113            p = p.."1"
114            end
115            if (x >= 7) and (x <= 9) then
116            a.write("2")
117            p = p.."2"
118            end
119            if (x >= 11) and (x <= 13) then
120            a.write("3")
121            p = p.."3"
122            end
123        end
124        if (y == 10) and (x >= 7) and (x <= 9) then
125        a.write("0")
126        p = p.."0"
127        end   
128    end
129 end
130
131 end
132
133 end
134
135 --Programmstart:
136
137 parallel.waitForAll(P(mon9, "orange"), P(mon11, "white"))

QuickMuffin8782

You can, but it will at least keep waiting for a event of that, so, to prevent problems, I think you should replace that as the string in the os.pullEvent function as it filters for that event.
"The Blue Blur" - Working on Net Star OS, a new frontier for Red Star OS 3.0
linktr.ee/quickmuffin8782