' Program:     MotorSlider

' Description:
' -This example:
'    o lets a user interactively adjust the angle of all motors through a slider
'    o displays all the information it can about the PhidgetMotorControl.
'    o is somewhat robust about ignoring other PhidgetMotorControls that are attached
'

Option Explicit
Dim WithEvents Stepper As PhidgetStepper

Private Sub AccelSlider_Click()
    Stepper.Acceleration(0) = AccelSlider.Value
End Sub

Private Sub EngagedCheck_Click()
    Stepper.Engaged(0) = EngagedCheck.Value
End Sub

Private Sub Form_Load()
    lblMessage.Caption = "Attach a PhidgetStepper"
    Set Stepper = Controls.Add("Phidget21COM.PhidgetStepper", "Stepper")
    Stepper.EnableVerboseErrors True
    Stepper.Open
    
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Stepper.Engaged(0) = False
    Stepper.Close
End Sub

Private Sub PositionSlider_Click()
    Stepper.TargetPosition(0) = PositionSlider.Value
End Sub

Private Sub resetPositionBtn_Click()
    Stepper.CurrentPosition(0) = 0
End Sub

Private Sub Stepper_OnAttach()
    'Show the current Motor information
    lblMessage.Caption = "PhidgetStepper Attached"
    lblNumMotors.Caption = Stepper.MotorCount
    lblSerialNumber.Caption = Stepper.SerialNumber
    
    VelocitySlider.Min = Stepper.VelocityMin(0)
    VelocitySlider.Max = Stepper.VelocityMax(0)
    VelocitySlider.TickFrequency = Stepper.VelocityMax(0) / 64
    VelocitySlider.LargeChange = Stepper.VelocityMax(0) / 64
    
    AccelSlider.Min = Stepper.AccelerationMin(0)
    AccelSlider.Max = Stepper.AccelerationMax(0)
    AccelSlider.TickFrequency = Stepper.AccelerationMin(0) * 4
    AccelSlider.LargeChange = Stepper.AccelerationMin(0)
    
    PositionSlider.Min = -20000
    PositionSlider.Max = 20000
    PositionSlider.TickFrequency = 1000
    PositionSlider.LargeChange = 250
    
    If Stepper.DeviceName <> "Phidget Bipolar Stepper Controller 1-motor" Then
        CurrentLimSlider.Visible = False
        CurrentLimLbl.Visible = False
        CurrentLimSlider.Enabled = False
        
        CurrentLbl.Visible = False
        CurrentDispLbl.Caption = ""
        CurrentDispLbl.Visible = False
        
        Label9.Visible = False
        input0Chk.Visible = False
        input1Chk.Visible = False
        input2Chk.Visible = False
        input3Chk.Visible = False
    Else
        CurrentLimSlider.Visible = True
        CurrentLimLbl.Visible = True
        CurrentLimSlider.Enabled = True
        
        CurrentLbl.Visible = True
        CurrentDispLbl.Caption = ""
        CurrentDispLbl.Visible = True
        CurrentLimDispLbl.Caption = ""
        CurrentLimDispLbl.Visible = True
        
        CurrentLimSlider.Min = Stepper.CurrentMin(0) * 100
        CurrentLimSlider.Max = Stepper.CurrentMax(0) * 100
        CurrentLimSlider.TickFrequency = 5
        CurrentLimSlider.LargeChange = 1
        CurrentLimSlider.Value = Stepper.CurrentMin(0) * 100
        CurrentLimDispLbl.Caption = Stepper.CurrentMin(0)
        Stepper.CurrentLimit(0) = Stepper.CurrentMin(0)
        
        Label9.Visible = True
        input0Chk.Visible = True
        input1Chk.Visible = True
        input2Chk.Visible = True
        input3Chk.Visible = True
    End If
End Sub

Private Sub Stepper_OnCurrentChange(ByVal Index As Long, ByVal newVal As Double)
    CurrentDispLbl.Caption = newVal
End Sub

Private Sub Stepper_OnDetach()
    'Show the current Motor information
    lblMessage.Caption = "Attach a PhidgetStepper"
    lblNumMotors.Caption = ""
    lblSerialNumber.Caption = ""
End Sub

Private Sub Stepper_OnInputChange(ByVal Index As Long, ByVal NewState As Boolean)
    If NewState = True Then
        If Index = 0 Then input0Chk.Value = 1
        If Index = 1 Then input1Chk.Value = 1
        If Index = 2 Then input2Chk.Value = 1
        If Index = 3 Then input3Chk.Value = 1
    Else
        If Index = 0 Then input0Chk.Value = 0
        If Index = 1 Then input1Chk.Value = 0
        If Index = 2 Then input2Chk.Value = 0
        If Index = 3 Then input3Chk.Value = 0
    End If
End Sub

Private Sub Stepper_OnPositionChange(ByVal Index As Long, ByVal newVal As Long)
    If Index = 0 Then
        CurrentPosition.Caption = newVal
        If Stepper.Stopped(0) = True Then
            StoppedCheck.Value = 1
        Else
            StoppedCheck.Value = 0
        End If
    End If
End Sub

Private Sub Stepper_OnVelocityChange(ByVal Index As Long, ByVal newVal As Double)
    If Index = 0 Then
        CurrentVelocity.Caption = newVal
        If Stepper.Stopped(0) = True Then
            StoppedCheck.Value = 1
        Else
            StoppedCheck.Value = 0
        End If
    End If
End Sub

Private Sub VelocitySlider_Click()
    Stepper.VelocityLimit(0) = VelocitySlider.Value
End Sub

Private Sub CurrentLimSlider_Click()
    Dim Current As Double
    Current = CurrentLimSlider.Value / 100
    If Current < Stepper.CurrentMin(0) Then
        Current = Stepper.CurrentMin(0)
    End If
    If Current > Stepper.CurrentMax(0) Then
        Current = Stepper.CurrentMax(0)
    End If
    
    Stepper.CurrentLimit(0) = Current
    CurrentLimDispLbl.Caption = Current
End Sub