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

Bit of multi-peripheral addition help.

Started by Inumel, 29 April 2015 - 04:15 AM
Inumel #1
Posted 29 April 2015 - 06:15 AM
I am using some delicious peripheral wrapping code provided in another post by Lyqyd

function wrapAll(type)
  local wrapped, names = {}, {}
  for _, name in ipairs(peripheral.getNames()) do
	if peripheral.getType(name) == type then
	  table.insert(names, name)
	end
  end
  for _, fn in ipairs(peripheral.getMethods(names[1])) do
	wrapped[fn] = function(...)
	  local returns = {}
	  for i = 1, #names do
		returns[i] = peripheral.call(names[i], fn, ...)
	  end
	  return returns
	end
  end
  return wrapped
end


which I am using on IC2 batboxes, batbox = wrapAll("batbox") That works all fine and dandy,

I wrote a quick thing to get its energy,

test = batbox.getEUStored()
  for k,v in pairs(test) do
   print(v)
	end
Which outputs the energy of all 24(yes, 24 batboxes) properly. The issue is, that insane amount of info is hard to display on a monitor, so i wanted to coalesce the energy outputs of each batbox, sadly I don't know how to do so in the required for loop.



Any help is of course appreciated, credit for all above code goes to LYQYD
Bomb Bloke #2
Posted 29 April 2015 - 07:50 AM
Your monitor will likely have plenty of columns, so I suppose you could use those to draw a bar chart. Calculate a percentage of how full each batbox is, and draw a line from the bottom of the screen leading up X% of the way to the top.

Either that or just print an average, I suppose. Or even just "(combined energy) / (total storage)".
Inumel #3
Posted 29 April 2015 - 04:26 PM
Your monitor will likely have plenty of columns, so I suppose you could use those to draw a bar chart. Calculate a percentage of how full each batbox is, and draw a line from the bottom of the screen leading up X% of the way to the top.

Either that or just print an average, I suppose. Or even just "(combined energy) / (total storage)".

It was my intention to combine all of the power and show a percentage of total power out of total capacity, but I don't know how to combine the energy readings, not with how they're presented
flaghacker #4
Posted 29 April 2015 - 04:34 PM
Just replace the print with something else:


totalEU = 0
test = batbox.getEUStored ()
for _, v in pairs do
  totalEU = totalEU + v
end
print (totalEU)
Edited on 29 April 2015 - 02:35 PM