Md at debian.org

tales of a debian maintainer

A Linux-based alarm clock

That is, my laptop. Since at night I keep it in the same room where I sleep, I figured that I could as well use it as an highly customizable alarm clock.

My alarm script uses rtcwake(8) from util-linux-ng to set the RTC alarm clock, suspends the computer and then plays some music at a set volume when it wakes up (if it never wakes up, check your BIOS settings). Since it uses the date(2) command it can parse the alarm time in many formats.

#!/bin/sh -e

# for a random tune, list multiple space-separated files
MUSIC='/home/mp3/The_Velvet_Underground/The_Velvet_Underground_&_Nico/07_Heroin.mp3'

# some days I like different music
DOW="$(date +%u)"
if [ $DOW -eq 6 -o $DOW -eq 7 ]; then
  MUSIC='/home/mp3/The_Velvet_Underground/The_Velvet_Underground_&_Nico/01_Sunday_Morning.mp3'
fi

# choose your favourite music player
player() { mpg123 --random --quiet --control --title "$@" || true; }
#player() { mplayer -loop 0 "$*"; }

# play the music at this volume
VOLUME='50%'
# ALSA mixer control used to set and restore the master volume
VOLUMECTL='iface=MIXER,name="Master Playback Volume"'

##############################################################################
# this is more elegant, but it needs rtcwake from util-linux-ng >= 2.14.2
rtcwake_set_alarm() {
  local when="$1"
  local rtc="$2"
  if [ "$rtc" ]; then rtc="--device $rtc"; fi

  local epochtime=$(date --date "$when" +%s)
  [ "$epochtime" ] || return 1

  sudo rtcwake $rtc --mode no --time $epochtime
}

# you can use this function instead if your system lacks a working rtcwake
set_alarm() {
  local when="$1"
  local rtc="$2"
  [ "$rtc" ] || rtc='rtc0'

  local epochtime=$(date --date "$when" +%s)
  [ "$epochtime" ] || return 1

  local alarmfile="/sys/class/rtc/$rtc/wakealarm"
  sudo sh -c "echo 0 > $alarmfile && echo $epochtime > $alarmfile"
}

##############################################################################
if [ "$1" ]; then
  WHEN="$1"
else
  echo "Usage: $0 WHEN"
  exit 1
fi

set_alarm "$WHEN"

# save the volume
oldvolume="$(amixer cget "$VOLUMECTL" | sed -nre '/ : /s/.*=//p')"
amixer -q cset "$VOLUMECTL" $VOLUME

# actually here I run a script which also deals with network interfaces,
# IM and IRC clients and so on
sudo pm-suspend

player $MUSIC

# restore the volume
amixer -q cset "$VOLUMECTL" "$oldvolume"

exit 0

About

This is the blog of Marco d'Itri.

S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          

See also:

My blogroll:


W3C HTML 4.01
W3C CSS 2.0     

Powered by Bryar.pm.