Starting to getting it to do what I want… This code is displaying the last second in the current time. (left out the library that can be found on github)
It is a step towards my goal, but I am trying to find a better way to handle multiple ports.
What probably would work is to do like I did, and define each port from 0 to 5. (6 numbers on the final display with one shift register each)
But I would prefer not having long lists with just port0, port1, port2, and led1, led2, led3… and so on.
In my setNum function I did manage to cook it all down to just two lines of code, that just reads a string to determine if the pin should be set high or low. But haven’t found a way to do it so it also includes the port.
My idea was to just call setNum(3, 0); and it would then show a zero on the display with address 3.
`
// Configure I2C bus
hardware.i2c89.configure(CLOCK_SPEED_100_KHZ);
// Create i/o port instances (note: each device on the same bus should have a different device address)
i2cPort0 <- MCP23008(hardware.i2c89, 0); // pinstrapped to device address 0
i2cPort1 <- MCP23008(hardware.i2c89, 1); // pinstrapped to device address 1
i2cPort2 <- MCP23008(hardware.i2c89, 2); // pinstrapped to device address 2
i2cPort3 <- MCP23008(hardware.i2c89, 3); // pinstrapped to device address 3
i2cPort4 <- MCP23008(hardware.i2c89, 4); // pinstrapped to device address 4
i2cPort5 <- MCP23008(hardware.i2c89, 5); // pinstrapped to device address 5
// Assign leds to port pins
led1 <- i2cPort1.pin1; // MCP23008 has pin1 to pin8
led2 <- i2cPort1.pin2; // MCP23008 has pin1 to pin8
led3 <- i2cPort1.pin3; // MCP23008 has pin1 to pin8
led4 <- i2cPort1.pin4; // MCP23008 has pin1 to pin8
led5 <- i2cPort1.pin5; // MCP23008 has pin1 to pin8
led6 <- i2cPort1.pin6; // MCP23008 has pin1 to pin8
led7 <- i2cPort1.pin7; // MCP23008 has pin1 to pin8
led8 <- i2cPort1.pin8; // MCP23008 has pin1 to pin8
// Configure pins as output
led1.configure(DIGITAL_OUT);
led2.configure(DIGITAL_OUT);
led3.configure(DIGITAL_OUT);
led4.configure(DIGITAL_OUT);
led5.configure(DIGITAL_OUT);
led6.configure(DIGITAL_OUT);
led7.configure(DIGITAL_OUT);
led8.configure(DIGITAL_OUT);
ledTable <-[led1, led2, led3, led4, led5, led6, led7, led8];
numBits <- [“11011101”, “00010001”, “10111100”, “10110101”, “01110001”, “11100101”, “11101101”, “10010001”, “11111101”, “11110001”];
function setNum(addr, number)
{
for (local i = 0; i < numBits[number].len(); i++)
{
ledTable[i].write(numBits[number].slice(i,i+1).tointeger());
}
}
function updateDisplay()
{
local theTime = time().tostring();
setNum(1, theTime.slice(-1).tointeger());
imp.wakeup(1, updateDisplay);
}
updateDisplay();`