Attribute VB_Name = "Graph"
'=====================================================================
'     Name: graph.bas
'  Purpose: declaring and implementing Functions and variables for the stripchart

'        Date     Initials    Description
'      12/21/03     YR      Original code written
'
'=====================================================================

Public xmin As Double
Public ymin As Double
Public xmax As Double
Public ymax As Double
Public ytext As String
Public yunits As String
Public graph_title As String
Public current_set As Integer
Public global_all As Boolean
Public chan_value(8) As Long


Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Declare Function CreateFontIndirect Lib "gdi32" Alias _
     "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
    Declare Function SelectObject Lib "gdi32" (ByVal hdc _
     As Long, ByVal hObject As Long) As Long
    Declare Function DeleteObject Lib "gdi32" (ByVal _
     hObject As Long) As Long
    Const LF_FACESIZE = 32

    Type LOGFONT
     lfHeight As Long
     lfWidth As Long
     lfEscapement As Long
     lfOrientation As Long
     lfWeight As Long
     lfItalic As Byte
     lfUnderline As Byte
     lfStrikeOut As Byte
     lfCharSet As Byte
     lfOutPrecision As Byte
     lfClipPrecision As Byte
     lfQuality As Byte
     lfPitchAndFamily As Byte
     lfFaceName As String * LF_FACESIZE
   End Type
   
   
Type StripChart
    Title As String
    min As Double
    max As Double
    yunits As String
    ylabel As String
    color As Long
    abbrev As String
    display As Boolean
End Type

Public Const total_channels = 12

Public channel(total_channels) As StripChart

'=====================================================
'Purpose: Get the value for a particular channel
'=====================================================
Public Function getvalue(I As Integer)
Call envReadTemp(TempNow, TempHi, TempLo, TempCfg, TempChannel)
    Select Case I
    Case 0
        value = type_value(I, anaType(I))
        getvalue = value
    Case 1
        value = type_value(I, anaType(I))
        getvalue = value
    Case 2
        value = type_value(I, anaType(I))
        getvalue = value
    Case 3
        value = type_value(I, anaType(I))
        getvalue = value
    Case 4
        value = type_value(I, anaType(I))
        getvalue = value
    Case 5
        value = type_value(I, anaType(I))
        getvalue = value
    Case 6
        value = type_value(I, anaType(I))
        getvalue = value
    Case 7
        value = type_value(I, anaType(I))
        getvalue = value
    Case 8
        'value = type_value(3, anaType(3))
        'getvalue = value
        
        'Humidity
        getvalue = Hum_current
    Case 9
        
        If TempNow >= 256 Then TempNow = -1 * (512 - TempNow)
            getvalue = (TempNow / 2)
    Case 10
        
            If TempHi >= 256 Then TempHi = -1 * (512 - TempHi)
            getvalue = (TempHi / 2)
    Case 11
        
            If TempLo >= 256 Then TempLo = -1 * (512 - TempLo)
            getvalue = (TempLo / 2)
    
    
    End Select
End Function

Public Function type_value(chan As Integer, chan_type As Long)
    Select Case chan_type
    Case 0
        value = envReadAna(chan + &H80 + &H0)
    Case 1
        value = envReadAna(chan + &H80 + &H20)
    Case 2
        value = envReadAna(chan + &H80 + &H40)
    Case 3
        value = envReadAna(chan + &H80 + &H60)
    Case Else
        value = envReadAna(chan + &H80 + &H0)
    End Select
    type_value = value
End Function
' Initialise the channels from the previously saved settings.
Public Sub Initialize_Channels()

            If isfile(App.Path & "\Settings.txt") Then
                
                f = FreeFile
                Open App.Path & "\Settings.txt" For Input As f
                For I = 0 To total_channels - 1
                    If EOF(f) Then
                        default_settings (I)
            
                    Else
                    
                        Input #f, T
                        channel(I).abbrev = T
                        Input #f, T
                        channel(I).color = T
                        Input #f, T
                        channel(I).max = T
                        Input #f, T
                        channel(I).min = T
                        Input #f, T
                        channel(I).Title = T
                        Input #f, T
                        channel(I).ylabel = T
                        Input #f, T
                        channel(I).yunits = T
            
                    End If
                Next
              Close (f)
        Else
            For I = 0 To total_channels - 1
                default_settings (I)
            Next
        End If
        

End Sub
' Default settings
Sub default_settings(I As Integer)
    Select Case I
    Case 0
            channel(I).abbrev = "Ana1"
            channel(I).color = vbBlack
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "Analog Sensor 1"
            channel(I).ylabel = "Volts*5/256"
            channel(I).yunits = "V"
    Case 1
            channel(I).abbrev = "Ana2"
            channel(I).color = vbBlue
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "Analog Sensor 2"
            channel(I).ylabel = "Volts*5/256"
            channel(I).yunits = "V"
    Case 2
            channel(I).abbrev = "Ana3"
            channel(I).color = vbYellow
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "Analog Sensor 3"
            channel(I).ylabel = "Volts*5/256"
            channel(I).yunits = "V"

    Case 3
            channel(I).abbrev = "Press"
            channel(I).color = vbGreen
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "Analog Sensor 4"
            channel(I).ylabel = "Volts*5/256"
            channel(I).yunits = "V"

    Case 4
            channel(I).abbrev = "Ana5"
            channel(I).color = vbRed
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "Analog Sensor 5"
            channel(I).ylabel = "Volts*5/256"
            channel(I).yunits = "V"

    Case 5
            channel(I).abbrev = "Ana6"
            channel(I).color = &H80&
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "Analog Sensor 6"
            channel(I).ylabel = "Volts*5/256"
            channel(I).yunits = "V"

    Case 6
            channel(I).abbrev = "Ana7"
            channel(I).color = 4227072
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "Analog Sensor 7"
            channel(I).ylabel = "Volts*5/256"
            channel(I).yunits = "V"

    Case 7
            channel(I).abbrev = "Ana8"
            channel(I).color = 8388608
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "Analog Sensor 8"
            channel(I).ylabel = "Volts*5/256"
            channel(I).yunits = "V"

    Case 8
            channel(I).abbrev = "Hum"
            channel(I).color = 80
            channel(I).max = 20
            channel(I).min = 0
            channel(I).Title = "Humidity Sensor"
            channel(I).ylabel = "Hum"
            channel(I).yunits = "%"
    Case 9
            channel(I).abbrev = "CTmp"
            channel(I).color = 8388608
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "Cur Temperature"
            channel(I).ylabel = "Temp"
            channel(I).yunits = "C/F"
    Case 10
            channel(I).abbrev = "HTmp"
            channel(I).color = 8388608
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "High Temperature"
            channel(I).ylabel = "Temp"
            channel(I).yunits = "C/F"
    Case 11
            channel(I).abbrev = "LTmp"
            channel(I).color = 8388608
            channel(I).max = 10
            channel(I).min = 0
            channel(I).Title = "Low Temperature"
            channel(I).ylabel = "Temp"
            channel(I).yunits = "C/F"
    
    End Select

End Sub

Function isfile(filename As String)

    If Dir$(filename) <> "" Then
        isfile = True
    Else
        isfile = False
    End If
End Function
