<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: alarm code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 12 Oct 2008 04:07:16 GMT</pubDate>
    <description>DZone Snippets: alarm code</description>
    <item>
      <title>My handy mp3 "alarm clock" written in Ruby.</title>
      <link>http://snippets.dzone.com/posts/show/3498</link>
      <description>// Simple tool to help wake me up in the morning. Not the cleanest code, but it took no time to write. Ruby rocks!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/env ruby&lt;br /&gt;#&lt;br /&gt;# A helper to setup an alarm to slowly and leisurely wake up in the morning.&lt;br /&gt;#&lt;br /&gt;# $Id: mp3wakeup.rb 6 2007-01-31 16:02:57Z btek $&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;if ARGV.length &lt; 1&lt;br /&gt;	puts "Usage: #{$0} &lt;time&gt; [mp3dir]"&lt;br /&gt;	exit 1&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;#############################&lt;br /&gt;# Configuration parameters. #&lt;br /&gt;#############################&lt;br /&gt;&lt;br /&gt;NUM_STEPS = 15&lt;br /&gt;STEP_DURATION = 60 * 1&lt;br /&gt;&lt;br /&gt;FINAL_VOLUME = 95&lt;br /&gt;INITIAL_VOLUME = 30&lt;br /&gt;VOLUME_STEP = (FINAL_VOLUME - INITIAL_VOLUME) / NUM_STEPS&lt;br /&gt;&lt;br /&gt;MIN_SEC_BEFORE_ALARM = NUM_STEPS * STEP_DURATION&lt;br /&gt;&lt;br /&gt;#############################&lt;br /&gt;#############################&lt;br /&gt;&lt;br /&gt;time = Regexp.quote ARGV[0]&lt;br /&gt;mp3dir = Regexp.quote(ARGV[1]) if ARGV.length &gt; 1&lt;br /&gt;&lt;br /&gt;def get_alarm_time(desired_hour, desired_minute)&lt;br /&gt;  now = Time.new&lt;br /&gt;  &lt;br /&gt;  alarm_time = Time.local(now.year, now.month, now.day, desired_hour, desired_minute, 0, 0)&lt;br /&gt;  &lt;br /&gt;  if alarm_time - now &lt; MIN_SEC_BEFORE_ALARM&lt;br /&gt;    alarm_time = alarm_time + 60*60*24&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  return alarm_time&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def run(cmd)&lt;br /&gt;  #puts cmd&lt;br /&gt;  system cmd&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts "Waking you up by #{time} with songs from #{mp3dir}."&lt;br /&gt;&lt;br /&gt;match = time.match(/(\d+)(:(\d+))?/)&lt;br /&gt;hour = match[1].to_i&lt;br /&gt;minute = if (match.length &gt; 1)&lt;br /&gt;           match[3].to_i &lt;br /&gt;         else&lt;br /&gt;           0&lt;br /&gt;         end&lt;br /&gt;&lt;br /&gt;alarm_time = get_alarm_time(hour, minute)&lt;br /&gt;&lt;br /&gt;for i in 0..NUM_STEPS&lt;br /&gt;  alert_time = alarm_time - (STEP_DURATION * i)&lt;br /&gt;  volume = FINAL_VOLUME - (VOLUME_STEP * i)&lt;br /&gt;  &lt;br /&gt;  at_cmd = "at #{alert_time.hour.to_s.rjust(2, '0')}:#{alert_time.min.to_s.rjust(2, '0')}"&lt;br /&gt;  run "echo amixer sset PCM #{volume}% | #{at_cmd}"&lt;br /&gt;  &lt;br /&gt;  if i == NUM_STEPS&lt;br /&gt;    # Start playing after time is confirmed set.&lt;br /&gt;    alert_time = alert_time + (60)&lt;br /&gt;    at_cmd = "at #{alert_time.hour.to_s.rjust(2, '0')}:#{alert_time.min.to_s.rjust(2, '0')}"&lt;br /&gt;    &lt;br /&gt;    if mp3dir != nil&lt;br /&gt;      run "echo audacious #{mp3dir} | #{at_cmd}"&lt;br /&gt;    else&lt;br /&gt;      run "echo audtool playback-play | #{at_cmd}"&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 12 Feb 2007 21:21:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3498</guid>
      <author>bhtek (Boon Hian Tek)</author>
    </item>
    <item>
      <title>Mobile timeout app</title>
      <link>http://snippets.dzone.com/posts/show/777</link>
      <description>&lt;code&gt;&lt;br /&gt;from appuifw import *&lt;br /&gt;from key_codes import *&lt;br /&gt;from graphics import Image&lt;br /&gt;from audio import *&lt;br /&gt;import e32, miso&lt;br /&gt;&lt;br /&gt;def change_duration():&lt;br /&gt;    global duration&lt;br /&gt;    answer = query(u'How long?', 'time', 18000.0)&lt;br /&gt;    if answer:&lt;br /&gt;        duration = int(answer/60)&lt;br /&gt;        if duration &lt;= 200:&lt;br /&gt;            if query(u'Change to hr:min ?', 'query'):&lt;br /&gt;                duration *= 60&lt;br /&gt;&lt;br /&gt;im = Image.new((60,48))&lt;br /&gt;def showtime(rect=None):&lt;br /&gt;    im.clear()&lt;br /&gt;    im.text((5,28), u"%02d:%02d" % divmod(duration,60), font='title')&lt;br /&gt;    c.blit(im, target=(0,0,180,144), scale=1)    # triple size&lt;br /&gt;&lt;br /&gt;duration = 300&lt;br /&gt;running = 0&lt;br /&gt;app.body = c = Canvas(showtime)&lt;br /&gt;alert = Sound.open(u'Z:\\Nokia\\Sounds\\Digital\\Cuckoo.awb')&lt;br /&gt;showtime()&lt;br /&gt;&lt;br /&gt;def start():&lt;br /&gt;    global duration, running&lt;br /&gt;    running = 1&lt;br /&gt;    while running:&lt;br /&gt;        duration -= 1&lt;br /&gt;        showtime()&lt;br /&gt;        if duration &lt;= 0:&lt;br /&gt;            alert.play(-2)  # repeat play&lt;br /&gt;            break&lt;br /&gt;        if duration % 10 == 0:&lt;br /&gt;            miso.reset_inactivity_time()&lt;br /&gt;        e32.ao_sleep(1)&lt;br /&gt;&lt;br /&gt;def toggle():&lt;br /&gt;    global running&lt;br /&gt;    if alert.state() == EPlaying:&lt;br /&gt;        alert.stop()&lt;br /&gt;        return&lt;br /&gt;    running = 1 - running&lt;br /&gt;    if running:&lt;br /&gt;        start()&lt;br /&gt;&lt;br /&gt;def quit():&lt;br /&gt;    global running&lt;br /&gt;    running = 0&lt;br /&gt;    if alert.state() == EPlaying:&lt;br /&gt;        alert.stop()&lt;br /&gt;    lock.signal()&lt;br /&gt;&lt;br /&gt;lock = e32.Ao_lock()&lt;br /&gt;c.bind(EKeySelect, toggle)  # start, pause, resume&lt;br /&gt;app.menu = [(u'Duration', change_duration),(u'Close', quit)]&lt;br /&gt;app.exit_key_handler = quit&lt;br /&gt;lock.wait()&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Remind yourself of how fast time fly with this app.&lt;br /&gt;You should change the sound file to the one you like.&lt;br /&gt;(I use cuckoo.awb)&lt;br /&gt;The duration input abues the time input by treating input&lt;br /&gt;as minutes and seconds instead of hours and minutes.&lt;br /&gt;But if the duration is too short, it will ask you if you mean&lt;br /&gt;hours and minutes.</description>
      <pubDate>Sat, 01 Oct 2005 19:07:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/777</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Make a phone vibrating periodically</title>
      <link>http://snippets.dzone.com/posts/show/415</link>
      <description>Some of my friends who practise mindfulness meditation&lt;br /&gt;use an alarm device that vibrate every 2 minutes and&lt;br /&gt;he will become mindful then.&lt;br /&gt;Danny O'Brien of Life Hacks fame asked me about this too.&lt;br /&gt;So, here's a short example (without parameter setting GUI)&lt;br /&gt;that does exactly this.&lt;br /&gt;&lt;br /&gt;You need to have miso library install. Only Series 60 &lt;br /&gt;2nd Ed FP2 device (Nokia 6630, Nokia 6680) can be used.&lt;br /&gt;See vibrate(...) in miso documentation&lt;br /&gt;http://pdis.hiit.fi/pdis/download/miso/miso-1.40-api.html&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import appuifw, miso, e32&lt;br /&gt;&lt;br /&gt;# run-and-break type of app&lt;br /&gt;running = 1&lt;br /&gt;def set_exit():&lt;br /&gt;    global running&lt;br /&gt;    running = 0&lt;br /&gt;appuifw.app.exit_key_handler= set_exit&lt;br /&gt;&lt;br /&gt;# main loop&lt;br /&gt;while running:&lt;br /&gt;    miso.vibrate(500, 100)  # vibrate for 500 millisec, at full speed&lt;br /&gt;    e32.ao_sleep(10)   # vibrate every 10 seconds&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;I don't have a FP2 phone to test this. Though the code is&lt;br /&gt;pretty straight forward, please report if there is a problem.</description>
      <pubDate>Mon, 27 Jun 2005 12:54:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/415</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
