
{  Edited by SM at 09:06   17/7/87  }

%begin
%include "Level1:graphinc.imp"
%include "inc:maths.imp"
%include "inc:fs.imp"
%include "inc:util.imp"

%record %format pointfm(%integer x, y)
%record %format linefm(%integer x0, x1, y)

%string(31) alarm time
%integer alarm width
%integer background=black
%constinteger bl=16_7800, grn=16_3C0, rd=16_1e, flashing=16_8000
%const %integer cx=344
%integer cy=256, ccy=768
%string(31) d, digital date, digital time=""
%integer dateend, datex=0, datey=0, datey1=0
%integer dt=0
%integer dr, dw, hr, mr, sr
%externalstring(255)%fnspec itod(%integer n)
%integer fnt, i, x, y, h, m, s, ss, sss, t, tt, ah, am, as,
   al h, al m, al s, state, alarm, ring, setting, sym, text x
%real piov6, piov30, piov1500
%integer size=250
%integer timeend, timex=0, timey=0, timey1=0
%string(31) u,v
%real xt, yt
%record(pointfm) a, b, c, ha, hb, hc, ma, mb, mc, mma, mmb, mmc,
       hha, hhb, hhc
%record(pointfm) %array da, db, dc(0:11)
%string(15) time
%half %integer %array colmap(0:255)

%routine message(%string(255) s,%string(15) mon)
%integer oldout
  oldout=outstream
  select output(0)
  print string(snl.s.snl)
  %monitor %if mon="mon"
  %stop %if mon="stop" %or mon="mon"
  select output(oldout)
%end

%routine draw clock(%integer cy)
   dr=size*31//32
   dw=size//32
   hr=size*5//8
   mr=size*25//32
   sr=size*30//32
   font(0)
   colour(white)
   text at(8, cy-248)
   show string("Alarm time :")
   colour(blue)
   piov6=pi
   piov30=piov6/30
   piov1500=piov6/1500
   piov6=piov6/6
   disc(cx, cy, size)
   colour(red)
   %for i=3, 1, 14 %cycle
      xt=cos(i*piov6)
      yt=sin(i*piov6)
      a_x=intpt(xt*(mr+1))
      a_y=intpt(yt*(mr+1))
      b_x=intpt(xt*dr+yt*dw)
      b_y=intpt(yt*dr-xt*dw)
      c_x=intpt(xt*dr-yt*dw)
      c_y=intpt(yt*dr+xt*dw)
      da(i-3)=a
      db(i-3)=b
      dc(i-3)=c
      a_x=a_x+cx
      a_y=a_y+cy
      b_x=b_x+cx
      b_y=b_y+cy
      c_x=c_x+cx
      c_y=c_y+cy
      triangle(a_x, a_y, b_x, b_y, c_x, c_y)
   %repeat
   a_x=cx
   a_y=cx
   ha=a
   hb=a
   hc=a
   ma=a
   mb=a
   mc=a
   hha=a
   hhb=a
   hhc=a
   mma=a
   mmb=a
   mmc=a
   ss =0
   sss=0
%end

%routine show time(%integer hrs, mins, secs)
%integer temp, x, wait
   x=rem(hrs,12)
   temp=cy
   cy=ccy
   ccy=temp
   temp=timey
   timey=timey1
   timey1=temp
   temp=datey
   datey=datey1
   datey1=temp
   colour(blue)
   triangle(hha_x, hha_y, hhb_x, hhb_y, hhc_x, hhc_y)
   triangle(mma_x, mma_y, mmb_x, mmb_y, mmc_x, mmc_y)
   yt=(3750-sss)*piov1500
   line(cx, cy, cx+intpt(cos(yt)*sr), cy+intpt(sin(yt)*sr))
   sss=rem((3125-sss)//250, 12)
   colour(red)
   a=da(sss)
   b=db(sss)
   c=dc(sss)
   a_x=a_x+cx
   a_y=a_y+cy
   b_x=b_x+cx
   b_y=b_y+cy
   c_x=c_x+cx
   c_y=c_y+cy
   triangle(a_x, a_y, b_x, b_y, c_x, c_y)
   colour(yellow)
   yt=(75-x*5-mins/12)*piov30
   xt=cos(yt)
   yt=sin(yt)
   a_x=cx+intpt(xt*hr)
   a_y=cy+intpt(yt*hr)
   b_x=cx+intpt(yt*dw)
   b_y=cy+intpt(-xt*dw)
   c_x=cx+intpt(-yt*dw)
   c_y=cy+intpt(xt*dw)
   hha=ha
   hhb=hb
   hhc=hc
   ha=a
   hb=b
   hc=c
   triangle(a_x, a_y, b_x, b_y, c_x, c_y)
   colour(green)
   yt=(75-mins-secs/3000)*piov30
   xt=cos(yt)
   yt=sin(yt)
   a_x=cx+intpt(xt*mr)
   a_y=cy+intpt(yt*mr)
   b_x=cx+intpt(yt*dw)
   b_y=cy+intpt(-xt*dw)
   c_x=cx+intpt(-yt*dw)
   c_y=cy+intpt(xt*dw)
   mma=ma
   mmb=mb
   mmc=mc
   ma=a
   mb=b
   mc=c
   triangle(a_x, a_y, b_x, b_y, c_x, c_y)
   colour(white)
   yt=(3750-secs)*piov1500
   line(cx, cy, cx+intpt(cos(yt)*sr), cy+intpt(sin(yt)*sr))
   disc(cx, cy, dw)
   sss=ss
   ss=secs
   font(fnt)
   colour(blue)
   fill(timex,timey-font depth,timex+timeend,timey+font height)
   fill(datex,datey-font depth,datex+dateend,datey+font height)
   colour(white)
   text at(timex,timey)
   show string(digital time)
   text at(datex,datey)
   show string(digital date)
   font(0)
   offset(0, cy-256)
   wait=cpu time+20
   %while cpu time<=wait %cycle
      print symbol(7) %unless ring=0
   %repeat
%end

%string(15) %function datime
   %string(255) s
   length(s)=fcommr('G0',"",charno(s,1),255)
   %result=s
%end

%integerfn dtoi(%string(255) s)
%integer i,j
  j=0
  %for i=1,1,length(s) %cycle
    j=10*j+charno(s,i)-'0' %if '0'<=charno(s,i)<='9'
  %repeat
  %result=j
%end

{!  Set clock from file-store clock.}
{   time=datime}
{   h=(charno(time, 11)-'0')*10+charno(time, 12)-'0'}
{   h=rem(h, 12)}
{   m=(charno(time, 14)-'0')*10+charno(time, 15)-'0'}
{   m=rem(m, 60)}

!  Read font and set-up space for digital date and time.
   read font("level1:Timesb7",fnt)
   font(fnt)
   timeend=string width(" 43 48 48 ")
   timex=cx-timeend//2
   timey=cy-3*font height; timey1=ccy-cy+timey
   dateend=string width(" 26/11/87 " )
   datex=cx-dateend//2
   datey=cy+3*font height; datey1=ccy-cy+datey
   digital date=""
   digital time=""
   font(0)
!  Read date from manual seting.
   prompt("Day:")
   read(u)
   u=u.v %while u->u.(" ").v
   digital date=" ".u
   prompt("Month:")
   read(u)
   u=u.v %while u->u.(" ").v
   digital date=digital date." ".u
   prompt("Year:")
   read(u)
   u=u.v %while u->u.(" ").v
   digital date=digital date." ".u
!  Read time from manual setting of clock.
   print symbol('R'); newline
   prompt("Hr:")
   read(h)
   h=rem(h, 24)
   prompt("Min:")
   read(m)
   m=rem(m, 60)
   prompt("Sec:")
   read(s)
   s=rem(s,60)
   s=s*1000
   t=test symbol %until t=nl    ;!  To get rid of rest of line including NL.
   t=cpu time
!  Draw clock-face.
   clear
   offset(0, 0)
   draw clock(256)
   draw clock(768)
   font(fnt)
   text at(datex,datey)
   show string(digital date)
   text at(datex,datey1)
   show string(digital date)
!  Initial setting of organisation for alarm.
   alarm=0; alarm time=""
   al h=0; al m=0; al s=0
   ah=0; am=0; as=0
   ring=0
   text x=112
   font(0)
   alarm width=string width("56 56 56 ")
!  Cycle, displaying time and looking for alarm setting.
   %cycle
      sym=test symbol
      %if sym>0 %thenstart
!  If alarm set then switch off alarm and clear alarm time.
         %if alarm=1 %thenstart
            alarm=0; ring=0
            al h=0; al m=0; al s=0
            ah=0; am=0; as=0
            colour map(0)=1
            font(0)
            colour(background)
            text x=112
            fill(text x,8,text x+alarm width,8+font height)
            fill(text x,520,text x+alarm width,520+font height)
         %finish
!  Deal with delete.
         %if sym=127 %thenstart
            %if length(alarm time)>0 %thenstart
               length(alarm time)=length(alarm time)-1
               text x=text x-8
               font(0)
               colour(background)
               fill(text x,8,text x+8,8+font height)
               fill(text x,520,text x+8,520+font height)
            %finish
!  Deal with NL.
         %finishelse %if sym=nl %thenstart
            font(0)
            colour(background)
            text x=112
            fill(text x,8,text x+alarm width,8+font height)
            fill(textx,520,text x+alarm width,520+font height)
            alarm=1
            alarm time=u."$".v %while alarm time->u.(" ").v %c
            %or alarm time->u.(":").v %or alarm time->u.(".").v
            alarm time=u."$".v %while alarm time->u.("$$").v
            alarm time=u." ".v %while alarm time->u.("$").v
            v=alarm time
            u=v %and v="" %unless v->u.(" ").v
            ah=dtoi(u)
            al h=rem(ah,24)
            u=v %and v="" %unless v->u.(" ").v
            am=dtoi(u)
            al m=rem(am,60)
            u=v %and v="" %unless v->u.(" ").v
            as=dtoi(u)
            al s=rem(as,60)
            colour(white)
            text at(text x,8)
            show string(alarm time)
            text at(text x,520)
            show string(alarm time)
            alarm time=""
!  Deal with general characters.
         %finishelsestart
            %if '0'<=sym<='9' %or sym=' ' %or sym=':' %or sym='.' %thenstart
              font(0)
              colour(white)
              text at(text x,8)
              show symbol(sym)
              text at(text x,520)
              show symbol(sym)
              text x=text x+8
              alarm time=alarm time.tostring(sym)
            %finish
         %finish
      %finish
!  Advance time.
      tt=t
      t=cpu time
      s=s+t-tt
      %if s>=60000 %start
         s=s-60000
         m=rem(m+1, 60)
         %if m=0 %thenstart
            h=rem(h+1, 24)
            %if h=0 %thenstart
               digital date=" ".date
               font(fnt)
               colour(background)
               fill(datex,datey,datex+dateend,datey+font height)
               fill(datex,datey1,datex+dateend,datey1+font height)
               text at(datex,datey)
               show string(digital date)
               text at(datex,datey1)
               show string(digital date)
            %finish
         %finish
      %finish
!  Set digital time every second.
      %if dt>1000 %thenstart
         dt=dt-1000
         d=itos(h,2)
         charno(d,2)='0' %if charno(d,2)=' '
         digital time=d
         d=itos(m,2)
         charno(d,2)='0' %if charno(d,2)=' '
         digital time=digital time.d
         d=itos(((s+50)//20)//50,2)
         charno(d,2)='0' %if charno(d,2)=' '
         digital time=digital time.d
      %finish
      %if alarm=0 %thenstart
!  Alarm time not yet set.
         ring=0
         colour map(0)=1    ;! Fudge to avoid using the read before write instruction.
      %finishelsestart
!  Alarm time set.
         %if al h=h %and al m=m %and al s=s//1000 %thenstart
            ring=1
            colour map(0)=flashing+rd+grn+bl
         %finish
      %finish
!  Display time.
      show time(h, m, (s+50)//20)
      dt=dt+t-tt
   %repeat
%end %of %program

