Raum Mpd Alarm Clock

From awesome
Jump to: navigation, search

This is a widget I use to control my alarm clock from Awesome. You can read more about it [here]. I basically press Meta+F12 and type in the time I want it to fire, the maximum volume it should reach and whether it should suspend or not. When chosen to suspend the computer wakes up just before the alarm goes off.

To get this to work you need: alsa, mpd, mpc, rtcwake (util-linux-ng) and sudo

The bash script that functions as the alarm is as follows:

 #!/bin/sh
  
 LOGFILE="/var/log/alarmlog"
 TIMEFORALARM=$1
 ALARMVOLUME=$2
 GOTOSLEEP=$3
 exec >> $LOGFILE 2>&1
  
 # Allows the user to cancel any alarms by issuing cancel as an argument
 if [ "$TIMEFORALARM" = "cancel" ]; then
     echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: killed all alarms"
     killall alarmclock
     exit
 fi
  
 # If user wants system to sleep, do so
 if [ "$GOTOSLEEP" -ne "0" ]
 then
     TIMEINSECSNOW="$(date +%s)"
     TIMEINSECSALARM="$(date -d "$TIMEFORALARM" "+%s")"
     if [ $TIMEINSECSALARM -gt $TIMEINSECSNOW ]
     then
         echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: set for $(date -d @$TIMEINSECSALARM +%c). Will go to sleep now."
         rtcwake -l -m mem -t $TIMEINSECSALARM > /dev/null
         echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: pc has woken up"
     else
     # If the time given is less than the time it is now, the alarm is probably for tomorrow, add 24 hours
         echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: set for $(date -d @$TIMEINSECSALARM +%c). Will go to sleep now."
         TIMEINSECSALARM=$(( $TIMEINSECSALARM + 86400 )) # 86400 is the seconds in 24 hours
         rtcwake -l -m mem -t $TIMEINSECSALARM > /dev/null
         echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: pc has woken up"
     fi
 else
     echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: set for $(date -d @$TIMEINSECSALARM +%c)."
 fi
   
 # Wait until it's time to fire
 until [ "$(date +'%H%M')" -eq $TIMEFORALARM ]; do
     sleep 2
 done
 echo "$(date +'%a %d-%m-%Y %H:%M:%S') Alarm: fired alarm for $TIMEFORALARM"
  
 mpc -q clear
 mpc -q load wakeup > /dev/null # We need to send this to /dev/null as not to clutter our log files
  
 # Set the speaker to 100% to make sure the alarm will be heared
 amixer -q sset 'Speaker' 100%
 amixer -q -c 0 sset 'Speaker',0 unmute
 amixer -q sset 'PCM' 100%
  
 # Gracefully increase the volume of our music
 amixer -q -c 0 sset 'Master',0 unmute
 mpc -q play
 for (( x=0; x<$ALARMVOLUME; x++ )); do
     amixer -q sset Master $x%
     sleep 0.4
 done

Using the following in my rc.lua, I can easily set the alarm with Meta+F12:

 awful.key({ altkey }, "F12", function ()
     awful.prompt.run({ prompt = "Alarm clock (time volume sleep): " }, promptbox[mouse.screen].widget,
         function (time)
             exec("sudo /home/myusername/Scripts/alarmclock "..time)
         end)
 end)

Some of the commands to get the computer to sleep in the script need root access. For that reason I have chosen to use sudo, as I can easily add this to my sudoers file with 'visudo':

 myusername ALL=(ALL) NOPASSWD:/home/myusername/Scripts/alarmclock

Don't forget to make a playlist for mpd called "wakeup".

Personal tools