Skip to content

Commit e2ffd91

Browse files
danielvyhzeller
authored andcommitted
Python matrix options (hzeller#268)
* Python - Added support for creating RGBMatrix with options. * RGBMatrix class initializer can accept either previous parameters (rows, chains, parallel; now optional) or RGBMatrixOptions * Modified samplebase.py to initialize RGBMatrix with RGBMatrixOptions built from command line arguments * Removed GPIO structure. This is now created internally. * Python - Renamed command line arguments names for samples to be consistent with the C++ arguments.
1 parent 4445552 commit e2ffd91

File tree

8 files changed

+3322
-860
lines changed

8 files changed

+3322
-860
lines changed

python/README.md

100644100755
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Building
55
--------
66

77
If you have a different than the standard wiring (for instance if you have an
8-
Adafruit HAT), edit the [../lib/Makefile](../lib/Makefile#L26) first to choose
9-
the hardware in question.
8+
Adafruit HAT), you can edit the [../lib/Makefile](../lib/Makefile#L26) first to choose
9+
the hardware in question (see below for setting it via command line argument).
1010

1111
Then, in the root directory for the matrix library simply type:
1212

@@ -93,6 +93,12 @@ cd samples
9393
sudo ./runtext.py --chain=4
9494
```
9595

96+
To use different wiring without recompiling the library to change the default,
97+
you can use `--hardware_mapping` (or `-m`). For example, to use Adafruit HAT:
98+
```bash
99+
sudo ./runtext.py --hardware_mapping=adafruit-hat
100+
```
101+
96102
Here a complete example how to write an image viewer:
97103
```python
98104
#!/usr/bin/env python

python/rgbmatrix/__init__.py

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
__version__ = "0.0.1"
55
__author__ = "Christoph Friedrich <christoph.friedrich@vonaffenfels.de>"
66

7-
from .core import RGBMatrix, FrameCanvas
7+
from .core import RGBMatrix, FrameCanvas, RGBMatrixOptions

python/rgbmatrix/core.cpp

Lines changed: 2531 additions & 405 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/rgbmatrix/core.pxd

100644100755
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ cdef class FrameCanvas(Canvas):
88

99
cdef class RGBMatrix(Canvas):
1010
cdef cppinc.RGBMatrix *__matrix
11-
cdef cppinc.GPIO *__gpio
11+
12+
cdef class RGBMatrixOptions:
13+
cdef cppinc.Options __options
14+
cdef cppinc.RuntimeOptions __runtime_options
15+
# Must keep a reference to the encoded bytes for hardware_mapping string
16+
# otherwise, when the Options struct is used, it will be garbage collected
17+
cdef bytes __py_encoded_hardware_mapping
18+
1219

1320
# Local Variables:
1421
# mode: python

python/rgbmatrix/core.pyx

100644100755
Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,99 @@ cdef class FrameCanvas(Canvas):
8383
def __get__(self): return (<cppinc.FrameCanvas*>self.__getCanvas()).pwmbits()
8484
def __set__(self, pwmBits): (<cppinc.FrameCanvas*>self.__getCanvas()).SetPWMBits(pwmBits)
8585

86+
cdef class RGBMatrixOptions:
87+
def __cinit__(self):
88+
self.__options = cppinc.Options()
89+
self.__runtime_options = cppinc.RuntimeOptions()
90+
91+
# RGBMatrix::Options properties
92+
property hardware_mapping:
93+
def __get__(self): return self.__options.hardware_mapping
94+
def __set__(self, value):
95+
self.__py_encoded_hardware_mapping = value.encode('utf-8')
96+
self.__options.hardware_mapping = self.__py_encoded_hardware_mapping
97+
98+
property rows:
99+
def __get__(self): return self.__options.rows
100+
def __set__(self, uint8_t value): self.__options.rows = value
101+
102+
property chain_length:
103+
def __get__(self): return self.__options.chain_length
104+
def __set__(self, uint8_t value): self.__options.chain_length = value
105+
106+
property parallel:
107+
def __get__(self): return self.__options.parallel
108+
def __set__(self, uint8_t value): self.__options.parallel = value
109+
110+
property pwm_bits:
111+
def __get__(self): return self.__options.pwm_bits
112+
def __set__(self, uint8_t value): self.__options.pwm_bits = value
113+
114+
property pwm_lsb_nanoseconds:
115+
def __get__(self): return self.__options.pwm_lsb_nanoseconds
116+
def __set__(self, uint32_t value): self.__options.pwm_lsb_nanoseconds = value
117+
118+
property brightness:
119+
def __get__(self): return self.__options.brightness
120+
def __set__(self, uint8_t value): self.__options.brightness = value
121+
122+
property scan_mode:
123+
def __get__(self): return self.__options.scan_mode
124+
def __set__(self, uint8_t value): self.__options.scan_mode = value
125+
126+
property disable_hardware_pulsing:
127+
def __get__(self): return self.__options.disable_hardware_pulsing
128+
def __set__(self, value): self.__options.disable_hardware_pulsing = value
129+
130+
property show_refresh_rate:
131+
def __get__(self): return self.__options.show_refresh_rate
132+
def __set__(self, value): self.__options.show_refresh_rate = value
133+
134+
property swap_green_blue:
135+
def __get__(self): return self.__options.swap_green_blue
136+
def __set__(self, value): self.__options.swap_green_blue = value
137+
138+
property inverse_colors:
139+
def __get__(self): return self.__options.inverse_colors
140+
def __set__(self, value): self.__options.inverse_colors = value
141+
142+
# RuntimeOptions properties
143+
144+
property gpio_slowdown:
145+
def __get__(self): return self.__runtime_options.gpio_slowdown
146+
def __set__(self, uint8_t value): self.__runtime_options.gpio_slowdown = value
147+
148+
property daemon:
149+
def __get__(self): return self.__runtime_options.daemon
150+
def __set__(self, uint8_t value): self.__runtime_options.daemon = value
151+
152+
property drop_privileges:
153+
def __get__(self): return self.__runtime_options.drop_privileges
154+
def __set__(self, uint8_t value): self.__runtime_options.drop_privileges = value
155+
86156

87157
cdef class RGBMatrix(Canvas):
88-
def __cinit__(self, int rows, int chains = 1, int parallel = 1):
89-
# TODO(Saij): this should initialize an RGBMatrix::Options and
90-
# RuntimeOptions, then call CreateMatrixFromOptions() instead of the
91-
# cppinc.RGBMatrix() constructor directly. No __gpio needed anymore.
92-
# The options allow to set more things, so they should probably be
93-
# available as named parameters in Python ?
94-
self.__gpio = new cppinc.GPIO()
95-
if not self.__gpio.Init():
96-
raise Exception("Error initializing GPIOs") # will segfault?!
97-
self.__matrix = new cppinc.RGBMatrix(self.__gpio, rows, chains, parallel)
158+
def __cinit__(self, int rows = 0, int chains = 0, int parallel = 0,
159+
RGBMatrixOptions options = None):
160+
161+
# If RGBMatrixOptions not provided, create defaults and set any optional
162+
# parameters supplied
163+
if options == None:
164+
options = RGBMatrixOptions()
165+
166+
if rows > 0:
167+
options.rows = rows
168+
if chains > 0:
169+
options.chain_length = chains
170+
if parallel > 0:
171+
options.parallel = parallel
172+
173+
self.__matrix = cppinc.CreateMatrixFromOptions(options.__options,
174+
options.__runtime_options)
98175

99176
def __dealloc__(self):
100177
self.__matrix.Clear()
101178
del self.__matrix
102-
del self.__gpio
103179

104180
cdef cppinc.Canvas* __getCanvas(self) except *:
105181
if <void*>self.__matrix != NULL:

python/rgbmatrix/cppinc.pxd

100644100755
Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,8 @@ cdef extern from "canvas.h" namespace "rgb_matrix":
1313
void Clear() nogil
1414
void Fill(uint8_t, uint8_t, uint8_t) nogil
1515

16-
cdef extern from "gpio.h" namespace "rgb_matrix":
17-
cdef cppclass GPIO:
18-
GPIO() except +
19-
bool Init()
20-
uint32_t InitOutputs(uint32_t)
21-
void SetBits(uint32_t)
22-
void ClearBits(uint32_t)
23-
void WriteMaskedBits(uint32_t, uint32_t)
24-
void Write(uint32_t)
25-
2616
cdef extern from "led-matrix.h" namespace "rgb_matrix":
2717
cdef cppclass RGBMatrix(Canvas):
28-
RGBMatrix(GPIO*, int, int, int) except +
29-
void SetGPIO(GPIO*)
3018
bool SetPWMBits(uint8_t)
3119
uint8_t pwmbits()
3220
void set_luminance_correct(bool)
@@ -40,6 +28,37 @@ cdef extern from "led-matrix.h" namespace "rgb_matrix":
4028
bool SetPWMBits(uint8_t)
4129
uint8_t pwmbits()
4230

31+
struct RuntimeOptions:
32+
RuntimeOptions() except +
33+
int gpio_slowdown
34+
int daemon
35+
int drop_privileges
36+
37+
38+
RGBMatrix *CreateMatrixFromOptions(Options &options, RuntimeOptions runtime_options)
39+
40+
41+
42+
cdef extern from "led-matrix.h" namespace "rgb_matrix::RGBMatrix":
43+
cdef struct Options:
44+
Options() except +
45+
46+
const char *hardware_mapping
47+
48+
int rows
49+
int chain_length
50+
int parallel
51+
int pwm_bits
52+
int pwm_lsb_nanoseconds
53+
int brightness
54+
int scan_mode
55+
56+
bool disable_hardware_pulsing
57+
bool show_refresh_rate
58+
bool swap_green_blue
59+
bool inverse_colors
60+
61+
4362
cdef extern from "graphics.h" namespace "rgb_matrix":
4463
cdef struct Color:
4564
Color(uint8_t, uint8_t, uint8_t) except +

0 commit comments

Comments
 (0)