meta data for this page
  •  

This is an old revision of the document!


Python For Maemo 5 Info

The python used in the Nokia N900 is known as PyMaemo. It is basically the normal python, with additional device related commands.

API Documentation (non-pythonic): Maemo 5 Api Docs

Python Tutorials: Python for newbies

It is also possible to access C APIs that don't have Python bindings. However, this can bring some interesting problems. Not recommended, as most of the maemo APIs should work just fine with Python. However, check Accessing APIs without Python bindings for more info.

Playing Sounds

N900 can play sounds with two different ways - Either using gstreamer or pygame. This example is made with pygame as it provides a higher-level API and requires less magic words ;-) Pygame documentation can be found behind the following link: PyGame Documentation

The sound file used in this example: Sound file

Here is a proper Object-Oriented implementation of a Player that can load and play sound files. I suggest creating a list of sound objects that are loaded with this player and then playing those when needed (faster than opening a file for playback each time separately).

soundplayer.py
"""
August 9, 2011
 
@author: Janne Parkkila
@email: japskua@gmail.com
 
@summmary: This file contains code for playing music/sounds on N900
           with Python programming language. This implementation uses
           pygame instead of gstreamer
"""
 
import pygame
 
class Player(object):
 
    def __init__(self):
        """
        @summary: The Constructor class
        """
        pygame.mixer.quit()
        pygame.mixer.init()
        pygame.init()
 
    def Play(self, sound):
        """
        @summary: This function is used to play the given sound
        @param sound: The sound to be played
        @type sound: pygame sound object 
        """
        sound.play()
 
    def Load(self, filename):
        """
        @summary: This function loads the given filename into a pygame
                  readable sound object
        @param filename: The path to the file to be loaded
        @type filename: String
        @return: Loaded pygame music object
        @rtype: pygame Sound
        """
 
        return pygame.mixer.Sound(filename)
 
if __name__ == "__main__":
    # Initialize the player
    player = Player()
    # Load the soundfile with the player
    sound = player.Load("button-4.wav")
    # Play the file
    Player.play(sound)

However, if you want to make something better yourself, here is the basic simple version of the same thing

simple-sounds.py
import pygame
 
pygame.mixer.init()
pygame.init()
 
sound = pygame.mixer.Sound("button-4.wav")
sound.play()

Using Microphone

Mic stuff goes here

Using Camera

Camera stuff here

Bluetooth

Bluetooth stuff here

Using Accelerometer

Download this source from here: Accelerometer.py

accelerometer.py
"""
Created August 9, 2011
 
@author: Janne Parkkila
@contact: japskua@gmail.com
 
@summary: This file contains class implementation on connecting 
          to maemo dbus and receiving accelerometer data
"""
 
import dbus
 
class Accelerometer(object):
    """
    @summary: This class is used to get accelerometer data
              from the dbus
    """
 
    bus = None
    accelerometer = None
 
    def __init__(self):
        """
        @summary: The Constructor for the class
        """
        self.bus = dbus.SystemBus()
        self.accelerometer = self.bus.get_object("com.nokia.mce",
                                            "/com/nokia/mce/request",
                                            "com.nokia.mce.request")
 
    def getData(self):
        """
        @summary: This function returns the data of the accelerometer
        @return: orientation, stand, face, x, y, z
        @rtype: list
        """
        return self.accelerometer.get_device_orientation()
 
 
    def displayData(self):
        """
        @summary: This function is just used to display data
        """
        orientation, stand, face, x, y, z = self.getData()
        print "Orientation: ", orientation
        print "Stand: ", stand
        print "Face: ", face
        print "X: ", x
        print "Y: ", y
        print "Z: ", z
 
 
# This is only if we want to run the accelerometer test straight from the command line
if __name__ == "__main__":
    print "Starting the Accelerometer test"
    accel = Accelerometer()
    accel.displayData()

Using Location API

Note: The python location might not be installed on the device. In this case, you need to install it with apt-get install python-location

IMPORTANT TO NOTICE The GPS on the N900 is not very fast! Acquiring a signal can take a great amount of time. Thus it is just recommended to simulate this in some other manner. The probability of receiving a signal in the basement is really small, so please, don't waste too much of your precious time with this.

More info on location API with pyMaemo can be found from PyMaemo Location API at maemo wiki

The following file is brutally ripped from the Maemo wiki:

gps.py
import location
import gobject
 
def on_error(control, error, data):
    print "location error: %d... quitting" % error
    data.quit()
 
def on_changed(device, data):
    if not device:
        return
    if device.fix:
        if device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
            print "lat = %f, long = %f" % device.fix[4:6]
            # data.stop() commented out to allow continuous loop for a reliable fix - press ctrl c to break the loop, or program your own way of exiting)
 
def on_stop(control, data):
    print "quitting"
    data.quit()
 
def start_location(data):
    data.start()
    return False
 
loop = gobject.MainLoop()
control = location.GPSDControl.get_default()
device = location.GPSDevice()
control.set_properties(preferred_method=location.METHOD_USER_SELECTED,
                       preferred_interval=location.INTERVAL_DEFAULT)
 
control.connect("error-verbose", on_error, loop)
device.connect("changed", on_changed, control)
control.connect("gpsd-stopped", on_stop, loop)
 
gobject.idle_add(start_location, control)
 
loop.run()