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)
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:
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
(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