Previous month:
August 2009
Next month:
December 2010

January 2010

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.


Running .NET CF applications and code on Symbian based Phones

J2ME has always been the standard in cross phone applications. Most java app are truly cross phone. The only phone we could write code in .NET was the Windows mobile or Pocket PC. A company called AppForge came up with a solution to run .NET code. But in 2007 the company was acquired by Oracle and shut down.

Now Red Five Labs have come up with a .NET based solution called Net60 which runs windows mobile based .NET code. Currently .NET 2.0 framework is supported and they plan to add support for compact framework 3.5. Now what made me exited is their licensing policy. With AppForge it was not possible to develop when the trial expired. With Net60 that is not not case. You can get a IMEI bound license for your non commercial application. Now, isn’t that wonderful? That means I can write my own apps to run on symbian and use them. That also paves way for .NET based open source apps. Lets face it, Nokia holds 41% market share for smartphone applications, which is a very large number (source).

I recently got myself a Nokia E63 phone, which is actually an economic version of Nokia E71 (only diff is 2Mpx cam instead of 5 Mpx cam and no GPS and plastic body instead of steel). It has the full qwerty keyboard. I spend a lot of time reading and writing mails as communication has become a large part of my role, so the full keyboard is really helpful. I was looking for a good way to write C# code and run it on my phone ( E71- Symbian OS S60 3rd edition feature pack 1).  After a few tries it worked very nicely. So here is what you need to do in order run .NET code on your Symbian phone.

Aquiring the SDK

1. Get the Net60 SDK from here. It is about 13MB. You would have to provide your phone IMEI no and email. That is the phone where the application would run. If you want a version where it will run on different phones, then you would have to get a commercial license.
2. Check your email to get the free non commercial express license  from Red Five Labs. ( I’d strongly suggest you see the video tutorial at their site, which is the next set of steps about)

Writing the Application

1. Create a compact framework 2.0 application (not CF 3.5)
2. After you are done with the application add a Genesis Setup Project to the solution
3. Point to the executable, fill up the necessary fields
4. Add Net60.sisx as dependency, also add the license that you received from RF Labs.
5.  Compile, install the output on your cell phone and run

Debug version vs Release Version

The debug version comes with logger and a runner tool. If you are developing, I would suggest that you use the debug version. Making a build and deploying each time you change the code can be pretty annoying. There is a NET60 Launcher tool that can run a .NET executable. So instead of building installation package and deploying each time you can just use that tool to run the exe. Make sure you copy the executable under c:\DATA\RedFiveLabs\Apps\ folder so that it shows on you NET60 Launcher.

Additional API

There are some additional API provided by RF Labs to control various features in Symbian OS. These are distributed over a few assemblies. These assemblies are

RedFiveLabs.Mobile.Audio
RedFiveLabs.Mobile.Bluetooth
RedFiveLabs.Mobile.Location
RedFiveLabs.Mobile.Messaging ( for SMS etc)
RedFiveLabs.Mobile.OpenGL ( oh yes open GL support)
RedFiveLabs.Mobile.Pim ( for contacts)  
RedFiveLabs.Mobile.Services
RedFiveLabs.Mobile.Vibration

Have fun .Netting on Symbian