Skip to content

Instantly share code, notes, and snippets.

@M0LTE
Last active April 15, 2018 17:07
Show Gist options
  • Save M0LTE/e39607426f18403b3c08fcb7f039dc51 to your computer and use it in GitHub Desktop.
Save M0LTE/e39607426f18403b3c08fcb7f039dc51 to your computer and use it in GitHub Desktop.
Pips for Jonathan (also now partly BY Jonathan)
#!/bin/bash -e
# you need to run this file at startup.
# copy it to your system, make it executable (chmod +x pips.sh), then use /etc/rc.local or whatever to start it up once at boot.
# shouldn't really ever crash, there's nothing to go wrong, as long as the audio player never fails. Then you might
# want some error checking.
while true; do
# get time now in nanoseconds since 1970-01-01 00:00:00
now=$(date +%s%N)
# get time at the top of the next hour in nanoseconds since 1970
# nexthour=$(date -d $(date -d 'next hour' '+%H:00:00') +%s%N) # this broke at 23:00
# this should be safer:
thishour=$(date -d $(date '+%H:00:00') +%s%N)
nexthour=$(($thishour + 3600000000000))
nexthalfhour=$(($nexthour-1800000000000))
nextquarterhour=$(($nexthour-2700000000000))
nextthreequarterhour=$(($nexthour-900000000000))
# take 6 seconds (6000000000 nanoseconds) off that to find next xx:59:54
next59=$(($nexthour-6000000000))
next29=$(($nexthalfhour-6000000000))
next44=$(($nextthreequarterhour-6000000000))
next14=$(($nextquarterhour-6000000000))
# for debugging, keep these commented out
#echo Nanoseconds to next hour: $(($nexthour - $now))
echo Nanoseconds to next hh:59:54: $(($next59 - $now))
echo Nanoseconds to next hh:29:54: $(($next29 - $now))
echo Nanoseconds to next hh:44:54: $(($next44 - $now))
echo Nanoseconds to next hh:14:54: $(($next14 - $now))
# if it is later than xx:29:54 BUT BEFORE xx:30:00
if [ "$now" -gt "$next29" ] && [ "$now" -lt "$nexthalfhour" ]
then
# yep, play the pips
aplay /home/pi/pips.wav # something with aplay or whatever. Note, the command must NOT return
# until the file has finished playing the audio file, and the file must
# be > 5s long, otherwise this will trigger multiple times.
#sleep 6 # this pretends to play file, remove it when the above echo statement is replaced with a command that plays audio
#echo "Back to clock-watching"
else
# nope, it's elsewhere in the hour.
sleep 0.005 # stops you from pegging the CPU. Don't need to check time more than maybe every 5ms.
# experiment to find a value that is accurate enough for you, but doesn't thrash the CPU.
fi
# if it is later than xx:44:54 BUT BEFORE xx:45:00
if [ "$now" -gt "$next44" ] && [ "$now" -lt "$nextthreequarterhour" ]
then
# yep, play the pips
aplay /home/pi/pips.wav # something with aplay or whatever. Note, the command must NOT return
# until the file has finished playing the audio file, and the file must
# be > 5s long, otherwise this will trigger multiple times.
#sleep 6 # this pretends to play file, remove it when the above echo statement is replaced with a command that plays audio
#echo "Back to clock-watching"
else
# nope, it's elsewhere in the hour.
sleep 0.005 # stops you from pegging the CPU. Don't need to check time more than maybe every 5ms.
# experiment to find a value that is accurate enough for you, but doesn't thrash the CPU.
fi
# if it is later than xx:59:54
if [ "$now" -gt "$next59" ]
then
# yep, play the pips
aplay /home/pi/pips.wav # something with aplay or whatever. Note, the command must NOT return
# until the file has finished playing the audio file, and the file must
# be > 5s long, otherwise this will trigger multiple times.
#sleep 6 # this pretends to play file, remove it when the above echo statement is replaced with a command that plays audio
#echo "Back to clock-watching"
else
# nope, it's elsewhere in the hour.
sleep 0.005 # stops you from pegging the CPU. Don't need to check time more than maybe every 5ms.
# experiment to find a value that is accurate enough for you, but doesn't thrash the CPU.
fi
# if it is later than xx:14:54 BUT BEFORE xx:15:00
if [ "$now" -gt "$next14" ] && [ "$now" -lt "$nextquarterhour" ]
then
# yep, play the pips
aplay /home/pi/pips.wav # something with aplay or whatever. Note, the command must NOT return
# until the file has finished playing the audio file, and the file must
# be > 5s long, otherwise this will trigger multiple times.
#sleep 6 # this pretends to play file, remove it when the above echo statement is replaced with a command that plays audio
#echo "Back to clock-watching"
else
# nope, it's elsewhere in the hour.
sleep 0.005 # stops you from pegging the CPU. Don't need to check time more than maybe every 5ms.
# experiment to find a value that is accurate enough for you, but doesn't thrash the CPU.
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment