Python / Pys60
Total Page:16
File Type:pdf, Size:1020Kb
Symbian OS Python / PyS60 1 Andreas Jakl, 2009 v1.2 – 01 March 2009 PyS60 – Examples ● ShakerRacer www.youtube.com/watch?v=EMjAYdF13cU 2 Andreas Jakl, 2009 PyS60 – Examples ● PyWuzzler (Benjamin Gmeiner, Yen-Chia Lin) 3 Andreas Jakl, 2009 Controlling Super Mario PyS60 – Examples by movements (jumping, walking, ...) ● NiiMe Playing drums Controlling flight / racing games with tilting http://www.niime.com/ 4 Andreas Jakl, 2009 PyS60 – Examples Time-lapse Photography http://blog.foozia.com/blog/2007/jan/21/python-s60-time-lapse-photography-using-nokia-n80/ aspyplayer Last.fm-Player for PyS60 http://code.google.com/p/aspyplayer/ pyPoziomica Use your phone as a level tool http://www.symbian-freak.com/news/007/12/pypoziomica_freeware_level_tool.htm PyED Edit Python source code on the phone http://sourceforge.net/projects/pyed/ 5 Andreas Jakl, 2009 What is it all about? Python 6 Andreas Jakl, 2009 Python ● Older than you might think: 1989 – 1991 by Guido van Rossum (National Research Institute for Mathematics and Computer Science, Netherlands) Named after Monty Python’s Flying Circus ● Today: Microcontrollers Mobile Phones Web servers 7 Andreas Jakl, 2009 Scalable ● Modular architecture ● Easy code reuse ● Huge Python standard library ● Extension with own C/C++ modules Shell scripts Huge projects 8 Andreas Jakl, 2009 Mature Helpful Object Interpreted error oriented, Very high & byte- messages, memory level compiled exception manager handling 9 Andreas Jakl, 2009 Fast ● Rapid Application Prototyping ● Easy to learn – can you read this code? import inbox, audio box = inbox.Inbox() msg_id = box.sms_messages()[0] msg_txt = u"Message: " + box.content(msg_id) audio.say(msg_txt) ● What would this be like in C++ or Java ME? ... 10 Andreas Jakl, 2009 Symbian OS + Runtimes Java ME Python .net Basic Perl Widgets Apache / Silverlight (Web Flash Lite Ruby PHP / MySQL (soon) Runtime) S60 (C++) Symbian OS 11 Andreas Jakl, 2009 UI Platforms: S60 ● Unified UI platform based on S60 Official UI platform of Symbian Foundation Former name: Series 60 th Nokia N97 ● Touchscreen support with S60 5 Edition 12 Andreas Jakl, 2009 UI Platforms: S60 www.s60.com Business High-End Multimedia Mass Market Nokia N96 Nokia E66 Samsung Omnia HD Nokia 6121 Classic Nokia 5800 XPressMusic Nokia E71 Nokia N85 SE Idou Nokia 5500 Sport Nokia E90 Samsung INNOV8 13 Andreas Jakl, 2009 Nokia 6210 Navigator PyS60 ● Python port to S60 ● Web Allows easy access to: Flash Python Accelerometer Managed code Camera Java Text-to-speech P.I.P.S. Location Ease of development Ease Symbian Web services Native code C++ Messaging Functionality and performance Bluetooth UI 14 Andreas Jakl, 2009 Setup – Phone PyS60 1.4.x: based on Python 2.3 PyS60 1.9.2+: based on Python 2.5.1, supports new sensor framework ● Install the latest Nokia PC Suite: of S60 3rd Ed., FP2+ http://europe.nokia.com/A4144903 ● Download and install PyS60: 1.4.x: http://sourceforge.net/projects/pys60/ 1.9.x+: https://garage.maemo.org/projects/pys60/ ● Phone: Phone software: PythonForS60_1_x_x_3rdEd.SIS Phone script shell: PythonScriptShell_1_x_x_3rdEd.SIS 15 Andreas Jakl, 2009 Setup – PC ● Extract SDK plug-in to the S60 SDK: PythonForS60_1_x_x_SDK_3rdEd.zip 16 Andreas Jakl, 2009 IDEs ● IDLE – comes with Python SDK C:\Program Files\Python\Lib\idlelib\idle.bat ● PythonWin + Win32 Extensions http://sourceforge.net/projects/pywi n32/ ● PyDev Eclipse/Carbide.c++ Plug-in http://pydev.sf.net/ ● SPE http://pythonide.stani.be/ 17 Andreas Jakl, 2009 Hello World ● Create a file hello.py: hello.py print “Hello World” ● Connect your phone to the PC (“PC Suite” connection mode) ● Transfer the script to E:\Python\ (memory card) using the Nokia PC suite file manager ● Run the script: 18 Andreas Jakl, 2009 Hello World – Emulator ● Copy hello.py to: <epocroot>\winscw\c\python\hello.py ● Start the emulator from: <epocroot>\release\winscw\udeb\epoc. exe 19 Andreas Jakl, 2009 PyDev + Emulator ● Either create the project / workspace directly in the python-dir of the emulator ● Or link the project files to source files in the dir: 20 Andreas Jakl, 2009 Starting with the UI PyS60 – User Interface 21 Andreas Jakl, 2009 Module ● Collection of related functions and data grouped together ● Has to be imported at the beginning: import appuifw ● Addressing a function of the module: appuifw.query(label, name) ● Import multiple modules in a single statement: import appuifw, e32 22 Andreas Jakl, 2009 import appuifw Query appuifw.query(u"Type a word:", "text", u"Hello") appuifw.query(u"Type a number:", "number", 7) appuifw.query(u"Type a date:", "date") appuifw.query(u"Type a time:", "time") appuifw.query(u"Type a password:", "code") appuifw.query(u"Do you like PyS60?", "query") Syntax: appuifw.query(label, type[, initial value]) number / text float date time code query 23 Andreas Jakl, 2009 import appuifw Note Dialog appuifw.note(u"Hello") appuifw.note(u"File not found", "error") appuifw.note(u"Upload finished", "conf") Syntax: appuifw.note(text[, type[, global] ] ) info / default error conf 24 Andreas Jakl, 2009 Variables ● Variables not declared ahead of time ● Implicit typing ● Automated memory management Reference counting, garbage collection ● Variable names can be “recycled” ● del statement allows explicit de-allocation 25 Andreas Jakl, 2009 Variables – Example age = 5 name = u"Andreas" # u in front of the string: unicode name += age # Doesn't work, the type isn’t converted automatically name += str(age) # name == Andreas5 name = age # name now points to the same object as age; name == 5 foo = "xyz" foo “xyz” bar = foo # bar points to the same object as foo bar foo = 123 # a new object is created for foo, bar still points to "xyz“ foo “xyz” bar 123 26 Andreas Jakl, 2009 Multi-Query Dialog ● Syntax: appuifw.multi_query(label1, label2) import appuifw pwd = u"secret" info = appuifw.multi_query(u"Username:", u"Password:") if info: // returns a tuple with the info login_id, login_pwd = info if login_pwd == pwd: appuifw.note(u"Login successful", "conf") else: appuifw.note(u"Wrong password", "error") else: // returns None – special type appuifw.note(u"Cancelled") 27 Andreas Jakl, 2009 if-statement ● Works like in other languages ● Blocks are defined by indentation – avoids dangling else ● if expression1: expr1_true_suite elif expression2: expr2_true_suite else: none_of_the_above_suite 28 Andreas Jakl, 2009 Lists, Tuples and Dictionaries ● Generic “arrays” for arbitrary number of arbitrary objects ● Ordered and accessed via index offsets ● List – created using [ ] myList = [1, 2, 3, 4] # [1, 2, 3, 4] print myList[0] # 1 # Subsets: sequence[starting_index:ending_index] print myList[1:3] # [2, 3] print myList[2:] # [3, 4] print myList[:3] # [1, 2, 3] myList[1] = 5 # [1, 5, 3, 4] myList[2] = ["bla", (-2.3+4j)] # [1, 5, ["bla", (-2.3+4j)], 4] print myList[2][1] # -2.3+4j print 4 in myList # True 29 Andreas Jakl, 2009 Lists, Tuples and Dictionaries ● Tuple – created using ( ) Immutable – can therefore be used as dictionary keys Also useful when you don’t want a function to be able to change your data myTuple1 = ('python', 's60', 27) print myTuple1[0] # python myTuple[1] = 'no' # tuples are immutable – exception! myTuple2 = ('symbian', 'uiq') myTuple3 = myTuple1 + myTuple2 print myTuple3 # ('python', 's60', 27, 'symbian', 'uiq') 30 Andreas Jakl, 2009 Lists, Tuples and Dictionaries ● Dictionary – created using { } Mapping type – like associative arrays or hashes in Perl Key-value pairs – Keys: almost any Python type, usually numbers or stings – Values: arbitrary Python object myDict = {'planet': 'earth'} myDict['port'] = 80 print myDict # {'planet': 'earth', 'port': 80} print myDict.keys() # ['planet', 'port'] print myDict['planet'] # earth for key in myDict: print "key=%s, value=%s" % (key, myDict[key]) # key=planet, value=earth # key=port, value=80 31 Andreas Jakl, 2009 Popup Menu ● Syntax: appuifw.popup_menu(list[, label ]) import appuifw items = [u"The Journey", u"RealReplay", u"ShakerRacer"] index = appuifw.popup_menu(items, u"Buy:") if index == 0: appuifw.note(u"Great choice") elif index == 1: appuifw.note(u"Cool") elif index == 2: appuifw.note(u"I like that") elif index == None: appuifw.note(u"Purchase cancelled") 32 Andreas Jakl, 2009 Selection List Search field appears after pressing a letter key ● Syntax: appuifw.selection_list(choices[, search_field=0]) import appuifw names = [u"Michael", u"Devon", u"Bonnie", u"April", u"RC3"] index = appuifw.selection_list(names, 1) if index == 2: print "I love you!" else: print "You're great!" 33 Andreas Jakl, 2009 Multi-Selection List ● Syntax: appuifw.multi_selection_list( choices[, style=„checkbox‟, search_field=0]) import appuifw names = [u"Michael", u"Devon", u"Bonnie", u"April", u"RC3"] selections = appuifw.multi_selection_list(names, 'checkbox', 1) print selections Style: checkmark Select multiple items with the pen key Not really important 34 Andreas Jakl, 2009 for loop ● for iter_var in iterable: suite_to_repeat ● With each loop, iter_var set to current element of iterable Similar to foreach in other languages 35 Andreas Jakl, 2009 for Loop – Examples # Iterating over a string current letter: T for eachLetter in "Text": current letter: e print "current letter:", eachLetter current letter: x current letter: t # Iterating by sequence item Charles nameList = ["Mike", "Sarah", "Charles"] for eachName in sorted(nameList): Mike print eachName Sarah # Iterating by sequence index Mike for nameIndex in range(len(nameList)): Sarah print nameList[nameIndex] Charles # Iterate over a range value: 0 for eachVal in range(3):