Python on S60 at SDForum ETech

I helped out some with the Python on Series60 Phones presentation at SDForum last week, and I’ve been meaning to post about it. Hartti already put a post up, so I really have to.

I had a bunch of little code snippets to show off, stuff running on the device, cause I figured a lot of the discussion would be around the capabilities of the Python port. And there was some interest around particular areas like accessing location information (both the cell ID of the current location as well as interfacing with external Bluetooth GPS devices) and transforming the audio stream of an ongoing phone call (you can use the play() method to push a sound into an ongoing phone call, or the record() method to record chunks of a call, but as far as I know you can’t hook into a call as an audio transformer intercepting both inbound and outbound audio in realtime). But overall a lot of the questions were really about the mobile development environment as a whole and how Python fits into it.

Folks in the audience seemed to be really doubtful about the Python port being a free (as in freedom of speech) tool. There were lots of questions about redistribution rights, and “gaurantees” from Nokia about the availability of the Python API going forward. We tried to explain as much as we could that the Python S60 effort isn’t at all like Java support on handsets, and more like Linux support on PCs. Or even Python support on PCs. The open source ecosystem for any environment has fundamentally different influences than what exists on the commercial side, and there isn’t always a good direct mapping. The Python port is open source itself. It used to be a closed project based on open source, which is something I think hurt the effort quite a bit. For the first few releases there was no source available for the port of the Python interpreter. But now there is, and the effort can claim open source in all regards.

The first thing I showed off was using the bluetooth console to connect to the Python interpreter running on the phone. You can find instructions for how to do it on Linux, Mac, and Windows linked to from the Python on Series 60 wiki. A lot of the samples I showed off came from bits and pieces of that as well. These are all examples of things you can throw right into the bluetooth console as soon as you have Python installed and your PC connected, they “just work”:

  • The graphical “hello world” for Python S60:
    #
    import appuifw
    appuifw.note(u'Hello SDForum!', 'info')
    
  • Scanning for Bluetooth devices is done using some built in dialogs. When you scan for devices using the bt_discover() method you get the dialog you might recognize from any standard S60 app:
    #
    import socket
    addr,services=socket.bt_discover()
    
  • Example of a slightly more complex UI interaction, a popup menu:
    #
    import appuifw
    opts = [u"C++", u"Java", u"Python", u"Scheme"]
    choice = appuifw.popup_menu(opts, u"Pick a language:")
    if choice == 0 :
        appuifw.note(u"You must be joking", "info")
    
    if choice == 1 :
        appuifw.note(u"It really is everywhere", "info")
    
    if choice == 2 :
        appuifw.note(u"A nice comfy environment", "info")
    
    if choice == 3 :
        appuifw.note(u"Simple and elegant", "info")
    
  • An example of using the camera to capture an image and save it to device memory, then use the content handler to display the image on the screen (yep, still all built in functionality):
    #
    import camera
    import e32
    filename=u'c:\\picture.jpg'
    image = camera.take_photo(size = (640,480), position = 1)
    image.save(filename)
    lock=e32.Ao_lock()
    content_handler = appuifw.Content_handler(lock.signal)
    content_handler.open(filename)
    lock.wait()
    
  • Use the packaged Python networking libs to send the image from the previous sample via POST to a server somewhere out on the Internet (madgat.com in this sample, my server… obviously my server isn’t included in the Python distro, but if you have a server to post to you can use it instead):
    #
    import httplib, urllib
    f = open( filename, 'rb' )
    i = f.read()
    f.close()
    params = urllib.urlencode({'image' : i})
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    conn = httplib.HTTPConnection("www.madgat.com")
    conn.request("POST", "/ps60/take.php", params, headers)
    response = conn.getresponse()
    conn.close()
    print response.status
    
  • Access to contacts:
    #
    import contacts
    ctact = contacts.open()
    ctact.find('rowehl')
    
  • Access to the calendar, finding all entries or todo items associated with today:
    #
    import calendar
    import time
    cal = calendar.open()
    cal.daily_instances(time.time())
    
  • That previous example will return you an array of items with id and datetime parameters. You can use the id to get the details for the entry (the __getitem__ method is what it’s called in the Python for S60 docs.. yes I know that’s the overloading endpoint for container emulation, so you can write cal.__getitem__(61) as simply cal.[61], but that doesn’t help someone searching the docs). You should use the id of an entry in your calendar in place of the 61 I use here:
    #
    entry=cal.__getitem__(61)
    entry.content
    entry.location
    
  • And writing to the built in app data, which is managed with transactional requests. Do this if you don’t mind overwriting the calendar/todo entry, and then pop out of Python and you’ll see the item has been changed in the native app:
    #
    entry.begin()
    entry.content = u'Mike was here'
    entry.commit()
    

One of the samples I had wanted to show off was a script that pulls a sample from a Bluetooth GPS and displays a Yahoo map image to go along with it. It was a longer sample though, and we were inside so I knew getting a GPS lock would take forever. Plus, of course, it’s against the Yahoo terms of service. I figure terms of service aren’t like the DMCA though, so posession of a program that violates the terms of service does not constitute violation… I’m guessing. I could go back and read the TOS again and see if they say anything. But instead I think it would just be more interesting to post the Python script for Series60 that reads a sample from a bluetooth GPS unit and displays an associated Yahoo map image. Really unfortunate that Yahoo can’t allow folks to use their API with realtime data, cause that image tile interface rocks hard core.

Links (should be all you need to get started, and the wiki for samples to play with):

3 Responses to “Python on S60 at SDForum ETech”

  1. SDForum Emerging Technology SIG » Python for Series 60 Slides Says:

    [...] Mike Rowehl, who also participated in the presentation, posted a lengthy summary of the talk here. [...]

  2. Walter Schulze Says:

    I wish Python could run a script in the background and make an alarm go off at certain times. If you know how please email me
    Thank you

  3. Python Kaynak Siteler - wWw.PyThoN-S60.CoM | Says:

    [...] on S60 (pys60) : mypapit gnu/linux blog mmw2008 MMW2008 Hands on session: PyS60 to PD mike rowel Mike Rowehl: This is Mobility Blog Archive Python on S60 at SDForum ETech python it Mobile mobapp http://mobapp.hiof.no/wp-content/upl…ain_pys601.pdf ebook [...]

Leave a Reply