This Mathematica notebook, © Copyright 2009 Scientific Arts, LLC, all rights reserved.

Almost nothing to do with A WorkLife FrameWork, but time will tell...

Posted on: Wednesday, July 11, 2007 at 03:12 AM UT  Notebook Version

Today I read Chris Carlson's Wolfram Blog posting about implementing an analog clock in Mathematica 6.

One of the main points of Chris's post was the comparison of the trials and tribulations that one has to endure in implementing such a thing in MatLab contrasted with the elemental straighforwardness of taking on the same task in Mathematica.

Chris posted a side-by-side of the two approaches. Of course it's striking.  But what I find even more striking is the simplicity with which one can modify the Mathematica code, as well as how one can use the code that Chris wrote as a starting point—a seedling—for an application of greater and greater sophistication.  I will give my example below, but the point that this can drive is that the process of developing applications in Mathematica is also vastly simpler and self-illuminating.  Chris does indeed comment on this, but I just feel like emphasizing this point somewhat more. Start from something and then continue to build on it.  And the iterative process of doing this in Mathematica is completely natural, transparent,  familiar—at least to me and, I suspect, to folks that come from a background of creative discovery in the sciences.  It's just the way one naturally explores.  Watch kids, they explore this way too...

So, starting with Chris's code, the first thing that I wanted to do is to make the clock smaller and to shrink the font as well.  My ultimate intention was to create a small window containing a small clock that I could leave open to keep track of the time (yes, there are gazillions of applications like this, but I was having fun, and the morning coffee wasn't ready yet).  Then I decided that it would be really annoying to have the clock ticking.  So I added this along with several options to control the ticking.

The final result (really, intermediate, since I am sure that I will have another go at it when I am in a future state of pre-coffee stupor) is this code:

TickTockClock::usage="TickTockClock[] opens an analog clock in a small window so that you can tell time in the old fashioned way.  The code is based on the code presented in http://blog.wolfram.com/2007/07/always_the_right_time.html. ";

TickTock::usage="TickTock is an option for TickTockClock that determines whether the TickTockClock will tick. It's default value, to keep you from going nuts, is False. ";

TickTockInstrument::usage="TickTockInstrument is an option for TickTockClock that specifies the instrument that is doing the ticking. Its default value is \"PizzicatoStrings\". See the Mathematica 6 documentation for the function SoundNote for a list of other possible choices for this option. ";

TickTockNote::usage="TickTockNote is an option for TickTockClock that specifies the pitch to use for the tick. ";

Options[TickTockClock]=
{FontSize->10,
FontFamily -> "Helvetica",
ImageSize->100,
CreateDocument->True,
TickTock->False,
TickTockInstrument->"PizzicatoStrings",
TickTockNote->"C6"};

TickTockClock[opts:OptionsPattern[]]:=
Module[{makeHand,hourHand,minuteHand,secondHand,fontSize,createDocument,theClock,fontFamily,imageSize,tickTock,tickTockInstrument,tickTockNote},

{fontSize,fontFamily,createDocument,imageSize,tickTock,tickTockInstrument,tickTockNote}={FontSize,FontFamily,CreateDocument,ImageSize,TickTock,TickTockInstrument,TickTockNote}/.Flatten[{opts,Options[TickTockClock]}];

makeHand[fl_, bl_, fw_, bw_] :=Polygon[{{-bw, -bl}, {bw, -bl}, {fw, fl}, {0, fl + 8 fw}, {-fw, fl}}/9];

hourHand = makeHand[5, (5/3), .1, .3];
minuteHand = makeHand[7, (7/3), .1, .3];
secondHand = {Red, EdgeForm[Black], makeHand[7, (7/3), .1/2,.3/2]};

theClock=Graphics[{

{Thickness[.03], Circle[]},(* Rim *)

{Thickness[.003], Table[Line[{.9 {Cos[a], Sin[a]}, .95 {Cos[a], Sin[a]}}], {a,0,2 π,((2 π)/60)}]}, (* Thin ticks *)

{Thickness[.01], Table[Line[{.9 {Cos[a], Sin[a]}, .95 {Cos[a], Sin[a]}}], {a,0,2 π,((2 π)/12)}]}, (* Thick ticks *)

Style[Table[Text[i, .77 {Cos[-((i π)/6)+(π/2)],Sin[-((i π)/6)+(π/2)]}], {i, 1, 12}], FontFamily -> fontFamily, FontSize -> fontSize], (* Numbers *)

Rotate[hourHand, Dynamic[Refresh[-30 Mod[((AbsoluteTime[])/3600), 60] °, UpdateInterval -> 60]], {0, 0}],

Rotate[minuteHand, Dynamic[Refresh[If[tickTock,EmitSound[Sound[SoundNote[tickTockNote,.1,tickTockInstrument,SoundVolume->.1]]]];-6 Mod[((AbsoluteTime[])/60), 60] °, UpdateInterval -> 1]], {0, 0}],

Rotate[secondHand, Dynamic[Refresh[-6 Mod[AbsoluteTime[], 60] °, UpdateInterval -> (1/20)]], {0, 0}]
  },FilterRules[Flatten[{opts,Options[TickTockClock]}],Options[Graphics]]];

Switch[createDocument,

True,
CreateDocument[{TextCell[Grid[{{theClock}}],TextAlignment->Center,CellMargins->{{0,0},{0,0}},ShowCellBracket->False]},
WindowFrame->"Palette",
WindowSize->{imageSize+5,imageSize+5},
WindowElements->{},
WindowFrameElements->{"CloseBox"},
WindowTitle->"Clock"],

_,
theClock

]
];

Then just execute the following to get your own pizzicato clock

TickTockClock[TickTock->True]

Or, for a slightly different effect:

TickTockClock[TickTock-> True, TickTockInstrument-> "Glockenspiel"]

To show the TickTockClock in-line rather than in a seperate window, just execute

TickTockClock[TickTock->True,CreateDocument->False]

And, of course, when you open this notebook in Mathematica 6 the clock will be working, rather than the Gif that the blog version of this posting displays.

It would be quite easy to turn this into an alarm clock which would play anything you program it to as its alarm. Or perhaps create an option to make it a Looking-glass Clock. But, I have other things to attend to, such as the continuing development of  A WorkLife FrameWork...

Chris says that creating his analog clock took him about five minutes.  I'm just not that fast, but I am also still coming up to speed with Mathematica 6, and he's one of the people who "wrote the book."  For me, starting with Chris's code, I took another 20 minutes to come up with the code above. Then, even though I didn't want to, I had to go attend to other things.  I acruued another 10 minutes making sure that the usage messages were in place for this blog posting.

I suspect that this next 20 minutes of development would have taken a bit more relative effort in MatLab.

Somewhere in between the 20 minutes and the 10 minutes I had a cup of coffee and lived out the middle of my day...

But what does this have to do with A WorkLife FrameWork?

So what does this have to do with A WorkLife FrameWork? I think that the main point is that A WorkLife FrameWork would not have been possible if it were not for the powerful self-illuminating way that Mathematica code can be created, and the ease with which one can go through the process of coding by creative discovery.  I was reminded of this experience "in the small" as I started to play with Chris's bit of code and add some additional ideas to it...

My experience "in the large" is the several thousand hours I spent (most of it enjoyable!) creating and coding A WorkLife FrameWork. My process of adding features and refining what is already in A WorkLife FrameWork feels exactly the same as the experience I had with Chris's code.

Notebook version....

You can download a Mathematica 6 notebook version of this blog post from the "Notebook" link just below...