Imports System.Threading

Public Class Form_DIAGS
    Dim DECcmds(3) As String
    Dim RAcmds(3) As String
    Dim DECindex As Integer = 0
    Dim RAindex As Integer = 0
    Public RArun As Boolean = False
    Public DECrun As Boolean = False
    Dim RunColor As Color = Color.White
    Dim RunBackColor As Color = Color.Red
    Dim NoRunColor As Color = Color.Black
    Dim NoRunBackColor As Color = SystemColors.Control

    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


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

    End Sub

    Private Sub Form_DIAGS_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        StopMotors()

    End Sub

    Private Sub StopMotors()
        If DECrun = True Then
            Thread.Sleep(200)
            ButtonDEC.PerformClick()
        End If

        If RArun = True Then
            Thread.Sleep(200)
            ButtonRA.PerformClick()
        End If

    End Sub

    Private Sub Form_DIAGS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Make sure form is on the screen
        If Me.Location.X < 0 Or Me.Location.Y < 0 Then
            Me.Location = New Point(0, 0)
        End If

        ' Make sure window is not off of screen
        If Me.Left + Me.Width > Form1.ScreenWidth Then
            Me.Left = Form1.ScreenWidth - Me.Width - 100
        End If
        If Me.Top + Me.Height > Form1.ScreenHeight Then
            Me.Top = Form1.ScreenHeight - Me.Height - 100
        End If

        DECcmds(0) = "N"
        DECcmds(1) = "S"
        DECcmds(2) = "StopN"
        DECcmds(3) = "StopS"
        RAcmds(0) = "E"
        RAcmds(1) = "W"
        RAcmds(2) = "StopE"
        RAcmds(3) = "StopW"

    End Sub

    Private Sub NumericUpDownRun_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDownRun.ValueChanged
        If NumericUpDownRun.Value > 0 Then
            TimerDEC.Interval = NumericUpDownRun.Value
            TimerRA.Interval = NumericUpDownRun.Value
        End If

    End Sub

    Private Sub NumericUpDownDelay_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDownDelay.ValueChanged
        If NumericUpDownDelay.Value > 0 Then
            TimerDECdelay.Interval = NumericUpDownDelay.Value
            TimerRAdelay.Interval = NumericUpDownDelay.Value
        Else
            TimerDECdelay.Interval = 1
            TimerRAdelay.Interval = 1
        End If

    End Sub

    Private Sub ButtonDEC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDEC.Click
        If DECrun = False Then
            Form1.AP_Command(DECcmds(DECindex))
            TimerDEC.Enabled = True
            ButtonDEC.Text = "Stop DEC"
            DECrun = True
            LabelN.ForeColor = RunColor
            LabelN.BackColor = RunBackColor
        Else
            TimerDEC.Enabled = False
            TimerDECdelay.Enabled = False
            Form1.AP_Command(DECcmds(DECindex + 2))
            ButtonDEC.Text = "Start DEC"
            DECrun = False
            LabelN.ForeColor = NoRunColor
            LabelN.BackColor = NoRunBackColor
            LabelS.ForeColor = NoRunColor
            LabelS.BackColor = NoRunBackColor
        End If

    End Sub

    Private Sub ButtonRA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRA.Click
        If RArun = False Then
            Form1.AP_Command(RAcmds(RAindex))
            TimerRA.Enabled = True
            ButtonRA.Text = "Stop RA"
            RArun = True
            LabelE.ForeColor = RunColor
            LabelE.BackColor = RunBackColor
        Else
            TimerRA.Enabled = False
            TimerRAdelay.Enabled = False
            Form1.AP_Command(RAcmds(RAindex + 2))
            ButtonRA.Text = "Start RA"
            RArun = False
            LabelE.ForeColor = NoRunColor
            LabelE.BackColor = NoRunBackColor
            LabelW.ForeColor = NoRunColor
            LabelW.BackColor = NoRunBackColor
        End If

    End Sub

    Private Sub TimerDEC_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerDEC.Tick
        TimerDEC.Enabled = False
        Form1.AP_Command(DECcmds(DECindex + 2))
        DECindex = DECindex + 1
        If DECindex > 1 Then DECindex = 0
        TimerDECdelay.Enabled = True
        LabelN.ForeColor = NoRunColor
        LabelN.BackColor = NoRunBackColor
        LabelS.ForeColor = NoRunColor
        LabelS.BackColor = NoRunBackColor

    End Sub

    Private Sub TimerRA_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerRA.Tick
        TimerRA.Enabled = False
        Form1.AP_Command(RAcmds(RAindex + 2))
        RAindex = RAindex + 1
        If RAindex > 1 Then RAindex = 0
        TimerRAdelay.Enabled = True
        LabelE.ForeColor = NoRunColor
        LabelE.BackColor = NoRunBackColor
        LabelW.ForeColor = NoRunColor
        LabelW.BackColor = NoRunBackColor

    End Sub

    Private Sub TimerDECdelay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerDECdelay.Tick
        TimerDECdelay.Enabled = False
        Form1.AP_Command(DECcmds(DECindex))
        TimerDEC.Enabled = True
        Select Case DECcmds(DECindex)
            Case "N"
                LabelN.ForeColor = RunColor
                LabelN.BackColor = RunBackColor
                LabelS.ForeColor = NoRunColor
                LabelS.BackColor = NoRunBackColor
            Case "S"
                LabelN.ForeColor = NoRunColor
                LabelN.BackColor = NoRunBackColor
                LabelS.ForeColor = RunColor
                LabelS.BackColor = RunBackColor
        End Select

    End Sub

    Private Sub TimerRAdelay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerRAdelay.Tick
        TimerRAdelay.Enabled = False
        Form1.AP_Command(RAcmds(RAindex))
        TimerRA.Enabled = True
        Select Case RAcmds(RAindex)
            Case "E"
                LabelE.ForeColor = RunColor
                LabelE.BackColor = RunBackColor
                LabelW.ForeColor = NoRunColor
                LabelW.BackColor = NoRunBackColor
            Case "W"
                LabelE.ForeColor = NoRunColor
                LabelE.BackColor = NoRunBackColor
                LabelW.ForeColor = RunColor
                LabelW.BackColor = RunBackColor
        End Select

    End Sub

End Class