Running .NET CF applications and code on Symbian based Phones
Fun with NTFS : Hide your data in Alternate Streams

Coding Python S60 Application on Nokia E72: Accelerometer to sense motion and move objects

Update : I am no longer using e72. I do not see symbian as preferred platform for developing smartphone applications. I have moved on to android which I personally find almost magical.

A little Background

A few days ago I got a Nokia E72 phone as a gift and I really loved the phone because of its 600 Mhz processor. I needed a phone with a full keyboard so that I can check and send emails easily from anywhere. I like windows mobile platform but did want to try raw processing power. As usual I tried writing .NET in Symbian ( usual Nokia phone OS ) and had fun. But I needed more functionality that is native to the OS and decided to go with python. Although Nokia has a cool gadget API, I wanted take advantage of the hardware on the phone. There is a python compiler for the S60 platform, I installed it and wrote my first tiny python code.

Can’t resist the urge to share the spec of E72 hardware. It has a 600 MHz ARM 11 Processor, 290 MB internal memory, 128 MB RAM, 16GB external mem capbility, Acceleretometer for motion detection, Digital Magnetic Sensor for compass, Ambient Light Sensor, Active GPS, 5 Megapixed cam, WLAN, Bluetooth, FM Radio, Optical Trackpad, HSDPA 10Mbps connectivity, Active noise cancellation etc.

Downloading and installing python on your S60 phone

Download the python for S60 from https://garage.maemo.org/frs/download.php/6448/PythonForS60_1.9.7.tar.gz.

Extract the contents then install the python runtime for your device. Then install the python shell script mine was S60 3rd edition feature pack 2.

Video Demo

This is what the application does

A python primer for a c# (or any) developer

Unlike perl, which looks horrible (sorry if any perl developer is reading this, I mean you no harm, it is just my opinion), python looks pretty structured somewhat like Delphi/Pascal. My objective was to write a few programs for my phone and play around so I wanted to learn python fast in an hour. Before we proceed further we should know a few facts about python. Firstly Python is a dynamic scripting language that is interpreted. Then …

Without correct indentation python code will not run

Wait a second, what!?
Yes, python has specific code indentation rules that makes your write code in beautiful formatted indentation. Python indentation works like C# braces. For example let me demonstrate a hello world method.

def say_hello():

 print u”hello world”

See the indentation that you have to make for the function? Yep that makes most python code look nice at the same time

No variable Declaration

There is no variable declaration required to use a variable, just make up a variable and use it. See example

myname = “Shafqat Ahmed” 

print myname

Importing namespaces

Like .NET you can import namespaces ( called modules ) in python.

import math 

import appuifw

A primer on python for Series 60

Since my objective was to write code on python for Symbian, I am going to share few little tidbits.

For Python for S60 make sure you use Unicode everywhere

The Python S60 SDK for Symbian (I think other phones also have this same requirement) requires you to use unicode strings in most places. See below

myname = u“Shafqat Ahmed” 

print myname

See that “u” in front of the string declaration? That is how you make a unicode string.

Important modules

For a PyS60 app usually the first required module is the Application UI Framework for short appuifw.  The e32 module contains some system function, the contacts module lets us use the contacts on the phone, the messaging lets us use sms messaging, the telephone library lets us dial or hang up a phone number, the audio module lets us play music, or record voice note or record a phone conversation.

Using the Symbian Native UI

There are simple functions that lets the user use the native Symbian UI widgets. For example if we want to read values as input. We could write something like

import appuifw 

name = appuifw.query(u”What is your nam?e”, u”text”)

Code

Reading the Accelerometer data on PyS60 on Nokia E72

In order to access the accelerometer data we need to use the sensor module. Then we will setup a callback where the accelerometer data will be sent. Here is how the code looks like ( I created a class to organize code, so the actual code look a little different)

import sensors 

from sensors import * 



accelerometer = AccelerometerXYZAxisData ( data_filter = LowPassFilter()) 

accelerometer.set_callback ( data_callback=my_callback)

Then I can access the data in my callback function each time by accessing accelerometer.x for X axis data, accelerometer.y for Y axis data and accelerometer.z for Z axis data.

Managing Sensitivity

The E72 has a -60 to +60 range value for each axis. We could use a constant value to move the graphic object, then that would move evenly. But we want to the app to be more responsive human like. So if we take the value of the axis and devide it by a value, say 10 then I would get 1 for a 10 value from the accelerometer, a 3 for 30 on accelerometer, a 6 for 60 (max value) on accelerometer. Similarly I would get a -1 for -10. A value of +/-10 mins the device is a little tilted, +/-60 means highly tilted. So can move the object faster if the device is more tilted.

Also I have added a menu for controlling sensitivity. If I use 5 instead of 10 then the division would result higher values thus making the movement faster. The application code looks like this.

def my_callback(self):

  x_incr = (self.accelerometer.x / self.sensitivity) * -1

  self.x= self.x + x_incr

  y_incr = (self.accelerometer.y / self.sensitivity ) 

  self.y= self.y + y_incr

  if self.y < 0:

    self.y = 0

  if self.y > 220:

    self.y = 220

  if self.x < 0:

    self.x = 0

  if self.x > 300:

    self.x = 300

  self.draw()

Then we draw the graphic on to an image then blit it down to the drawing canvas.

Download

Please download the draw.py(2.0K) from here. And keep in your E:\data\python folder, use c: if you have installed python in the internal memory not the memory card.

Running the app

Run python shell, select ‘Run Script’ from the menu and run draw.py. To exit press the right softkey. You can select high sensitivity from the left softkey menu to have more fun.

Comments

Zahid Rahman

great work.. or research i guess! i didn't pay any attention that my phone has an accelerometer till now! =)

manu

i m ur fan..

sunshine

Hello., we are planning to develop an application for nokia series 60 v5 or v3. you are good at programming :)Im your fan. do you think we can program or modify data transferring through wifi using python? tnx

hazwoper

Finally that new beer pong app will be available.

JJ

Hi,
I hv very limited knowledge of programming (java applications for PC - none for phone yet).
I am looking for application, or a way to write a simple script that will change the mode (business / private) on my e72 and at the same time dial the number default stored on my No 1 speed dial.
The Idea is that I have 2 numbers on my SIM card, and I configured speed dial 1 to swap between the numbers. I want to assign a different screen color according to the active number - thus change mode AND dial speed dial 1 together :)

Any ideas??

Meco

Hi , great work. I really want to have a accelerometer in my E72 , but i dont understand nothing of this :S help?

TV Phone

Nokia E72 works great really...

Ahsan Jehangir

Thanks for sharing your good working,
I have installed python 2.0 but how to view image using accelerometer by flipping the mobile. Its not working through in setting its mentioned in Sensors:Turning control
Silencing calls
snoozing alarms
both checked but they are also not working why ?

how to enable them and how to overcome image viewer ?

Any suggestions?
Thanks

The comments to this entry are closed.