From b5e39ca154797fa62784f1369070553055f8c5f6 Mon Sep 17 00:00:00 2001 From: John Berry Date: Tue, 30 Aug 2016 02:22:24 +0000 Subject: [PATCH 1/2] Add support for DrawText in Python module --- .gitignore | 1 + rgbmatrix.cc | 35 ++++++++++++++++++++++++++++++----- texttest.py | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 texttest.py diff --git a/.gitignore b/.gitignore index e6b0faa72..831b8b83a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.o *.a *.so +*.sw[pc] /led-matrix /minimal-example /text-example diff --git a/rgbmatrix.cc b/rgbmatrix.cc index 9fdd545d6..a13dd225b 100644 --- a/rgbmatrix.cc +++ b/rgbmatrix.cc @@ -10,6 +10,7 @@ #include #include #include "led-matrix.h" +#include "graphics.h" using rgb_matrix::GPIO; using rgb_matrix::RGBMatrix; @@ -17,8 +18,8 @@ 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. @@ -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; @@ -122,9 +123,9 @@ static PyObject *SetBuffer(RGBmatrixObject *self, PyObject *data) return NULL; } - for(y=0; ymatrix, font, x, y, 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 }, @@ -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 } }; diff --git a/texttest.py b/texttest.py new file mode 100644 index 000000000..779f86e53 --- /dev/null +++ b/texttest.py @@ -0,0 +1,19 @@ +#!/usr/bin/python + +import sys, time +from rgbmatrix import Adafruit_RGBmatrix + +matrix = Adafruit_RGBmatrix(16, 1) + +matrix.Fill(0xFF0000) +time.sleep(1.0) +matrix.Clear() + +bdf_font_file = "fonts/5x7.bdf" +msg = u'Hi Mom!' +msg.encode('utf-8') +matrix.DrawText(bdf_font_file, 1, 1, 0xFF, 0xFF, 0, msg); +time.sleep(5.0) + +matrix.Clear() +print "good night." From 940b9ad62b760576e8a5f149db28caa9b86c3de2 Mon Sep 17 00:00:00 2001 From: John Berry Date: Wed, 31 Aug 2016 03:23:59 +0000 Subject: [PATCH 2/2] Adjust y position properly based on font height --- rgbmatrix.cc | 2 +- texttest.py | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/rgbmatrix.cc b/rgbmatrix.cc index a13dd225b..423bcb3b0 100644 --- a/rgbmatrix.cc +++ b/rgbmatrix.cc @@ -265,7 +265,7 @@ static PyObject *DrawText(RGBmatrixObject *self, PyObject *arg) { return NULL; } rgb_matrix::DrawText( - self->matrix, font, x, y, color, utf8_text); + self->matrix, font, x, y + font.baseline(), color, utf8_text); } // TODO pass the integer returned by DrawText back to Python diff --git a/texttest.py b/texttest.py index 779f86e53..8a3e34774 100644 --- a/texttest.py +++ b/texttest.py @@ -3,17 +3,15 @@ import sys, time from rgbmatrix import Adafruit_RGBmatrix -matrix = Adafruit_RGBmatrix(16, 1) - -matrix.Fill(0xFF0000) -time.sleep(1.0) -matrix.Clear() +font_width = 4 +font_height = 6 +lines = ["Hi Mom!", "I'm cool."] -bdf_font_file = "fonts/5x7.bdf" -msg = u'Hi Mom!' -msg.encode('utf-8') -matrix.DrawText(bdf_font_file, 1, 1, 0xFF, 0xFF, 0, msg); -time.sleep(5.0) +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() -print "good night."