Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # accl_with_gc.py
- from sensor import *
- import e32
- import time
- from appuifw import *
- from graphics import *
- import gc
- # Define exit function
- def quit():
- t.cancel()
- App_lock.signal()
- app.exit_key_handler = quit
- def draw(rect):
- try: canvas.blit(img)
- except: pass
- app.screen = 'large'
- app.directional_pad = False
- app.orientation = 'portrait'
- app.body = canvas = Canvas(redraw_callback=draw)
- xm, ym = canvas.size
- img = Image.new((xm, ym))
- xx = xm/2
- yy = ym/2
- # Function which draws circle with given radius at given co-ordinate
- def circle(x,y,radius=10, outline=0, fill=0xffff00, width=1):
- img.ellipse((x-radius, y-radius, x+radius, y+radius), outline, fill, width)
- t = e32.Ao_timer()
- def light_on():
- #Reset the user inactivity time, turning the backlight on
- e32.reset_inactivity()
- #Set the timer to call this function every few seconds
- t.after(10, light_on)
- class SensorConnection():
- def __init__(self):
- self.accelerometer = \
- AccelerometerXYZAxisData(data_filter=LowPassFilter())
- self.accelerometer.set_callback(data_callback=self.sensor_callback)
- def sensor_callback(self):
- x = self.accelerometer.x
- y = self.accelerometer.y
- z = self.accelerometer.z
- gc.collect() ### prevents glitches -- GC is not recommended...
- ### but seems to be the only very fast solution, especially vs delaying the process
- layout(x,y,z)
- def run(self):
- self.accelerometer.start_listening()
- def layout(x,y,z):
- img.clear()
- x2 = xx+x*3
- y2 = yy-y*3
- img.line((0,yy,xm,yy),outline=0,width=1)
- img.line((xx,0,xx,ym),outline=0,width=1)
- circle(x2, y2, 12, fill=0x999999, width=0)
- img.text((20,40),'X:'+unicode(x),(255,0,0))
- img.text((20,80),'Y:'+unicode(y),(0,210,0))
- img.text((20,120),'Z:'+unicode(z),(0,0,255))
- img.line((xx,30,xx+x,30),(255,0,0),width=10)
- img.line((xx,70,xx+y,70),(0,210,0),width=10)
- img.line((xx,110,xx+z,110),(0,0,255),width=10)
- draw(())
- acl = SensorConnection()
- acl.run()
- light_on()
- App_lock = e32.Ao_lock()
- App_lock.wait() # Wait for exit event
- acl.accelerometer.stop_listening()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement