Option Explicit

Private Sub Form_Load()
    ReadIni
    update_com_port
    update_sample_rate
    If check_start_minimized.Value Then
        Me.WindowState = vbMinimized
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    close_iic_bus
    SaveIni
End Sub


Private Sub option_1_min_Click()
    update_sample_rate
End Sub

Private Sub option_1_min_KeyPress(KeyAscii As Integer)
    update_sample_rate
End Sub

Private Sub option_1_sec_Click()
    update_sample_rate
End Sub

Private Sub option_1_sec_KeyPress(KeyAscii As Integer)
    update_sample_rate
End Sub


Private Sub option_30_sec_Click()
    update_sample_rate
End Sub

Private Sub option_30_sec_KeyPress(KeyAscii As Integer)
    update_sample_rate
End Sub


Private Sub option_6_sec_Click()
    update_sample_rate
End Sub

Private Sub option_6_sec_KeyPress(KeyAscii As Integer)
    update_sample_rate
End Sub


Private Sub option_com1_Click()
    update_com_port
End Sub

Private Sub option_com1_KeyPress(KeyAscii As Integer)
    update_com_port
End Sub

Private Sub option_com2_Click()
    update_com_port
End Sub

Private Sub option_com2_KeyPress(KeyAscii As Integer)
    update_com_port
End Sub

Private Sub option_com3_Click()
    update_com_port
End Sub

Private Sub option_com3_KeyPress(KeyAscii As Integer)
    update_com_port
End Sub

Private Sub option_com4_Click()
    update_com_port
End Sub

Private Sub option_com4_KeyPress(KeyAscii As Integer)
    update_com_port
End Sub

Private Sub option_com5_Click()
    update_com_port
End Sub

Private Sub option_com5_KeyPress(KeyAscii As Integer)
    update_com_port
End Sub
Private Sub option_com6_Click()
    update_com_port
End Sub

Private Sub option_com6_KeyPress(KeyAscii As Integer)
    update_com_port
End Sub
Private Sub option_com7_Click()
    update_com_port
End Sub

Private Sub option_com7_KeyPress(KeyAscii As Integer)
    update_com_port
End Sub

Private Sub option_com8_Click()
    update_com_port
End Sub

Private Sub option_com8_KeyPress(KeyAscii As Integer)
    update_com_port
End Sub



Private Sub Timer1_Timer()
    Dim t As Double
    Dim q As Double
    Dim unit As String
    adc_kiolvas
    If check_farenheit Then
        unit = "°F"
    Else
        unit = "°C"
    End If
    
    t = temperature(&H48)
    If t = ERROR_TEMPERATURE_NOT_READ Then
        MsgBox "Unable to read padlószint temperature", vbOKOnly, "Error"
        Timer1.Enabled = False
        label_temperature_padlószint.Caption = "--.---" + unit
    Else
        If check_farenheit Then
            t = farenheit(t)
        End If
        label_temperature_padlószint.Caption = Format(t, "#00.000")
   End If
   
   If option_double Then
        t = temperature(&H49)
        If t = ERROR_TEMPERATURE_NOT_READ Then
            MsgBox "Unable to read külsődéli temperature", vbOKOnly, "Error"
            Timer1.Enabled = False
            label_temperature_külsődéli.Caption = "--.---" + unit
        Else
            If check_farenheit Then
                t = farenheit(t)
            End If
            label_temperature_külsődéli.Caption = Format(t, "#00.000")
        End If
   Else
        label_temperature_külsődéli.Caption = "--.---" + unit
   End If
If option_double Then
        t = temperature(&H4A)
        If t = ERROR_TEMPERATURE_NOT_READ Then
            MsgBox "Unable to read radiator temperature", vbOKOnly, "Error"
            Timer1.Enabled = False
            label_temperature_radiator.Caption = "--.---" + unit
        Else
            If check_farenheit Then
                t = farenheit(t)
            End If
            label_temperature_radiator.Caption = Format(t, "#00.000")
        End If
   Else
        label_temperature_radiator.Caption = "--.---" + unit
   End If
   If option_double Then
        t = temperature(&H4B)
        If t = ERROR_TEMPERATURE_NOT_READ Then
            MsgBox "Unable to read fancoilbe temperature", vbOKOnly, "Error"
            Timer1.Enabled = False
            Label_temperature_fancoilbe.Caption = "--.---" + unit
        Else
            If check_farenheit Then
                t = farenheit(t)
            End If
            Label_temperature_fancoilbe.Caption = Format(t, "#00.000")
        End If
   Else
        Label_temperature_fancoilbe.Caption = "--.---" + unit
   End If
   If option_double Then
        t = temperature(&H4C)
        If t = ERROR_TEMPERATURE_NOT_READ Then
            MsgBox "Unable to read fancoilel temperature", vbOKOnly, "Error"
            Timer1.Enabled = False
            Label_temperature_fancoilel.Caption = "--.---" + unit
        Else
            If check_farenheit Then
                t = farenheit(t)
            End If
            Label_temperature_fancoilel.Caption = Format(t, "#00.000")
        End If
   Else
        Label_temperature_fancoilel.Caption = "--.---" + unit
   End If
   If option_double Then
        t = temperature(&H4D)
        If t = ERROR_TEMPERATURE_NOT_READ Then
            MsgBox "Unable to read opoph424be temperature", vbOKOnly, "Error"
            Timer1.Enabled = False
            Label_temperature_opoph424be.Caption = "--.---" + unit
        Else
            If check_farenheit Then
                t = farenheit(t)
            End If
            Label_temperature_opoph424be.Caption = Format(t, "#00.000")
        End If
   Else
        Label_temperature_opoph424be.Caption = "--.---" + unit
   End If
   If option_double Then
        t = temperature(&H4E)
        If t = ERROR_TEMPERATURE_NOT_READ Then
            MsgBox "Unable to read opoph424el temperature", vbOKOnly, "Error"
            Timer1.Enabled = False
            Label_temperature_opoph424el.Caption = "--.---" + unit
        Else
            If check_farenheit Then
                t = farenheit(t)
            End If
            Label_temperature_opoph424el.Caption = Format(t, "#00.000")
        End If
   Else
        Label_temperature_opoph424el.Caption = "--.---" + unit
   End If
   If option_double Then
        t = temperature(&H4F)
        If t = ERROR_TEMPERATURE_NOT_READ Then
            MsgBox "Unable to read boiler temperature", vbOKOnly, "Error"
            Timer1.Enabled = False
            Label_temperature_boiler.Caption = "--.---" + unit
        Else
            If check_farenheit Then
                t = farenheit(t)
            End If
            Label_temperature_boiler.Caption = Format(t, "#00.000")
        End If
   Else
        Label_temperature_boiler.Caption = "--.---" + unit
   End If
   If option_double Then
        't = temperature(&H48)
        If t = ERROR_TEMPERATURE_NOT_READ Then
            MsgBox "Unable to read ADC0 tomegaram", vbOKOnly, "Error"
            Timer1.Enabled = False
            adc_value.Caption = "--.---" + unit
        Else
            If check_farenheit Then
                q = farenheit(t)
            End If
            'Label_temperature_ADC.Caption = Format(t, "#00.000") * 6
        End If
   Else
        'Label_temperature_ADC.Caption = "--.---" + unit
   End If
   If option_double Then
        frm_thermometer.Caption = label_temperature_padlószint.Caption + "    ( out " + label_temperature_külsődéli.Caption + " )"
   Else
        frm_thermometer.Caption = label_temperature_padlószint.Caption
   End If

   log_to_file

End Sub

Private Function temperature(ByVal address As Integer) As Double

    Dim temperature_int As Long
    Dim temperature_frac As Long
    
    'For I2C bus communication, addresses are shifted one place to left,
    'as the least significant bit is used for the R/W flag.
    'In binary, shifting to left is equivalent to  multiplying by two
    address = address * 2
    
    On Error GoTo errors
    open_iic_bus MSComm1.object
    
    'an extra stop doesn't hurt...and ensures we start from a clean bus condition
    IIC_stop
    
    'read sequence, as per DS1621 datasheet
    IIC_start                           'Bus Master initiates a START condition.
    IIC_tx_byte address                 'Bus Master sends DS1621 address; R/ W= 0 (DS1621 generates acknowledge bit).
    IIC_tx_byte &HAC                    'Bus Master sends Access Config command protocol.DS1621 generates acknowledge bit.
    IIC_tx_byte &H1                     'Bus Master sets up DS1621 for output polarity active low, one-shot conversion.
                                        'DS1621 generates acknowledge bit.
   
    IIC_start                           'Bus Master generates a repeated START condition.
    IIC_tx_byte address                 'Bus Master sends DS1621 address; R/ W= 0.DS1621 generates acknowledge bit.
    IIC_tx_byte &HEE                    'Bus Master sends Start Convert T command protocol.DS1621 generates acknowledge bit.
    IIC_stop                            'Bus Master initiates STOP condition.

    
    IIC_start                           'Bus Master initiates a START condition.
    IIC_tx_byte address                 'Bus Master sends DS1621 address; R/ W= 0 (DS1621 generates acknowledge bit).
    IIC_tx_byte &HAA                    'Bus Master sends Read Temperature command protocol.DS1621 generates acknowledge bit.
    IIC_start                           'Bus Master generates a repeated START condition.
    IIC_tx_byte address + 1             'Bus Master sends DS1621 address; R/ W= 1 = READING (DS1621 generates acknowledge bit).
    temperature_int = IIC_rx_byte(1)    'Bus Master receives first byte of data and generates acknowledge.
    
    temperature_frac = IIC_rx_byte(0)   'Bus Master receives second byte of data from DS162 and does not generate acknowledge to signal end of reception.
    IIC_stop                            'Bus Master initiates STOP condition.
    
    'some bynary math to convert to a data format Visual Basic can understand
    temperature = (temperature_int * 256 + temperature_frac) / 128 * 5 / 10
    If temperature_int >= 128 Then
        temperature = temperature - 256
    End If
    Exit Function

errors:
    temperature = ERROR_TEMPERATURE_NOT_READ
End Function

Private Function update_sample_rate()
    If option_1_sec Then
        Timer1.Interval = 1000
    ElseIf option_6_sec Then
        Timer1.Interval = 6000
    ElseIf option_30_sec Then
        Timer1.Interval = 30000
    ElseIf option_1_min Then
        Timer1.Interval = 60000
    Else
        Timer1.Interval = 1000
    End If
End Function
Private Function update_com_port()
    If MSComm1.PortOpen Then
        MSComm1.PortOpen = False
    End If
    
    If option_com1 Then
        MSComm1.CommPort = 1
        Timer1.Enabled = True
    ElseIf option_com2 Then
        MSComm1.CommPort = 2
        Timer1.Enabled = True
    ElseIf option_com3 Then
        MSComm1.CommPort = 3
        Timer1.Enabled = True
    ElseIf option_com4 Then
        MSComm1.CommPort = 4
        Timer1.Enabled = True
    ElseIf option_com5 Then
        MSComm1.CommPort = 5
        Timer1.Enabled = True
    ElseIf option_com6 Then
        MSComm1.CommPort = 6
        Timer1.Enabled = True
    ElseIf option_com7 Then
        MSComm1.CommPort = 7
        Timer1.Enabled = True
    ElseIf option_com8 Then
        MSComm1.CommPort = 8
        Timer1.Enabled = True
    Else
        MSComm1.CommPort = 1
        Timer1.Enabled = False
    End If
    
End Function

Private Function farenheit(ByVal t As Double)
    farenheit = 32 + 9 / 5 * t
End Function

Private Function log_to_file()
    If check_log_to_file Then
        Open "pc_thermometer.csv" For Append As #1
        Print #1, Date$, Time$, label_temperature_padlószint.Caption, label_temperature_külsődéli.Caption, Label_temperature_fancoilbe.Caption, Label_temperature_fancoilel.Caption, Label_temperature_boiler.Caption, Label_temperature_opoph424el.Caption, Label_temperature_opoph424be.Caption, label_temperature_radiator.Caption
    Close #1
    End If
End Function


Private Sub adc_kiolvas()
'For I2C bus communication, addresses are shifted one place to left,
    'as the least significant bit is used for the R/W flag.
    'In binary, shifting to left is equivalent to  multiplying by two
  Dim address
    update_com_port
    address = &H48
    address = address * 2
    
    'On Error GoTo errors
    open_iic_bus MSComm1.object
    
    'an extra stop doesn't hurt...and ensures we start from a clean bus condition
    IIC_stop
    
    'read sequence, as per DS1621 datasheet
    IIC_start                           'Bus Master initiates a START condition.
    IIC_tx_byte address                 'Bus Master sends DS1621 address; R/ W= 0 (DS1621 generates acknowledge bit).
    IIC_tx_byte &H24                    'elküldjük a 100001 csatorna cím
    'IIC_tx_byte &H1                     'Bus Master sets up DS1621 for output polarity active low, one-shot conversion.
                                        'DS1621 generates acknowledge bit.
   
    'IIC_start                           'Bus Master generates a repeated START condition.
    'IIC_tx_byte address                 'Bus Master sends DS1621 address; R/ W= 0.DS1621 generates acknowledge bit.
    'IIC_tx_byte &HEE                    'Bus Master sends Start Convert T command protocol.DS1621 generates acknowledge bit.
    'IIC_stop                            'Bus Master initiates STOP condition.

    
    'IIC_start                           'Bus Master initiates a START condition.
    'IIC_tx_byte address                 'Bus Master sends DS1621 address; R/ W= 0 (DS1621 generates acknowledge bit).
    'IIC_tx_byte &HAA                    'Bus Master sends Read Temperature command protocol.DS1621 generates acknowledge bit.
    IIC_start                           'Bus Master generates a repeated START condition.
    IIC_tx_byte address + 1             'Bus Master sends DS1621 address; R/ W= 1 = READING (DS1621 generates acknowledge bit).
    
    adc_value.Caption = IIC_rx_byte(1)    'Bus Master receives first byte of data and generates acknowledge.
    
    'temperature_frac = IIC_rx_byte(0)   'Bus Master receives second byte of data from DS162 and does not generate acknowledge to signal end of reception.
    IIC_stop                            'Bus Master initiates STOP condition.
    
    'some bynary math to convert to a data format Visual Basic can understand
    'temperature = (temperature_int * 256 + temperature_frac) / 128 * 5 / 10
    'If temperature_int >= 128 Then
     '   temperature = temperature - 256
    
    
End Sub

