Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # stopwatch.py
- from appuifw import *
- import e32,audio,appuifw,os,sys,key_codes,time,math,random,graphics,sensor,copy,key_codes,datetime
- from graphics import *
- # Colors
- RGB_BLACK = ( 0, 0, 0)
- RGB_WHITE = ( 255, 255, 255)
- RGB_RED = ( 255, 0, 0)
- RGB_GREEN = ( 0, 210, 0)
- RGB_BLUE = ( 0, 0, 210)
- RGB_GREY = ( 180, 180, 180)
- RGB_YELLOW = ( 255, 255, 0)
- # 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)
- light_on()
- #
- def draw(rect=0):
- if img:
- canvas.blit(img)
- #
- def quit():
- cv.go=0
- app.exit_key_handler = quit
- img = None
- xm,ym = 360,640 # testrun
- app.orientation = 'landscape'
- app.screen = 'large'
- app.directional_pad = False
- canvas = Canvas(redraw_callback = draw)
- app.body = canvas
- xm, ym = canvas.size
- img = graphics.Image.new((xm, ym))
- xx = xm/2
- yy = ym/2
- def tap(pos):
- print time.time(), pos
- x,y = pos
- if y > 240:
- if x < 220:
- start_x_pause()
- elif x < 440:
- reset()
- '''
- else:
- quit()
- '''
- canvas.bind(key_codes.EButton1Down,tap)
- class cv: pass
- cv=cv()
- def start_x_pause():
- if cv.freeze:
- cv.freeze = 0
- cv.state = (1,0)[cv.state]
- if cv.curr == zero:
- cv.prev = datetime.datetime.utcnow()
- cv.fg=RGB_RED
- def reset():
- img.clear(bg)
- cv.prev = datetime.datetime.utcnow()
- cv.curr = zero
- cv.freeze = 0
- plot()
- def freeze():
- if cv.state:
- cv.freeze = (1,0)[cv.freeze]
- #
- skip=0
- app.menu = [(u'Start', start_x_pause), (u'Reset', reset)]
- cv.go=1
- def update_timeText():
- dtx = datetime.datetime.utcnow() - cv.prev
- cv.prev = datetime.datetime.utcnow()
- if cv.state:
- cv.curr += dtx
- plot()
- def plot():
- img.clear(bg)
- dt = cv.curr
- s = dt.seconds
- ms = dt.microseconds / 100000
- m = s / 60
- h = s / (60*60)
- if cv.curr == zero: h=m=s=ms=0
- img.text((20,195), u"%02d:%02d" % (m%60,s%60), fg, font=('normal',240))
- img.text((180,240), u"Hours: " + str(h), fg, font=('normal',40))
- img.text((250,300), u'RESET', fg, font=('normal',60))
- #img.text((500,300), u'EXIT', fg, font=('normal',60))
- img.text((560,240), u".%01d" % (ms), font=('normal',80))
- #
- cv.state = 0
- skip = 0
- zero = datetime.datetime.now() - datetime.datetime.now()
- fg = 0xffffff
- bg = 0x5555ff
- reset()
- while cv.go:
- if time.time() > skip:
- skip=time.time()+0.05
- update_timeText()
- sw='START'
- if cv.state: sw='PAUSE'
- img.rectangle((0,240,220,360), fill=bg)
- img.text((30,300), u''+sw, fg, font=('normal',60))
- draw()
- e32.ao_yield()
- #
- print "ended"
Add Comment
Please, Sign In to add comment