PyS60 Beispiele
Contents
Beispiel-Code aus dem PyCologne-Vortrag Programmierung von Mobiltelefonen mit Python. Der Code ist getestet mit den PyS60-Versionen 1.4.5 und 1.9.1
Hello World
1 import appuifw 2 appuifw.app.title = u"Hello World" 3 appuifw.note(u"Hello World!", 'info')
Versenden einer SMS
1 # Copyright (c) 2008 Andreas Schreiber 2 # based on a script by Jürgen Scheible 3 4 import appuifw 5 import messaging 6 7 data = appuifw.query(u"what are you doing?", "text") 8 9 receiver = "+491729407775" 10 11 if appuifw.query(u"Send message?", "query") == True: 12 messaging.sms_send(receiver, data) 13 14 appuifw.note(u"Message sent.", "info") 15 else: 16 appuifw.note(u"Messages not sent!", "info")
File: send_sms.py
SMS-Empfang und Sound
1 # Copyright (c) 2008 Andreas Schreiber 2 3 import os 4 import appuifw 5 import e32 6 import audio 7 import inbox 8 9 class SoundRecorder: 10 filename = 'e:\\boo.wav' # SD card 11 mySound = None 12 13 def record(self): 14 self.mySound = audio.Sound.open(self.filename) 15 self.mySound.record() 16 17 def play(self): 18 try: 19 self.mySound = audio.Sound.open(self.filename) 20 self.mySound.play() 21 except: 22 appuifw.note(u"No sound recorded!" , "error") 23 24 def stop(self): 25 if self.mySound: 26 self.mySound.stop() 27 self.mySound.close() 28 29 def clear(self): 30 self.stop() 31 os.remove(self.filename) 32 33 recorder = SoundRecorder() 34 35 def message_received(msg_id): 36 box = inbox.Inbox() 37 sms_text = box.content(msg_id) 38 appuifw.note(u"sms content: " + sms_text , "info") 39 if sms_text == "play": 40 recorder.play() 41 elif sms_text == "record": 42 recorder.record() 43 elif sms_text == "stop": 44 recorder.stop() 45 elif sms_text == "clear": 46 recorder.clear() 47 app_lock.signal() 48 49 50 box = inbox.Inbox() 51 box.bind(message_received) 52 53 def quit(): 54 app_lock.signal() 55 appuifw.app.set_exit() 56 57 print "Waiting for new SMS messages..." 58 appuifw.app.exit_key_handler = quit 59 app_lock = e32.Ao_lock() 60 app_lock.wait()
File:
Kontakte-Datenbank
1 # Copyright (c) 2008 Andreas Schreiber 2 3 import contacts 4 5 db = contacts.open() 6 7 print u"Searching contacts...\n" 8 found = db.find('guy') # search in name, email, etc. 9 10 for guy in found: 11 firstname = guy.find('first_name')[0].value 12 company = guy.find('company_name')[0].value 13 email_address = guy.find('email_address')[0].value # first only 14 # other fields: email_address, url, company_name, job_title, 15 # phone_number, fax_number, note, etc. 16 print u"%s\n %s\n %s" % (firstname, company, email_address)
File: contacs_demo.py
Kamera und File-Upload
1 # Copyright (c) 2008 Andreas Schreiber 2 3 import time 4 import httplib 5 import urllib 6 import base64 7 import os.path 8 9 import e32 10 import appuifw 11 import camera 12 import graphics 13 import key_codes 14 15 text = appuifw.Text() 16 canvas = appuifw.Canvas() 17 18 def finder_callback(im): 19 canvas.blit(im) 20 21 22 def imageToURL(aPath): 23 # read the binary data of the picture 24 data = open(aPath, 'rb').read() 25 # encoded it to base64 26 encodedData = base64.encodestring( data ) 27 headers = {"Content-type": "application/x-www-form-urlencoded", 28 "Accept": "text/plain", 29 } 30 31 params = urllib.urlencode({ u'fileName': os.path.split(aPath)[1], 32 u'data':encodedData}) 33 34 conn = httplib.HTTPConnection("www.onyame.de") 35 conn.request("POST", "/image_uploader.php", params, headers ) 36 response = conn.getresponse() 37 38 # returns "True" or "False" if failed 39 print response.read() 40 41 # status for debugging 42 print response.status 43 44 conn.close() 45 return response.status 46 47 48 def take_photo(): 49 camera.stop_finder() 50 51 filename = time.strftime("n95-%Y%m%d%H%M%S.jpg", time.gmtime()) 52 53 image = camera.take_photo() 54 image.save(filename) 55 56 appuifw.note(u"Photo saved to %s" % filename, 'info') 57 text.add(u"Photo saved to %s\n" % filename) 58 59 if imageToURL(filename) == 200: 60 appuifw.note(u"Photo uploaded successfully.", 'info') 61 text.add(u"Photo uploaded successfully.") 62 else: 63 appuifw.note(u"Photo upload failed!", 'error') 64 text.add(u"Photo upload failed!") 65 66 camera.start_finder(finder_callback) 67 68 appuifw.app.menu = [(u"Take photo", take_photo),] 69 appuifw.app.screen = 'normal' 70 appuifw.app.title = u"Photo Uploader" 71 72 appuifw.app.body = canvas 73 camera.start_finder(finder_callback) 74 canvas.bind(key_codes.EKeySelect, take_photo) 75 76 def quit(): 77 app_lock.signal() 78 79 appuifw.app.exit_key_handler = quit 80 81 app_lock = e32.Ao_lock() 82 app_lock.wait()
File: camera_upload.py
Die hochgeladenen Fotos sind zu finden unter: http://www.onyame.de/upload/fb.php
Mandelbrot-Menge
File: mandelbrot.py
Bewegungssensor
Sensor Analyzer: http://jouni.miettunen.googlepages.com/sensoranalyzer
Location Based Service
1 # Copyright (c) 2008 Andreas Schreiber 2 3 import appuifw 4 import e32 5 import messaging 6 import location 7 8 HOME_CELL_ID = u"98521779" # Ersetzen durch eigene Zellen-ID 9 WIFE = u"+491732472xxx" 10 11 mood = u"" 12 13 def shout(): 14 index = listbox.current() 15 print index 16 print entries[index] 17 mood = entries[index] 18 19 entries = [u"freu mich auf Dich!", 20 u"hab Hunger.", 21 u"hab schlechte Laune."] 22 listbox = appuifw.Listbox(entries, shout) 23 24 home = 0 25 while home == 0: 26 country, provider, lac, cell = location.gsm_location() 27 if (cell== HOME_CELL_ID): 28 home = 1 29 message = u"Bin gleich zuhause und %s" % mood 30 messaging.sms_send(WIFE, message) 31 32 33 def quit(): 34 app_lock.signal() 35 appuifw.app.set_exit() 36 37 38 appuifw.app.title = u'Home Announcer' 39 appuifw.app.exit_key_handler = quit 40 app_lock = e32.Ao_lock() 41 app_lock.wait()
File: cell_id_demo.py
OpenGL
Rotierender Kubus mit Textur: http://discussion.forum.nokia.com/forum/showthread.php?t=104331