"tm" command line timer

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

"tm" command line timer

#1 Post by jpeps »

USAGE: tm [min]:[sec][sound]
or: tm [min]

examples "tm 20:5" (20 min, 5 sec)
"tm 30" (30 min)
"tm :5" (5 sec)
"tm :10 bark.au" (10 seconds, plays bark.au)


Comes with gong-burmese.wav, which it plays by default.

Main script:

Code: Select all

#!/bin/sh 

### Command line Timer  jpeps 1/11

## Help
## Sound path is /usr/share/audio

if [ "$1" == "" -o "$1" == "-h" ]; then
  echo "USAGE: [min]:[sec] [sound]" 
  echo "USAGE: [min]"
  echo "sound path: /usr/share/audio"
               
  exit
fi

dir="/usr/share/audio/"

## Set Sound 
if [ "$2" ]; then
snd="$2"
else
snd="gong-burmese.wav" 
fi


sep="$(echo ${1} | grep ":")"

min="$(echo ${1} | cut -d":" -f1)"
[ "$min" ] || min="0"
sec="$(echo ${1} | cut -d":" -f2)"
[ "$sec" ] || sec="0"
tmin="$((${min}*60 ))"

## Allow for entree without ":"  for minutes
if [ "$sep" == "" ]; then 
 tsec="$((${1}*60))"  
else
 tsec="$((${tmin}+${sec}))" 
fi 

x="tsec"
n="0"
for ((i=0;i<=${x};i++)); do
n="$((${n} + 1))"
if [ "$n" == "${tsec}" ]; then
aplay ${dir}${snd} 2>/dev/null
fi
sleep 1
done

exit
Attachments
cli-timer.pet
(39.26 KiB) Downloaded 570 times

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#2 Post by Lobster »

Very well done.
Timers can be most useful
You might be interested in this thread.
http://www.murga-linux.com/puppy/viewto ... 068#351139
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#3 Post by jpeps »

Lobster wrote:Very well done.
Timers can be most useful
You might be interested in this thread.
http://www.murga-linux.com/puppy/viewto ... 068#351139
Thanks...thought you might like the gong :)

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#4 Post by technosaurus »

here is a little stopwatch snippet i wrote

Code: Select all

#!/bin/ash
read -p "Enter to start"
while(true)do clear
read -t 1 -p "Enter to stop  "${h}:${m}:$((s++)) && break
[ "$s" -gt 59 ] && m=$((m+1)) && s=0 && [ "$m" -gt 59 ] && h=$((h+1)) && m=0
done
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#5 Post by zigbert »

Image

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

gui version

#6 Post by don570 »

I made a GUI version of this program, simplifying it a great deal
but it gives a visual message and a nice sound.
I call it PmusicTimer. I believe that this is the
first time that Pmusic has been used as a simple timer.
However Pmusic can be used with Pschedule to delay the playing
of a playlist.

Pmusic has the benefit that mp3 files can be used and
nearly all Puppies have pmusic built-in.

There is a bug that I found with the GUI. Only short sounds can
be used... but that isn't much of a problem since it
is to used as a simple alarm timer.

Here it is . Feel free to change it any way you want.
A nice loud sound file is included.
http://www.datafilehost.com/download-26eb0ce4.html

_______________________________________________________

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

PmusicTimer script

#7 Post by don570 »

Here's the script and pet package
http://www.datafilehost.com/download-26eb0ce4.html

Code: Select all

     #! /bin/bash

     export DIALOG="
       <window title=\"Pmusic Timer\">

     <vbox>
     <pixmap><input file>/usr/share/images/document-open-recent.png</input></pixmap>
     <text width-request=\"190\">  <label>    \n
                </label></text>
                
     <hbox>
     <text width-request=\"190\">  <label>  Enter number of minutes</label></text>
       <entry width-request=\"100\">
         <variable>ENTRY</variable>
       </entry>
       </hbox>
       
       <hbox>
     <text width-request=\"190\">  <label>    \n
                </label></text>
       </hbox>
       
       <hbox>
         <button ok></button>
         <button cancel></button>
       </hbox>
     </vbox>
     
 </window>"
     I=$IFS; IFS=""
     for STATEMENTS in  $(gtkdialog3 --geometry=+200+100 --program DIALOG); do
       eval $STATEMENTS        
     done
     
dir="/usr/share/audio/"
snd="clock.mp3" 
min="$ENTRY"
tsec="$((${min}*60 ))" 
x="tsec"
n="0"

for ((i=0;i<=${x};i++)); do
n="$((${n} + 1))"
if [ "$n" == "${tsec}" ]; then
pmusic -b  ${dir}${snd} 2>/dev/null 
yaf-splash   -bg orange -fontsize x-large    -text  " $ENTRY minutes is up.
 Timer has finished!!!           " -close box &
fi
sleep 1
done

pmusic -s quit 
exit

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

Re: gui version

#8 Post by jpeps »

don570 wrote:
There is a bug that I found with the GUI. Only short sounds can
be used... but that isn't much of a problem since it
is to used as a simple alarm timer.
You might put the player outside the loop.

Code: Select all


mflag=''
for ((i=0;i<=${x};i++)); do
n="$((${n} + 1))"
if [ "$n" == "${tsec}" ]; then
mflag="1"
break
fi
sleep 1
done

[ "$mflag" ] && pmusic ${dir}${snd} 2>/dev/null

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#9 Post by zigbert »

don570
Really cool to see that you build a gui :D

Try to run pmusic as a standalone process:

Code: Select all

pmusic -b  ${dir}${snd} 2>/dev/null &

Sigmund

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#10 Post by zigbert »


User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

new countdown timer

#11 Post by don570 »

Here's the newest pet
http://www.datafilehost.com/download-e2c1fa85.html

I've given up trying to make pmusic into timer, especially
when I discovered that the current playlist vanishes after
running the timer, so I'll stick with aplay

Here's my most current try. It's only 25k uncompressed.
That's because I use 2barks.wav which is built into Puppy.
I've basically broken up the program into 2 parts.
One is called tm ------> the original cli program,
the other tmtimer ---> the GUI

They are stored in the same place, /usr/local/bin/

Here is the code for the GUI part

Code: Select all

#! /bin/bash


export DIALOG='
 <vbox>
 <hbox> <text>  <label>    \n
 
                </label></text></hbox>
      
      <hbox>
      <text>  
     <label>  Enter number of minutes</label>
     </text>
       <entry>
         <variable>ENTRY</variable>
       </entry>
       </hbox>
        
         <hbox> <text>  <label>    \n
                </label></text></hbox>
 

     <hbox>
      <button>
        <label>Start</label>
        <input file>/usr/share/images/document-open-recent.png</input>
        <action>echo You pressed the exit button</action>
        <action type="exit">Exit by button</action>
      </button>
     <text>  <label> "                      " </label></text>     
    </hbox>

 <hbox> <text>  <label>    \n
 
                </label></text></hbox>
      

 </vbox>
'
     I=$IFS; IFS=""
     for STATEMENTS in  $(gtkdialog3 --geometry=+200+100 --program DIALOG); do
       eval $STATEMENTS        
     done
     

tm $ENTRY  
exit

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#12 Post by jpeps »

Might as well allow seconds:

Code: Select all

<label>  Enter number of minutes:seconds</label> 
You could also have an entry box for sound

Code: Select all

tm ${ENTRY} ${SND}

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

improved tm timer

#13 Post by don570 »

It wasn't too hard to include an option for selection of a sound file.
It just required a few extra lines of code.

Code: Select all

                 
<frame Sound file>
<hbox>
   <text>
     <label>   /root/puppy-reference/audio/</label>
     </text>
       <entry>
    <default>2barks.wav</default>
    <variable>SND</variable>
  </entry>
</hbox>
</frame>
Here it is.
http://www.datafilehost.com/download-bb8e323d.html

This is as far as I can take this project. Someone else will have to take it
over and add more features. A tray app to tell the remaining time
would be nice.
______________________________________________

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#14 Post by big_bass »

Here's an easy function to add to the list of optional solutions

Joe
-------------------
cfajohnson
Shell programmer, author

http://www.unix.com/shell-programming-s ... cript.html


Code: Select all

countdown()
(
  IFS=:
  set -- $*
  secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} ))
  while [ $secs -gt 0 ]
  do
    sleep 1 &
    printf "\r%02d:%02d:%02d" $((secs/3600)) $(( (secs/60)%60)) $((secs%60))
    secs=$(( $secs - 1 ))
    wait
  done
  echo
)

# format  hours: minutes :seconds 
  countdown "00:02:00"

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#15 Post by jpeps »

big_bass wrote:Here's an easy function to add to the list of optional solutions

Joe
Running sleep in background with the wait command is clever.

edit: ran better with:

Code: Select all

secs=$(( ${1:0} * 3600 + ${2:0} * 60 + ${3:0} ))

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

GUI Project for tm

#16 Post by don570 »

I decided to add a message field to the GUI
and to localize it using Zigbert's method.

ptmtimer-1.3.pet

and here's the locals(French,German,Spanish)
ptmtimer-NLS-1.3.pet


Here's how I added a message field...

Code: Select all

<frame '$LOC200'>
<hbox>  
  <entry>
    <default>'$LOC201' </default>
    <variable>MESSAGE</variable>
  </entry>
</hbox>
</frame>
There is now three variable values that I pass to 'tm'

Code: Select all

/usr/local/ptmtimer/tm ${ENTRY} ${SND}  ${MESSAGE}
Following Zigbert's method , I create a folder /usr/local/ptmtimer/
to hold the program but the app has to be launched from an executable
path so I create a small script just to launch the app.
________________________________

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#17 Post by zigbert »

Pmusic 1.6.4 has got a -B switch witch makes it possible to use Pmusic as a dead simple (fast and low cpu usage) command-line player.


Sigmund

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

script loops a given number of sconds

#18 Post by don570 »

I made a change to tm script to make it loop indefinitely
with a given period (in seconds). It might be useful to
someone who has an operation that must be done continually,
like checking a file. I'm not a programmer so
I don't know how this script works. I was just tinkering.

Code: Select all

#!/bin/sh 
#This script loops endlessly  with a period of tsec
until [ -z 4 ]
do
tsec=9
n="0"
for ((i=0;i<=${tsec};i++)); do
n="$((${n} + 1))"
if [ "$n" == "${tsec}" ]; then

gxmessage  "Hello  Message"


fi
sleep 1
done
done
exit
____________________________________________
Last edited by don570 on Thu 10 Mar 2011, 23:10, edited 1 time in total.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

change line

#19 Post by don570 »

If you change the line 'gxmessage "Hello Message"' to

Code: Select all

yaf-splash   -bg orange  -fontsize x-large    -text  "  minutes is up.
 Timer has finished!!!           "  -close box & 
there is slightly different behaviour as the message keeps popping up
and becomes very annoying.

Post Reply