User avatar
Posts: 219
Joined: Wed Jan 25, 2012 9:39 pm

raspberrypi-multimedia python module

Mon Jun 25, 2012 10:05 pm

Since my new module series can (somewhat) show graphics and read user input, I thought it'd be worthwhile to show it here.


Here's a sample how it provides input to the user:

Code: Select all

from eventloop import EventLoop, InputManager

devices = InputManager()

def onadd(device):
    print "[*] %r" % device
    def onevent(timestamp, type, code, value):
        print device, timestamp, type, code, value
    device.onevent = onevent

def onremove(device):
    print "[ ] %r" % device

devices.onadd = onadd
devices.onremove = onremove

eventloop = EventLoop(devices) / 60.0)
Here's some more sample about how it provides video to the user. The input and video modules are separate, because they can. :twisted:

Code: Select all

# Copyright (c) 2012 Cheery
# See the file license.txt for copying permission.
import sys, videosys, oes, time
from array import array
from PIL import Image

image ='logo.png')
image_width, image_height = image.size
image_data = image.tostring()

image_attr = (image_width, image_height, image_data)

fsrc = open("fragment.shader").read()
vsrc = open("vertex.shader").read()

triangle_data = [
    0.0, 0.0, 0.0,   0.0, 0.0,
    0.5, 0.0, 0.0,   1.0, 0.0,
    0.5, 0.5, 0.0,   1.0, 1.0,

    0.0, 0.0, 0.0,   0.0, 0.0,
    0.5, 0.5, 0.0,   1.0, 1.0,
    0.0, 0.5, 0.0,   0.0, 1.0,
data = array('f', triangle_data).tostring()

display = videosys.Display()
context = display.create_context()
surface = display.create_window(videosys.fullscreen(0))

triangle = oes.Drawable(oes.TRIANGLES, dict(


texture = oes.Texture2D()

program = oes.Shader((fsrc, vsrc))

oes.clearColor(0.15, 0.25, 0.35, 1.0)

program.i('texture', 0)

triangle.draw(program, 0, 6)



#unused features:
# display.create_global_pixmap(width, height)  -  create pixmap that other processes can read, returning surface object has .handle you can read.
# image = display.create_global_image(handle)  -  access the global image
# image.target_texture(         -  make a texture from the global image, (the texture must be bound)

# there's more details in the sources, if you dare to look into them.
I hacked the most of this together last weekend, but the input system is a result of today's evening session. Idea was to hack up something that'd feel nice to use. Didn't write a better key processing interface, sorry. The input that you get is somewhat sane still though!

It even does one cute thing no other modules are doing yet for you. It can be used to open global pixbuffer objects, which can be used as texture targets by other processes that have the handle. It's the magic sauce for compositing desktop managers.

I'm bit disappointed to the performance of those python_games -examples in the debian directory, so I might use this to pimp them a bit for an usage example.

Return to “Graphics, sound and multimedia”