'--------------------------------------------------------------------------
' JoyPad plugin version 2.0
' By Joakim Hjort 2009 - 2011.
'
' Auto generated macro from JoyPad plugin, M800.m1s.
'
' This macro is executed if a button is pressed and assigned script
' function in JoyPadSetup program.
'
' M800.m1s must be placed in Mach3 macros folder under the active
' machine profile.
'
' Documentation for JoyPad plugin can be found in JoyPad User manual.
' Script documentation can be found at Mach support web:
'   http://www.machsupport.com
'
' Three parameters is passed to this script:
'
'   EState: Mach engine state
'   Button: The button pressed, 0 .. 15
'   SState: Shift buttons pressed (SL, SR, AL and AR)
'--------------------------------------------------------------------------
Option Explicit

' Constants for EState.
Const ESTOP      = 0 ' STOP
Const ERUN       = 1 ' RUN
Const EPAUSE     = 2 ' PAUSE
Const EFINISHING = 3 ' FINISHING
Const EMDISTATE  = 4 ' MDISTATE
Const EWAITING   = 5 ' WAITING
Const ESINGLE    = 6 ' SINGLE

' Constants for Button.
Const BN =  0
Const BE =  1
Const BS =  2
Const BW =  3
Const B1 =  4
Const B2 =  5
Const B3 =  6
Const B4 =  7
Const B5 =  8 ' B5 to B8 not on standard game pads
Const B6 =  9
Const B7 = 10
Const B8 = 11
Const PN = 12 ' POV buttons
Const PE = 13
Const PS = 14
Const PW = 15

'--------------------------------------------------------------------------
' Variables to hold parameters passed from JoyPad plugin.
Dim EState As Integer ' Engine state (RUN, STOP, ...)
Dim Button As Integer ' Button pressed (BN, BE, BS, ...)
Dim SState As Integer ' Shift buttons pressed (SL, SR, AL and AR)

' SL, SR, AL and AR. True if pressed:
Dim SL As Boolean
Dim SR As Boolean
Dim AL As Boolean
Dim AR As Boolean

' True if mach is accepting commands
Dim Idle as Boolean

'--------------------------------------------------------------------------
' Read parameters passed from JoyPad plugin. Don't edit.
EState = CInt(Param1())
Button = CInt(Param2())
SState = CInt(Param3())

SL = (SState And &h01) > 0
SR = (SState And &h02) > 0
AL = (SState And &h04) > 0
AR = (SState And &h08) > 0

Idle = (EState = EStop)

'==========================================================================
' <-- Put your own script code here.

'END ' Uncomment END to end execution of script here.
     ' Or delete text to end of file.
'==========================================================================

'--------------------------------------------------------------------------
' The following script code is an example/test acript and can be deleted.
'
' Writes a message to Mach with the parameters received from the plugin:
' Engine state, shift pressen and button pressen.
'
' To activate script for all buttons; run JoyPadSetup and select Standard
' Assignment: All buttons script
' Save and restart Mach3.
' Pressing POV or buttons will activate this macro and send message to Mach
' message field.

' Convert Engine state to text in EText
Dim EText as String
Select Case EState
  Case ESTOP:      EText = "STOP"       ' Is idle
  Case ERUN:       EText = "RUN"        ' Running GCode
  Case EPAUSE:     EText = "PAUSE"      ' Paused in GCode
  Case EFINISHING: EText = "FINISHING"  ' Plugin loading ?
  Case EMDISTATE:  EText = "MDISTATE"   ' ?
  Case EWAITING:   EText = "WAITING"    ' ?
  Case ESINGLE:    EText = "SINGLE"     ' Single step ?
End Select

' Convert Safety and Alt buttons to text in SText.
' There can be zero or more pressed at once.
Dim SText as String
If SL Then ' Is Safety Left pressed?
  SText = "SL"
End If

If SR Then ' Is Safety Right pressed?
  If Len(SText) > 0 Then SText = SText & "+"
  SText = SText & "SR"
End If

If AL Then ' Is Alt Left pressed?
  If Len(SText) > 0 Then SText = SText & "+"
  SText = SText & "AL"
End If

If AR Then ' Is Alt Right pressed?
  If Len(SText) > 0 Then SText = SText & "+"
  SText = SText & "AR"
End If
If Len(SText) = 0 Then SText = "None"

' Convert button to text in BText.
Dim BText as String
Select Case Button
  Case BN: BText = "BN" ' 4 face buttons
  Case BE: BText = "BE"
  Case BS: BText = "BS"
  Case BW: BText = "BW"
  Case B1: BText = "B1" ' 2 center buttons
  Case B2: BText = "B2"
  Case B3: BText = "B3" ' Under joystick buttons
  Case B4: BText = "B4"
  Case B5: BText = "B5" ' 4 extra buttons
  Case B6: BText = "B6"
  Case B7: BText = "B7"
  Case B8: BText = "B8"
  Case PN: BText = "PN" ' 4 POV buttons
  Case PE: BText = "PE"
  Case PS: BText = "PS"
  Case PW: BText = "PW"
 End Select

' Write parameters to Mach as a text message.
Message "JoyPad plugin -> Engine: " & EText & ", Shift: " & SText & ", Button: " & BText

' End of example/test script.
'--------------------------------------------------------------------------
