Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

M Clock on pys60

See this article in Guido's blog
http://www.artima.com/weblogs/viewpost.jsp?thread=122250
I then try to implement M Clock on pys60. I remove a few
things to make the code short.
   1  
   2  from __future__ import division
   3  from appuifw import *
   4  import math, time, e32
   5  
   6  app.body = c = Canvas()
   7  
   8  radius = 72
   9  bigsize = radius * .975
  10  litsize = radius * .67
  11  mx, my = 88, 72
  12  N = 9   # 2,3,4,5,6, 32, 128
  13  
  14  def draw(hh, mm, ss, colors=(0, 1, 2)):
  15      # Set bigd, litd to angles in degrees for big, little hands
  16      # 12 => 90, 3 => 0, etc.
  17      bigd = (90 - (mm*60 + ss) / 10) % 360
  18      litd = (90 - (hh*3600 + mm*60 + ss) / 120) % 360
  19      # Set bigr, litr to the same values in radians
  20      bigr = bigd * math.pi / 180
  21      litr = litd * math.pi / 180
  22      # Draw the background colored arcs
  23      drawbg(bigd, litd, colors)
  24      # Draw the hands
  25      c.line([mx, my, 
  26              mx + bigsize*math.cos(bigr),
  27              my - bigsize*math.sin(bigr)],
  28              0, width = radius/50)
  29      c.line([mx, my, 
  30              mx + litsize*math.cos(litr),
  31              my - litsize*math.sin(litr)],
  32              0, width = radius/33)
  33      # Draw the text
  34      c.text([5, 144-5], u"%02d:%02d:%02d" % (hh, mm, ss), 0xffffff)
  35  
  36  def drawbg(bigd, litd, colors=(0, 1, 2)):
  37      c.clear(0)
  38      table = []
  39      for angle, colorindex in [(bigd - 180/N, 0),
  40                                (litd - 180/N, 1),
  41                                (  90 - 180/N, 2)]:
  42          angle %= 360
  43          for i in range(N):
  44              color = 255
  45              if colorindex in colors:
  46                  color = (N-1-i)*color//(N-1)
  47              table.append((angle, color, colorindex))
  48              angle += 360/N
  49              if angle >= 360:
  50                  angle -= 360
  51                  table.append((0, color, colorindex))
  52      table.sort()
  53      table.append((360, None))
  54      fill = [0, 0, 0]
  55      for i in range(len(table)-1):
  56          angle, color, colorindex = table[i]
  57          fill[colorindex] = color
  58          if angle < 359:     # for bug when 359==360==0
  59              c.pieslice([mx-radius,my-radius,mx+radius,my+radius], 
  60                        angle * math.pi/180, 0,    # start, end
  61                        fill=tuple(fill), width=0)
  62      c.line([mx+1, my, mx+radius-1, my], tuple(fill))  # complete at 360 deg
  63  
  64  running = 1
  65  def quit():
  66      global running
  67      running = 0
  68  app.exit_key_handler= quit
  69  
  70  while running:  # redraw loop
  71      t = time.time() + time.clock()%1    # time() lack decimal precision
  72      hh, mm, ss = time.localtime(t)[3:6] # +7*60*60
  73      draw(hh, mm, ss, (0,1,2))
  74      e32.ao_sleep(1-t%1)

You can see the sceenshot from here.
http://flickr.com/photos/korakot/32337789/
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS