Mobile Programming in Python using PyS60: Getting Started

Smart phones are becoming a common sight nowadays. Above all, the "smartness" of smart phones come from the fact that they can be programmed. In other words, one can develop custom applications for these mobile devices and use them in the devices. This article covers the basics of developing these kinds of applications in a variant of Python.

Almost all languages, from C to Java and Perl to Ruby, provide APIs to access and create services for smart phones. Among them, nothing, at present, can beat the flexibility and ease-of-development that Python provides. Symbian is the most common OS used by smart phones. The Symbian port of Python is known as pyS60.

in this discussion I will be focusing on the basics of pyS60. The first two sections will focus on answering the question "what is pyS60?" In the third section, I will detail the steps in developing and deploying a pyS60 application. In the last section, a real world application will be developed. That sets the outline for this discussion.

What is PyS60?

PyS60, also known as Python for S60 version, is a port of Python, released by Nokia for Symbian OS and targeted at the N60 series of mobiles. The libraries provided by PyS60 can be divided into broad categories:

  1. Built-in libraries

  2. Dynamically loadable libraries

Since PyS60 is essentially an extension to the standard Python, it is known as the Python for S60 extension. Hence the categories can also be called built-in extensions and dynamically loadable extensions. Here are the details.

Built-in libraries or extensions are one of the two native C++ extensions available in PyS60. These provide non-proprietary APIs to Symbian 60’s (S60) platform. The built-in extensions again are divided into two modules, e32 and appuifw.

The e32 module provides access to those services that are not available in standard Python libraries. Essentially, this module is built into the Python interpreter to provide access to the Platform services of S60. These services include the timer service (Ao_timer class), system information (sysinfo module and its classes), listing of the drives (drive_list method) and more.

By using the appuifw module, the developer can access the API related to the UI elements of the S60 platform. The main UI elements include Text, Listbox, Canvas, and so forth. Using the elements a GUI can be constructed either as part of a window or as the part of screen itself. If the elements or controls are part of the window then the window becomes the container.

{mospagebreak title=Dynamically loadable libraries or extensions}

These extensions, as the name suggests, are loaded dynamically i.e. only when needed as opposed to the built-in extensions which are loaded when the import statement is encountered. The reason for this behavior is that the dynamically loadable extensions provide access to the proprietary S60 API. There are 16 dynamically loadable extensions. The most commonly used among them are:

  • Graphics, which provides access to the graphics manipulation capabilities of the S60 platform including loading, saving and resizing of images.
  • Messaging, which provides access to the messaging capabilities of the S60. It includes SMS and MMS services.
  • The inbox module, which allows one to access the inbox of the mobile on which the application is being executed. The information one can access includes the message, time of message, the address of the sender and so forth. 
  • The camera module, which grants access to all the functionalities of the camera. This includes image modes, flash modes, maximum zoom available, and so on. 
  • The audio extension, which provides access to the audio capabilities of the device. The functionalities include playback of different formats of audio files, recording of voice, text to speech and so on. 
  • The calendar module, for using the calendar functionalities of the device. 
  • The contacts module, through which one can access the contact list in a device. The contacts are provided in the form of dictionary. 

That completes the services provided by PyS60. Next, let us see the steps in accessing the different services of the mobile using Python.

{mospagebreak title=Accessing Services, Step By Step}

The ease of programming using Python reduces the steps required to access the different services. However, there are three steps that are always required. They are:

  1. Instantiating the object of the service.

  2. Setting the required properties.

  3. Calling the methods to access the required functionality.

These steps are generic to not only the S60 platform but also to any mobile platform, including embedded Linux, which is another mobile platform. I will use the sound module as an example for all the steps.

Instantiating the object of the service

Most of the time this is the first step in accessing the service. This step is essential when working with built-in extensions. However, for most of the dynamically loadable extensions, objects are not required as their functionalities can be directly accessed. The reason for this is that the methods of the dynamically loadable modules are static. For example, to create a text field (the name of text field component is query) the statements would be

input= query(u”Enter Text”,”Text”)

On the other hand, to use sound module to load a file, one must simply call the open method of the Sound class. The open method is a static, so the object of the Sound class is not required. The following statement opens a .wav file named test.wav.

Sound.open(“test.wav”)

 Setting the required attributes

The next step is to set up the attributes of the object instantiated. If the service can be accessed through static methods, then attributes can also be set or accessed through static mutators (setters) or accessors (getters). As with all the classes of Python, the attributes can be set at the time of instantiating the object. For example, selection_list can be used to show a list of items to the user, from which he or she can make his or her choice. The option to show a search field can be set at instantiation. The following statements display a list of fonts available on the device and let the user select one of the available fonts.

li =available_fonts()

sl=selection_list(li,1)

However, to set the volume of the Sound object one may simply call the set_volume() method, which is again a static method. The following statement sets the volume to 10.

Sound.set_volume(10)


Calling the methods to access the required functionality

The last step is to call the required methods on the objects to access the services. If the object is a UI component then the ‘service’ will either be displaying some value to the user or getting some value from the user. If it were a device-based service such as Sound, then accessing the functionality would mean requesting the platform to either play or stop the audio file. For example, the query component returns the value entered by the user after initialization. Therefore, the instantiation returns the value entered by the user. The returned value can be displayed using note object. The following statements accept a string from the user and display it to the user

data=entry(u”Enter your message:”, “text”)

note(u”Your message was: “+data, “info”)

If one needs to access the play service provided by Sound module, he or she has to just call the play() method of Sound class. The following statement just does that: 

Sound.play()

That completes the steps required to work with PyS60. One point to keep in mind is that the string being passed to the methods and constructors needs to be Unicode i.e. the string should be prefixed with “u” otherwise the string may not be displayed correctly. Next, I am going to develop a small application that would accept a path to a .wav file and then play it.

{mospagebreak title=PyS60 Programming in the Real World}

The application to be developed will have the following functionalities:

  1. An Input box to accept the path for the file to be played.

  2. The ability to play a selected file when entered by the user.

Here we go. First comes the imports. Since both UI and Sound are required, the appuifw and audio modules need to be imported.

from appuifw import *

import audio

Next we need to show the entry box to the user and get the path entered by him or her. To display the text box, we can use the query component. The first argument is the prompt to be shown and the second argument is the type of query. The type can be ‘text’, ’date’, ’time’, ’float’ etc. Here the type will be text as we need textual data i.e. a string. The value returned by the query component will be stored in a variable named data. 

from appuifw import *

import audio

data=query(u’Enter the path of the file to be played’, ‘text’)

Now we need to load the file. For that we need to call the load method of sound. As discussed in the previous section every method in the sound module is static. We will also check whether the user clicked cancel. If cancel has been clicked, then data will contain null.

from appuifw import *

import audio

data=query(u’Enter the path of the file to be played’, ‘text’)

if data is not Null:

Sound.load(data)

else:

note(u’Give the path name’, ‘info’)

Next let us tell Symbian to play it.

from appuifw import *

import audio

data=query(u’Enter the path of the file to be played’, ‘text’)

if data is not Null:

Sound.load(data)

Sound.play()

else:

note(u’Give the path name’, ‘info’)

That completes our application. Though it is small, it introduces several important concepts of PyS60. In the coming discussions, I will go into more depth on each module. Till then…

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan