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

Help adapting lua script to work with bundled outputs.

Started by osholt, 19 July 2013 - 03:57 PM
osholt #1
Posted 19 July 2013 - 05:57 PM
Hi, I spent all evening wrangling with the following code in attempt to allow it to output via bundled cables.

(Unmodified code, I started off with)

-- RedIo class for Computer craft
-- This program connects the 6 faces of a computer to the pin of mbed.
-- @example
-- RedWireBridge.new(read()):run()
local RedIo={}
RedIo.new=function(ip)
local inst={}
inst._addr=ip;
-- LED1,LED2,LED3,LED4,P5,P6  
inst._stbl={"top","bottom","left","right","front","back"}
inst.writers=function(self,p)
  local c=0
  local v=1
  for i=1, table.maxn(self._stbl), 1 do
   local f=bit.band(p,v)~=0;
   redstone.setOutput(self._stbl[i],f);
   v=v*2;
  end
end
inst.updatemcu=function(self,v)
  local h,r
  h=http.get("http://"..(self._addr).."/rsb/?p="..(math.floor(v)));
  r=h.readLine();
  return math.floor(r);
end
inst.readrs=function(self)
  local c
  local v
  local i
  v=1
  c=0
  for i=1, table.maxn(self._stbl), 1 do
   if redstone.getInput(self._stbl[i]) then
	c=c+v
   end
   v=v*2;
  end
  return c
end
inst.run=function(self)
  print("Run RedWireBridge.")
  self:writers(0);
  local pin=self:updatemcu(0);
  self:writers(pin);
  local lpin=pin
  while true do
   local rs=self:readrs();
   os.sleep(0.3)
   pin=self:updatemcu(rs);
   if lpin~=pin then
	self:writers(pin);
	lpin=pin
   end
  end
end
--initialize
inst:writers(0);
local mv=inst:updatemcu(0);
inst:writers(mv);
return inst;
end
--main
write("input RedWireBridge ip address: ");
RedIo.new(read()):run()

The code is to interface Computercraft with a program running on an mbed microcontroller.

The code is supposed to send and receive signals from a program running on the microcontroller. The microcontroller is attached to 16 LEDs which I am trying to make glow according to what the states of each coloured input such that white cable (1) turns on LED1 etc.

The code also works in reverse taking inputs from switches on the board so that when switch one is high on the microcontroller the white cable (1) is high.

Here is where I have got to so far:


-- RedIo class for Computer craft
-- This program connects the 6 faces of a computer to the pin of mbed.
-- @example
-- RedWireBridge.new(read()):run()
local RedIo={}
RedIo.new=function(ip)
local inst={}
inst._addr=ip;
-- LED1,LED2,LED3,LED4,P5,P6  
inst._stbl={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
inst.writers=function(self,p)
  local c=0
  local v=1
  for i=1, table.maxn(self._stbl), 1 do
   local f=bit.band(p,v)~=0;
   if f==false then
	redstone.setBundledOutput("back", colors.combine(c, self._stbl[i]));
   else
	redstone.setBundledOutput("back", colors.subtract(c, self._stbl[i]));
   end
   v=v+1;
  end
end
inst.updatemcu=function(self,v)
  local h,r
  h=http.get("http://"..(self._addr).."/rsb/?p="..(math.floor(v)));
  r=h.readLine();
  return math.floor(r);
end
inst.readrs=function(self)
  local c
  local v
  local i
  v=1
  c=0
  for i=1, table.maxn(self._stbl), 1 do
   if colors.test(redstone.getBundledInput("back"), self._stbl[i]) then
	c=c+v
   end
   v=v+1;
  end
  return c
end
inst.run=function(self)
  print("Run RedWireBridge.")
  self:writers(0);
  local pin=self:updatemcu(0);
  self:writers(pin);
  local lpin=pin
  while true do
   local rs=self:readrs();
   os.sleep(0.001)
   pin=self:updatemcu(rs);
   if lpin~=pin then
	self:writers(pin);
	lpin=pin
   end
  end
end
--initialize
inst:writers(0);
local mv=inst:updatemcu(0);
inst:writers(mv);
return inst;
end
--main
write("input RedWireBridge ip address: ");
RedIo.new(read()):run()

This turns on each LED (upto LED 4) without a problem but when you want multiple LEDS or higher numbers then random LEDs seem to come on.

The only problem with code as far as I can tell is how I've been incrementing "v" and the numbers that end up going into redstone.getBundledInput() and redstone.setBundledInput().

If someone could show or attempt to show e where I've gone wrong (or even better fix it!) that would be great.

Thanks,

osholt
Lyqyd #2
Posted 19 July 2013 - 07:58 PM
Split into new topic.
albrat #3
Posted 19 July 2013 - 08:56 PM
redstone.setBundledOutput("back", (redstone.getBundledInput("back") - colors.white ) ) would turn off the white cable (1) only…

I know that the bundled output is done in 16 Bit. eg..

0000000000000001 = 1 (white)
0000000000000010 = 2 (erm, I forgot) *orange
0000000000000100 = 4 etc…

after the first value the next value to turn on just one light out of them all is "x * 2" Where x is your last result. eg. 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768 < these are your key numbers to trigger each individual cable on it's own. (combine them all and you trigger all the lights at once.) * I actually think there are only 15 combinations… plus 0. so up to 16384 then add everything below to make 32767 ( all on ). – Proved myself wrong.

so if you stored the 1,2,4,8… you could just call the value from the table to turn on or off each input.

eg, values = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}
you call the value for white as values[1] , for orange values[2], for black values[16]


random Waffle…
SpoilerThis Takes me back to my years in school when we had computers like the Old Spectrum 64k… (lol)
I wrote a simple program that had teachers in awe.
let x =1
for a = 1 to 16
x=x *2
print x
next
it basically outputted the string of numbers above. Ahh the simpler times when not many knew how to use a computer. lol
this was in a spoiler because it is nothing to do with the topic really. lol
… End of waffle.


as I got further into this post I started to research more and more… ( http://computercraft...olors_%28API%29 )

65535 is everything on.
osholt #4
Posted 20 July 2013 - 12:15 PM
Thanks so much! It now works flawlessly!

The problem was just the numbers I was using to represent the colours.

Now I just have to tidy my code up a bit.

osholt