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.
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.
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.
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:
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.
blog comments powered by Disqus