$lib "glcdKS108.lib"
$crystal = 8000000


$regfile = "m32def.dat"


'Dim variables
Dim State As Byte                                           ' Menu State

Dim J As Byte                                               ' a counter used all over the place
Dim K As Single                                             ' For Working Out temp and Humidity
Dim X As Byte                                               ' Lines in setup and selection in day of week
Dim Y As Byte                                               ' Lines in setup and selection in day of week
Dim X1 As Byte                                              ' Lines in setup
Dim Y1 As Byte                                              ' Lines in setup
Dim Count As Byte                                           ' For timeout

Dim Odd As Bit                                              ' Toggled every second for blinking effects

Dim Alarmtime As String * 5                                 ' Alarm Time String
Dim _alarmhour As Byte                                      ' Alarm Hours
Dim _alarmmin As Byte                                       'Alarm Minutes
Dim Alarm As Bit                                            ' alarm state
Dim Alarmday(7) As Byte                                     'days alarm goes off on
Dim Alarmset As Bit                                         ' Alarm Set
Dim Alarmoveride As Bit                                     ' Alarm Overide
Dim Snooze As Bit                                           ' Snooze set
Dim Snoozemincount As Byte                                  ' Snooze Minute Counter
Dim Snoozesec As Byte                                       ' Snooze Pressed Seconds Holder
Dim Alarmbeepcount As Byte                                  ' Alarm Blip Count

Dim Carpos As Byte                                          ' Where character gets placed
Dim Stringcount As Byte                                     ' Count thru time$
Dim Disp As String * 8                                      'built up time string minus :'s
Dim N As String * 6                                         ' string of picked number

Dim Hours As String * 2                                     ' Hours
Dim Minutes As String * 2                                   'Minutes
Dim Seconds As String * 2                                   'seconds


Dim Key As Byte                                             ' Key Value
Dim Keybeep As Bit                                          'keySound on or off

Dim Menupos As Byte                                         'Menu Position
Dim Menurow As Byte                                         'Menu Position

Dim 24h As Bit                                              ' 24hour = 1 12 hour = 0

Dim Day As String * 3                                       'Day Wording

Dim Temp As Byte                                            ' Temp
Dim Tempmax As Byte                                         'Max Temp
Dim Tempmin As Byte                                         'Min Temp
Dim Templog(25) As Byte                                     'Temp Log over 24 hours
Dim Tempmaxtime As String * 5                               'Time Of Max Temp
Dim Tempmintime As String * 5                               'time of min temp

Dim Hum As Byte                                             ' Humidity
Dim Hummax As Byte                                          ' Humidity Max
Dim Hummin As Byte                                          ' Humidity Min
Dim Humlog(25) As Byte                                      ' Humidity log over 24 hours
Dim Hummaxtime As String * 5                                'Time Of Max humidity
Dim Hummintime As String * 5                                'Time of Min Humidity

Dim _adc As Word                                            ' ADC Readings
Dim Bvolts As String * 5                                    ' Battery Voltage
Dim _bvolts As Single                                       ' battery Voltage Variable
Dim Volts As String * 5                                     ' Plugpack Voltage
Dim _volts As Single                                        ' Plugpack Voltage Variable
Dim _light As Single                                        'Light Measurement
Dim Light As String * 5

Dim Bllevel As Single                                       ' Backlight trigger level

Dim _single As Single                                       ' Placeholder to work with

Dim Onsecond As Bit                                         ' More to do on Second
Dim Onhour As Bit                                           ' Stuff to do on the hour

Humlog(25) = 50
Humlog(24) = 25
Humlog(23) = 50
Humlog(22) = 99
Humlog(21) = 75


Enable Interrupts

Disable Serial
Config Pinc.= Input                                       'key1
Config Pinc.= Input                                       'key2
Config Pinc.= Input                                       'key3
Config Pinc.= Input                                       'key4
Config Pinc.= Input                                       'snooze button
Config Pina.= Output                                      ' speaker
Config Pina.= Output                                      'backlight
Config Pina.= Input                                       'Humidity input
Config Pina.= Input                                       'temp input
Config Pina.= Input                                       'Battery Voltage
Config Pina.= Input                                       'PLugPack Voltage
Config Pina.= Input                                       ' Backlight sensor
Config Pina.= Input

'First we define that we use a graphic LCD
Config Graphlcd = 128 * 64sed , Dataport = Portd , Controlport = Portb , Ce = 0 , Ce2 = 1 , Cd = 4 , Rd = 3 , Reset = 2 , Enable = 5

' Config Clock
Config Clock = Soft , Gosub = Sectic
Config Date = Dmy , Separator = -                           'ANSI-Format
Date= "01/01/00"
Time= "00:30:00"

24h = 0
Odd = 1
Menupos = 4
Menurow = 1
Tempmax = 0
Tempmin = 100
Hummax = 0
Hummin = 100
Carpos = 0
State = 0                                                   ' 0=time, 1=SetAlarm, 2=Settime, 3=SetDate, 4=12/24set, 5=SetAlarmDay,6 BeepOnOff,7 Backlight,8 Setup,9 temp,10 humidity,11 temp trend, 12 hum trend
Alarm = 0
Alarmset = 0
Alarmtime = "00:01"
_alarmhour = 12
_alarmmin = 0
Keybeep = 0
Snooze = 0
Alarmbeepcount = 1
Count = 0
Onsecond = 1
Onhour = 0
Bllevel = 1


'specify the font we want to use
Setfont Alarm

'Set up ADC
Config Adc = Single , Prescaler = Auto

'Now give power to the chip
Start Adc

' Set backlight to full
'Reset Porta.7

Cls

'Goto Setup

' Main Routine----------------------------------------------------------------------------------------------------------
Main:

' Seperate time
Disp = Time$
Hours = Left(time, 2)
Minutes = Mid(time, 4 , 2)
Seconds = Right(time, 2)

If Minutes = "00" Then Onhour = 1


If Onsecond = 1 Then
   ' Read Backup Battery Voltage
   _adc = Getadc(2)
   _bvolts = _adc * 0.00492
   Bvolts = Fusing(_bvolts , "#.##")

   ' Read Plug Pack Voltage
   _adc = Getadc(3)
   _volts = _adc * 0.00492
   _volts = _volts * 2
   _volts = _volts * 26
   Volts = Fusing(_volts , "#.##")

   'Get Light Value of room
   _adc = Getadc(4)
   _light = _adc * 0.00492
   Light = Fusing(_light , "#.##")


   If _light => Bllevel Then
      If _volts > 2 Then
         Reset Porta.7
      End If
   End If

   If _light < Bllevel Then
      Set Porta.7
   End If

   ' Check Mains Voltage is connected and if not, set display dim
   If _volts < 2 Then
      Set Porta.7
   End If


   ' Get Temp and Humidity
   _adc = Getadc(0)
   _single = _adc * 0.492
   Hum = Int(_single)

   _adc = Getadc(1)
   _single = _adc * 0.492
   _single = _single - 5
   Temp = Int(_single)


   Onsecond = 0
End If

' process temp and humidity max and min's

If Hum > 99 Then
   Hum = 99
End If

If Hum > Hummax Then
   Hummax = Hum
   Hummaxtime = Hours + ":" + Minutes
End If

If Hum < Hummin Then
   Hummin = Hum
   Hummintime = Hours + ":" + Minutes
End If


If Temp > 99 Then
   Temp = 99
End If

If Temp > Tempmax Then
   Tempmax = Temp
   Tempmaxtime = Hours + ":" + Minutes
End If

If Temp < Tempmin Then
   Tempmin = Temp
   Tempmintime = Hours + ":" + Minutes
End If

' Reset Min/Max's at midnight
If Time= "00:00:00" Then
   Hummax = Hum
   Hummaxtime = Hours + ":" + Minutes
   Hummin = Hum
   Hummintime = Hours + ":" + Minutes
   Tempmax = Temp
   Tempmaxtime = Hours + ":" + Minutes
   Tempmin = Temp
   Tempmintime = Hours + ":" + Minutes
End If

'Stuff to do on the hour
If Onhour = 1 Then
   If Minutes = "01" Then                                   'Shift temp and hum logs left by one
      Humlog(1) = Hum
      Templog(1) = Temp

      For J = 25 To 2 Step - 1
         X = J - 1
         Humlog(j) = Humlog(x)
         Templog(j) = Templog(x)
      Next J
      If State = 11 Then Showpic 0 , 0 , Temptrend
      If State = 12 Then Showpic 0 , 0 , Humtrend
      Onhour = 0

   End If
End If

'Display time-------------------------------------------------------------------------------------------------------------------
If State = 0 Then

   Gosub Checkkeys

   If Key = 1 Then
      Cls
      Menupos = 4
      Menurow = 1                                           ' Setup
      Goto Setup
   End If

   If Key = 2 Then
      Cls
      State = 9                                             'temp
      Goto Main
   End If

   If Key = 3 Then
      Cls
      State = 10                                            'humidity
      Goto Main
   End If

   If Key = 4 Then
      Toggle Alarmoveride                                   'Turn Alarm On Or Off
   End If

   If Key = 5 Then                                          ' snooze pressed
      If Alarm = 1 Then
         Set Snooze
         Reset Alarm
         Snoozemincount = 0
         Snoozesec = _sec
      Else
         Reset Snooze
         Reset Alarm

      End If
   End If

   ' Check Battery Voltage and display Bat Low if low
   If _bvolts < 2 Then
      Lcdat 7 , 1 , ";$"
   Else
      Lcdat 7 , 1 , "  "
   End If

   J = Dayofweek(date$)
   Day = Lookupstr(, Weekdays)
   Lcdat 7 , 16 , Day ; " " ; Date$
   Showpic 0 , 56 , Mainbar

'check alarm------------------------------------------------------------------------------------------------------------

   ' check to see if it's an alarm day and if it is, set alarmset on
   If Alarmoveride = 0 Then
      J = J + 1
      If Alarmday(j) = 1 Then
         Set Alarmset
      Else
         Reset Alarmset
      End If
   Else
      Alarmset = 0
   End If

   ' If alarmset is set and snooze if off and time = alarmtime turn alarm on
   If _sec = 1 Then
      If Alarmset = 1 Then
         If Snooze = 0 Then
            Disp = Hours + ":" + Minutes
            If Disp = Alarmtime Then
               Set Alarm
            End If
         End If
      End If
   End If

   If Alarmset = 1 Then                                     ' display alarm icon
      Disp = "*"
   Else
      Disp = "-"
   End If

   If Alarmoveride = 1 Then                                 ' Display Alarm Cancelled Icon
      Disp = "X"
   End If

   If Snooze = 1 Then
      Showpic 112 , 32 , Snooze
      If Snoozemincount > 9 Then                            ' 10 Minute Snooze
         Set Alarm
         Reset Snooze
      End If
   Else
      Showpic 112 , 32 , 16x16blank
   End If

   'Lcdat 7 , 1 , Snoozemincount ; " " ; Snooze ; " " ; Count ; "  "

   'Sound Alarm if Alarm is on
   If Alarm = 1 Then
      If Alarmbeepcount < 5 Then
         Toggle Porta.7
         Sound Porta., 50 , 350
         Toggle Porta.7
         Waitms 100
      End If
      Incr Alarmbeepcount
      If Alarmbeepcount = 50 Then Alarmbeepcount = 1
   End If

' Update Display--------------------------------------------------------------------------------------------------------
   Gosub 12hour                                             'make 12 hour if 12 hour mode selected

   If J < 10 Then
     Hours = "-" + Str(j)
   End If


   If Odd = 0 Then
      Disp = Disp + Hours + ":" + Minutes
   Else
      Disp = Disp + Hours + "-" + Minutes
   End If

   Lcdat 4 , 104 , ":" ; Seconds

End If

' Display Temp----------------------------------------------------------------------------------------------------------
If State = 9 Then
   Disp = Str(temp) + "C"
   If Temp < 10 Then Disp = "-" + Disp
   Showpic 0 , 56 , Temphum
   Line(60 , 0) -(60 , 56) , 255
   Line(60 , 28) -(128 , 28) , 255
   Lcdat 1 , 66 , "MAX:"
   Lcdat 2 , 66 , Tempmax ; "#C  "
   Lcdat 3 , 66 , "AT " ; Tempmaxtime
   Lcdat 5 , 66 , "MIN:"
   Lcdat 6 , 66 , Tempmin ; "#C  "
   Lcdat 7 , 66 , "AT " ; Tempmintime
   Gosub Checkkeys

   If Key = 1 Then                                          'reset max
       Tempmax = Temp
       Tempmaxtime = Hours + ":" + Minutes
   End If

   If Key = 2 Then
      Tempmin = Temp                                        'reset min
      Tempmintime = Hours + ":" + Minutes
   End If

   If Key = 3 Then
      Cls
      Showpic 0 , 0 , Temptrend
      State = 11
      Goto Main
   End If

   If Key = 4 Then
      Cls
      State = 0
      Goto Main
   End If

   Gosub Timeout

End If

'Display Humidity-------------------------------------------------------------------------------------------------------
If State = 10 Then

   Disp = Str(hum) + "%"
   If Hum < 10 Then Disp = "-" + Disp

   Showpic 0 , 56 , Temphum
   Line(60 , 0) -(60 , 56) , 255
   Line(60 , 28) -(128 , 28) , 255
   Lcdat 1 , 66 , "MAX:"
   Lcdat 2 , 66 , Hummax ; "%  "
   Lcdat 3 , 66 , "AT " ; Hummaxtime
   Lcdat 5 , 66 , "MIN:"
   Lcdat 6 , 66 , Hummin ; "%  "
   Lcdat 7 , 66 , "AT " ; Hummintime
   Gosub Checkkeys

   If Key = 1 Then
       Hummax = Hum                                         'reset max
       Hummaxtime = Hours + ":" + Minutes
   End If

   If Key = 2 Then
      Hummin = Hum                                          'reset min
      Hummintime = Hours + ":" + Minutes
   End If

   If Key = 3 Then
      Cls
      Showpic 0 , 0 , Humtrend
      State = 12
      Goto Main
   End If


   If Key = 4 Then
     Cls
     State = 0
     Goto Main
   End If

   Gosub Timeout

End If

'Display temp trend-----------------------------------------------------------------------------------------------------
If State = 11 Then
   Gosub Checkkeys
   X = 3
   X1 = 8
   Y1 = 47
   For J = 25 To 1 Step -1
      X = X + 5
      If X > 127 Then X = 127
      _single = Templog(j)
      _single = _single * 1.2
      _single = 48 - _single
      Y = _single
      If Y < 0 Then Y = 0
      If Y > 48 Then Y = 48
      Line(x1 , Y1) -(, Y) , 255
      X1 = X
      Y1 = Y
   Next J

   If Key = 4 Then
     Cls
     State = 0
     Goto Main
   End If

   Gosub Timeout

   Goto Main
End If

'Display Humdity trend-----------------------------------------------------------------------------------------------------
If State = 12 Then
   Gosub Checkkeys
   X = 3
   X1 = 8
   Y1 = 47
   For J = 25 To 1 Step -1
      X = X + 5
      If X > 127 Then X = 127
      _single = Humlog(j)
      _single = _single / 2
      _single = 48 - _single
      Y = _single
      If Y < 0 Then Y = 0
      If Y > 48 Then Y = 48
      Line(x1 , Y1) -(, Y) , 255
      X1 = X
      Y1 = Y
   Next J

   If Key = 4 Then
     Cls
     State = 0
     Goto Main
   End If

   Gosub Timeout

   Goto Main
End If

' Set Time--------------------------------------------------------------------------------------------------------------
If State = 2 Then
   Lcdat 6 , 96 , ":"
   Lcdat 7 , 1 , "    SET TIME"
      Disp = Time$
      Disp = "-"

   Gosub 12hour                                             'make 12hour

   If J < 10 Then
      Hours = "-" + Str(j)
   End If


   If Odd = 0 Then
      If Menupos = 1 Then
         Disp = Disp + "--" + ":" + Minutes
         Lcdat 6 , 104 , Seconds
      End If

      If Menupos = 2 Then
         Disp = Disp + Hours + ":" + "--"
         Lcdat 6 , 104 , Seconds
      End If

      If Menupos = 3 Then
         Disp = Disp + Hours + ":" + Minutes
         Lcdat 6 , 104 , Seconds , 1
      End If

      Else
         Disp = Disp + Hours + ":" + Minutes
         Lcdat 6 , 104 , Seconds
   End If

   Gosub Checkkeys
   If Key <> 0 Then Toggle Odd
   Showpic 0 , 56 , Updownright

   'adjust Hour
   If Menupos = 1 Then
      If Key = 1 Then Incr _hour
      If _hour = 24 Then _hour = 0
      If Key = 2 Then Decr _hour
      If _hour = 255 Then _hour = 23
   End If

   ' Adjust Minutes
   If Menupos = 2 Then
      If Key = 1 Then Incr _min
      If _min = 60 Then _min = 0
      If Key = 2 Then Decr _min
      If _min = 255 Then _min = 59
   End If

   'Adjust Seconds
   If Menupos = 3 Then
      If Key = 1 Then Incr _sec
      If _sec = 60 Then _sec = 0
      If Key = 2 Then Decr _sec
      If _sec = 255 Then _sec = 59
   End If

   If Key = 3 Then Incr Menupos
   If Menupos = 4 Then Menupos = 1

   If Key = 4 Then
      Cls
      State = 0
      Menupos = 1
      Goto Main
   End If

   Gosub Timeout

End If

' Set Date--------------------------------------------------------------------------------------------------------------
If State = 3 Then
   Lcdat 7 , 1 , "    SET DATE"
   Disp = Time$
   Disp = ""
   Hours = Left(date, 2)
   Minutes = Mid(date, 4 , 2)
   Seconds = Right(date, 2)
   Gosub Checkkeys
   If Key <> 0 Then Toggle Odd
   Showpic 0 , 56 , Updownright

   If Odd = 0 Then
      If Menupos = 1 Then
         Disp = Disp + "--" + "/" + Minutes + "/" + Seconds

      End If
      If Menupos = 2 Then
         Disp = Disp + Hours + "/" + "--" + "/" + Seconds

      End If

      If Menupos = 3 Then
         Disp = Disp + Hours + "/" + Minutes + "/" + "--"

      End If
   Else
      Disp = Disp + Hours + "/" + Minutes + "/" + Seconds

   End If


   'adjust Days
   If Menupos = 1 Then
      If Key = 1 Then Incr _day
      If _day = 32 Then _day = 1
      If Key = 2 Then Decr _day
      If _day = 0 Then _day = 31
   End If

' Adjust Month
   If Menupos = 2 Then
      If Key = 1 Then Incr _month
      If _month = 13 Then _month = 1
      If Key = 2 Then Decr _month
      If _month = 0 Then _month = 12
   End If

'Adjust year
   If Menupos = 3 Then
      If Key = 1 Then Incr _year
      If _year = 99 Then _year = 0
      If Key = 2 Then Decr _year
      If _year = 255 Then _year = 0
   End If

   If Key = 3 Then Incr Menupos
   If Menupos = 4 Then Menupos = 1

   If Key = 4 Then
      Cls
      State = 0
      Menupos = 1
      Goto Main
   End If

   Gosub Timeout

End If

' Set Alarm-------------------------------------------------------------------------------------------------------------
If State = 1 Then
   Hours = Str(_alarmhour)
   Hours = Format(hours , "00")
   Minutes = Str(_alarmmin)
   Minutes = Format(minutes , "00")
   Alarmtime = Hours + ":" + Minutes


   Lcdat 7 , 1 , "   SET ALARM"
      Disp = Time$
      Disp = "-"

   Gosub 12hour                                             ' make 12 hour

   If J < 10 Then
      Hours = "-" + Str(j)
   End If


   If Odd = 0 Then
      If Menupos = 1 Then
         Disp = Disp + "--" + ":" + Minutes
      End If
      If Menupos = 2 Then
         Disp = Disp + Hours + ":" + "--"
      End If
   Else
      Disp = Disp + Hours + ":" + Minutes
   End If

   Gosub Checkkeys
   If Key <> 0 Then Toggle Odd
   Showpic 0 , 56 , Updownright

   'adjust Hour
   If Menupos = 1 Then
      If Key = 1 Then Incr _alarmhour
      If _alarmhour = 24 Then _alarmhour = 0
      If Key = 2 Then Decr _alarmhour
      If _alarmhour = 255 Then _alarmhour = 23
   End If

   ' Adjust Minutes
   If Menupos = 2 Then
      If Key = 1 Then Incr _alarmmin
      If _alarmmin = 60 Then _alarmmin = 0
      If Key = 2 Then Decr _alarmmin
      If _alarmmin = 255 Then _alarmmin = 59
   End If


   If Key = 3 Then Incr Menupos
   If Menupos = 3 Then Menupos = 1

   If Key = 4 Then
      Cls
      State = 0
      Menupos = 1
      Goto Main
   End If

   Gosub Timeout

End If

' Set alarm days--------------------------------------------------------------------------------------------------------

If State = 5 Then
   J = 1
   Odd = 0
   Lcdat 1 , 1 , "SET ALARM DAYS:"
   Showpic 0 , 56 , Updownset

   Alarmdays:                                               ' so it loops

   Gosub Checkkeys

   If Odd = 0 Then
      For K = 1 To 7
         Y = K - 1
         Day = Lookupstr(, Weekdays)
         Y = K
         If Alarmday(y) = 1 Then
            If K < 5 Then
               X = K + 2
               Lcdat X , 24 , "*" ; Day                     'tick on
            Else
               X = K - 2
               Lcdat X , 72 , "*" ; Day                     'tick on
            End If

         Else
            If K < 5 Then
               X = K + 2
               Lcdat X , 24 , "&" ; Day                     'tick off
            Else
               X = K - 2
               Lcdat X , 72 , "&" ; Day                     'tick off
            End If

         End If
      Next K
   Else

      Y = J - 1
      Day = Lookupstr(, Weekdays)
      If J < 5 Then
         X = J + 2
         Lcdat X , 32 , Day , 1
      Else
         X = J - 2
         Lcdat X , 80 , Day , 1
      End If
   End If

   If Key = 1 Then                                          ' scroll up
      Decr J
      If J = 0 Then J = 7
      Odd = 0
   End If

   If Key = 2 Then                                          'scroll down
      Incr J
      If J = 8 Then J = 1
      Odd = 0
   End If

   If Key = 4 Then                                          'exit
      Cls
      State = 0
      Menupos = 1
      Goto Main
   End If

   If Key = 3 Then                                          'select

      If Alarmday(j) = 1 Then
         Alarmday(j) = 0
      Else
         Alarmday(j) = 1
      End If
      Odd = 0
   End If

   Gosub Timeout

   Goto Alarmdays
End If


'Set 24 Hour Mode-------------------------------------------------------------------------------------------------------
If State = 4 Then
   Toggle 24h
   If 24h = 0 Then
      Lcdat 4 , 1 , "  12 HOUR MODE"
   Else
      Lcdat 4 , 1 , "  24 HOUR MODE"
   End If

   Wait 2
   State = 0
   Cls
   Goto Main
End If


' Turn Beep On or Off---------------------------------------------------------------------------------------------------
If State = 6 Then
   Toggle Keybeep
   If Keybeep = 1 Then
      Lcdat 4 , 1 , "   KEY BEEP ON"
      Sound Porta., 20 , 250
   Else
      Lcdat 4 , 1 , "  KEY BEEP OFF"
   End If

   Wait 2
   State = 0
   Cls
   Goto Main
End If

' Fiddle Menu----------------------------------------------------------------------------------------------
If State = 7 Then
   Lcdat 1 , 1 , "FIDDLE MENU:"
   Lcdat 2 , 1 , "BATT:  " ; Bvolts
   Lcdat 3 , 1 , "MAINS: " ; Volts
   Lcdat 4 , 1 , "LIGHT  " ; Light
   Wait 5
   State = 0
   Cls
   Goto Main
End If

'Write Display----------------------------------------------------------------------------------------------------------

Stringcount = 1
Do
= Mid(disp , Stringcount , 1)
= Stringcount - 1
Carpos = J * 16
Select Case Asc(n)
   Case 37 : Showpic Carpos , 0 , Percent
   Case 42 : Showpic Carpos , 16 , Alarmon
   Case 45 : Showpic Carpos , 0 , Blank
   Case 47 : Showpic Carpos , 0 , Slash
   Case 48 : Showpic Carpos , 0 , Zero
   Case 49 : Showpic Carpos , 0 , One
   Case 50 : Showpic Carpos , 0 , Two
   Case 51 : Showpic Carpos , 0 , Three
   Case 52 : Showpic Carpos , 0 , Four
   Case 53 : Showpic Carpos , 0 , Five
   Case 54 : Showpic Carpos , 0 , Six
   Case 55 : Showpic Carpos , 0 , Seven
   Case 56 : Showpic Carpos , 0 , Eight
   Case 57 : Showpic Carpos , 0 , Nine
   Case 58 : Showpic Carpos , 0 , Dots
   Case 67 : Showpic Carpos , 0 , Degrees
   Case 88 : Showpic Carpos , 16 , Alarmoveride
End Select

Incr Stringcount
= Len(disp) + 1
Loop Until Stringcount = J

Goto Main

Stop                                                        ' end of program

'SUBROUTINES------------------------------------------------------------------------------------------------------------

' Setup Menu------------------------------------------------------------------------------------------------------------
Setup:
Showpic 0 , 0 , Setupmenu

= 0
= 0
Menupos = 4
Menurow = 1
Setuploop:
   Gosub Checkkeys

   If Key = 1 Then
      Decr Menupos
      If Menupos = 0 Then Menupos = 4
      Showpic 0 , 0 , Setupmenu
   End If

   If Key = 2 Then
      If Menurow = 1 Then
         Menurow = 0
      Else
         If Menurow = 0 Then Menurow = 1
      End If
      Showpic 0 , 0 , Setupmenu
   End If

   If Key = 3 Then
      Incr Menupos
      If Menupos = 5 Then Menupos = 1
      Showpic 0 , 0 , Setupmenu
   End If

   If Key = 4 Then
         If Menurow = 1 Then
            State = Menupos + 4
            Menupos = 1
            If State = 8 Then State = 0
            Cls
            Goto Main
         Else
            State = Menupos
            Menupos = 1
            Cls
            Goto Main
         End If
   End If

   If Odd = 1 Then
      X = Menupos
      If Menurow = 1 Then
         X = Menupos + 4
      End If

      Select Case X
         Case 1 : Showpic 0 , 0 , Setupmenu1
         Case 2 : Showpic 0 , 0 , Setupmenu2
         Case 3 : Showpic 0 , 0 , Setupmenu3
         Case 4 : Showpic 0 , 0 , Setupmenu4
         Case 5 : Showpic 0 , 0 , Setupmenu5
         Case 6 : Showpic 0 , 0 , Setupmenu6
         Case 7 : Showpic 0 , 0 , Setupmenu7
         Case 8 : Showpic 0 , 0 , Setupmenu8
      End Select

'      X = Menupos - 1
'      X = X * 32
'      X1 = X + 31
'      If Menurow = 0 Then Y = 0
'      If Menurow = 1 Then Y = 28
'      Y1 = Y + 27
'      Line(x , Y) -(x1 , Y) , 255
'      Line(x1 , Y) -(x1 , Y1) , 255
'      Line(x1 , Y1) -(x , Y1) , 255
'      Line(x , Y1) -(x , Y) , 255
   Else
      Showpic 0 , 0 , Setupmenu
   End If

   Gosub Timeout

Goto Setuploop

'CheckKeys--------------------------------------------------------------------------------------------------------------
Checkkeys:
   Key = 0
   Debounce Pinc., 1 , Key1 , Sub
   Debounce Pinc., 1 , Key2 , Sub
   Debounce Pinc., 1 , Key3 , Sub
   Debounce Pinc., 1 , Key4 , Sub
   Debounce Pinc., 1 , Key5 , Sub
   If Key <> 0 Then
      Count = 0
      If Keybeep = 1 Then
         Sound Porta., 20 , 250
      End If
   End If
Return

Key1:
Key = 1
Return
Key2:
Key = 2
Return
Key3:
Key = 3
Return
Key4:
Key = 4
Return
Key5:
Key = 5
Return

' to do on the second---------------------------------------------------------------------------------------------------
Sectic:

Toggle Odd                                                  ' toggle the dots/odd even seconds
Incr Count                                                  ' Incr count for timeout

If Snooze = 1 Then
   If _sec = Snoozesec Then
      Incr Snoozemincount
   End If
End If

Onsecond = 1

Return

' Return to state 0 if longer than 30 seconds non use-------------------------------------------------------------------
Timeout:
   If Count = 30 Then
      Cls
      State = 0
      Goto Main
   End If
Return


' Make 12 Hour if 12 hour selected -------------------------------------------------------------------------------------
12hour:
   J = Val(hours)
   If 24h = 0 Then

      ' Display AM or PM

      If J <= 11 Then
        Showpic 112 , 0 , Am
      Else
         Showpic 112 , 0 , Pm
      End If

      If J => 13 Then
        J = J - 12
         Hours = Str(j)
      End If

      If J = 0 Then
         J = 12
         Hours = "12"
      End If

   End If
Return

'include the picture data-----------------------------------------------------------------------------------------------
Weekdays:
Data "MON" , "TUE" , "WED" , "THU" , "FRI" , "SAT" , "SUN"

$include "alarm.font"

Zero:
$bgf "0.bgf"

One:
$bgf "1.bgf"

Two:
$bgf "2.bgf"

Three:
$bgf "3.bgf"

Four:
$bgf "4.bgf"

Five:
$bgf "5.bgf"

Six:
$bgf "6.bgf"

Seven:
$bgf "7.bgf"

Eight:
$bgf "8.bgf"

Nine:
$bgf "9.bgf"

Alarmon:
$bgf "alarmon.bgf"

Alarmoveride:
$bgf "alarmoveride.bgf"

Dots:
$bgf "dots.bgf"

Slash:
$bgf "slash.bgf"

Blank:
$bgf "blank.bgf"

Am:
$bgf "am.bgf"

Pm:
$bgf "pm.bgf"

Degrees:
$bgf "degrees.bgf"

Percent:
$bgf "percent.bgf"

Updownright:
$bgf "updownright.bgf"

Updownset:
$bgf "updownset.bgf"

Setupmenu:
$bgf "Setupmenu.bgf"

Mainbar:
$bgf "Mainbar.bgf"

Temphum:
$bgf "TempHum.bgf"

Snooze:
$bgf "snooze.bgf"

16x16blank:
$bgf "16x16Blank.bgf"

Temptrend:
$bgf "temptrend.bgf"

Humtrend:

$bgf "humtrend.bgf"

Setupmenu1:
$bgf "Setupmenu1.bgf"

Setupmenu2:
$bgf "Setupmenu2.bgf"

Setupmenu3:
$bgf "Setupmenu3.bgf"

Setupmenu4:
$bgf "Setupmenu4.bgf"

Setupmenu5:
$bgf "Setupmenu5.bgf"

Setupmenu6:
$bgf "Setupmenu6.bgf"

Setupmenu7:
$bgf "Setupmenu7.bgf"

Setupmenu8:
$bgf "Setupmenu8.bgf"

'THE END----------------------------------------------------------------------------------------------------------------