Nautical bell: Difference between revisions
No edit summary |
(correcting code→{{header|C++}}) |
||
Line 23: | Line 23: | ||
void start() |
void start() |
||
{ |
{ |
||
_inst = this; |
_inst = this; _w = 5; |
||
watch[0] = "Middle"; watch[1] = "Morning"; watch[2] = "Forenoon"; watch[3] = "Afternoon"; watch[4] = "Dog"; watch[5] = "First"; |
watch[0] = "Middle"; watch[1] = "Morning"; watch[2] = "Forenoon"; watch[3] = "Afternoon"; watch[4] = "Dog"; watch[5] = "First"; |
||
count[0] = "One"; count[1] = "Two"; count[2] = "Three"; count[3] = "Four"; count[4] = "Five"; count[5] = "Six"; count[6] = "Seven"; count[7] = "Eight"; |
count[0] = "One"; count[1] = "Two"; count[2] = "Three"; count[3] = "Four"; count[4] = "Five"; count[5] = "Six"; count[6] = "Seven"; count[7] = "Eight"; |
||
Line 52: | Line 52: | ||
SYSTEMTIME st; GetLocalTime( &st ); |
SYSTEMTIME st; GetLocalTime( &st ); |
||
int b = ( 2 * st.wHour + st.wMinute / 30 ) % 8; b = b == 0 ? 8 : b; |
int b = ( 2 * st.wHour + st.wMinute / 30 ) % 8; b = b == 0 ? 8 : b; |
||
int w = ( 60 * st.wHour + st.wMinute ); w = w % 30 + ( w / 240 ); |
|||
char hr[32]; wsprintf( hr, "%.2d:%.2d", st.wHour, st.wMinute ); |
char hr[32]; wsprintf( hr, "%.2d:%.2d", st.wHour, st.wMinute ); |
||
cout << left << hr << " - " << setw( 9 ) << watch[w] << " watch - " << setw( 5 ) << count[b - 1] << " Bell"; |
cout << left << hr << " - " << setw( 9 ) << watch[w] << " watch - " << setw( 5 ) << count[b - 1] << " Bell"; |
||
if( b > 1 ) cout << "s"; else cout << " "; cout << " Gone." << endl; |
if( b > 1 ) cout << "s"; else cout << " "; cout << " Gone." << endl; |
||
if( b == 8 ) if( ++_w > 5 ) _w = 0; |
|||
for( int x = 0, c = 0; x < b; x++, c++ ) |
for( int x = 0, c = 0; x < b; x++, c++ ) |
||
{ |
{ |
||
Line 66: | Line 64: | ||
} |
} |
||
int _w; |
|||
string watch[7], count[8]; |
string watch[7], count[8]; |
||
static bells* _inst; |
static bells* _inst; |
Revision as of 14:52, 4 May 2013
You are encouraged to solve this task according to the task description, using any language you may know.
The task is to write a small program that emulates a nautical bell producing a ringing bell pattern at certain times throughout the day. The bell timing should be in accordance with Greenwich Mean Time, unless locale dictates otherwise.
It is permissible for the program to daemonize, or to slave off a scheduler, and it is permissible to use alternative notification methods (such as producing a written notice "Two Bells Gone"), if these are more usual for the system type.
C++
This version uses local time. <lang cpp>
- include <iostream>
- include <iomanip>
- include <string>
- include <windows.h>
//-------------------------------------------------------------------------------------------------- using namespace std;
//-------------------------------------------------------------------------------------------------- class bells { public:
void start() {
_inst = this; _w = 5; watch[0] = "Middle"; watch[1] = "Morning"; watch[2] = "Forenoon"; watch[3] = "Afternoon"; watch[4] = "Dog"; watch[5] = "First"; count[0] = "One"; count[1] = "Two"; count[2] = "Three"; count[3] = "Four"; count[4] = "Five"; count[5] = "Six"; count[6] = "Seven"; count[7] = "Eight"; CreateThread( NULL, 0, bell, NULL, 0, NULL );
}
private:
static DWORD WINAPI bell( LPVOID p ) {
DWORD wait = _inst->whatTime(); while( true ) {
Sleep( wait );
_inst->playBell(); wait = _inst->whatTime(); } return 0;
}
DWORD whatTime() {
SYSTEMTIME ts; GetLocalTime( &ts ); int m = ts.wMinute >= 30 ? ts.wMinute - 30 : ts.wMinute; return( 1800000 - ( ( m * 60 + ts.wSecond ) * 1000 + ts.wMilliseconds ) );
}
void playBell() {
SYSTEMTIME st; GetLocalTime( &st ); int b = ( 2 * st.wHour + st.wMinute / 30 ) % 8; b = b == 0 ? 8 : b; char hr[32]; wsprintf( hr, "%.2d:%.2d", st.wHour, st.wMinute ); cout << left << hr << " - " << setw( 9 ) << watch[w] << " watch - " << setw( 5 ) << count[b - 1] << " Bell"; if( b > 1 ) cout << "s"; else cout << " "; cout << " Gone." << endl;
if( b == 8 ) if( ++_w > 5 ) _w = 0;
for( int x = 0, c = 0; x < b; x++, c++ ) { cout << "\7"; Sleep( 500 ); if( !( c % 2 ) ) Sleep( 300 ); }
}
int _w; string watch[7], count[8]; static bells* _inst;
}; //-------------------------------------------------------------------------------------------------- bells* bells::_inst = 0; //-------------------------------------------------------------------------------------------------- int main( int argc, char* argv[] ) {
bells b; b.start(); while( 1 ); return 0;
} //-------------------------------------------------------------------------------------------------- </lang> Output:
00:00 - First watch - Eight Bells Gone. 00:30 - Middle watch - One Bell Gone. 01:00 - Middle watch - Two Bells Gone. 01:30 - Middle watch - Three Bells Gone. 02:00 - Middle watch - Four Bells Gone. 02:30 - Middle watch - Five Bells Gone. 03:00 - Middle watch - Six Bells Gone. 03:30 - Middle watch - Seven Bells Gone. 04:00 - Middle watch - Eight Bells Gone. 04:30 - Morning watch - One Bell Gone. 05:00 - Morning watch - Two Bells Gone. 05:30 - Morning watch - Three Bells Gone. 06:00 - Morning watch - Four Bells Gone. 06:30 - Morning watch - Five Bells Gone. 07:00 - Morning watch - Six Bells Gone. 07:30 - Morning watch - Seven Bells Gone. 08:00 - Morning watch - Eight Bells Gone. 08:30 - Forenoon watch - One Bell Gone. 09:00 - Forenoon watch - Two Bells Gone. 09:30 - Forenoon watch - Three Bells Gone. 10:00 - Forenoon watch - Four Bells Gone. 10:30 - Forenoon watch - Five Bells Gone. 11:00 - Forenoon watch - Six Bells Gone. 11:30 - Forenoon watch - Seven Bells Gone. 12:00 - Forenoon watch - Eight Bells Gone. 12:30 - Afternoon watch - One Bell Gone. 13:00 - Afternoon watch - Two Bells Gone. 13:30 - Afternoon watch - Three Bells Gone. 14:00 - Afternoon watch - Four Bells Gone. 14:30 - Afternoon watch - Five Bells Gone. 15:00 - Afternoon watch - Six Bells Gone. 15:30 - Afternoon watch - Seven Bells Gone. 16:00 - Afternoon watch - Eight Bells Gone. 16:30 - Dog watch - One Bell Gone. 17:00 - Dog watch - Two Bells Gone. 17:30 - Dog watch - Three Bells Gone. 18:00 - Dog watch - Four Bells Gone. 18:30 - Dog watch - Five Bells Gone. 19:00 - Dog watch - Six Bells Gone. 19:30 - Dog watch - Seven Bells Gone. 20:00 - Dog watch - Eight Bells Gone. 20:30 - First watch - One Bell Gone. 21:00 - First watch - Two Bells Gone. 21:30 - First watch - Three Bells Gone. 22:00 - First watch - Four Bells Gone. 22:30 - First watch - Five Bells Gone. 23:00 - First watch - Six Bells Gone. 23:30 - First watch - Seven Bells Gone.
D
This code uses local time instead of Greenwich Mean Time. <lang d>import std.stdio, core.thread, std.datetime;
class NauticalBell : Thread {
private shared bool stopped;
this() { super(&run); }
void run() { uint numBells; auto time = cast(TimeOfDay)Clock.currTime(); auto next = TimeOfDay();
void setNextBellTime() { next += minutes(30); numBells = 1 + (numBells % 8); }
while (next < time) setNextBellTime();
while (!this.stopped) { time = cast(TimeOfDay)Clock.currTime(); if (next.minute == time.minute && next.hour == time.hour) { immutable bells = numBells == 1 ? "bell" : "bells"; writefln("%s : %d %s", time, numBells, bells); setNextBellTime(); } sleep(dur!"msecs"(100)); yield(); } }
void stop() { this.stopped = true; }
}
void main() {
auto bells = new NauticalBell(); bells.isDaemon(true); bells.start(); try { bells.join(); } catch (ThreadException e) { writeln(e.msg); }
}</lang> This output is from an actual test run.
09:30:00 : 3 bells 10:00:00 : 4 bells 10:30:00 : 5 bells 11:00:00 : 6 bells 11:30:00 : 7 bells 12:00:00 : 8 bells 12:30:00 : 1 bell 13:00:00 : 2 bells 13:30:00 : 3 bells 14:00:00 : 4 bells 14:30:00 : 5 bells 15:00:00 : 6 bells 15:30:00 : 7 bells 16:00:00 : 8 bells 16:30:00 : 1 bell 17:00:00 : 2 bells 17:30:00 : 3 bells 18:00:00 : 4 bells 18:30:00 : 5 bells 19:00:00 : 6 bells 19:30:00 : 7 bells 20:00:00 : 8 bells 20:30:00 : 1 bell 21:00:00 : 2 bells 21:30:00 : 3 bells 22:00:00 : 4 bells 22:30:00 : 5 bells 23:00:00 : 6 bells 23:30:00 : 7 bells 00:00:00 : 8 bells 00:30:00 : 1 bell 01:00:00 : 2 bells 01:30:00 : 3 bells 02:00:00 : 4 bells 02:30:00 : 5 bells 03:00:00 : 6 bells 03:30:00 : 7 bells 04:00:00 : 8 bells 04:30:00 : 1 bell 05:00:00 : 2 bells 05:30:00 : 3 bells 06:00:00 : 4 bells 06:30:00 : 5 bells 07:00:00 : 6 bells 07:30:00 : 7 bells 08:00:00 : 8 bells 08:30:00 : 1 bell 09:00:00 : 2 bells
Perl 6
Perl 6 uses UTC (GMT) time internally and by default. This will display the current UTC time and on the half hour, display a graphical representation of the bell. If run in a terminal with the system bell enabled, will also chime the system alarm bell.
<lang perl6>my @watch = <Middle Morning Forenoon Afternoon Dog First>; my @ordinal = <One Two Three Four Five Six Seven Eight>;
my $thishour; my $thisminute = ;
loop {
my $utc = DateTime.new(time); if $utc.minute ~~ any(0,30) and $utc.minute != $thisminute { $thishour = $utc.hour; $thisminute = $utc.minute; bell($thishour, $thisminute); } printf "%s%02d:%02d:%02d", "\r", $utc.hour, $utc.minute, $utc.second; sleep(1);
}
sub bell ($hour, $minute) {
my $bells = (($hour % 4) * 2 + $minute div 30) || 8; printf "%s%02d:%02d %9s watch, %6s Bell%s Gone: \t", "\b" x 9, $hour, $minute, @watch[($hour div 4 - !?($minute + $hour % 4) + 6) % 6], @ordinal[$bells - 1], $bells == 1 ?? !! 's'; chime($bells); sub chime ($count) {
for 1..$count div 2 { print "\a♫ "; sleep .25; print "\a"; sleep .75; } if $count % 2 { print "\a♪"; sleep 1;
} print "\n"; }
}</lang>
- Output:
00:00 First watch, Eight Bells Gone: ♫ ♫ ♫ ♫ 00:30 Middle watch, One Bell Gone: ♪ 01:00 Middle watch, Two Bells Gone: ♫ 01:30 Middle watch, Three Bells Gone: ♫ ♪ 02:00 Middle watch, Four Bells Gone: ♫ ♫ 02:30 Middle watch, Five Bells Gone: ♫ ♫ ♪ 03:00 Middle watch, Six Bells Gone: ♫ ♫ ♫ 03:30 Middle watch, Seven Bells Gone: ♫ ♫ ♫ ♪ 04:00 Middle watch, Eight Bells Gone: ♫ ♫ ♫ ♫ 04:30 Morning watch, One Bell Gone: ♪ 05:00 Morning watch, Two Bells Gone: ♫ 05:30 Morning watch, Three Bells Gone: ♫ ♪ 06:00 Morning watch, Four Bells Gone: ♫ ♫ 06:30 Morning watch, Five Bells Gone: ♫ ♫ ♪ 07:00 Morning watch, Six Bells Gone: ♫ ♫ ♫ 07:30 Morning watch, Seven Bells Gone: ♫ ♫ ♫ ♪ 08:00 Morning watch, Eight Bells Gone: ♫ ♫ ♫ ♫ 08:30 Forenoon watch, One Bell Gone: ♪ 09:00 Forenoon watch, Two Bells Gone: ♫ 09:30 Forenoon watch, Three Bells Gone: ♫ ♪ 10:00 Forenoon watch, Four Bells Gone: ♫ ♫ 10:30 Forenoon watch, Five Bells Gone: ♫ ♫ ♪ 11:00 Forenoon watch, Six Bells Gone: ♫ ♫ ♫ 11:30 Forenoon watch, Seven Bells Gone: ♫ ♫ ♫ ♪ 12:00 Forenoon watch, Eight Bells Gone: ♫ ♫ ♫ ♫ 12:30 Afternoon watch, One Bell Gone: ♪ 13:00 Afternoon watch, Two Bells Gone: ♫ 13:30 Afternoon watch, Three Bells Gone: ♫ ♪ 14:00 Afternoon watch, Four Bells Gone: ♫ ♫ 14:30 Afternoon watch, Five Bells Gone: ♫ ♫ ♪ 15:00 Afternoon watch, Six Bells Gone: ♫ ♫ ♫ 15:30 Afternoon watch, Seven Bells Gone: ♫ ♫ ♫ ♪ 16:00 Afternoon watch, Eight Bells Gone: ♫ ♫ ♫ ♫ 16:30 Dog watch, One Bell Gone: ♪ 17:00 Dog watch, Two Bells Gone: ♫ 17:30 Dog watch, Three Bells Gone: ♫ ♪ 18:00 Dog watch, Four Bells Gone: ♫ ♫ 18:30 Dog watch, Five Bells Gone: ♫ ♫ ♪ 19:00 Dog watch, Six Bells Gone: ♫ ♫ ♫ 19:30 Dog watch, Seven Bells Gone: ♫ ♫ ♫ ♪ 20:00 Dog watch, Eight Bells Gone: ♫ ♫ ♫ ♫ 20:30 First watch, One Bell Gone: ♪ 21:00 First watch, Two Bells Gone: ♫ 21:30 First watch, Three Bells Gone: ♫ ♪ 22:00 First watch, Four Bells Gone: ♫ ♫ 22:30 First watch, Five Bells Gone: ♫ ♫ ♪ 23:00 First watch, Six Bells Gone: ♫ ♫ ♫ 23:30 First watch, Seven Bells Gone: ♫ ♫ ♫ ♪
Python
As well as typing output to stdout, this program plays a sound for each bell as the ␇ characters are printed (The spaces between the ␇ characters are mirrored as varying delays between each ring). <lang python>import time, calendar, sched, winsound
duration = 750 # Bell duration in ms freq = 1280 # Bell frequency in hertz bellchar = "\u2407" watches = 'Middle,Morning,Forenoon,Afternoon,First/Last dog,First'.split(',')
def gap(n=1):
time.sleep(n * duration / 1000)
off = gap
def on(n=1):
winsound.Beep(freq, n * duration)
def bong():
on(); off(0.5)
def bongs(m):
for i in range(m): print(bellchar, end=' ') bong() if i % 2: print(' ', end=) off(0.5) print()
scheds = sched.scheduler(time.time, time.sleep)
def ships_bell(now=None):
def adjust_to_half_hour(atime): atime[4] = (atime[4] // 30) * 30 atime[5] = 0 return atime
debug = now is not None rightnow = time.gmtime() if not debug: now = adjust_to_half_hour( list(rightnow) ) then = now[::] then[4] += 30 hr, mn = now[3:5] watch, b = divmod(int(2 * hr + mn // 30 - 1), 8) b += 1 bells = '%i bell%s' % (b, 's' if b > 1 else ' ') if debug: print("%02i:%02i, %-20s %s" % (now[3], now[4], watches[watch] + ' watch', bells), end=' ') else: print("%02i:%02i, %-20s %s" % (rightnow[3], rightnow[4], watches[watch] + ' watch', bells), end=' ') bongs(b) if not debug: scheds.enterabs(calendar.timegm(then), 0, ships_bell) #print(time.struct_time(then)) scheds.run()
def dbg_tester():
for h in range(24): for m in (0, 30): if (h,m) == (24,30): break ships_bell( [2013, 3, 2, h, m, 15, 5, 61, 0] )
if __name__ == '__main__':
ships_bell()</lang>
- Output:
00:00, First watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇ 00:30, Middle watch 1 bell ␇ 01:00, Middle watch 2 bells ␇ ␇ 01:30, Middle watch 3 bells ␇ ␇ ␇ 02:00, Middle watch 4 bells ␇ ␇ ␇ ␇ 02:30, Middle watch 5 bells ␇ ␇ ␇ ␇ ␇ 03:00, Middle watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇ 03:30, Middle watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ 04:00, Middle watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇ 04:30, Morning watch 1 bell ␇ 05:00, Morning watch 2 bells ␇ ␇ 05:30, Morning watch 3 bells ␇ ␇ ␇ 06:00, Morning watch 4 bells ␇ ␇ ␇ ␇ 06:30, Morning watch 5 bells ␇ ␇ ␇ ␇ ␇ 07:00, Morning watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇ 07:30, Morning watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ 08:00, Morning watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇ 08:30, Forenoon watch 1 bell ␇ 09:00, Forenoon watch 2 bells ␇ ␇ 09:30, Forenoon watch 3 bells ␇ ␇ ␇ 10:00, Forenoon watch 4 bells ␇ ␇ ␇ ␇ 10:30, Forenoon watch 5 bells ␇ ␇ ␇ ␇ ␇ 11:00, Forenoon watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇ 11:30, Forenoon watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ 12:00, Forenoon watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇ 12:30, Afternoon watch 1 bell ␇ 13:00, Afternoon watch 2 bells ␇ ␇ 13:30, Afternoon watch 3 bells ␇ ␇ ␇ 14:00, Afternoon watch 4 bells ␇ ␇ ␇ ␇ 14:30, Afternoon watch 5 bells ␇ ␇ ␇ ␇ ␇ 15:00, Afternoon watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇ 15:30, Afternoon watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ 16:00, Afternoon watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇ 16:30, First/Last dog watch 1 bell ␇ 17:00, First/Last dog watch 2 bells ␇ ␇ 17:30, First/Last dog watch 3 bells ␇ ␇ ␇ 18:00, First/Last dog watch 4 bells ␇ ␇ ␇ ␇ 18:30, First/Last dog watch 5 bells ␇ ␇ ␇ ␇ ␇ 19:00, First/Last dog watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇ 19:30, First/Last dog watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ 20:00, First/Last dog watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇ 20:30, First watch 1 bell ␇ 21:00, First watch 2 bells ␇ ␇ 21:30, First watch 3 bells ␇ ␇ ␇ 22:00, First watch 4 bells ␇ ␇ ␇ ␇ 22:30, First watch 5 bells ␇ ␇ ␇ ␇ ␇ 23:00, First watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇ 23:30, First watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇
REXX
The local time is used instead of Greenwich mean time.
If any arguments are specified, that text is used as a prefix to the times shown (once a minute). Also, the number of bells sounded are shown (if any arguments are specified). If no arguments are specified, no times are shown. In all cases, the PC speaker is used to sound the bells (albeit a poorly sounded bell).
This REXX program makes use of DELAY
BIF, which delays (sleeps) for a specified amount of seconds (some REXXes doen't have a DELAY
BIF, so one is included here), and SOUND
BIF, which produces sounds via the PC speaker (some REXXes doen't have a SOUND
BIF, so one is included here).
<lang rexx>/*REXX pgm sounds "bells" (using PC speaker) when running (perpetually).*/
echo= arg()\==0 /*echo time & bells if any args. */
signal on halt /*allow a clean way to stop prog.*/
t.1 = '00:30 01:00 01:30 02:00 02:30 03:00 03:30 04:00'
t.2 = '04:30 05:00 05:30 06:00 06:30 07:00 07:30 08:00'
t.3 = '08:30 09:00 09:30 10:00 10:30 11:00 11:30 12:00'
do forever; t=time(); ss=right(t,2); mn=right(t,2) /*times.*/ ct=time('C') /*[↓] add leading zero.*/ hhmmc=left( right( ct, 7, 0), 5) /*HH:MM (leading zero).*/ if echo then say center(arg(1) ct, 79) /*echo arg1 with time ?*/ if ss\==00 & mn\==00 & mn\==30 then /*wait for next min ? */ do; call delay 60-ss; iterate; end /*delay fraction of min*/
/*[↓] # bells to peel.*/ do j=1 for 3 until $\==0; $=wordpos(hhmmc,t.j); end /*j*/
if $\==0 & echo then say center($ "bells", 79) /*echo bells? */
do k=1 for $; call sound 650,1; call delay 1+(k//2==0); end /*k*/ /*[↑] peel and pause.*/ call delay 60 /*ensure don't re-peel.*/ end /*forever*/
halt: /*stick a fork in it, we're done.*/</lang> output when using the input of: the time is:
the time is: 1:48pm the time is: 1:49pm the time is: 1:50pm the time is: 1:51pm the time is: 1:52pm the time is: 1:53pm the time is: 1:54pm the time is: 1:55pm the time is: 1:56pm the time is: 1:57pm the time is: 1:58pm the time is: 1:59pm the time is: 2:00pm 4 bells the time is: 2:01pm the time is: 2:02pm ∙ ∙ ∙
Tcl
This code was originally based on the Perl 6 version, but with numerous adaptations, alterations and (some) corrections. <lang tcl># More sophisticated versions are possible, such as playing a bell sample
- using the Snack library.
proc ringTheBell {} {
puts -nonewline "\a"
}
- The code to convert the (parsed) time into rings of the ship's bell and
- printing of the name of the bell.
proc strikeBell {hour minute} {
global suppressNormalOutput set watches {
Middle Middle Morning Morning Forenoon Forenoon Afternoon Afternoon {First dog} {Last dog} First First
} set cardinals {one two three four five six seven eight} set bells [expr {(($hour % 4) * 2 + $minute / 30)}] if {!$bells} {set bells 8} puts -nonewline [format "%02d:%02d %9s watch, %6s bell%s gone: \t" \
$hour $minute [lindex $watches [expr { ($hour/2 - ($minute==0 && $hour%2==0)) % 12 }]] [lindex $cardinals [expr {$bells - 1}]] \ [expr {$bells == 1 ? "" : "s"}]]
# Set up the ringing of the bells to be done asynchronously set t 0 set suppressNormalOutput 1 for {set i 0} {$i < $bells-1} {incr i 2} {
after $t { ringTheBell puts -nonewline "\u266b " } incr t 250 after $t { ringTheBell } incr t 750
} if {$bells % 2} {
after $t { ringTheBell puts -nonewline "\u266a\n" set suppressNormalOutput 0 }
} else {
after $t { puts "" set suppressNormalOutput 0 }
}
}
- Main handler; designed to be called every second, which is plenty.
proc nauticalBell {} {
global last suppressNormalOutput scan [clock format [clock seconds] -format "%H:%M" -gmt 1] "%d:%d" h m if {$last != $m} {
set last $m if {$m%30 == 0} { strikeBell $h $m } elseif {!$suppressNormalOutput} { puts -nonewline [format "%02d:%02d\r" $h $m] }
}
}
- Set things up, using Tcl's event loop to do the processing
proc every {delay script} {
after $delay [list every $delay $script] uplevel #0 $script
} set last "" set suppressNormalOutput 0 fconfigure stdout -buffering none every 1000 nauticalBell vwait forever; # Only needed if not running an event loop otherwise</lang>
- Sample output:
⋮ 08:00 Morning watch, Eight Bells Gone: ♫ ♫ ♫ ♫ ⋮ 18:00 First dog watch, Four Bells Gone: ♫ ♫ 18:30 Last dog watch, Five Bells Gone: ♫ ♫ ♪ ⋮