A Simple Clock, Well Maybe Not That Simple...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers A Simple Clock, Well Maybe Not That Simple...
# 1  
Old 07-17-2013
A Simple Clock, Well Maybe Not That Simple...

The attachment says it all really...

It is a DEMO at a glance digital readout using the "date" command to make it useful...

For a Mocbook Pro 13", OSX 10.7.5, but may well work on Linux variants too.

Enjoy...
Code:
#!/bin/bash
#
# Clock.sh
# A bash DEMO to create a 6 x 7 character set using the whitespace character.
# It is a functional digital clock but this is not important as I want this
# method for an _at_a_glance_ digital display for a kids level shell digital
# voltmeter I am in the process of doing.
#
# The clock in normal size is white on black near the top. The extra large clock
# is green on black and in the centre of the terminal..
#
# $VER: Clock.sh_Version_1.00.00_(C)2013_B.Walker_G0LCU.
# Issued under GPL2.
#
# Written so the anyone can understand how it works.

# Set the window to white foreground on black background.
printf "\x1B[0;37;40m"
clear
# Remove the cursor.
tput civis
# Set up all _variables_ as is required.
TIME=`date "+%H:%M"`
char="0"
# The plot _variable_ "p".
p="(C)2013, B.Walker, G0LCU."
# The background colours.
bg="\x1B[0;37;40m"
# The foreground colours.
fg="\x1B[0;37;42m"
# The initial character plotting points.
horiz=10
vert=9
# This function reads the time and stores it in "TIME".
clock()
{
	TIME=`date "+%H:%M"`
	printf "\x1B[2;32f$bg The time is $TIME.\n"
}
# This function is required to coreectly print out the large characters.
plot()
{
	p="\x1B["$vert";"$horiz"f"
	vert=$[ ( $vert + 1 ) ]
}
# *********************************************************
# The eleven characters required for this DEMO are 0 to 9
# and the : colon character.
zero()
{
	plot
	printf "$p$bg $fg   $bg  "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$fg $bg  $fg  $bg "
	plot
	printf "$p$fg $bg $fg $bg $fg $bg "
	plot
	printf "$p$fg  $bg  $fg $bg "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$bg $fg   $bg  "
}
one()
{
	plot
	printf "$p$bg   $fg $bg  "
	plot
	printf "$p$bg  $fg  $bg  "
	plot
	printf "$p$bg   $fg $bg  "
	plot
	printf "$p$bg   $fg $bg  "
	plot
	printf "$p$bg   $fg $bg  "
	plot
	printf "$p$bg   $fg $bg  "
	plot
	printf "$p$bg  $fg   $bg "
}
two()
{
	plot
	printf "$p$bg $fg   $bg  "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$bg    $fg $bg "
	plot
	printf "$p$bg $fg   $bg  "
	plot
	printf "$p$fg $bg     "
	plot
	printf "$p$fg $bg     "
	plot
	printf "$p$fg     $bg "
}
three()
{
	plot
	printf "$p$bg $fg   $bg  "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$bg    $fg $bg "
	plot
	printf "$p$bg $fg   $bg  "
	plot
	printf "$p$bg    $fg $bg "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$bg $fg   $bg  "
}
four()
{
	plot
	printf "$p$bg   $fg $bg  "
	plot
	printf "$p$bg  $fg  $bg  "
	plot
	printf "$p$bg $fg $bg $fg $bg  "
	plot
	printf "$p$fg $bg  $fg $bg  "
	plot
	printf "$p$fg     $bg "
	plot
	printf "$p$bg   $fg $bg "
	plot
	printf "$p$bg   $fg $bg "
}
five()
{
	plot
	printf "$p$fg     $bg "
	plot
	printf "$p$fg $bg     "
	plot
	printf "$p$fg $bg     "
	plot
	printf "$p$bg $fg   $bg  "
	plot
	printf "$p$bg    $fg $bg "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$bg $fg   $bg  "
}
six()
{
	plot
	printf "$p$bg $fg   $bg  "
	plot
	printf "$p$fg $bg     "
	plot
	printf "$p$fg $bg     "
	plot
	printf "$p$fg    $bg  "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$bg $fg   $bg  "
}
seven()
{
	plot
	printf "$p$fg     $bg "
	plot
	printf "$p$bg    $fg $bg "
	plot
	printf "$p$bg   $fg $bg  "
	plot
	printf "$p$bg  $fg $bg   "
	plot
	printf "$p$bg $fg $bg    "
	plot
	printf "$p$fg $bg     "
	plot
	printf "$p$fg $bg     "
}
eight()
{
	plot
	printf "$p$bg $fg   $bg  "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$bg $fg   $bg  "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$bg $fg   $bg  "
}
nine()
{
	plot
	printf "$p$bg $fg   $bg  "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$fg $bg   $fg $bg "
	plot
	printf "$p$bg $fg    $bg "
	plot
	printf "$p$bg    $fg $bg "
	plot
	printf "$p$bg    $fg $bg "
	plot
	printf "$p$bg $fg   $bg  "
}
colon()
{
	plot
	printf "$p$bg      "
	plot
	printf "$p$bg      "
	plot
	printf "$p$bg  $fg $bg   "
	plot
	printf "$p$bg      "
	plot
	printf "$p$bg  $fg $bg   "
	plot
	printf "$p$bg      "
	plot
	printf "$p$bg      "
}
# End of the character set.
# *********************************************************
# Print all of these characters first just to display them.
# This will last for 5 seconds only...
# Done longhand purely for fun...
horiz=10
vert=9
zero
horiz=16
vert=9
one
horiz=22
vert=9
two
horiz=28
vert=9
three
horiz=34
vert=9
four
horiz=40
vert=9
five
horiz=46
vert=9
six
horiz=52
vert=9
seven
horiz=58
vert=9
eight
horiz=64
vert=9
nine
horiz=70
vert=9
colon
# Now display the clock in the normal character size...
clock
sleep 5
# Now clear the screen and display the big digits.
clear
while true
do
	clock
	for subscript in $( seq 0 1 4)
	do
		# Take each character in turn and do the plots of them.
		char="${TIME:${subscript}:1}"
		horiz=$[ ( 26 + ( $subscript * 6 ) ) ]
		vert=9
		if [ "$char" == ":" ]
		then
			colon
		fi
		if [ "$char" == "0" ]
		then
			zero
		fi
		if [ "$char" == "1" ]
		then
			one
		fi
		if [ "$char" == "2" ]
		then
			two
		fi
		if [ "$char" == "3" ]
		then
			three
		fi
		if [ "$char" == "4" ]
		then
			four
		fi
		if [ "$char" == "5" ]
		then
			five
		fi
		if [ "$char" == "6" ]
		then
			six
		fi
		if [ "$char" == "7" ]
		then
			seven
		fi
		if [ "$char" == "8" ]
		then
			eight
		fi
		if [ "$char" == "9" ]
		then
			nine
		fi
	done
	sleep 1
done
# There is no code to clean up the terminal for this session in this DEMO.
# It is SOOO easy to do it manually that I expect you to be able to do
# that yourselves.
# Enjoy finding simple solutions to often very difficult questions.

A Simple Clock, Well Maybe Not That Simple...-clockjpg
# 2  
Old 07-17-2013
A fun little app, and works great here.

When you find yourself writing pages of nearly-identical if/then, though, and if you want to use this code for teaching -- it's really time to learn the case statement:

Code:
case "$char" in
:)        colon ;;
0)        zero  ;;
1)        one   ;;
...
esac

Fancier but less-friendly code:

Code:
                if [ "$char" == ":" ]
                then
                        colon
                else
                        # Functions are named d0 through d9
                        d$char
                fi


Last edited by Corona688; 07-17-2013 at 04:03 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-17-2013
Thanks for the advice, I will certainly use it from now on...

The clock side is of little interest to me but a digital voltmeter project is what it is intended for and I will heed your advice and use "case" when I finally set about it.
# 4  
Old 07-17-2013
I kept looking at this, since it seemed a little off somehow... Writing the same function 11 times with slight changes in each, is work fit for a computer. Why not use the same function, with different inputs?

Like...
Code:
# 11 functions with one use
print_lines_for_one
print_lines_for_two
print_lines_for_three
...

# One function with 11 uses

LINES_FOR_ONE="
##.
.#.
.#.
.#.
###"

print_lines ${LINES_FOR_ONE}
print_lines ${LINES_FOR_TWO}
print_lines ${LINES_FOR_THREE}
...

And then inside, you can have a loop:

Code:
print_lines() {
        for X in "$@"
        do
                plot
                printf "$p$X"
        done
}

You could move 'plot' inside it completely, actually.

Last edited by Corona688; 07-17-2013 at 05:08 PM..
# 5  
Old 07-17-2013
Phew; bear in mind I only get chance to experiment in my own time and I am not that good yet. ;o)

But I will experiment with your methods for the real usage of this DEMO code in due course.

Thanks for the input...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Simple Chess Clock

I am trying to implement a simple chess clock. It should have the following options: start, stop, reset, read. Reset will set the time to zero Start will start the clock Stop will stop the clock My problem is that I want that start continues counting the time from the time it had when it... (6 Replies)
Discussion started by: kristinu
6 Replies

2. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. Shell Programming and Scripting

need very simple help

i have a part of script that is not getting me what i want? Below the script count=5 count1=1 if then echo $count1 count1=`expr $count1 + 1` fi -------------------------------------------------------- The result i want is 1 2 3 4 5 But its giving me result as only 1. Plz help... (2 Replies)
Discussion started by: ali560045
2 Replies

5. Shell Programming and Scripting

Something simple, I think...

Hello to all, I have a very simple script that Ii run to CD into a specific directory. When I run it without a dot notation or shell notation (as a subshell, therefore), the script does not actually change the directory. But how do I avoid using the dot or shell notation and still be able to... (1 Reply)
Discussion started by: confutatis
1 Replies

6. Shell Programming and Scripting

Simple to you not simple to me pattern matchin help

hey all, im new and my first question is: say i have a word "blahblah" how do i get and replace the last letter of the word with say k, so replace the h with a k. However you cant just replace the h it has to change the LAST LETTER of the word. Cheers In advance. :b: (0 Replies)
Discussion started by: aleks001
0 Replies

7. Programming

Simple C question... Hopefully it's simple

Hello. I'm a complete newbie to C programming. I have a C program that wasn't written by me where I need to write some wrappers around it to automate and make it easier for a client to use. The problem is that the program accepts standard input to control the program... I'm hoping to find a simple... (6 Replies)
Discussion started by: Xeed
6 Replies

8. Shell Programming and Scripting

Simple if

Hi, I have a variable (x) that holds a file name - I need to write this: if x matches the patern *ABC* or the pattern DEF*T then action A else action B fi how to do that? Also, how can i change the or test to AND test? Thanks. (2 Replies)
Discussion started by: GNMIKE
2 Replies

9. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies
Login or Register to Ask a Question