Skip to content

Instantly share code, notes, and snippets.

@plugnburn
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugnburn/a0d977e87ebdc21b1abb to your computer and use it in GitHub Desktop.
Save plugnburn/a0d977e87ebdc21b1abb to your computer and use it in GitHub Desktop.
WatchCoder - encode any number from 0 to 5183999 to the values you can store in your Casio electronic watch, decode the stored values back into number
#!/bin/bash
# The WatchCoder method guarantees to encode an unsigned 22-bit (or signed 21-bit converted to unsigned)
# integer into the values present in any electronic Casio watch: an alarm setting and 1-hour stopwatch
# Maximal theoretically encodable unsigned value is 5183999
case "$1" in
num2watch)
[ -z "$2" ] && echo "Specify the number to encode" && exit 1
NUM=$2
SWDABS=$(($NUM / 1440))
ALMABS=$(($NUM % 1440))
SWDMIN=$(($SWDABS / 60))
SWDSEC=$(($SWDABS % 60))
ALMMIN=$(($ALMABS / 60))
ALMSEC=$(($ALMABS % 60))
printf "Alarm: %02d:%02d, stopwatch: %02d:%02d\n" $ALMMIN $ALMSEC $SWDMIN $SWDSEC
;;
watch2num)
[ -z "$2" ] && echo "Specify alarm display in hh:mm format" && exit 1
[ -z "$3" ] && echo "Specify stopwatch display in hh:mm format" && exit 1
ALM=(${2//:/ })
SWD=(${3//:/ })
ALMSEC=$((${ALM[0]} * 60 + ${ALM[1]}))
SWDSEC=$((${SWD[0]} * 60 + ${SWD[1]}))
echo $(($SWDSEC * 1440 + $ALMSEC))
;;
*)
echo "Usage $0 {num2watch <number>|watch2num <alarm> <stopwatch>}"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment