# This code has been significantly modified from the original source, which # is distributed with Nokia's Python for Series 60, by Christopher Schmidt, # and is released under the same license as that code (see below). # # Copyright (c) 2008 Christopher Schmidt # Copyright (c) 2005 Nokia Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import appuifw from graphics import * import e32 import sensor class SensorConnection(object): delta = [] def __init__(self): """Connect to the sensor.""" sens = sensor.sensors()['AccSensor'] self.s = sensor.Sensor(sens['id'], sens['category']) self.s.connect(self.callme) def callme(self, state): self.delta = [] for key in ['data_1', 'data_2', 'data_3']: val = state[key] self.delta.append(int(val + 40)/80) def cleanup(self): """Cleanup after yourself. *Must be called* before exiting.""" self.s.disconnect() sense_conn = SensorConnection() appuifw.app.screen='full' img=None def handle_redraw(rect): if img: canvas.blit(img) appuifw.app.body=canvas=appuifw.Canvas( redraw_callback=handle_redraw) img=Image.new(canvas.size) running=1 def quit(): global running running=0 appuifw.app.exit_key_handler=quit location=[img.size[0]/2,img.size[1]/2] speed=[0.,0.] blobsize=16 xs,ys=img.size[0]-blobsize,img.size[1]-blobsize acceleration=0.05 friction = 0.993 import time start_time=time.clock() n_frames=0 # To speed things up, we prerender the text. labeltext=u'Tilt the phone to move' textrect=img.measure_text(labeltext, font='normal')[0] text_img=Image.new((textrect[2]-textrect[0],textrect[3]-textrect[1])) text_img.clear(0) text_img.text((-textrect[0],-textrect[1]),labeltext,fill=0xffffff,font='normal') while running: img.clear(0) img.blit(text_img, (0,0)) img.point((location[0]+blobsize/2,location[1]+blobsize/2), 0x00ff00,width=blobsize) handle_redraw(()) e32.ao_yield() e32.reset_inactivity() speed[0]*=friction speed[1]*=friction location[0]+=speed[0] location[1]+=speed[1] n_frames+=1 if not sense_conn: continue if not len(sense_conn.delta): continue x_bounce_factor = .8 * (1 - min(6,abs(sense_conn.delta[0])) / 9) y_bounce_factor = .8 * (1 - min(6,abs(sense_conn.delta[1])) / 9) if location[0]>xs: location[0]=xs-(location[0]-xs) speed[0]= -x_bounce_factor * speed[0] speed[1]=0.90*speed[1] if location[0]<0: location[0]=-location[0] speed[0]= -x_bounce_factor * speed[0] speed[1]=0.90*speed[1] if location[1]>ys: location[1]=ys-(location[1]-ys) speed[0]=0.90*speed[0] speed[1]= -y_bounce_factor * speed[1] if location[1]<0: location[1]=-location[1] speed[0]=0.90*speed[0] speed[1]= -y_bounce_factor * speed[1] speed[0] -= (sense_conn.delta[1]) * acceleration speed[1] -= (sense_conn.delta[0]) * acceleration speed[0] = max(min(xs / 2, speed[0]), -xs/2) speed[1] = max(min(ys / 2, speed[1]), -ys/2) end_time=time.clock() total=end_time-start_time sense_conn.cleanup() print "%d frames, %f seconds, %f FPS, %f ms/frame."%(n_frames,total, n_frames/total, total/n_frames*1000.)