forked from hzeller/rpi-rgb-led-matrix
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathrgbmatrix.cc
316 lines (275 loc) · 8.73 KB
/
rgbmatrix.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*------------------------------------------------------------------------
Python module to control Adafruit RGB Matrix HAT for Raspberry Pi.
Very very simple at the moment...just a wrapper around the SetPixel,
Fill, Clear and SetPWMBits functions of the librgbmatrix library.
Graphics primitives can be handled using the Python Imaging Library
and the SetImage method.
------------------------------------------------------------------------*/
#include <python2.7/Python.h>
#include <python2.7/Imaging.h>
#include "led-matrix.h"
using rgb_matrix::GPIO;
using rgb_matrix::RGBMatrix;
static GPIO io;
typedef struct { // Python object for matrix
PyObject_HEAD
RGBMatrix *matrix;
} RGBmatrixObject;
// Rows & chained display values are currently both required parameters.
// Might expand this to handle kwargs, etc.
static PyObject *RGBmatrix_new(
PyTypeObject *type, PyObject *arg, PyObject *kw) {
RGBmatrixObject *self = NULL;
int rows, chain;
if((PyTuple_Size(arg) == 2) &&
PyArg_ParseTuple(arg, "II", &rows, &chain)) {
if((self = (RGBmatrixObject *)type->tp_alloc(type, 0))) {
self->matrix = new RGBMatrix(&io, rows, chain);
Py_INCREF(self);
}
}
return (PyObject *)self;
}
static int RGBmatrix_init(RGBmatrixObject *self, PyObject *arg) {
self->matrix->Clear();
return 0;
}
static void RGBmatrix_dealloc(RGBmatrixObject *self) {
delete self->matrix;
self->ob_type->tp_free((PyObject *)self);
}
static PyObject *Clear(RGBmatrixObject *self) {
self->matrix->Clear();
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *Fill(RGBmatrixObject *self, PyObject *arg) {
uint32_t c;
uint8_t r, g, b;
int status=0;
switch(PyTuple_Size(arg)) {
case 1: // Packed color
if((status = PyArg_ParseTuple(arg, "I", &c))) {
r = (c >> 16) & 0xFF;
g = (c >> 8) & 0xFF;
b = c & 0xFF;
}
break;
case 3: // R, G, B
status = PyArg_ParseTuple(arg, "BBB", &r, &g, &b);
break;
}
if(status) self->matrix->Fill(r, g, b);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *SetPixel(RGBmatrixObject *self, PyObject *arg) {
uint32_t x, y, c;
uint8_t r, g, b;
int status=0;
switch(PyTuple_Size(arg)) {
case 3: // X, Y, packed color
if((status = PyArg_ParseTuple(arg, "IIBBB", &x, &y, &c))) {
r = (c >> 16) & 0xFF;
g = (c >> 8) & 0xFF;
b = c & 0xFF;
}
break;
case 5: // X, Y, R, G, B
status = PyArg_ParseTuple(arg, "IIBBB", &x, &y, &r, &g, &b);
break;
}
if(status) self->matrix->SetPixel(x, y, r, g, b);
Py_INCREF(Py_None);
return Py_None;
}
// Copy whole display buffer to display from a list of bytes [R1,G1,B1,R2,G2,B2...]
static PyObject *SetBuffer(RGBmatrixObject *self, PyObject *data)
{
Py_ssize_t count;
int w, h, offset, y, x;
uint8_t r, g, b;
count = PyList_Size(data);
w = self->matrix->width(); // Matrix dimensions
h = self->matrix->height();
if(count != w*h*3)
{
PyErr_SetString(PyExc_ValueError,
"Data buffer incorrect size.");
return NULL;
}
for(y=0; y<h; y++)
{
for(x=0; x<w; x++)
{
offset = (y*w*3)+(x*3);
r = PyInt_AsLong(PyList_GetItem(data, offset));
g = PyInt_AsLong(PyList_GetItem(data, offset+1));
b = PyInt_AsLong(PyList_GetItem(data, offset+2));
self->matrix->SetPixel(x, y, r, g, b);
}
}
Py_INCREF(Py_None);
return Py_None;
}
// Copy Python Image to matrix. Not all modes are supported;
// RGB (or RGBA, but alpha is ignored), 8-bit paletted and 1-bit.
static PyObject *SetImage(RGBmatrixObject *self, PyObject *arg) {
Imaging im;
long id;
int status=0, mw, mh, sx=0, sy=0, dx=0, dy=0, n, x, y, w, h;
uint32_t c;
uint8_t r, g, b;
switch(PyTuple_Size(arg)) {
case 1: // Image ID
status = PyArg_ParseTuple(arg, "L", &id);
break;
case 3: // Image ID, dest X & Y
status = PyArg_ParseTuple(arg, "LII", &id, &dx, &dy);
break;
}
if(status) {
im = (Imaging)id; // -> Imaging struct
mw = self->matrix->width(); // Matrix dimensions
mh = self->matrix->height();
w = im->xsize;
h = im->ysize;
n = dx + w - 1; // x2
if(n >= mw) w -= n - mw + 1; // Clip right
n = dy + h - 1; // y2
if(n >= mh) h -= n - mh + 1; // Clip bottom
if(dx < 0) { // Clip left
w += dx;
sx -= dx;
dx = 0;
}
if(dy < 0) { // Clip top
h += dy;
sy -= dy;
dy = 0;
}
if((w > 0) && (h > 0)) {
if(!strncasecmp(im->mode, "RGB", 3)) {
// RGB image; alpha ignored if present
for(y=0; y<h; y++) {
for(x=0; x<w; x++) {
c = *(uint32_t *)(
&IMAGING_PIXEL_RGB(
im, sx + x, sy + y));
b = (c >> 16) & 0xFF;
g = (c >> 8) & 0xFF;
r = c & 0xFF;
self->matrix->SetPixel(
dx + x, dy + y, r, g, b);
}
}
} else if(!strcasecmp(im->mode, "1")) {
// Bitmap image
for(y=0; y<h; y++) {
for(x=0; x<w; x++) {
r = IMAGING_PIXEL_1(
im, sx + x, sy + y) ? 255 : 0;
self->matrix->SetPixel(
dx + x, dy + y, r, r, r);
}
}
} else if((!strcasecmp(im->mode, "P")) &&
(!strncasecmp(im->palette->mode, "RGB", 3))) {
// Paletted image (w/RGB palette)
for(y=0; y<h; y++) {
for(x=0; x<w; x++) {
c = IMAGING_PIXEL_P(
im, sx + x, sy + y) * 4;
r = im->palette->palette[c];
g = im->palette->palette[c+1];
b = im->palette->palette[c+2];
self->matrix->SetPixel(
dx + x, dy + y, r, g, b);
}
}
} else {
// Unsupported image mode
}
}
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *SetPWMBits(RGBmatrixObject *self, PyObject *arg) {
uint8_t b;
if((PyTuple_Size(arg) == 1) && PyArg_ParseTuple(arg, "B", &b)) {
self->matrix->SetPWMBits(b);
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *SetWriteCycles(RGBmatrixObject *self, PyObject *arg) {
uint8_t b;
if((PyTuple_Size(arg) == 1) && PyArg_ParseTuple(arg, "B", &b)) {
io.writeCycles = b;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef methods[] = {
{ "Clear" , (PyCFunction)Clear , METH_NOARGS , NULL },
{ "Fill" , (PyCFunction)Fill , METH_VARARGS, NULL },
{ "SetBuffer" , (PyCFunction)SetBuffer , METH_O, NULL },
{ "SetPixel" , (PyCFunction)SetPixel , METH_VARARGS, NULL },
{ "SetImage" , (PyCFunction)SetImage , METH_VARARGS, NULL },
{ "SetPWMBits" , (PyCFunction)SetPWMBits , METH_VARARGS, NULL },
{ "SetWriteCycles", (PyCFunction)SetWriteCycles, METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};
static PyTypeObject RGBmatrixObjectType = {
PyObject_HEAD_INIT(NULL)
0, // ob_size (not used, always set to 0)
"rgbmatrix.Adafruit_RGBmatrix", // tp_name (module name, object name)
sizeof(RGBmatrixObject), // tp_basicsize
0, // tp_itemsize
(destructor)RGBmatrix_dealloc, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
0, // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
methods, // tp_methods
0, // tp_members
0, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
(initproc)RGBmatrix_init, // tp_init
0, // tp_alloc
RGBmatrix_new, // tp_new
0, // tp_free
};
PyMODINIT_FUNC initrgbmatrix(void) { // Module initialization function
PyObject* m;
if(io.Init() && // Set up GPIO pins. MUST run as root.
(m = Py_InitModule("rgbmatrix", methods)) &&
(PyType_Ready(&RGBmatrixObjectType) >= 0)) {
Py_INCREF(&RGBmatrixObjectType);
PyModule_AddObject(m, "Adafruit_RGBmatrix",
(PyObject *)&RGBmatrixObjectType);
}
}