hippy
Ex-Staff (retired)
Because of the way things are, #MACRO and #IFDEF do not play well together in PICAXE basic. To create a macro which does one thing or another depending on a #DEFINE definition can seem impossible. For example, neither of the following two will pass syntax check -
The solution is to code things slightly differently.
Here we specify two separate #MACRO's, 'PrintLetterA' and 'PrinteLetterX', to do the two different things we require. And then we use #IFDEF to selectively #DEFINE what 'PrintLetter' actually is -
That code can be simulated in PE6 and will show the affect of including or removing the '#Define PRINT_A'.
Because #MACRO commands only generate code when invoked, no matter how much code is in each of the two #MACRO's, only the code in the one used will be included in the compiled program.
If there is a lot of common code required within each #MACRO; it may be possible that the common code can be moved out into its own #MACRO and then a macro expansion used in each #MACRO -
One thing to note: If the macros have parameters, then those parameters must also be included in the #DEFINE statements as well.
If that is not done there will be "( expected" errors generated.
Again, the effect of including and removing '#Define PRINT_A' can be tested using simulation under PE6.
Code:
#Macro PrintLetter
#IfDef PRINT_A
SerTxd( "A" )
#Else
SerTxd( "X" )
#EndIf
#EndMacro
Code:
#IfDef PRINT_A
#Macro PrintLetter
SerTxd( "A" )
#EndMacro
#Else
#Macro PrintLetter
SerTxd( "X" )
#EndMacro
#EndIf
Here we specify two separate #MACRO's, 'PrintLetterA' and 'PrinteLetterX', to do the two different things we require. And then we use #IFDEF to selectively #DEFINE what 'PrintLetter' actually is -
Code:
#Define PRINT_A
#IfDef PRINT_A
#Define PrintLetter PrintLetterA
#Else
#Define PrintLetter PrintLetterX
#EndIf
#Macro PrintLetterA
SerTxd( "A" )
#EndMacro
#Macro PrintLetterX
SerTxd( "X" )
#EndMacro
Do
PrintLetter
Loop
Because #MACRO commands only generate code when invoked, no matter how much code is in each of the two #MACRO's, only the code in the one used will be included in the compiled program.
If there is a lot of common code required within each #MACRO; it may be possible that the common code can be moved out into its own #MACRO and then a macro expansion used in each #MACRO -
Code:
#Macro Prefix
SerTxd( "Prefix" )
#EndMacro
#Macro PrintLetterA
Prefix
SerTxd( "A" )
#EndMacro
#Macro PrintLetterX
Prefix
SerTxd( "X" )
#EndMacro
If that is not done there will be "( expected" errors generated.
Rich (BB code):
#Define PRINT_A
#IfDef PRINT_A
#Define PrintLetter(n) PrintLetterA(n)
#Else
#Define PrintLetter(n) PrintLetterX(n)
#EndIf
#Macro PrintLetterA(n)
SerTxd( "A", #n )
#EndMacro
#Macro PrintLetterX(n)
SerTxd( "X", #n )
#EndMacro
Do
For b0 = 1 To 10
PrintLetter(b0)
Next
Loop