obsolete.computer/geekery/

Commodore 128 Analog Clock

C128 Analog Clock - 1 of 2 C128 Analog Clock - 2 of 2

My 3yo son is obsessed with clocks, in particular he loves analog clocks. So I decided to take a stab at making one on the C128. Ahem, what I mean is, I decided to ask Grok to take a stab at making one. That got me about 80% of the way there. After some tinkering right on the 128's old school command line, I got it working reasonably well. There were some initial errors, but most of the tinkering was actually optimizing the drawing routing so that it only has to redraw the hands when they actually move. Here is the listing (note petcat renders the pi symbol as ~):

   10 rem analog clock for commodore 128
   20 rem uses ti$ ("hhmmss") variable
   30 rg=rgr(0) : rem return to the same display we started on
   40 color 0,1 : color 4,1 : color 1,16 : rem bk=blk, border=blk, fg=yellow
   50 graphic 1,1     : rem hires mode + clear screen
   60 scale 0
   70 gosub 600
   80 rem === main loop ===
   90 do
  100 get a$ : if a$=chr$(27) then goto 500
  110 t$=ti$
  120 h=val(left$(t$,2)) : m=val(mid$(t$,3,2)) : s=val(right$(t$,2))
  130 gosub 720       : rem draw clock hands
  140 do : loop while t$=ti$
  150 loop
  160 :
  500 if rg=0 then graphic 0: else graphic 5
  510 color 5,2 : scnclr : end
  600 rem === draw clock face ===
  610 draw 1,160,100 to 160,20   : rem 12 o'clock reference line
  620 circle 1,160,100,80        : rem outer circle
  630 circle 1,160,100,4         : rem small center dot
  640 :
  650 rem === hour marks ===
  660 for i=0 to 11
  670 a=i*30 : x=sin(a*~/180)*72 : y=-cos(a*~/180)*72
  680 draw 1,160+x,100+y to 160+x*1.15,100+y*1.15
  690 next
  700 :
  710 rem === calculate angles (0 degrees at 3 o'clock) ===
  720 sa = s * 6                         : rem secs
  730 ma = int( m * 6 + s * 0.1 )        : rem mins
  740 ha = int((h and 11) * 30 + m * 0.5): rem hours
  750 :
  760 rem === erase old hands (draw in background color=0) then draw new ===
  770 if sa <> so then draw 0,160,100 to 160+sin(so*~/180)*68,100-cos(so*~/180)*68   : so = sa : rem erase sec
  780 if ma <> mo then draw 0,160,100 to 160+sin(mo*~/180)*62,100-cos(mo*~/180)*62   : mo = ma : rem erase min
  790 if ha <> ho then draw 0,160,100 to 160+sin(ho*~/180)*48,100-cos(ho*~/180)*48   : ho = ha : rem erase hour
  800 draw 1,160,100 to 160+sin(sa*~/180)*68,100-cos(sa*~/180)*68   : rem draw sec
  810 draw 1,160,100 to 160+sin(ma*~/180)*62,100-cos(ma*~/180)*62   : rem draw min
  820 draw 1,160,100 to 160+sin(ha*~/180)*48,100-cos(ha*~/180)*48   : rem draw hour
  830 circle 1,160,100,4         : rem redraw small center dot
  840 return