Presentation is loading. Please wait.

Presentation is loading. Please wait.

Daniel Rocha Forum Nokia

Similar presentations


Presentation on theme: "Daniel Rocha Forum Nokia"— Presentation transcript:

1 Daniel Rocha Forum Nokia
Python for S60 Daniel Rocha Forum Nokia

2 Agenda Introduction to Python for S60 (PyS60)
Getting started with PyS60 Packages Installation on a device Running sample apps Application development basics Tools, Emulators, Utilities packages Creating a stand-alone Python app PyS60 API Tour and Example apps Conclusion Slide 2

3 Introduction

4 About Python, the language...
Open Source development since 1990, led by Guido van Rossum Design goals: maximizing programmer productivity and extensibility Special emphasis on code readability “small language, large library” Easily extensible with native code Modern, full featured: classes, inheritance, function objects, exceptions, automatic memory management, threads object-oriented language classes, multiple inheritance, attributes, properties and operator overloading functional language first class functions, closures, lambda expressions, eval(), map(), filter(), apply() Dynamic language: variable types determined at runtime, reflextion Scripting language: interactive mode: evaluate and experiment with code on the fly, compilation is transparent Code examples shows the Python design principles programmer productivity code readability Minimum punctuation Indentation based syntax extensibility “There should be one – and preferably only one – obvious way to do it.” cf. Perl: “There is more than one way to do it”, TIMTOWTDI Slide 4

5 Quick code example >>> Numbers are 10, 30, 40 List is [1, 3, 5, 7, 10], List[0] is 1, List[0:2] is [1, 3] Is 9 in list? False Looping: Tuple is (10, 20, 30), Typle[0] is 10 Unpacking tuple (10, 20, 30) into: a=10, b=20, c=30 Assoc is {'daniel': 29, 'jack': '80', 'susan': 28}, Assoc['daniel'] is 29, Is susan in assoc? True >>> ## first python script - this is a comment ## x = y = z = 0 ## multiple assignment x = 10 ##variable attribution y = x + 20 z = y + x print "Numbers are %s, %s, %s" % (x,y,z) ## lists, tuples and associative arrays list = [1,3,5,7,10] tuple = (10,20,30) assoc = {'daniel': 29, 'susan': 28, 'jack':'80'} print "List is %s, List[0] is %s, List[0:2] is %s" % (list, list[0], list[:2]) print "Is 9 in list?", 9 in list print "Looping:", for x in list: print x, print "Tuple is %s, Typle[0] is %s" % (tuple, tuple[0]) a, b, c = tuple print "Unpacking tuple %s into: a=%s, b=%s, c=%s" % (tuple, a, b, c) print "Assoc is %s, Assoc['daniel'] is %s, Is susan in assoc? %s" % (assoc,assoc['daniel'],'susan' in assoc) Slide 5

6 Python for S60 (PyS60) Simplifies application development and provides a scripting solution for Symbian C++ APIs Usable in different ways: GUI application background (“daemon”) process embeddable in your C++ app Easier way of writing native applications Open source implementation, easy to extend using Python/C API Slide 6

7 Python for S60 (PyS60) – contd.
Based on Python 2.2.2 Consists of: Python runtime: Python interpreter DLL Standard and proprietary Python library modules Python Script Shell: Execution environment for Python scripts For S60 prior to 3rd. Edition: Recognizer plug-in Automatic Installer SDK Plug-in for writing extensions and testing scripts on a PC (Windows-only) Slide 7

8 Python for S60 (PyS60) – Libraries
Support for Python Standard Libraries – not all standard and optional modules but most can be added without modifications to PyS60 Support of many custom S60 libraries e32 sysinfo appuifw graphics camera keycapture audio telephone messaging contacts calendar e32db E32dbm inbox positioning sensor Show and walk through the PyS60 Documentation during this slide Slide 8

9 Getting started with PyS60

10 Python on S60 Mobile Phones
SourceForge project site: Source code Docs Distributed by flavor of S60 Interpreter SDK Script Shell You need to install the Interpreter and the Script Shell to the phone. The SDK is used to develop python extensions. There are two versions of the script shell, signed and unsigned - The signed is handicapped - To access protected funtionality (like GPS) you need to get a developers certificate and sign this shell script yourself In the interactive demo, bring up the mobile python shell in the hyperterminal. Show how the terminal works the same as the PC terminal. Slide 10

11 What’s available at SourceForge
pyS60 full C++ source code pyS60 Runtime, distributed by flavor of S60: PythonForS60_1_4_4_<yourS60version>.SIS for devices PythonForS60_1_4_4_SDK_<yourS60SDK>.zip for running Python scripts on S60 emulator and compiling your own extensions (Windows-only) pyS60 Script Shell, distributed by flavor of S60: Ideal for testing and developing scripts PythonScriptShell_1_4_4_<yourS60version>.SIS Also unsigned version, if more capabilities are needed. Complete API reference guide Release notes Slide 11

12 Getting started – PyS60 on a mobile device
Install PyS60 Runtime interpreter package. In our example, Nokia E61i is a 3rd. Edition device so we install PythonForS60_1_4_4_3rd.SIS You can perform installation either via PC Suite (Windows) or by sending the .SIS file via Bluetooth (Mac and Linux) This is the runtime package, script shell will be installed next Slide 12

13 Getting started – PyS60 on a mobile device
Install PyS60 Script Shell. In our example, Nokia E61i is a 3rd. Edition device so we install PythonScriptShell_1_4_4_3rd.SIS Script shell is useful for quickly running and testing scripts, run interactive consoles Python app visible in the main application menu Slide 13

14 Getting started – PyS60 on a mobile device
Run script -> Reads from E:\Python and C:\Python There is an interactive console for on-device editing Bluetooth console for remote editing of Python scripts Not required for running Python stand-alone apps! Interpreter and Script shell are not dependent on each other Slide 14

15 Testing PyS60 example scripts
Start the Python Script Shell Click Options / Run Script Choose one of the scripts in the list and hit OK Slide 15

16 Application development basics

17 Tools of the trade - Windows
Some version of Python installed and running -> ActivePython ( comes with Python 2.5.1, PyWin editor and Python shell. S60 Platform SDKs for Symbian OS, for C++ -> provides PC-based emulation environment for creating and running scripts and Python extensions ( Package PythonForS60_1_4_4_SDK_3rdEd.zip for plugging Python into the S60 SDK Packaging tool for creating .SIS installable files (Ensymble - – Py2sis tool comes with the SDK, not usable outside Windows If using Ensymble, OpenSSL command-line utilities -> S60 device with optional memory card Nokia PC Suite – optional but a timesaver Show how to create a full screen app: Import appuifw appuifw.app.body = appuifw.Canvas() appuifw.app.screen = “full” appuifw.app.body.text((20,20), u’Hello World’) Talk about unicode strings for the UI Show the examples from Show 001-helloworld.py Show 006-selectionlist.py Show 012-firstmenus.py Slide 17

18 Basic development cycle - Windows
Create a .py file using PyWin containing valid Python for S60 code Save it to C:\Symbian\9.1\S60_3rd_MR\Epoc32\winscw\c\python Start the S60 SDK Emulator Click on the Python Script Shell, choose your script from the list Click “Select” to run it Slide 18

19 Tools of the trade - Linux
Some version of Python installed and running Emulation environment not available Packaging tool for creating .SIS installable files (Ensymble OpenSSL command line utilities PyS60-compat – Emulation library for some PyS60 APIs ( S60 device. Having a memory card greatly facilitiates script testing Bluetooth-enabled workstation Show how to create a full screen app: Import appuifw appuifw.app.body = appuifw.Canvas() appuifw.app.screen = “full” appuifw.app.body.text((20,20), u’Hello World’) Talk about unicode strings for the UI Show the examples from Show 001-helloworld.py Show 006-selectionlist.py Show 012-firstmenus.py Slide 19

20 Basic development cycle - Linux
Create a .py file using any text editor Send your script to the E:\Python phone using obexftp or connecting the phone as a usb drive Start Python Script Shell Choose your script from the list Click “Select” to run it No emulator, you have to run your script on the phone, or with PyS60-compat Slide 20

21 Creating a Stand-alone Python Application
Easiest: Ensymble For S60 3rd Edition, PyS or later Linux, Mac, Windows py2sis with standard PyS60 source distribution Both tools create a .sis file which can be installed on the phone as a native application, appear in the main menu and have its own icon C:\dev\python>ensymble_python py py2sis simplecube.py Slide 21

22 Symbian OS 9.1 Platform Security – affects Python
Accessing potentially sensitive features now requires capabilities Global key capture Reading the cell ID Reading the (internal) GPS location Setting the system time Protected File Access Need devcert (w/ Publisher ID) or Open Signed Online Register with Register your phone IMEI with the devcert Sign your application with the devcert Install your application on your phone Slide 22

23 PyS60 API Tour and Example apps

24 Application framework basics
Application type can be used to access and modify the UI = appuifw.app UI elements: Title Tabs Body (Text, ListBox, Canvas) LSK and RSK Define handlers for Options menu and Exit key Wait for user input using an Active Object lock – other threads cannot access UI Slide 24

25 Application framework basics
UI Controls are implemented as Python types: ListBox Text Canvas Controls are set to the application body (using app.body = control) Dialogs are implemented as module-level functions Form is a dialog implemented as a type, due to its versatility Content_handler object used to open files of given types Slide 25

26 UI framework – Components
Dialogs – notes, query, multi-query, popup- menu, selection list, multi-selection list Form Canvas Menus Text Listbox Slide 26

27 UI framework – Components
Slide 27

28 Multimedia – audio, video, camera
Multimedia applications can be developed using modules: Audio – allows playing and recording of audio and text-to-speech functions Camera – for taking pictures, recording videos, controlling camera aspects and querying camera capabilities Graphics – Image saving and loading Demo no E61i que tem o ImageExpo. Slide 28

29 Messaging and Inbox – sms
Inbox module is used to read through your message Inbox Messaging module is for sending SMS and MMS messages Slide 29

30 Messaging and Inbox – sms example Source: sms.py
import inbox, appuifw, inbox, messaging box = inbox.Inbox() for sms_id in box.sms_messages()[:5]: msg = box.content(sms_id) appuifw.note(msg) messaging.sms_send(" ", u"Hello Bossa!")    Slide 30

31 Connectivity: Bluetooth, TCP, HTTP
Python applications can use standard socket module for communication, via Bluetooth, TCP, etc. High-level urllib can be used for HTTP and Web commnucations Demo no E61i que tem ImageExpo Demo: Tracker Slide 31

32 Connectivity – urllib Source: webclient.py
import urllib, appuifw, e32 URL = " dest_file = u"C:\\Data\\Images\\python-logo.gif" urllib.urlretrieve(URL, dest_file) lock = e32.Ao_lock() viewer = appuifw.Content_handler(lock.signal) viewer.open(dest_file) lock.wait() Slide 32

33 Connectivity – bluetooth photo sender Source: bluetooth.py
import camera, e32, socket, appuifw PHOTO = u"C:\\Data\\Images\\bt_photo_send.jpg" def send_photo(): try: address, services = socket.bt_obex_discover() except: appuifw.note(u"OBEX Push not available", "error") return if u'OBEX Object Push' in services: channel = services[u'OBEX Object Push'] socket.bt_obex_send_file(address, channel, PHOTO) appuifw.note(u"photo sent", "info") else: Slide 33

34 Many more! Explore the API
Positioning Location E32dbm Telephone Contacts Sensor… Demo: Shake your music Slide 34

35 Conclusion Introduction to the Python Language www.python.org/doc
Examples from Python Datatypes A variety of basic data types are available: numbers (floating point, complex, and unlimited-length long integers), strings (both ASCII and Unicode), lists, and dictionaries. - String + String - Strings are immutable Key built in functions dir(), help(), type(), (dir on built-ins) Defining a function in python def hello(): Working with a source file Python test.py Dynamic typing Data types are strongly and dynamically typed. Mixing incompatible types (e.g. attempting to add a string and a number) causes an exception to be raised, so errors are caught sooner. Exceptions The language supports raising and catching exceptions, resulting in cleaner error handling. Memory Management Python's automatic memory management frees you from having to manually allocate and free memory in your code. Slide 35

36 Backup

37 UI framework – Dialog notes and queries Source: notes.py
import appuifw, time ##### note appuifw.note(u"Hello!",'info') ## may also take 'error' and 'conf' ##### query name = appuifw.query(u"Type your name:",'text',"Daniel") age = appuifw.query(u"Type your age:",'number') date = appuifw.query(u"Type a date:",'date') code = appuifw.query(u"Type a password:",'code') query = appuifw.query(u"Wanna see your data?",'query') timetuple = time.localtime(date) input = u"You name is: %s, your age is: %s, year is %s, pwd is %s" % (name, age, timetuple[0],code) if query: appuifw.note(input) else: appuifw.note(u"Insensitive clod!",'error') Slide 37

38 UI framework – List dialogs Source: lists.py
import appuifw ##### popup fruits = [u"Apple",u"Banana",u"Orange"] index = appuifw.popup_menu(fruits,u"Select a fruit:") if index in range(len(fruits)): appuifw.note(u"You've chosen:" + fruits[index]) ##### selection list phones = [u"Nokia",u"Nokia",u"The ones from Finnish company"] index = appuifw.selection_list(phones,0) if index in range(len(phones)): appuifw.note(u"You've chosen:" + phones[index]) ##### multi selection with search cars = [u"Ferrari",u"Lamborghini",u"McLaren",u"Renault"] selections = appuifw.multi_selection_list(cars,'checkbox',1) ## search field result = "" if len(selections) > 0: for x in selections: result+= cars[x] + "," appuifw.note(u"Your choices:"+result[:-1]) else: appuifw.note(u"You don't like cars") Slide 38

39 Application framework – Basic application example Source: app1.py
import appuifw, e32 ##handler for Exit def quit(): appuifw.note(u"Application exiting...") app_lock.signal() ##sets app UI title appuifw.app.exit_key_handler = quit appuifw.app.title = u"Hello!!!" appuifw.note(u"Application has been started...") ##wait for input app_lock = e32.Ao_lock() app_lock.wait() Slide 39

40 UI framework – Menu example Source: menu.py
import appuifw, e32 ##menu def menu_choice1(): appuifw.note(u"Choice 1") def menu_choice2(): appuifw.note(u"Choice 2") def menu_choice3(): appuifw.note(u"Choice 3") ## main mymenu = [(u"Choice 1",menu_choice1), (u"Choice 2",menu_choice2), (u"Choice 3",((u"Sub-choice",menu_choice3), (u"Sub-choice1",menu_choice1)))] appuifw.app.menu = mymenu Slide 40

41 UI framework – Text example Source: text.py
##sets Text tx=appuifw.Text() tx.color=(0,0,128) tx.font=u'LatinBold17' tx.style = appuifw.STYLE_BOLD tx.set(u"Text") ## new color and style tx.color=(0,0,0) tx.style = appuifw.STYLE_ITALIC tx.add(u"Italic!") ## new font tx.font = u"albi17b" # tx.add(u"Albi17b text!") appuifw.app.body = tx ## Code for handling exit key omitted Slide 41

42 UI framework – ListBox example Source: listbox.py
import appuifw, e32 ##handler for Exit def quit(): appuifw.note(u"Application exiting...") app_lock.signal() ##sets app UI title appuifw.app.exit_key_handler = quit appuifw.app.title = u"Hello!!!" ##callback def lbox_observe(): appuifw.note(u"You have chosen " + entries[listBox.current()][0]) ##sets ListBox icon1 = appuifw.Icon(u'z:\\resource\\apps\\avkon2.mbm', 28, 29) icon2 = appuifw.Icon(u'z:\\resource\\apps\\avkon2.mbm', 40, 41) entries = [(u"Signal", icon1), (u"Battery", icon2)] listBox = appuifw.Listbox(entries, lbox_observe) appuifw.app.body = listBox Slide 42

43 Multimedia framework – audio example Source: audio.py
import appuifw, audio, os MENU = [u"Play",u"Record",u"Delete",u"Say!"] SOUNDFILE = u'C:\\Data\\Images\\sound.wav' sound = None while True: index = appuifw.popup_menu(MENU,u"Select operation") if sound: sound.stop() sound = audio.Sound.open(SOUNDFILE) if index == 0: sound.play() sound.close() elif index == 1: sound.record() appuifw.query(u"Press OK to stop",'query') elif index == 2: os.remove(SOUNDFILE) Slide 43

44 Multimedia framework – camera example Source: camera.py
import e32, camera, appuifw, key_codes      def viewfinder(img):       canvas.blit(img)   def shoot():       camera.stop_finder()       photo = camera.take_photo(size = (640, 480))       w, h = canvas.size       canvas.blit(photo, target = (0, 0, w, 0.75 * w), scale = 1)       photo.save('e:\\Images\\photo.jpg')   appuifw.app.body = canvas = appuifw.Canvas()   appuifw.app.title = u"Camera"   camera.start_finder(viewfinder)   canvas.bind(key_codes.EKeySelect, shoot)   Slide 44

45 Multimedia framework – camera example Source: video.py
import e32, camera, appuifw, key_codes, time def viewfinder(img): canvas.blit(img) def shoot(): camera.start_record("C:\\Data\\Videos\\video.mp4",donothing) appuifw.query(u"Press OK to stop",'query') camera.stop_record() def donothing(a,b): pass canvas = appuifw.Canvas() appuifw.app.body = canvas appuifw.app.title = u"Camera" camera.start_finder(viewfinder) canvas.bind(key_codes.EKeySelect, shoot)    Slide 45

46 Messaging and Inbox – sms example Source: sms.py
import inbox, appuifw, inbox, messaging box = inbox.Inbox() for sms_id in box.sms_messages()[:5]: msg = box.content(sms_id) appuifw.note(msg) messaging.sms_send(" ", u"Hello Bossa!")    Slide 46

47 Connectivity – urllib Source: webclient.py
import urllib, appuifw, e32 URL = " dest_file = u"C:\\Data\\Images\\python-logo.gif" urllib.urlretrieve(URL, dest_file) lock = e32.Ao_lock() viewer = appuifw.Content_handler(lock.signal) viewer.open(dest_file) lock.wait() Slide 47

48 Connectivity – bluetooth photo sender Source: bluetooth.py
import camera, e32, socket, appuifw PHOTO = u"C:\\Data\\Images\\bt_photo_send.jpg" def send_photo(): try: address, services = socket.bt_obex_discover() except: appuifw.note(u"OBEX Push not available", "error") return if u'OBEX Object Push' in services: channel = services[u'OBEX Object Push'] socket.bt_obex_send_file(address, channel, PHOTO) appuifw.note(u"photo sent", "info") else: Slide 48

49 Thank you! Questions?


Download ppt "Daniel Rocha Forum Nokia"

Similar presentations


Ads by Google