HTBasic Help
×
Menu
Index

Example: Alarm Clock

 
10     ! *********************************************************************
20     !  Example: Alarm Clock
30     !
40     !  This program builds a digital alarm clock. You can set an alarm
50     !  time using a SCROLLBAR. A TOGGLEBUTTON allows you to disable
60     !  or enable alarm operation. A PUSHBUTTON allows you to turn off
70     !  the alarm.
80     !
90     ! *********************************************************************
100   !
110   CLEAR SCREEN
120   OPTION BASE 1
130   !
140   INTEGER Alarmval,Hour,Minute,N,Noisy
150   Noisy=0
160   !
170   DIM A$[8],T$[8]
180   !
190   INTEGER Black,White,Red,Yellow,Green,Cyan,Blue,Magenta
200   !
210   Black=0
220   White=1
230   Red=2
240   Yellow=3
250   Green=4
260   Cyan=5
270   Blue=6
280   Magenta=7
290   !
300   INTEGER A(6),Nlines
310   REAL Dw,Dh,Vh,Pw,Ph,Px,Py
320   !
330   GESCAPE CRT,3;A(*)                                ! Get display bounds
340   Dw=A(3)-A(1)+1                                                ! Compute display width in pixels
350   Dh=A(4)-A(2)+1                                                ! Compute display height in pixels
360   STATUS CRT,13;Nlines                                ! Get number of text lines on display
370   Vh=Dh*(1-6/Nlines)                                ! Height above DISP line
380   !
390   Pw=320                                                                ! PANEL width is half display width
400   Ph=240                                                                ! Same for PANEL height
410   Px=(Dw-Pw)/2                                                ! Center panel horizontally
420   Py=(Vh-Ph)/2                                                ! Center panel above DISP line
430   !
440   ! Build the main PANEL
450   !
460   ASSIGN @Main TO WIDGET "PANEL";SET ("VISIBLE":0)
470   CONTROL @Main;SET ("X":Px,"Y":Py,"WIDTH":1.2*Pw,"HEIGHT":Ph)
480   CONTROL @Main;SET ("TITLE":" Example: Alarm Clock")
490   CONTROL @Main;SET ("MAXIMIZABLE":0,"RESIZABLE":0)
500   CONTROL @Main;SET ("BACKGROUND":Blue)
510   CONTROL @Main;SET ("SYSTEM MENU":"Quit")
520   ON EVENT @Main,"SYSTEM MENU" GOTO Finis
530   !
540   ! Dimensions and coordinates for child widgets
550   !
560   REAL Ih,Iw                                                                ! Inside height and width
570   REAL Gw,Gh,Lw,Bw,Lh,Sw,Sh,Bgap                ! Child widget dimensions
580   REAL Y1,Y2,Y3,Y4,X1,X2                                                ! Child widget coordinates
590   !
600   STATUS @Main;RETURN ("INSIDE WIDTH":Iw,"INSIDE HEIGHT":Ih)
610   !
620   Gh=Ih*.05                                                ! Arbitrary height of vertical gap
630   Gw=Iw*.02                                                ! Arbitrary width of horizontal gap
640   Wh=Ih*.3                                                ! Arbitrary height of LABEL & BUTTON widgets
650   Lw=Iw*.6                                                ! Arbitrary width of LABEL widgets
660   !
670   X1=Gw                                                ! Right side of LABELs & SCROLLBAR
680   X2=X1+Lw+Gw                                ! Right side of BUTTONs
690   Bw=Iw-(X2+Gw)                                ! BUTTON width
700   Sw=Iw-2*Gw                                ! SCROLLBAR width
710   !
720   Y1=Gh                                                ! Position of first row of widgets
730   Y2=Y1+Wh+Gh                                ! Position of second row of widgets
740   Y3=Y2+Wh                                ! Position of bottom of second row of widgets
750   Bgap=Ih-Y3                                ! Height of bottom gap on PANEL
760   !
770   ! Create LABEL for time value
780   !
790   ASSIGN @Clock TO WIDGET "LABEL";PARENT @Main
800   CONTROL @Clock;SET ("X":X1,"Y":Y1,"WIDTH":Lw,"HEIGHT":Wh)
810   CONTROL @Clock;SET ("FONT":"18 BY 30,BOLD")
820   CONTROL @Clock;SET ("BACKGROUND":Black,"PEN":Red,"BORDER":0)
830   !
840   ! Create TOGGLEBUTTON to enable/disable alarm
850   !
860   ASSIGN @Toggle TO WIDGET "TOGGLEBUTTON";PARENT @Main
870   CONTROL @Toggle;SET ("X":X2,"Y":Y1,"WIDTH":Bw,"HEIGHT":Wh)
880   CONTROL @Toggle;SET ("FONT":"18 BY 16,BOLD","LABEL":" ENABLE ALARM")
890   !
900   ! Create LABEL for alarm value
910   !
920   ASSIGN @Alarm TO WIDGET "LABEL";PARENT @Main
930   CONTROL @Alarm;SET ("X":X1,"Y":Y2,"WIDTH":Lw,"HEIGHT":Wh)
940   !
950   ! Create PUSHBUTTON to stop alarm
960   !
970   ASSIGN @Stop TO WIDGET "PUSHBUTTON";PARENT @Main
980   CONTROL @Stop;SET ("X":X2,"Y":Y2,"WIDTH":Bw,"HEIGHT":Wh)
990   CONTROL @Stop;SET ("FONT":"18 BY 16,BOLD","LABEL":" STOP ALARM")
1000  CONTROL @Stop;SET ("LABEL":"STOP ALARM")
1010  !
1020  ! Create SCROLLBAR, then center
1030  !
1040  ASSIGN @Scroll TO WIDGET "SCROLLBAR";PARENT @Main
1050  CONTROL @Scroll;SET ("ORIENTATION":"HORIZONTAL")
1060  STATUS @Scroll;RETURN ("HEIGHT":Sh)
1070  Y4=Y3+(Bgap-Sh)/2
1080  CONTROL @Scroll;SET ("X":X1,"Y":Y4,"WIDTH":Sw)
1090  !
1100  ! Set scale on SCROLLBAR to run through minutes of day.
1110  ! Allow slider to be incremented by hour and minute values.
1120  !
1130  CONTROL @Scroll;SET ("MINIMUM":0,"MAXIMUM":1439)
1140  CONTROL @Scroll;SET ("MINOR INCREMENT":1,"MAJOR INCREMENT":60)
1150  !
1160  ! Set initial time and alarm
1170  !
1180  GOSUB Update
1190  GOSUB Newalarm
1200  !
1210  ! Make application visible
1220  !
1230  CLEAR SCREEN
1240  CONTROL @Main;SET ("VISIBLE":1)
1250  !
1260  ! Enable widget event
1270  !
1280  ON EVENT @Scroll,"CHANGED" GOSUB Newalarm
1290  ON CYCLE 1 GOSUB Update
1300  !
1310  ! Loop until the user presses a key, then exit
1320  !
1330  LOOP
1340    WAIT FOR EVENT
1350  END LOOP
1360  !
1370  ! ***************** End of Main Program **************************
1380  !
1390  ! This routine updates the clock time. If the alarm is on,
1400  ! (Noisy=1), it will beep.
1410  !
1420  ! Once it updates the time, it checks the time for a match against the
1430  ! alarm. If there is a match, it sets Noisy to cause an alarm, causes
1440  ! a beep, enables an event on the PUSHBUTTON to turn off the alarm,
1450  ! and turns the PUSHBUTTON red.
1460  !
1470 Update:!
1480  IF Noisy=1 THEN BEEP 3000,.15
1490  T$=TIME$(TIMEDATE)
1500  CONTROL @Clock;SET ("VALUE":T$)
1510  !
1520  STATUS @Toggle;RETURN ("VALUE":N)                                ! Is alarm enabled?
1530  IF N=1 THEN                                                                                ! Yes --
1540    IF A$=T$ THEN                                                                                ! Compare time
1550      Noisy=1
1560      BEEP 3000,.15
1570      ON EVENT @Stop,"ACTIVATED" GOSUB Killnoise
1580      CONTROL @Stop;SET ("BACKGROUND":Red)
1590    END IF
1600  END IF
1610  RETURN
1620  !
1630  ! This routine turns off the alarm
1640  !
1650 Killnoise:!
1660  OFF EVENT @Stop,"ACTIVATED"
1670  CONTROL @Stop;SET ("BACKGROUND":Blue)
1680  Noisy=0
1690  RETURN
1700  !
1710  ! This routine updates the alarm value. The value provided by
1720  ! the SCROLLBAR is converted into a time strng that matches
1730  ! that provided by TIME$(TIMEDATE).
1740  !
1750 Newalarm:!
1760  STATUS @Scroll;RETURN ("VALUE":Alarmval)
1770  !
1780  Hour=Alarmval DIV 60
1790  SELECT Hour
1800  CASE 0
1810    A$="12"
1820  CASE 1 TO 9
1830    A$="0"&VAL$(Hour)
1840  CASE ELSE
1850    A$=VAL$(Hour)
1860  END SELECT
1870  A$=A$&":"
1880  !
1890  Minute=Alarmval MOD 60
1900  SELECT Minute
1910  CASE 0 TO 9
1920    A$=A$&"0"&VAL$(Minute)
1930  CASE ELSE
1940    A$=A$&VAL$(Minute)
1950  END SELECT
1960  A$=A$&":00"
1970  !
1980  CONTROL @Alarm;SET ("VALUE":A$)
1990  RETURN
2000  !
2010 Finis:!
2020  ASSIGN @Main TO *                                ! Delete PANEL widget
2030  CLEAR SCREEN
2040  END