Hello Guest it is December 15, 2019, 06:18:33 PM

Author Topic: A Simple VB Macro Pre-Processor  (Read 4783 times)

0 Members and 1 Guest are viewing this topic.

A Simple VB Macro Pre-Processor
« on: December 13, 2009, 08:11:32 PM »
I suspect anyone who is used to programming in any modern language feels the same way I do about writing VB macros for Mach3 - It's about as much fun as a root canal with no anesthesia!  To make my own life easier until Mach3 v4 comes along, I tossed together a VERY rudimentary pre-processor for my VB scripts, that gives me the ability to use "include" files, and very rudimentary text macros.  This not only cuts down on the amount of typing I need to do, and greatly reduces the number of errors, but lets me follow at least a few of the more basic rules of good programming practice, like NOT using hard-coded numbers for things like OEM codes.  I can instead use meaningful names, which makes the code MUCH easier to write, read, and maintain.  These can be defined in a single place, and used in many places, so they're easy to change if necessary.

If others have any interest in using this (with NO guarantees of any support whatsoever....), I can pretty easily clean it up enough to make it easily usable.  It is written in Perl, which does require installing ActiveState Perl, which is a free download from www.activestate.com, and a total no-brainer to install.

Below is a trivial example of what it does.  First, the macro code as I actually write it:

''
'   X Axis Plus Edge Finder Macro
''

#filename M910.m1s
#include "Common.def"
#include "Probing.def"

''========================================================================================
''   Start of Macro Code
''========================================================================================

        ' Set the macro busy flag
        #IncrementMacroBusyFlag

        ' Call the primitive edge finder macro
        Code EdgeFindMacro & " P" & XAxis & " Q" & Plus
        
        ' Wait until it's done
        #WaitUntilSubMacrosDone

        ' Clear the macro busy flag
       #DecrementMacroBusyFlag

 
Here is the code that comes out of the pre-processor, ready to run in Mach3:

''========================================================================================
'  Created From: Mid_XPlusEdgeFind.m3m
'  Includes:
'       Common.def
'       Probing.def
''========================================================================================

''========================================================================================
''    Included Definitions:
''========================================================================================
Busy =                               1             ' Busy, for waiting for macros to complete
EdgeFindMacro =           "M900"     ' Edge finder - requires MacroParameter1DRO & MacroParameter2DRO
MacroBusySemaphoreDRO =  1108       ' Busy flag, used by macros.  Must be zeroed by top-level macros
Plus =                                1             ' Symbol for argument passing
XAxis =                              0             ' Symbol for argument passing
''========================================================================================

''
'   X Axis Plus Edge Finder Macro
''

'#filename M910.m1s
' #include "Common.def"
' #include "Probing.def"

''========================================================================================
''   Start of Macro Code
''========================================================================================

        ' Set the macro busy flag
        ' Macro:         #IncrementMacroBusyFlag
        BusyFlag = GetUserDRO(MacroBusySemaphoreDRO)
        SetUserDRO(MacroBusySemaphoreDRO, BusyFlag + 1)
        Sleep 100
        ' End Macro: #IncrementMacroBusyFlag

        ' Call the primitive edge finder macro
   Code EdgeFindMacro & " P" & XAxis & " Q" & Plus
        
        ' Wait until it's done
        ' Macro:         #WaitUntilSubMacrosDone
        ' Wait until it's done
        While GetUserDRO(MacroBusySemaphoreDRO) > BusyFlag
            Sleep 100
        Wend
        ' End Macro: #WaitUntilSubMacrosDone

        ' Clear the macro busy flag
        ' Macro:    #DecrementMacroBusyFlag
        BusyFlag = GetUserDRO(MacroBusySemaphoreDRO)
        SetUserDRO(MacroBusySemaphoreDRO, BusyFlag - 1)
        Sleep 100
        ' End Macro: #DecrementMacroBusyFlag

 
Notice that DRO, LED, macro numbers are pulled in from the included files (Common.def and Probing.def), so they don't need to be re-defined in every macro.  Simple text macros, like the DecrementMacroBusyFlag macro let you easily re-use small sections of tested code.

So, any interest?
« Last Edit: December 13, 2009, 08:14:46 PM by HimyKabibble »
Regards,
Ray L.
Re: A Simple VB Macro Pre-Processor
« Reply #1 on: December 13, 2009, 10:21:36 PM »
I'm interested.
Re: A Simple VB Macro Pre-Processor
« Reply #2 on: December 14, 2009, 11:28:54 AM »
Here is a better example of the power of this.  Below are some actual files from my suite of probing macros:

Common.def is my "common" include file which defines a whole lot of useful constants that most of my macros require
Probing.def is my probing include file, which defines all the constants associated with probing
Example1.m3m is the actual source code file for one of my probing macros
M666.m1s is the executable Mach3 macro file that the pre-processor creates from the above files 

Note that, using the pre-processor, I only had to write the 2 lines of code in Example1.m3m, rather than the 15 lines of code required for the actual macro to work in Mach3.  And, since those 2 lines are invoking pre-tested pre-processor macros, it is pretty much guaranteed to work first time.

Regards,
Ray L.
Regards,
Ray L.
Re: A Simple VB Macro Pre-Processor
« Reply #3 on: December 14, 2009, 12:23:55 PM »
Thanks. I'll review it ASAP. Dave
Re: A Simple VB Macro Pre-Processor
« Reply #4 on: December 14, 2009, 12:38:39 PM »
Below is the latest version of the pre-processor, along with a directory full of sample macros (my new probing macros).  See the README file for installation and operation instructions.

I expect to be fully supporting nesting of pre-processor macros later today.

Regards,
Ray L.
Regards,
Ray L.