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 

My handy mp3 "alarm clock" written in Ruby.

// Simple tool to help wake me up in the morning. Not the cleanest code, but it took no time to write. Ruby rocks!

#!/bin/env ruby
#
# A helper to setup an alarm to slowly and leisurely wake up in the morning.
#
# $Id: mp3wakeup.rb 6 2007-01-31 16:02:57Z btek $
#

if ARGV.length < 1
	puts "Usage: #{$0} <time> [mp3dir]"
	exit 1
end

#############################
# Configuration parameters. #
#############################

NUM_STEPS = 15
STEP_DURATION = 60 * 1

FINAL_VOLUME = 95
INITIAL_VOLUME = 30
VOLUME_STEP = (FINAL_VOLUME - INITIAL_VOLUME) / NUM_STEPS

MIN_SEC_BEFORE_ALARM = NUM_STEPS * STEP_DURATION

#############################
#############################

time = Regexp.quote ARGV[0]
mp3dir = Regexp.quote(ARGV[1]) if ARGV.length > 1

def get_alarm_time(desired_hour, desired_minute)
  now = Time.new
  
  alarm_time = Time.local(now.year, now.month, now.day, desired_hour, desired_minute, 0, 0)
  
  if alarm_time - now < MIN_SEC_BEFORE_ALARM
    alarm_time = alarm_time + 60*60*24
  end
  
  return alarm_time
end

def run(cmd)
  #puts cmd
  system cmd
end

puts "Waking you up by #{time} with songs from #{mp3dir}."

match = time.match(/(\d+)(:(\d+))?/)
hour = match[1].to_i
minute = if (match.length > 1)
           match[3].to_i 
         else
           0
         end

alarm_time = get_alarm_time(hour, minute)

for i in 0..NUM_STEPS
  alert_time = alarm_time - (STEP_DURATION * i)
  volume = FINAL_VOLUME - (VOLUME_STEP * i)
  
  at_cmd = "at #{alert_time.hour.to_s.rjust(2, '0')}:#{alert_time.min.to_s.rjust(2, '0')}"
  run "echo amixer sset PCM #{volume}% | #{at_cmd}"
  
  if i == NUM_STEPS
    # Start playing after time is confirmed set.
    alert_time = alert_time + (60)
    at_cmd = "at #{alert_time.hour.to_s.rjust(2, '0')}:#{alert_time.min.to_s.rjust(2, '0')}"
    
    if mp3dir != nil
      run "echo audacious #{mp3dir} | #{at_cmd}"
    else
      run "echo audtool playback-play | #{at_cmd}"
    end
  end
end

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.

Make a phone vibrating periodically

Some of my friends who practise mindfulness meditation
use an alarm device that vibrate every 2 minutes and
he will become mindful then.
Danny O'Brien of Life Hacks fame asked me about this too.
So, here's a short example (without parameter setting GUI)
that does exactly this.

You need to have miso library install. Only Series 60
2nd Ed FP2 device (Nokia 6630, Nokia 6680) can be used.
See vibrate(...) in miso documentation
http://pdis.hiit.fi/pdis/download/miso/miso-1.40-api.html
import appuifw, miso, e32

# run-and-break type of app
running = 1
def set_exit():
    global running
    running = 0
appuifw.app.exit_key_handler= set_exit

# main loop
while running:
    miso.vibrate(500, 100)  # vibrate for 500 millisec, at full speed
    e32.ao_sleep(10)   # vibrate every 10 seconds

I don't have a FP2 phone to test this. Though the code is
pretty straight forward, please report if there is a problem.
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS