Advertisement
here2share

# accl_with_gc.py

Jan 5th, 2015
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # accl_with_gc.py
  2.  
  3. from sensor import *
  4. import e32
  5. import time
  6. from appuifw import *
  7. from graphics import *
  8.  
  9. import gc
  10.  
  11. # Define exit function
  12. def quit():
  13.     t.cancel()
  14.     App_lock.signal()
  15. app.exit_key_handler = quit
  16.  
  17. def draw(rect):
  18.     try: canvas.blit(img)
  19.     except: pass
  20.  
  21. app.screen = 'large'
  22. app.directional_pad = False
  23. app.orientation = 'portrait'
  24. app.body = canvas = Canvas(redraw_callback=draw)
  25. xm, ym = canvas.size
  26. img = Image.new((xm, ym))
  27. xx = xm/2
  28. yy = ym/2
  29.  
  30.  
  31. # Function which draws circle with given radius at given co-ordinate
  32. def circle(x,y,radius=10, outline=0, fill=0xffff00, width=1):
  33.     img.ellipse((x-radius, y-radius, x+radius, y+radius), outline, fill, width)
  34.  
  35. t = e32.Ao_timer()
  36. def light_on():
  37.     #Reset the user inactivity time, turning the backlight on
  38.     e32.reset_inactivity()
  39.     #Set the timer to call this function every few seconds
  40.     t.after(10, light_on)
  41.  
  42. class SensorConnection():
  43.     def __init__(self):
  44.         self.accelerometer = \
  45.         AccelerometerXYZAxisData(data_filter=LowPassFilter())
  46.         self.accelerometer.set_callback(data_callback=self.sensor_callback)
  47.  
  48.     def sensor_callback(self):
  49.         x = self.accelerometer.x
  50.         y = self.accelerometer.y
  51.         z = self.accelerometer.z
  52.  
  53.         gc.collect() ### prevents glitches -- GC is not recommended...
  54.         ### but seems to be the only very fast solution, especially vs delaying the process
  55.         layout(x,y,z)
  56.  
  57.     def run(self):
  58.         self.accelerometer.start_listening()
  59.  
  60. def layout(x,y,z):
  61.     img.clear()
  62.     x2 = xx+x*3
  63.     y2 = yy-y*3
  64.     img.line((0,yy,xm,yy),outline=0,width=1)
  65.     img.line((xx,0,xx,ym),outline=0,width=1)
  66.     circle(x2, y2, 12, fill=0x999999, width=0)
  67.     img.text((20,40),'X:'+unicode(x),(255,0,0))
  68.     img.text((20,80),'Y:'+unicode(y),(0,210,0))
  69.     img.text((20,120),'Z:'+unicode(z),(0,0,255))
  70.     img.line((xx,30,xx+x,30),(255,0,0),width=10)
  71.     img.line((xx,70,xx+y,70),(0,210,0),width=10)
  72.     img.line((xx,110,xx+z,110),(0,0,255),width=10)
  73.    
  74.     draw(())
  75.  
  76. acl = SensorConnection()
  77. acl.run()
  78. light_on()
  79. App_lock = e32.Ao_lock()
  80. App_lock.wait()  # Wait for exit event
  81. acl.accelerometer.stop_listening()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement