raspberrypi-multimedia python module


1 post
by cheery » 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.

Repository: https://github.com/cheery/raspberrypi-multimedia/

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)
eventloop.run(1 / 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 = Image.open('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))
context.make_current(surface)

triangle = oes.Drawable(oes.TRIANGLES, dict(
    position=(3,0,5),
    texcoord=(2,3,5),
))
triangle.upload(data)

oes.enable(oes.TEXTURE_2D)

texture = oes.Texture2D()
texture.bind(0)
texture.upload(*image_attr)
texture.linear()

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

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

program.use()
program.i('texture', 0)

triangle.draw(program, 0, 6)

surface.swap_buffers()

time.sleep(2)
sys.exit(0)

#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(texture.target)         -  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.
User avatar
Posts: 219
Joined: Wed Jan 25, 2012 9:39 pm