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

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

Mobile timeout app

from appuifw import *
from key_codes import *
from graphics import Image
from audio import *
import e32, miso

def change_duration():
    global duration
    answer = query(u'How long?', 'time', 18000.0)
    if answer:
        duration = int(answer/60)
        if duration <= 200:
            if query(u'Change to hr:min ?', 'query'):
                duration *= 60

im = Image.new((60,48))
def showtime(rect=None):
    im.clear()
    im.text((5,28), u"%02d:%02d" % divmod(duration,60), font='title')
    c.blit(im, target=(0,0,180,144), scale=1)    # triple size

duration = 300
running = 0
app.body = c = Canvas(showtime)
alert = Sound.open(u'Z:\\Nokia\\Sounds\\Digital\\Cuckoo.awb')
showtime()

def start():
    global duration, running
    running = 1
    while running:
        duration -= 1
        showtime()
        if duration <= 0:
            alert.play(-2)  # repeat play
            break
        if duration % 10 == 0:
            miso.reset_inactivity_time()
        e32.ao_sleep(1)

def toggle():
    global running
    if alert.state() == EPlaying:
        alert.stop()
        return
    running = 1 - running
    if running:
        start()

def quit():
    global running
    running = 0
    if alert.state() == EPlaying:
        alert.stop()
    lock.signal()

lock = e32.Ao_lock()
c.bind(EKeySelect, toggle)  # start, pause, resume
app.menu = [(u'Duration', change_duration),(u'Close', quit)]
app.exit_key_handler = quit
lock.wait()

Remind yourself of how fast time fly with this app.
You should change the sound file to the one you like.
(I use cuckoo.awb)
The duration input abues the time input by treating input
as minutes and seconds instead of hours and minutes.
But if the duration is too short, it will ask you if you mean
hours and minutes.

Mobile timer app

from appuifw import *
from key_codes import *
import e32, time

class StopWatch:
    running = 0
    time_start = None
    elap = 0.0

    def __init__(self):
        self.canvas = Canvas(self.update)
        app.body = self.canvas
        self.canvas.bind(EKeySelect, self.toggle)
        self.update()

    def update(self, rect=None):
        if self.running:
            self.elap = time.clock() - self.time_start
            e32.ao_sleep(0.05, self.update)
        t = self.elap
        min = int(t / 60)
        sec =  int(t - min*60)
        hsec = int((t - min*60 - sec)*100)
        self.canvas.clear()
        self.canvas.text((20,40), u"%02d:%02d:%02d" % (min,sec,hsec), font='title')

    def toggle(self):
        if self.running:
            self.running = 0
            self.elap = time.clock() - self.time_start
        else:
            self.running = 1
            self.time_start = time.clock() - self.elap
            self.update()

    def reset(self):
        self.running = 0
        self.elap = 0.0
        self.update()


sw = StopWatch()
lock = e32.Ao_lock()
app.menu = [(u'Reset', sw.reset), (u'Close', lock.signal)]
app.exit_key_handler = lock.signal
lock.wait()

This will run and pause, resume when you press the select key.

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.
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/
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS