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

Bundled Cable Events

Started by KeefKalif, 17 February 2012 - 05:30 PM
KeefKalif #1
Posted 17 February 2012 - 06:30 PM
Hi,
I want to build a item counting system:
I'm searching for a way to register all items without looping and checking all the time.
Advert #2
Posted 17 February 2012 - 07:05 PM
Hi,
I want to build a item counting system:
I'm searching for a way to register all items without looping and checking all the time.
Welcome to the forums, KeefKalif!

That looks amazing!

From the looks of it, you'll need multiple computers.

It would really help if you could post the code you have so far.

The first idea that comes to my mind would be to have a few slave computers, each connected to five bundles, sending data to one or two computers (what bundle went off, what color, etc), that send it to a final counting-computer, where you could see the totals.

There is no way to avoid looping here, unfortunately.
KeefKalif #3
Posted 18 February 2012 - 02:28 PM
I have written some code for this.Is this going to work or does the sleep cause the whole computer to hang?

oldState = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false}
newState = {}
count = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
computer = 1
sending = coroutine.create(SendCount)
coroutine.resume(sending)
while true do
  colorvalue = rs.getBundledInput("back")
  newState[0] = colors.test(colorvalue,colors.white)
  newState[1] = colors.test(colorvalue,colors.orange)
  newState[2] = colors.test(colorvalue,colors.magenta)
  newState[3] = colors.test(colorvalue,colors.lightblue)
  newState[4] = colors.test(colorvalue,colors.yellow)
  newState[5] = colors.test(colorvalue,colors.lime)
  newState[6] = colors.test(colorvalue,colors.pink)
  newState[7] = colors.test(colorvalue,colors.gray)
  newState[8] = colors.test(colorvalue,colors.lightgray)
  newState[9] = colors.test(colorvalue,colors.cyan)
  newState[10] = colors.test(colorvalue,colors.purple)
  newState[11] = colors.test(colorvalue,colors.blue)
  newState[12] = colors.test(colorvalue,colors.brown)
  newState[13] = colors.test(colorvalue,colors.green)
  newState[14] = colors.test(colorvalue,colors.red)
  newState[15] = colors.test(colorvalue,colors.black)
  for i = 0, 15, 1 do
   if oldState[i] == false and newState[i] == true then
	count[i] = count[i] + 1;
   end
  end
  oldState = newState
  newState = {}
  os.sleep(100)
end

function SendCount()
  while true do
   for i = 0, 15, 1 do
	if count[i] != 0 then
	 http.request("url?item=".. i + 1 .."&computer="..computer.."&count="..count[i] )
	end
   end
   count = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
   os.sleep(10000);
  end
end
Advert #4
Posted 18 February 2012 - 02:53 PM
I have written some code for this.Is this going to work or does the sleep cause the whole computer to hang?

oldState = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false}
newState = {}
count = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
computer = 1
sending = coroutine.create(SendCount)
coroutine.resume(sending)
while true do
  colorvalue = rs.getBundledInput("back")
  newState[0] = colors.test(colorvalue,colors.white)
  newState[1] = colors.test(colorvalue,colors.orange)
  newState[2] = colors.test(colorvalue,colors.magenta)
  newState[3] = colors.test(colorvalue,colors.lightblue)
  newState[4] = colors.test(colorvalue,colors.yellow)
  newState[5] = colors.test(colorvalue,colors.lime)
  newState[6] = colors.test(colorvalue,colors.pink)
  newState[7] = colors.test(colorvalue,colors.gray)
  newState[8] = colors.test(colorvalue,colors.lightgray)
  newState[9] = colors.test(colorvalue,colors.cyan)
  newState[10] = colors.test(colorvalue,colors.purple)
  newState[11] = colors.test(colorvalue,colors.blue)
  newState[12] = colors.test(colorvalue,colors.brown)
  newState[13] = colors.test(colorvalue,colors.green)
  newState[14] = colors.test(colorvalue,colors.red)
  newState[15] = colors.test(colorvalue,colors.black)
  for i = 0, 15, 1 do
   if oldState[i] == false and newState[i] == true then
	count[i] = count[i] + 1;
   end
  end
  oldState = newState
  newState = {}
  os.sleep(100)
end

function SendCount()
  while true do
   for i = 0, 15, 1 do
	if count[i] != 0 then
	 http.request("url?item=".. i + 1 .."&computer="..computer.."&count="..count[i] )
	end
   end
   count = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
   os.sleep(10000);
  end
end

The sleep causes the computer to wait, and discard events for arg seconds, in your case, 10000 seconds.

What are you trying to do with http.request? You shouldn't need to use that unless you're contacting a real-world webserver (i.e http://google.com)

You can't use !=, use ~=, it's Lua's !=.

Also, a note: coroutines are NOT parallel; i.e only one part of your code will work at once.

You can use timers and os.pullEvent(), your code would be structured something like this:

local webTimer = os.startTimer(10) -- Returns a table that identifies a 10 second, once-off timer.
local ... keep track of the redstone state here ...

function updateWeb()
webTimer = os.startTimer(10) -- We need to create a new timer, as they fire once only.
  ... code to update data to webserver here ...
end

function redstoneEvent()
...this will be called when redstone happens...
end

repeat
local event, p1, p2 = os.pullEvent()
if event == "redstone" then
  redstoneEvent(p1, p2)
elseif event == "timer" then
  if p1 == webTimer then --Make sure we have the right timer
   updateWeb()
  end
end
until event == "key" and p1 == "Q" -- Quit when you press Q (capital) on the computer
Espen #5
Posted 18 February 2012 - 03:53 PM
Hey Advert, a while ago I've posted information about how you can actually run multiple parts of your code in parallel.
But I only edited an old post, so it might've not updated as a new post and thus I think it might've escaped everyone's attention.^^
The coroutines won't really run concurrently at the same time, but they run in their own "thread" and things like sleep() or read() and the like in one coroutine won't have any effect on another coroutine, etc.
This is my edited post where I'm talking about that if anyone's looking for parallel running code:
http://www.computerc...indpost__p__333

There are some neat possibilities opening up with that.
Advert #6
Posted 18 February 2012 - 04:10 PM
Hey Advert, a while ago I've posted information about how you can actually run multiple parts of your code in parallel.
But I only edited an old post, so it might've not updated as a new post and thus I think it might've escaped everyone's attention.^^
The coroutines won't really run concurrently at the same time, but they run in their own "thread" and things like sleep() or read() and the like in one coroutine won't have any effect on another coroutine, etc.
This is my edited post where I'm talking about that if anyone's looking for parallel running code:
http://www.computerc...indpost__p__333

There are some neat possibilities opening up with that.

That's pretty interesting!

Looks a lot easier than using os.pullEvent() loops to handle stuff for yourself; I'll have to mess around with it some :)/>/>