Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.o
*.a
*.so
*.sw[pc]
/led-matrix
/minimal-example
/text-example
35 changes: 30 additions & 5 deletions rgbmatrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
#include <python2.7/Python.h>
#include <python2.7/Imaging.h>
#include "led-matrix.h"
#include "graphics.h"

using rgb_matrix::GPIO;
using rgb_matrix::RGBMatrix;

static GPIO io;

typedef struct { // Python object for matrix
PyObject_HEAD
RGBMatrix *matrix;
PyObject_HEAD
RGBMatrix *matrix;
} RGBmatrixObject;

// Rows & chained display values are currently both required parameters.
Expand Down Expand Up @@ -105,7 +106,7 @@ static PyObject *SetPixel(RGBmatrixObject *self, PyObject *arg) {
}

// Copy whole display buffer to display from a list of bytes [R1,G1,B1,R2,G2,B2...]
static PyObject *SetBuffer(RGBmatrixObject *self, PyObject *data)
static PyObject *SetBuffer(RGBmatrixObject *self, PyObject *data)
{
Py_ssize_t count;
int w, h, offset, y, x;
Expand All @@ -122,9 +123,9 @@ static PyObject *SetBuffer(RGBmatrixObject *self, PyObject *data)
return NULL;
}

for(y=0; y<h; y++)
for(y=0; y<h; y++)
{
for(x=0; x<w; x++)
for(x=0; x<w; x++)
{
offset = (y*w*3)+(x*3);
r = PyInt_AsLong(PyList_GetItem(data, offset));
Expand Down Expand Up @@ -249,6 +250,29 @@ static PyObject *SetWriteCycles(RGBmatrixObject *self, PyObject *arg) {
return Py_None;
}

static PyObject *DrawText(RGBmatrixObject *self, PyObject *arg) {
const char *bdf_font_file = NULL;
rgb_matrix::Font font;
uint32_t x, y;
uint8_t r, g, b;
char *utf8_text; // NOTE this should be UTF-8 encoded text
if((PyTuple_Size(arg) == 7) &&
PyArg_ParseTuple(arg, "sIIBBBs", &bdf_font_file, &x, &y, &r, &g, &b, &utf8_text)) {
rgb_matrix::Color color(r, g, b);
if (!font.LoadFont(bdf_font_file)) {
PyErr_SetString(PyExc_ValueError,
"Can not read BDF font file.");
return NULL;
}
rgb_matrix::DrawText(
self->matrix, font, x, y + font.baseline(), color, utf8_text);
}
// TODO pass the integer returned by DrawText back to Python

Py_INCREF(Py_None);
return Py_None;
}

static PyMethodDef methods[] = {
{ "Clear" , (PyCFunction)Clear , METH_NOARGS , NULL },
{ "Fill" , (PyCFunction)Fill , METH_VARARGS, NULL },
Expand All @@ -257,6 +281,7 @@ static PyMethodDef methods[] = {
{ "SetImage" , (PyCFunction)SetImage , METH_VARARGS, NULL },
{ "SetPWMBits" , (PyCFunction)SetPWMBits , METH_VARARGS, NULL },
{ "SetWriteCycles", (PyCFunction)SetWriteCycles, METH_VARARGS, NULL },
{ "DrawText" , (PyCFunction)DrawText , METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};

Expand Down
17 changes: 17 additions & 0 deletions texttest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/python

import sys, time
from rgbmatrix import Adafruit_RGBmatrix

font_width = 4
font_height = 6
lines = ["Hi Mom!", "I'm cool."]

matrix = Adafruit_RGBmatrix(16, 1)
bdf_font_file = "fonts/{0}x{1}.bdf".format(font_width, font_height)
matrix.DrawText(bdf_font_file, 1, 1, 0xFF, 0xFF, 0, lines[0])
time.sleep(2.0)
matrix.DrawText(bdf_font_file, 1, 1 + font_height, 0xFF, 0, 0xFF, lines[1])
time.sleep(10.0)

matrix.Clear()