DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

M Clock On Pys60

08.08.2005
| 6753 views |
  • submit to reddit
        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.
from __future__ import division
from appuifw import *
import math, time, e32

app.body = c = Canvas()

radius = 72
bigsize = radius * .975
litsize = radius * .67
mx, my = 88, 72
N = 9   # 2,3,4,5,6, 32, 128

def draw(hh, mm, ss, colors=(0, 1, 2)):
    # Set bigd, litd to angles in degrees for big, little hands
    # 12 => 90, 3 => 0, etc.
    bigd = (90 - (mm*60 + ss) / 10) % 360
    litd = (90 - (hh*3600 + mm*60 + ss) / 120) % 360
    # Set bigr, litr to the same values in radians
    bigr = bigd * math.pi / 180
    litr = litd * math.pi / 180
    # Draw the background colored arcs
    drawbg(bigd, litd, colors)
    # Draw the hands
    c.line([mx, my, 
            mx + bigsize*math.cos(bigr),
            my - bigsize*math.sin(bigr)],
            0, width = radius/50)
    c.line([mx, my, 
            mx + litsize*math.cos(litr),
            my - litsize*math.sin(litr)],
            0, width = radius/33)
    # Draw the text
    c.text([5, 144-5], u"%02d:%02d:%02d" % (hh, mm, ss), 0xffffff)

def drawbg(bigd, litd, colors=(0, 1, 2)):
    c.clear(0)
    table = []
    for angle, colorindex in [(bigd - 180/N, 0),
                              (litd - 180/N, 1),
                              (  90 - 180/N, 2)]:
        angle %= 360
        for i in range(N):
            color = 255
            if colorindex in colors:
                color = (N-1-i)*color//(N-1)
            table.append((angle, color, colorindex))
            angle += 360/N
            if angle >= 360:
                angle -= 360
                table.append((0, color, colorindex))
    table.sort()
    table.append((360, None))
    fill = [0, 0, 0]
    for i in range(len(table)-1):
        angle, color, colorindex = table[i]
        fill[colorindex] = color
        if angle < 359:     # for bug when 359==360==0
            c.pieslice([mx-radius,my-radius,mx+radius,my+radius], 
                      angle * math.pi/180, 0,    # start, end
                      fill=tuple(fill), width=0)
    c.line([mx+1, my, mx+radius-1, my], tuple(fill))  # complete at 360 deg

running = 1
def quit():
    global running
    running = 0
app.exit_key_handler= quit

while running:  # redraw loop
    t = time.time() + time.clock()%1    # time() lack decimal precision
    hh, mm, ss = time.localtime(t)[3:6] # +7*60*60
    draw(hh, mm, ss, (0,1,2))
    e32.ao_sleep(1-t%1)
You can see the sceenshot from here.
http://flickr.com/photos/korakot/32337789/    

Comments

Snippets Manager replied on Tue, 2009/03/03 - 6:29am

How would the M Clock work with other data at def drawbg? Think it will work in a colored diamonds clock at the Fancy Diamonds website?