Public Class Form_FocusASCOM2
    Dim FocusPos As Integer = 0
    Dim FocusPos2 As Integer = 0
    Dim MoveMultiplyer As Integer
    Dim MoveMultiplyer2 As Integer
    Dim MaxStep As Integer
    Dim MaxStep2 As Integer
    Dim IncDecSize As Integer
    Dim IncDecSize2 As Integer

    Dim NormColor, ActiveColor As Color
    Public WithEvents KeyControl As New KeyButton()
    Dim KeyHeld As Boolean = False
    Dim IgnoreKey As Boolean = False
    Dim DriverID As String
    Dim DriverID2 As String

    Private driver As ASCOM.DriverAccess.Focuser
    Private driver2 As ASCOM.DriverAccess.Focuser

    REM ASCOM Focus motor #1

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            Const CS_DBLCLKS As Int32 = &H8
            Const CS_NOCLOSE As Int32 = &H200
            cp.ClassStyle = CS_DBLCLKS Or CS_NOCLOSE
            Return cp
        End Get
    End Property

    ''' <summary>
    ''' This event is where the driver is choosen. The device ID will be saved in the settings.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
    Private Sub buttonChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonChoose.Click
        DriverID = ASCOM.DriverAccess.Focuser.Choose(DriverID)
        SetUIState()
    End Sub
    ''' <summary>
    ''' Connects to the device to be tested.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
    Private Sub buttonConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonConnect.Click
        If (IsConnected) Then
            driver.Connected = False
        Else
            Try
                driver = New ASCOM.DriverAccess.Focuser(DriverID)
                driver.Connected = True
                SetProps()
                FocusPos = driver.Position
                TextBoxPos.Text = Format(FocusPos, "00000")
                MaxStep = driver.MaxStep
                LabelMaxStep.Text = "MaxStep = " & MaxStep
                NumericUpDownMoveSize.Maximum = MaxStep

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "ASCOM Connect Error")

            End Try
        End If
        SetUIState()
    End Sub

    Private Sub SetProps()
        Label1.Text = driver.Description
        LabelDriverVersion.Text = driver.DriverVersion

    End Sub

    ''' <summary>
    ''' Sets the state of the UI depending on the device state
    ''' </summary>
    Private Sub SetUIState()
        buttonConnect.Enabled = Not String.IsNullOrEmpty(DriverID)
        buttonChoose.Enabled = Not IsConnected
        buttonConnect.Text = IIf(IsConnected, "Disconnect", "Connect")
        buttonConnect.Enabled = Not String.IsNullOrEmpty(DriverID)
        buttonChoose.Enabled = Not IsConnected
        buttonConnect.Text = IIf(IsConnected, "Disconnect", "Connect")
        labelDriverId.Text = DriverID

        ButtonProp.Enabled = Not IsConnected
        ButtonFocusIn.Enabled = IsConnected
        ButtonFocusOut.Enabled = IsConnected
        ButtonFocusStop.Enabled = IsConnected
        ButtonFocusSet.Enabled = IsConnected
        ButtonFocusPos.Enabled = IsConnected
        ButtonStepInc.Enabled = IsConnected
        ButtonStepDec.Enabled = IsConnected
        KeyControl.Enabled = IsConnected

    End Sub

    ''' <summary>
    ''' Gets a value indicating whether this instance is connected.
    ''' </summary>
    ''' <value>
    ''' 
    ''' <c>true</c> if this instance is connected; otherwise, <c>false</c>.
    ''' 
    ''' </value>
    Private ReadOnly Property IsConnected() As Boolean
        Get
            If Me.driver Is Nothing Then Return False
            Return driver.Connected
        End Get
    End Property

    REM ASCOM Focus motor #2

    ''' <summary>
    ''' This event is where the driver is choosen. The device ID will be saved in the settings.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
    Private Sub buttonChoose2_Click(sender As System.Object, e As System.EventArgs) Handles buttonChoose2.Click
        DriverID2 = ASCOM.DriverAccess.Focuser.Choose(DriverID2)
        SetUIState2()
    End Sub
    ''' <summary>
    ''' Connects to the device to be tested.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
    Private Sub buttonConnect2_Click_1(sender As System.Object, e As System.EventArgs) Handles buttonConnect2.Click
        If (IsConnected2) Then
            driver2.Connected = False
        Else
            Try
                driver2 = New ASCOM.DriverAccess.Focuser(DriverID2)
                driver2.Connected = True
                SetProps2()
                FocusPos2 = driver2.Position
                TextBoxPos2.Text = Format(FocusPos2, "00000")
                MaxStep2 = driver2.MaxStep
                LabelMaxStep2.Text = "MaxStep = " & MaxStep2
                NumericUpDownMoveSize2.Maximum = MaxStep2

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "ASCOM Connect2 Error")

            End Try
        End If
        SetUIState2()
    End Sub

    Private Sub SetProps2()
        Label12.Text = driver2.Description
        LabelDriverVersion2.Text = driver2.DriverVersion

    End Sub


    ''' <summary>
    ''' Sets the state of the UI depending on the device state
    ''' </summary>
    Private Sub SetUIState2()
        buttonConnect2.Enabled = Not String.IsNullOrEmpty(DriverID2)
        buttonChoose2.Enabled = Not IsConnected2
        buttonConnect2.Text = IIf(IsConnected2, "Disconnect", "Connect")
        buttonConnect2.Enabled = Not String.IsNullOrEmpty(DriverID2)
        buttonChoose2.Enabled = Not IsConnected2
        buttonConnect2.Text = IIf(IsConnected2, "Disconnect", "Connect")
        labelDriverId2.Text = DriverID2

        ButtonProp2.Enabled = Not IsConnected2
        ButtonFocusIn2.Enabled = IsConnected2
        ButtonFocusOut2.Enabled = IsConnected2
        ButtonFocusStop2.Enabled = IsConnected2
        ButtonFocusSet2.Enabled = IsConnected2
        ButtonFocusPos2.Enabled = IsConnected2
        ButtonStepInc2.Enabled = IsConnected2
        ButtonStepDec2.Enabled = IsConnected2
        KeyControl.Enabled = IsConnected2

    End Sub

    ''' <summary>
    ''' Gets a value indicating whether this instance is connected.
    ''' </summary>
    ''' <value>
    ''' 
    ''' <c>true</c> if this instance is connected; otherwise, <c>false</c>.
    ''' 
    ''' </value>
    Private ReadOnly Property IsConnected2() As Boolean
        Get
            If Me.driver2 Is Nothing Then Return False
            Return driver2.Connected
        End Get
    End Property


    Private Sub Form_FocusASCOM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim Kx, Ky, Kw, Kh As Integer
        Dim Kt As String

        TextBoxName.Text = Form1.FocuserASCOMname
        TextBoxName2.Text = Form1.FocuserASCOMname2

        NumericUpDownMoveSize.Value = Form1.MoveSize
        NumericUpDownIncDec.Value = Form1.IncDecSize
        DriverID = Form1.FocusASCOMid

        NumericUpDownMoveSize2.Value = Form1.MoveSize2
        NumericUpDownIncDec2.Value = Form1.IncDecSize2
        DriverID2 = Form1.FocusASCOMid2

        If DriverID <> "" Then
            driver = New ASCOM.DriverAccess.Focuser(DriverID)
            SetProps()
        Else
            buttonConnect.Enabled = False
            ButtonProp.Enabled = False
        End If

        If DriverID2 <> "" Then
            driver2 = New ASCOM.DriverAccess.Focuser(DriverID2)
            SetProps2()
        Else
            buttonConnect2.Enabled = False
            ButtonProp2.Enabled = False
        End If

        ' Create button for keyboard control using arrows
        Kx = ButtonKeyboardDummy.Location.X
        Ky = ButtonKeyboardDummy.Location.Y
        Kw = ButtonKeyboardDummy.Size.Width
        Kh = ButtonKeyboardDummy.Size.Height
        Kt = ButtonKeyboardDummy.Text

        KeyControl.Size = New Size(Kw, Kh)
        KeyControl.Location = New Point(Kx, Ky)
        KeyControl.Text = New String(Kt)
        Me.Controls.Add(KeyControl)
        ToolTip1.SetToolTip(KeyControl, "Click to activate keyboard control")
        KeyControl.Enabled = False
        KeyControl.BringToFront()

        NormColor = Form1.NormColor
        ActiveColor = Form1.ActiveColor

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        SaveFocusSettings()
        If IsConnected Then
            driver.Connected = False
        End If
        If IsConnected2 Then
            driver2.Connected = False
        End If

    End Sub

    Private Sub SaveFocusSettings()
        Form1.MoveSize = NumericUpDownMoveSize.Value
        Form1.IncDecSize = NumericUpDownIncDec.Value
        Form1.FocusASCOMid = DriverID
        Form1.FocuserASCOMname = TextBoxName.Text

    End Sub

    Private Sub SaveFocusSettings2()
        Form1.MoveSize2 = NumericUpDownMoveSize2.Value
        Form1.IncDecSize2 = NumericUpDownIncDec2.Value
        Form1.FocusASCOMid2 = DriverID2
        Form1.FocuserASCOMname2 = TextBoxName2.Text

    End Sub

    Private Sub ButtonProp_Click(sender As System.Object, e As System.EventArgs) Handles ButtonProp.Click
        driver.SetupDialog()

    End Sub

    Private Sub ButtonProp2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonProp2.Click
        driver2.SetupDialog()

    End Sub

    Private Sub ButtonFocusPos_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusPos.Click
        LabelMoving.Visible = False
        FocusPos = driver.Position
        TextBoxPos.Text = Format(FocusPos, "00000")

    End Sub

    Private Sub ButtonFocusPos2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusPos2.Click
        LabelMoving2.Visible = False
        FocusPos2 = driver2.Position
        TextBoxPos2.Text = Format(FocusPos2, "00000")

    End Sub

    Private Sub ButtonFocusIn_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusIn.Click
        If IsConnected = True Then
            If CheckBoxRev.Checked = False Then
                FocusPos = FocusPos - MoveMultiplyer
                If FocusPos < 0 Then FocusPos = 0
                LabelMoving.Text = "Moving In"
            Else
                FocusPos = FocusPos + MoveMultiplyer
                If FocusPos > MaxStep Then FocusPos = driver.MaxStep
                LabelMoving.Text = "Moving Out"
            End If
            driver.Move(FocusPos)
            TimerIsMoving.Enabled = True
        End If

    End Sub

    Private Sub ButtonFocusIn2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusIn2.Click
        If IsConnected2 = True Then
            If CheckBoxRev2.Checked = False Then
                FocusPos2 = FocusPos2 - MoveMultiplyer2
                If FocusPos2 < 0 Then FocusPos2 = 0
                LabelMoving2.Text = "Moving In"
            Else
                FocusPos2 = FocusPos2 + MoveMultiplyer2
                If FocusPos2 > MaxStep2 Then FocusPos2 = driver2.MaxStep
                LabelMoving2.Text = "Moving Out"
            End If
            driver2.Move(FocusPos2)
            TimerIsMoving2.Enabled = True
        End If

    End Sub

    Private Sub ButtonFocusOut_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusOut.Click
        If IsConnected = True Then
            If CheckBoxRev.Checked = False Then
                FocusPos = FocusPos + MoveMultiplyer
                If FocusPos > MaxStep Then FocusPos = driver.MaxStep
                LabelMoving.Text = "Moving Out"
            Else
                FocusPos = FocusPos - MoveMultiplyer
                If FocusPos < 0 Then FocusPos = 0
                LabelMoving.Text = "Moving In"
            End If
            driver.Move(FocusPos)
            TimerIsMoving.Enabled = True
        End If

    End Sub

    Private Sub ButtonFocusOut2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusOut2.Click
        If IsConnected2 = True Then
            If CheckBoxRev2.Checked = False Then
                FocusPos2 = FocusPos2 + MoveMultiplyer2
                If FocusPos2 > MaxStep2 Then FocusPos2 = driver2.MaxStep
                LabelMoving2.Text = "Moving Out"
            Else
                FocusPos2 = FocusPos2 - MoveMultiplyer2
                If FocusPos2 < 0 Then FocusPos2 = 0
                LabelMoving2.Text = "Moving In"
            End If
            driver2.Move(FocusPos2)
            TimerIsMoving2.Enabled = True
        End If

    End Sub

    Private Sub ButtonFocusStop_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusStop.Click
        If IsConnected = True Then
            TimerIsMoving.Enabled = False
            driver.Halt()
            ButtonFocusPos.PerformClick()
        End If

    End Sub

    Private Sub ButtonFocusStop2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusStop2.Click
        If IsConnected2 = True Then
            TimerIsMoving2.Enabled = False
            driver2.Halt()
            ButtonFocusPos2.PerformClick()
        End If

    End Sub

    Private Sub TimerIsMoving_Tick(sender As System.Object, e As System.EventArgs) Handles TimerIsMoving.Tick
        LabelMoving.Visible = True
        If driver.IsMoving = False Then
            LabelMoving.Visible = False
            TimerIsMoving.Enabled = False
            ButtonFocusPos.PerformClick()
        Else
            FocusPos = driver.Position
            TextBoxPos.Text = Format(FocusPos, "00000")
        End If

    End Sub

    Private Sub TimerIsMoving2_Tick(sender As System.Object, e As System.EventArgs) Handles TimerIsMoving2.Tick
        LabelMoving2.Visible = True
        If driver2.IsMoving = False Then
            LabelMoving2.Visible = False
            TimerIsMoving2.Enabled = False
            ButtonFocusPos2.PerformClick()
        Else
            FocusPos2 = driver2.Position
            TextBoxPos2.Text = Format(FocusPos2, "00000")
        End If

    End Sub

    Private Sub NumericUpDownMoveSize_ValueChanged(sender As System.Object, e As System.EventArgs) Handles NumericUpDownMoveSize.ValueChanged
        MoveMultiplyer = NumericUpDownMoveSize.Value

    End Sub

    Private Sub NumericUpDownMoveSize2_ValueChanged(sender As System.Object, e As System.EventArgs) Handles NumericUpDownMoveSize2.ValueChanged
        MoveMultiplyer2 = NumericUpDownMoveSize2.Value

    End Sub

    Private Sub ButtonFocusSet_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusSet.Click
        If TextBoxPos.Text > MaxStep Then
            TextBoxPos.Text = Format(MaxStep, "000000")
        End If
        FocusPos = TextBoxPos.Text
        driver.Move(FocusPos)
        LabelMoving.Text = "Moving to " & FocusPos
        TimerIsMoving.Enabled = True

    End Sub

    Private Sub ButtonFocusSet2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFocusSet2.Click
        If TextBoxPos2.Text > MaxStep2 Then
            TextBoxPos2.Text = Format(MaxStep2, "000000")
        End If
        FocusPos2 = TextBoxPos2.Text
        driver2.Move(FocusPos)
        LabelMoving2.Text = "Moving to " & FocusPos2
        TimerIsMoving2.Enabled = True

    End Sub

    Private Sub NumericUpDownIncDec_ValueChanged(sender As System.Object, e As System.EventArgs) Handles NumericUpDownIncDec.ValueChanged
        IncDecSize = NumericUpDownIncDec.Value

    End Sub

    Private Sub NumericUpDownIncDec2_ValueChanged(sender As System.Object, e As System.EventArgs) Handles NumericUpDownIncDec2.ValueChanged
        IncDecSize2 = NumericUpDownIncDec2.Value

    End Sub

    Private Sub ButtonStepInc_Click(sender As System.Object, e As System.EventArgs) Handles ButtonStepInc.Click
        MoveMultiplyer = MoveMultiplyer + IncDecSize
        If MoveMultiplyer > 10000 Then
            MoveMultiplyer = 10000
        End If
        NumericUpDownMoveSize.Value = MoveMultiplyer

    End Sub

    Private Sub ButtonStepInc2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonStepInc2.Click
        MoveMultiplyer2 = MoveMultiplyer2 + IncDecSize2
        If MoveMultiplyer2 > 10000 Then
            MoveMultiplyer2 = 10000
        End If
        NumericUpDownMoveSize2.Value = MoveMultiplyer2

    End Sub

    Private Sub ButtonStepDec_Click(sender As System.Object, e As System.EventArgs) Handles ButtonStepDec.Click
        MoveMultiplyer = MoveMultiplyer - IncDecSize
        If MoveMultiplyer < 0 Then
            MoveMultiplyer = 0
        End If
        NumericUpDownMoveSize.Value = MoveMultiplyer

    End Sub

    Private Sub ButtonStepDec2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonStepDec2.Click
        MoveMultiplyer2 = MoveMultiplyer2 - IncDecSize2
        If MoveMultiplyer2 < 0 Then
            MoveMultiplyer2 = 0
        End If
        NumericUpDownMoveSize2.Value = MoveMultiplyer2

    End Sub

    Private Sub KeyControl_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles KeyControl.KeyDown
        Dim K As String

        If IgnoreKey = False Then
            If KeyHeld = False Then
                K = e.KeyCode
                If K = Keys.Menu Then
                    IgnoreKey = True
                Else
                    IgnoreKey = False
                    Select Case TabControl1.SelectedIndex
                        Case 0
                            ' Motor 1
                            driver.Halt()
                            ButtonFocusPos.PerformClick()
                            Select Case K
                                Case Keys.PageUp, Keys.OemOpenBrackets, Keys.NumPad6
                                    ButtonFocusIn.PerformClick()
                                Case Keys.PageDown, Keys.OemCloseBrackets, Keys.NumPad4
                                    ButtonFocusOut.PerformClick()
                                Case Keys.OemPeriod, Keys.Add
                                    ButtonStepInc.PerformClick()
                                Case Keys.Oemcomma, Keys.Subtract
                                    ButtonStepDec.PerformClick()
                            End Select
                        Case 1
                            ' Motor 2
                            driver2.Halt()
                            ButtonFocusPos2.PerformClick()
                            Select Case K
                                Case Keys.PageUp, Keys.OemOpenBrackets, Keys.NumPad6
                                    ButtonFocusIn2.PerformClick()
                                Case Keys.PageDown, Keys.OemCloseBrackets, Keys.NumPad4
                                    ButtonFocusOut2.PerformClick()
                                Case Keys.OemPeriod, Keys.Add
                                    ButtonStepInc2.PerformClick()
                                Case Keys.Oemcomma, Keys.Subtract
                                    ButtonStepDec2.PerformClick()
                            End Select

                    End Select
                End If

                If Form1.GroupBox1.Enabled = True Then
                    ' Mount movement
                    If Form1.CheckBoxSlewTime.Checked = False Then
                        Select Case K
                            Case Keys.Up, Keys.NumPad5
                                Form1.ProgNstart()
                            Case Keys.Down, Keys.NumPad2
                                Form1.ProgSstart()
                            Case Keys.Right, Keys.NumPad3
                                Form1.ProgEstart()
                            Case Keys.Left, Keys.NumPad1
                                Form1.ProgWstart()
                        End Select
                    End If
                End If

                KeyHeld = True
            End If
        End If

    End Sub

    Private Sub KeyControl_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles KeyControl.KeyUp
        Dim K As String = ""

        K = e.KeyCode
        If IgnoreKey = False Then

            If Form1.GroupBox1.Enabled = True Then
                ' Mount movement
                If Form1.CheckBoxSlewTime.Checked = False Then
                    Select Case K
                        Case Keys.Up, Keys.NumPad5
                            Form1.ProgNend()
                        Case Keys.Down, Keys.NumPad2
                            Form1.progSend()
                        Case Keys.Right, Keys.NumPad3
                            Form1.progEend()
                        Case Keys.Left, Keys.NumPad1
                            Form1.progWend()
                    End Select
                Else
                    Select Case K
                        Case Keys.Up, Keys.NumPad5
                            Form1.ButtonN.PerformClick()
                        Case Keys.Down, Keys.NumPad2
                            Form1.ButtonS.PerformClick()
                        Case Keys.Right, Keys.NumPad3
                            Form1.ButtonE.PerformClick()
                        Case Keys.Left, Keys.NumPad1
                            Form1.ButtonW.PerformClick()
                    End Select
                End If
            End If

            KeyHeld = False
        End If

    End Sub

    Private Sub KeyControl_GotFocus(sender As Object, e As System.EventArgs) Handles KeyControl.GotFocus
        KeyControl.BackColor = ActiveColor
        LabelKey.Visible = True

    End Sub

    Private Sub KeyControl_LostFocus(sender As Object, e As System.EventArgs) Handles KeyControl.LostFocus
        KeyControl.BackColor = Color.Transparent
        LabelKey.Visible = False

    End Sub

    Private Sub LabelKeyHelp_Click(sender As System.Object, e As System.EventArgs) Handles LabelKeyHelp.Click
        If Form_FocusHelp.Visible = True Then
            Form_FocusHelp.Close()
        Else
            Form_FocusHelp.Show()
        End If

    End Sub

    Private Sub ButtonClose_Click(sender As System.Object, e As System.EventArgs) Handles ButtonClose.Click
        SaveFocusSettings()
        SaveFocusSettings2()
        Me.Hide()

    End Sub

End Class