Control LED matrix over serial

From openframeworks to adafruit i2c LED backpack

First check out adafruits guides they will get you most of the way. What I was missing was the pieces to control the whole thing over serial.

I am running 2 single color LED backpacks side by side, controlling them from an 16x8 image in openframeworks.

This is the code on the arduino board.

In openframewors I am using a framebuffer to make it easy to draw to the matrix. Declare in the header:

void updateMatrix(int display);

ofSerial serial;
ofImage matrix;
ofFbo matrixFbo;
ofPixels tmpPix;

In setup allocate the images and open the serial connection:

matrix.allocate(16, 8, OF_IMAGE_GRAYSCALE);
matrixFbo.allocate(16, 8);
tmpPix.allocate(16, 8, OF_IMAGE_GRAYSCALE);

serial.listDevices();
vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();

int baud = 9600;
serial.setup(0, baud);

The updateMatrix function looks like this, call it once for each matrix:

// update 2 8x8 matrixes from 16x8 image
void testApp::updateMatrix(int display) {

unsigned char bytes[10];
int cO = 0;
// displays are flipped was easy to fix here
if(display == 1) { bytes[8] = 2; }
if(display == 2) { bytes[8] = 1; cO = 8; }

for(int r=0; r<matrix1.getHeight(); r++) {

    char val = 0;
    for(int c=0; c<8; c++) {
        if(matrix1.getColor(r+cO, c).getBrightness() > 128) {
            val |= 0x01 << c;
        }
    }
    bytes[r] = val;
}
bytes[9] = '\n';
serial.writeBytes(bytes, 10);
}

Drawing to the matrix can look like this:

matrixFbo.begin();

ofBackground(0);
ofSetColor(255);

// happy smiley

ofNoFill();
 ofCircle(4, 4, 3.5);

 glBegin(GL_POINTS);
 glVertex2d(2, 2);
 glVertex2d(5, 2);
 glVertex2d(5, 4);
 glVertex2d(4, 5);
 glVertex2d(3, 5);
 glVertex2d(2, 4);
 glEnd();

matrixFbo.end();