o Improved directory structure of Python code.

o Use some new features in python examples.
This commit is contained in:
Saij 2015-07-23 23:43:34 -07:00 committed by Henner Zeller
parent 7a0da0e136
commit f293053346
13 changed files with 4777 additions and 780 deletions

View file

@ -1,7 +1,7 @@
PYTHON ?= python
CYTHON ?= cython
SETUP := setup.py
BUILD_ARGS := build
BUILD_ARGS := build --build-lib .
INST_ARGS := install
ifdef DESTDIR
INST_ARGS += --root=$(DESTDIR)
@ -11,6 +11,14 @@ CLEAN_ARGS := clean --all
MANPAGES := $(patsubst %.txt,%,$(wildcard *.txt))
TXTTOMAN := a2x -f manpage
CPP_SOURCES = rgbmatrix/core.cpp rgbmatrix/graphics.cpp
# Where our library resides. It is split between includes and the binary
# library in lib
RGB_LIBDIR=../lib
RGB_LIBRARY_NAME=rgbmatrix
RGB_LIBRARY=$(RGB_LIBDIR)/lib$(RGB_LIBRARY_NAME).a
ifneq "$(wildcard debian/changelog)" ""
PKGNAME := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
VERSION := $(shell dpkg-parsechangelog | sed -n 's/^Version: \([^-]*\).*/\1/p')
@ -22,9 +30,18 @@ all: build
build: build-python build-man
install: install-python install-man
clean: clean-python clean-man
find ./rgbmatrix -type f -name \*.so -delete
find . -type f -name \*.pyc -delete
$(RM) build-* install-* test-*
$(RGB_LIBRARY):
$(MAKE) -C $(RGB_LIBDIR)
# For updating the generated code (not a direct dependency of build-python
# as the generated file is included in the distribution)
%.cpp : %.pyx
$(CYTHON) --cplus -o $@ $^
test: test-python
test-python:
ifneq "$(wildcard tests/*.py)" ""
@ -32,7 +49,6 @@ ifneq "$(wildcard tests/*.py)" ""
else
$(info Test suite is not implemented...)
endif
touch $@
ifneq "$(wildcard debian/control)" ""
PYVERS := $(shell pyversions -r -v debian/control)
@ -41,49 +57,37 @@ BUILD_ARGS += --executable=/usr/bin/$(PYEXEC)
INST_ARGS += --no-compile -O0
build-python: $(PYVERS:%=build-python-%)
build-python-%: $(RGB_LIBRARY)
build-python-%: $(RGB_LIBRARY) $(CPP_SOURCES)
$(info * Doing build for $(PYTHON)$* ...)
$(PYTHON)$* $(SETUP) $(BUILD_ARGS)
touch $@
install-python: $(PYVERS:%=install-python-%)
install-python-%:
$(info * Doing install for $(PYTHON)$* ...)
$(PYTHON)$* $(SETUP) $(INST_ARGS)
touch $@
clean-python: $(PYVERS:%=clean-python-%)
clean-python-%:
$(PYTHON)$* $(SETUP) $(CLEAN_ARGS)
else
build-python:
build-python: $(RGB_LIBRARY) $(CPP_SOURCES)
$(PYTHON) $(SETUP) $(BUILD_ARGS)
touch $@
install-python:
$(PYTHON) $(SETUP) $(INST_ARGS)
touch $@
clean-python:
$(PYTHON) $(SETUP) $(CLEAN_ARGS)
endif
# For updating the generated code (not a direct dependency of build-python
# as the generated file is included in the distribution)
rgbmatrix.cpp : rgbmatrix.pyx
%.cpp : %.pyx
$(CYTHON) --cplus -o $@ $^
build-man: $(MANPAGES:%=build-man-%)
build-man-%: %.txt
$(info * Creating $* ...)
$(TXTTOMAN) $<
touch $@
install-man: $(MANPAGES:%=install-man-%)
install-man-%:
$(info Installation of $* not implemented...)
touch $@
clean-man: $(MANPAGES:%=clean-man-%)
clean-man-%:

View file

@ -1,135 +0,0 @@
# distutils: language = c++
from libcpp cimport bool
from libc.stdint cimport uint8_t, uint32_t
########################
### External classes ###
########################
cdef extern from "gpio.h" namespace "rgb_matrix":
cdef cppclass CPPGPIO "rgb_matrix::GPIO":
CPPGPIO() except +
bool Init()
uint32_t InitOutputs(uint32_t)
void SetBits(uint32_t)
void ClearBits(uint32_t)
void WriteMaskedBits(uint32_t, uint32_t)
void Write(uint32_t)
cdef extern from "led-matrix.h" namespace "rgb_matrix":
cdef cppclass CPPRGBMatrix "rgb_matrix::RGBMatrix":
CPPRGBMatrix(CPPGPIO*, int, int, int) except +
void SetGPIO(CPPGPIO*)
bool SetPWMBits(uint8_t)
uint8_t pwmbits()
void set_luminance_correct(bool)
bool luminance_correct()
int width()
int height()
void SetPixel(int, int, uint8_t, uint8_t, uint8_t)
void Clear()
void Fill(uint8_t, uint8_t, uint8_t)
void SetBrightness(uint8_t)
uint8_t brightness()
CPPFrameCanvas *CreateFrameCanvas()
CPPFrameCanvas *SwapOnVSync(CPPFrameCanvas*)
cdef cppclass CPPFrameCanvas "rgb_matrix::FrameCanvas":
bool SetPWMBits(uint8_t)
uint8_t pwmbits()
int width()
int height()
void SetPixel(int, int, uint8_t, uint8_t, uint8_t)
void Clear()
void Fill(uint8_t, uint8_t, uint8_t)
######################
### Module Classes ###
######################
cdef class FrameCanvas:
cdef CPPFrameCanvas *__canvas
def __dealloc__(self):
if <void*>self.__canvas != NULL:
self.__canvas = NULL
@staticmethod
cdef FrameCanvas __createInternal(CPPFrameCanvas* newCanvas):
canvas = FrameCanvas()
canvas.__canvas = newCanvas
return canvas
cdef CPPFrameCanvas* __getCanvas(self) except *:
if <void*>self.__canvas != NULL:
return self.__canvas
raise Exception("FrameCanvas was destroyed or not initialized, you cannot use this object anymore")
def Fill(self, uint8_t red, uint8_t green, uint8_t blue):
self.__getCanvas().Fill(red, green, blue)
def Clear(self):
self.__getCanvas().Clear()
def SetPixel(self, int x, int y, uint8_t red, uint8_t green, uint8_t blue):
self.__getCanvas().SetPixel(x, y, red, green, blue)
property width:
def __get__(self): return self.__getCanvas().width()
property height:
def __get__(self): return self.__getCanvas().height()
property pwmBits:
def __get__(self): return self.__getCanvas().pwmbits()
def __set__(self, pwmBits): self.__getCanvas().SetPWMBits(pwmBits)
cdef class RGBMatrix:
cdef CPPRGBMatrix *__matrix
cdef CPPGPIO *__gpio
def __cinit__(self, int rows, int chains = 1, int parallel = 1):
self.__gpio = new CPPGPIO()
if not self.__gpio.Init():
raise Exception("Error initializing GPIOs")
self.__matrix = new CPPRGBMatrix(self.__gpio, rows, chains, parallel)
def __dealloc__(self):
self.__matrix.Clear()
del self.__matrix
del self.__gpio
def Fill(self, uint8_t red, uint8_t green, uint8_t blue):
self.__matrix.Fill(red, green, blue)
def SetPixel(self, int x, int y, uint8_t red, uint8_t green, uint8_t blue):
self.__matrix.SetPixel(x, y, red, green, blue)
def Clear(self):
self.__matrix.Clear()
def CreateFrameCanvas(self):
return FrameCanvas.__createInternal(self.__matrix.CreateFrameCanvas())
def SwapOnVSync(self, FrameCanvas newFrame):
return FrameCanvas.__createInternal(self.__matrix.SwapOnVSync(newFrame.__canvas))
property luminanceCorrect:
def __get__(self): return self.__matrix.luminance_correct()
def __set__(self, luminanceCorrect): self.__matrix.set_luminance_correct(luminanceCorrect)
property pwmBits:
def __get__(self): return self.__matrix.pwmbits()
def __set__(self, pwmBits): self.__matrix.SetPWMBits(pwmBits)
property brightness:
def __get__(self): return self.__matrix.brightness()
def __set__(self, brightness): self.__matrix.SetBrightness(brightness)
property height:
def __get__(self): return self.__matrix.height()
property width:
def __get__(self): return self.__matrix.width()

View file

@ -0,0 +1,4 @@
__version__ = "0.0.1"
__author__ = "Christoph Friedrich <christoph.friedrich@vonaffenfels.de>"
from core import RGBMatrix, FrameCanvas

File diff suppressed because it is too large Load diff

11
python/rgbmatrix/core.pxd Normal file
View file

@ -0,0 +1,11 @@
cimport cppinc
cdef class Canvas:
cdef cppinc.Canvas *__getCanvas(self) except +
cdef class FrameCanvas(Canvas):
cdef cppinc.FrameCanvas *__canvas
cdef class RGBMatrix(Canvas):
cdef cppinc.RGBMatrix *__matrix
cdef cppinc.GPIO *__gpio

94
python/rgbmatrix/core.pyx Normal file
View file

@ -0,0 +1,94 @@
# distutils: language = c++
from libcpp cimport bool
from libc.stdint cimport uint8_t, uint32_t
cdef class Canvas:
cdef cppinc.Canvas* __getCanvas(self) except +:
raise Exception("Not implemented")
cdef class FrameCanvas(Canvas):
def __dealloc__(self):
if <void*>self.__canvas != NULL:
self.__canvas = NULL
cdef cppinc.Canvas* __getCanvas(self) except *:
if <void*>self.__canvas != NULL:
return self.__canvas
raise Exception("Canvas was destroyed or not initialized, you cannot use this object anymore")
def Fill(self, uint8_t red, uint8_t green, uint8_t blue):
(<cppinc.FrameCanvas*>self.__getCanvas()).Fill(red, green, blue)
def Clear(self):
(<cppinc.FrameCanvas*>self.__getCanvas()).Clear()
def SetPixel(self, int x, int y, uint8_t red, uint8_t green, uint8_t blue):
(<cppinc.FrameCanvas*>self.__getCanvas()).SetPixel(x, y, red, green, blue)
property width:
def __get__(self): return (<cppinc.FrameCanvas*>self.__getCanvas()).width()
property height:
def __get__(self): return (<cppinc.FrameCanvas*>self.__getCanvas()).height()
property pwmBits:
def __get__(self): return (<cppinc.FrameCanvas*>self.__getCanvas()).pwmbits()
def __set__(self, pwmBits): (<cppinc.FrameCanvas*>self.__getCanvas()).SetPWMBits(pwmBits)
cdef class RGBMatrix(Canvas):
def __cinit__(self, int rows, int chains = 1, int parallel = 1):
self.__gpio = new cppinc.GPIO()
if not self.__gpio.Init():
raise Exception("Error initializing GPIOs")
self.__matrix = new cppinc.RGBMatrix(self.__gpio, rows, chains, parallel)
def __dealloc__(self):
self.__matrix.Clear()
del self.__matrix
del self.__gpio
cdef cppinc.Canvas* __getCanvas(self) except *:
if <void*>self.__matrix != NULL:
return self.__matrix
raise Exception("Canvas was destroyed or not initialized, you cannot use this object anymore")
def Fill(self, uint8_t red, uint8_t green, uint8_t blue):
self.__matrix.Fill(red, green, blue)
def SetPixel(self, int x, int y, uint8_t red, uint8_t green, uint8_t blue):
self.__matrix.SetPixel(x, y, red, green, blue)
def Clear(self):
self.__matrix.Clear()
def CreateFrameCanvas(self):
return __createFrameCanvas(self.__matrix.CreateFrameCanvas())
def SwapOnVSync(self, FrameCanvas newFrame):
return __createFrameCanvas(self.__matrix.SwapOnVSync(newFrame.__canvas))
property luminanceCorrect:
def __get__(self): return self.__matrix.luminance_correct()
def __set__(self, luminanceCorrect): self.__matrix.set_luminance_correct(luminanceCorrect)
property pwmBits:
def __get__(self): return self.__matrix.pwmbits()
def __set__(self, pwmBits): self.__matrix.SetPWMBits(pwmBits)
property brightness:
def __get__(self): return self.__matrix.brightness()
def __set__(self, brightness): self.__matrix.SetBrightness(brightness)
property height:
def __get__(self): return self.__matrix.height()
property width:
def __get__(self): return self.__matrix.width()
cdef __createFrameCanvas(cppinc.FrameCanvas* newCanvas):
canvas = FrameCanvas()
canvas.__canvas = newCanvas
return canvas

View file

@ -0,0 +1,60 @@
from libcpp cimport bool
from libc.stdint cimport uint8_t, uint32_t
########################
### External classes ###
########################
cdef extern from "canvas.h" namespace "rgb_matrix":
cdef cppclass Canvas:
int width()
int height()
void SetPixel(int, int, uint8_t, uint8_t, uint8_t)
void Clear()
void Fill(uint8_t, uint8_t, uint8_t)
cdef extern from "gpio.h" namespace "rgb_matrix":
cdef cppclass GPIO:
GPIO() except +
bool Init()
uint32_t InitOutputs(uint32_t)
void SetBits(uint32_t)
void ClearBits(uint32_t)
void WriteMaskedBits(uint32_t, uint32_t)
void Write(uint32_t)
cdef extern from "led-matrix.h" namespace "rgb_matrix":
cdef cppclass RGBMatrix(Canvas):
RGBMatrix(GPIO*, int, int, int) except +
void SetGPIO(GPIO*)
bool SetPWMBits(uint8_t)
uint8_t pwmbits()
void set_luminance_correct(bool)
bool luminance_correct()
void SetBrightness(uint8_t)
uint8_t brightness()
FrameCanvas *CreateFrameCanvas()
FrameCanvas *SwapOnVSync(FrameCanvas*)
cdef cppclass FrameCanvas(Canvas):
bool SetPWMBits(uint8_t)
uint8_t pwmbits()
cdef extern from "graphics.h" namespace "rgb_matrix":
cdef struct Color:
Color(uint8_t, uint8_t, uint8_t) except +
uint8_t r
uint8_t g
uint8_t b
cdef cppclass Font:
Font() except +
bool LoadFont(const char*)
int height()
int baseline()
int CharacterWidth(uint32_t)
int DrawGlyph(Canvas*, int, int, const Color, uint32_t);
cdef int DrawText(Canvas*, const Font, int, int, const Color, const char*)
cdef void DrawCircle(Canvas*, int, int, int, const Color)
cdef void DrawLine(Canvas*, int, int, int, int, const Color)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,7 @@
cimport cppinc
cdef class Color:
cdef cppinc.Color __color
cdef class Font:
cdef cppinc.Font __font

View file

@ -0,0 +1,44 @@
# distutils: language = c++
from libcpp cimport bool
from libc.stdint cimport uint8_t, uint32_t
cimport core
cdef class Color:
property red:
def __get__(self): return self.__color.r
def __set__(self, uint8_t value): self.__color.r = value
property green:
def __get__(self): return self.__color.g
def __set__(self, uint8_t value): self.__color.g = value
property blue:
def __get__(self): return self.__color.b
def __set__(self, uint8_t value): self.__color.b = value
cdef class Font:
def CharacterWidth(self, uint32_t char):
return self.__font.CharacterWidth(char)
def LoadFont(self, const char* file):
return self.__font.LoadFont(file)
def DrawGlyph(self, core.Canvas c, int x, int y, Color color, uint32_t char):
return self.__font.DrawGlyph(c.__getCanvas(), x, y, color.__color, char)
property height:
def __get__(self): return self.__font.height()
property baseline:
def __get__(self): return self.__font.baseline()
def DrawText(core.Canvas c, Font f, int x, int y, Color color, const char* text):
return cppinc.DrawText(c.__getCanvas(), f.__font, x, y, color.__color, text)
def DrawCircle(core.Canvas c, int x, int y, int r, Color color):
cppinc.DrawCircle(c.__getCanvas(), x, y, r, color.__color)
def DrawLine(core.Canvas c, int x1, int y1, int x2, int y2, Color color):
cppinc.DrawLine(c.__getCanvas(), x1, y1, x2, y2, color.__color)

View file

@ -1,4 +1,6 @@
import argparse, time, sys
import argparse, time, sys, os
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/..'))
from rgbmatrix import RGBMatrix
class SampleBase(argparse.ArgumentParser):

View file

@ -6,18 +6,20 @@ class SimpleSquare(SampleBase):
super(SimpleSquare, self).__init__(*args, **kwargs)
def Run(self):
offsetCanvas = self.matrix.CreateFrameCanvas()
while True:
for x in range(0, self.matrix.width):
self.matrix.SetPixel(x, x, 255, 255, 255)
self.matrix.SetPixel(self.matrix.height - 1 - x, x, 255, 0, 255)
offsetCanvas.SetPixel(x, x, 255, 255, 255)
offsetCanvas.SetPixel(offsetCanvas.height - 1 - x, x, 255, 0, 255)
for x in range(0, self.matrix.width):
self.matrix.SetPixel(x, 0, 255, 0, 0)
self.matrix.SetPixel(x, self.matrix.height - 1, 255, 255, 0)
for x in range(0, offsetCanvas.width):
offsetCanvas.SetPixel(x, 0, 255, 0, 0)
offsetCanvas.SetPixel(x, offsetCanvas.height - 1, 255, 255, 0)
for y in range(0, self.matrix.height):
self.matrix.SetPixel(0, y, 0, 0, 255)
self.matrix.SetPixel(self.matrix.width - 1, y, 0, 255, 0)
for y in range(0, offsetCanvas.height):
offsetCanvas.SetPixel(0, y, 0, 0, 255)
offsetCanvas.SetPixel(offsetCanvas.width - 1, y, 0, 255, 0)
offsetCanvas = self.matrix.SwapOnVSync(offsetCanvas)
# Main function

View file

@ -1,22 +1,33 @@
#!/usr/bin/python
from distutils.core import setup, Extension
matrix_ext = Extension(
name = 'rgbmatrix',
sources = ['rgbmatrix.cpp'],
core_ext = Extension(
name = 'core',
sources = ['rgbmatrix/core.cpp'],
include_dirs = ['../include'],
library_dirs = ['../lib'],
libraries = ['rgbmatrix'],
extra_compile_args = ["-O3", "-Wall"],
language = 'c++'
)
graphics_ext = Extension(
name = 'graphics',
sources = ['rgbmatrix/graphics.cpp'],
include_dirs = ['../include'],
library_dirs = ['../lib'],
libraries = ['rgbmatrix'],
extra_compile_args = ["-O3", "-Wall"],
language = 'c++'
)
setup(
name = 'RGBMatrix Python Library',
name = 'rgbmatrix',
version = '0.0.1',
author = 'Christoph Friedrich',
author_email = 'christoph.friedrich@vonaffenfels.de',
classifiers = ['Development Status :: 3 - Alpha'],
ext_modules = [
matrix_ext
],
ext_package = 'rgbmatrix',
ext_modules = [core_ext, graphics_ext],
packages = ['rgbmatrix']
)