#macro with #ifdef

cpedw

Senior Member
I wanted to include optionally a debugging line in a macro so I tried
Code:
#Macro Sendout(code,d1,d2)
    PULSOUT srout, pwake
    SEROUT srout, baud, (code, d1, d2)
 #IFDEF diagnose
      SERtxd (#code,"  ",#d1,"  ", #d2,cr,lf)
 #ENDIF
     PAUSE p100
#EndMacro
But that got an error "Expected #ENDMACRO" at the #IFDEF line. The online command description for #macro explains "As a macro is processed as a 'text substitution' before compilation it cannot contain any other directive. "

So I tried the less elegant
Code:
#IFDEF diagnose
#Macro Sendout(code,d1,d2)
  PULSOUT srout, pwake
  SEROUT srout, baud, (code, d1, d2)
    SERtxd (#code,"  ",#d1,"  ", #d2,cr,lf)
  PAUSE p100
#EndMacro
#ELSE
#Macro Sendout(code,d1,d2)
  PULSOUT srout, pwake
  SEROUT srout, baud, (code, d1, d2)
  PAUSE p100
#EndMacro
#ENDIF
But that got the error "Expected #ELSEIF, #ELSEDEF, #ELSE or #ENDIF" at the first #EndMacro line. I couldn't find an explanation for this problem.

Is there a straightforward route to conditionally incorporate a diagnostic line in a macro?

Derek
 

hippy

Ex-Staff (retired)
There is a way to do it ...

https://picaxeforum.co.uk/threads/conditional-macros-macro-with-ifdef.30160

Which for your own code would translates as ...
Code:
#IFDEF diagnose
  #Define Sendout(code,d1,d2) Sendout_Diagnose(code,d1,d2) 
#ELSE
  #Define Sendout(code,d1,d2) Sendout_NoDiagnose(code,d1,d2) 
#ENDIF

#Macro Sendout_Diagnose(code,d1,d2)
  PULSOUT srout, pwake
  SEROUT srout, baud, (code, d1, d2)
  SERtxd (#code,"  ",#d1,"  ", #d2,cr,lf)
  PAUSE p100
#EndMacro

#Macro Sendout_NoDiagnose(code,d1,d2)
  PULSOUT srout, pwake
  SEROUT srout, baud, (code, d1, d2)
  PAUSE p100
#EndMacro
Then you can use your "Sendout" macro command as you'd like ...
Code:
Sendout(1,2,3)
 

techElder

Well-known member
Additional explanation to Hippy's fine example, is that a Macro is a replacement mechanism.

When you use a Macro name within your code, the compilation process replaces the Macro "call" with the code that is defined in #Macro. It isn't like a subroutine (or a function in other languages), it is a substitution process at compile time.

So, those "#IFDEF" directives just don't work in the code that is going into the PICAXE and are rejected. So, as Hippy has shown, you have to provide code that will work in what is going into the PICAXE.
 
Top