tonybaldwin | blog

non compos mentis

Bashful, Ticklish Snoozing (sleeping with bash and tcl)

leave a comment »

So, sometimes you want to take a snooze, or, maybe time something else (like that bacon explosion in the oven).  We can use the bash command “sleep” to help us out.  Something as simple as:

sleep 600 ; echo ‘Hey, get the bacon out the oven, d00d’ &

That will wait 10 minutes (600 seconds), and then print ‘Hey, get the bacon out the oven, d00d’ to your terminal.  The only thing is, that terminal is waiting, and you can’t do anything else in it, so you open another, and then never see the message.  Useless, really… But, say we’re going to take a nap, and we just want to be awakened in an hour.  We won’t be using the terminal (and a printed message isn’t going to wake us up).

Let’s play tunes, instead then!  This one will require mplayer.

sleep 3600 ; mplayer ~/Music/roxors/opeth/blackwater_park/08_blackwater_park.ogg

That’ll get you going!  In one hour, mplayer will pipe up with Opeth’s BlackWater Park! (If that doesn’t wake you, you’ve probably died in your sleep).

Clearly, the path to YOUR Opeth collection may be different, but you get the point.

Now, you could facilitate the whole process with a little script. I have:

#!/bin/bash
# snooze time
echo “How long ya want, pal?”
read m
secs=$(($m * 60 ))
echo Okay, that gives us about $secs seconds
echo Then we will play the Imperial March.
echo Make sure the speakers are on and turned up.
echo That will get you going.
sleep $secs ; mplayer ~/tunes/starwars/03-star_wars_the_empire_strikes_back-the_imperial_march.mp3 &
exit

Or, we can wrap that up in a little gui with some tcl/tk.

#!/usr/bin/wish8.5

##########
# sleep timer with musical wake-up

# set vars

global count
global secs
global tunage

set count “0”
set mins “0”
set secs “0”
set tunage “none”

set filetypes {
{“All files” * }
{“MP3” {.mp3}}
{“OGG” {.ogg}}
}

# gui

wm title . “Snooze Timer”

grid [ttk::label .secs -text “Minutes:”]\
[ttk::entry .min -textvariable mins]

grid [ttk::button .bu -text “seconds: ” -command sex ]\
[ttk::label .bn -textvariable secs ]

proc sex {} {
if {[catch {
set ::secs [ expr {$::mins * 60} ]
}]!=0} {
set ::secs “?”
}
}

grid [ttk::button .chtune -text “Choose song:” -command chutu]\
[ttk::entry .tuna -textvariable tunage]

grid [ttk::button .go -text “SNOOZE” -command snooze]\
[ttk::button .stop -text “QUIT” -command exit]

proc calcu {} {
exec tcalcu
}

proc chutu {} {
global tunage
set tunage [tk_getOpenFile -filetypes $::filetypes -initialdir “~/Music”]
}

proc snooze {} {
set snooz “sleep $::secs ; mplayer $::tunage”
exec sh -c $snooz &
}

That gives you a little gui that sets the time, and even allows you to pick a song (in the image, I picked Ministry’s “Jesus Built My Hot Rod“).  It has a file chooser to pick the song.  You just feed it a number of minutes (press the button to calculate seconds), pick a song, click snooze, and kick back.  After your chosen time interval, your chosen tune will come on and wake you! (or remind you to get the bacon explosion out of the oven).

./tony

Advertisements

Written by tonybaldwin

February 2, 2011 at 7:59 am

Posted in free software, gnu/linux, hacking

Tagged with , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: