here2share

s60_stopwatch.py

Mar 5th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # stopwatch.py
  2.  
  3. from appuifw import *
  4. import e32,audio,appuifw,os,sys,key_codes,time,math,random,graphics,sensor,copy,key_codes,datetime
  5. from graphics import *
  6.  
  7. # Colors
  8. RGB_BLACK    = (   0,   0,   0)
  9. RGB_WHITE    = ( 255, 255, 255)
  10. RGB_RED      = ( 255,   0,   0)
  11. RGB_GREEN    = (   0, 210,   0)
  12. RGB_BLUE     = (   0,   0, 210)
  13. RGB_GREY     = ( 180, 180, 180)
  14. RGB_YELLOW   = ( 255, 255,   0)
  15.  
  16. # Function which draws circle with given radius at given co-ordinate
  17. def circle(x,y,radius=10, outline=0, fill=0xffff00, width=1):
  18.     img.ellipse((x-radius, y-radius, x+radius, y+radius), outline, fill, width)
  19. #
  20. t = e32.Ao_timer()
  21. def light_on():
  22.     #Reset the user inactivity time, turning the backlight on
  23.     e32.reset_inactivity()
  24.     #Set the timer to call this function every few seconds
  25.     t.after(10, light_on)
  26. light_on()
  27. #
  28. def draw(rect=0):
  29.     if img:
  30.         canvas.blit(img)
  31. #
  32. def quit():
  33.     cv.go=0
  34. app.exit_key_handler = quit
  35.  
  36. img = None
  37. xm,ym = 360,640 # testrun
  38. app.orientation = 'landscape'
  39. app.screen = 'large'
  40. app.directional_pad = False
  41. canvas = Canvas(redraw_callback = draw)
  42. app.body = canvas
  43. xm, ym = canvas.size
  44. img = graphics.Image.new((xm, ym))
  45.  
  46. xx = xm/2
  47. yy = ym/2
  48.  
  49. def tap(pos):
  50.     print time.time(), pos
  51.     x,y = pos
  52.     if y > 240:
  53.         if x < 220:
  54.             start_x_pause()
  55.         elif x < 440:
  56.             reset()
  57.         '''
  58.         else:
  59.             quit()
  60.         '''
  61. canvas.bind(key_codes.EButton1Down,tap)
  62.  
  63. class cv: pass
  64. cv=cv()
  65.  
  66. def start_x_pause():
  67.     if cv.freeze:
  68.         cv.freeze = 0
  69.     cv.state = (1,0)[cv.state]
  70.     if cv.curr == zero:
  71.         cv.prev = datetime.datetime.utcnow()
  72.     cv.fg=RGB_RED
  73.  
  74. def reset():
  75.     img.clear(bg)
  76.     cv.prev = datetime.datetime.utcnow()
  77.     cv.curr = zero
  78.     cv.freeze = 0
  79.     plot()
  80.  
  81. def freeze():
  82.     if cv.state:
  83.         cv.freeze = (1,0)[cv.freeze]
  84. #
  85. skip=0
  86. app.menu = [(u'Start', start_x_pause), (u'Reset', reset)]
  87. cv.go=1
  88.  
  89. def update_timeText():
  90.     dtx = datetime.datetime.utcnow() - cv.prev
  91.     cv.prev = datetime.datetime.utcnow()
  92.     if cv.state:
  93.         cv.curr += dtx
  94.         plot()
  95.  
  96. def plot():
  97.     img.clear(bg)
  98.     dt = cv.curr
  99.     s = dt.seconds
  100.     ms = dt.microseconds / 100000
  101.     m = s / 60
  102.     h = s / (60*60)
  103.     if cv.curr == zero: h=m=s=ms=0
  104.     img.text((20,195), u"%02d:%02d" % (m%60,s%60), fg, font=('normal',240))
  105.     img.text((180,240), u"Hours: " + str(h), fg, font=('normal',40))
  106.     img.text((250,300), u'RESET', fg, font=('normal',60))
  107.     #img.text((500,300), u'EXIT', fg, font=('normal',60))
  108.     img.text((560,240), u".%01d" % (ms), font=('normal',80))
  109. #
  110.  
  111. cv.state = 0
  112. skip = 0
  113. zero = datetime.datetime.now() - datetime.datetime.now()
  114. fg = 0xffffff
  115. bg = 0x5555ff
  116.  
  117. reset()
  118. while cv.go:
  119.     if time.time() > skip:
  120.         skip=time.time()+0.05
  121.         update_timeText()
  122.         sw='START'
  123.         if cv.state: sw='PAUSE'
  124.         img.rectangle((0,240,220,360), fill=bg)
  125.         img.text((30,300), u''+sw, fg, font=('normal',60))
  126.         draw()
  127.     e32.ao_yield()
  128. #
  129.  
  130. print "ended"
Add Comment
Please, Sign In to add comment