Nested macroes

In a program written in PE6, this sequence :
Code:
#IFDEF test
sertxd(" %",msg,13,10)
#ENDIF
is often repeated, so I would like to put it in a macro. However, this doesn't work:
Code:
#macro println(msg)
#IFDEF test
sertxd(" %",msg,13,10)
#ENDIF
#endmacro
Is there a way to define such a macro in PE6?
 

hippy

Technical Support
Staff member
The way precedence of directives is handled it is currently not possible. The best you can do is ...

#macro println(msg)
sertxd(" %",msg,13,10)
#endmacro

#IFDEF test
println( ...... )
#ENDIF
 

irskir

New Member
What I really really want to do is :
#macro turnOnRedLed
#ifdef PCBoardVersion1
low LEDRed
#else
high LEDRed
#endif
#endmacro

Can't see any way to do this except to call a subroutine each time which takes much more code space, or of course clutter the program with this pattern over and over and hope they are all correct. This, and the #rem instructions inside a #ifdef construct makes going to version 6 useless. At least 5 could do the latter.
 

stan74

Senior Member
macros are a waste of space especially when they nest,usually late spring. I end up de-macroing other peoples code. Don't do it.It's not big or clever.
 

edmunds

Senior Member
macros are a waste of space especially when they nest,usually late spring. I end up de-macroing other peoples code. Don't do it.It's not big or clever.
I agree to some extend as the code gets very difficult to debug in simulator. And re-writing back and forth to subroutines introduces errors on top of the ones you already have. However, gosubs are painfully slow. If you need speed, there is not a thing like a macro. Also, a thing I have not mastered fully, but see good code every now and then is passing parameters to a macros. Because picaxe basic lacks functions with parameters, this can sometimes be very useful when writing complex code.

Edmunds
 

stan74

Senior Member
I'm not sold on using macros. I don't want to re-iterate a discussion but can't see the difference from a gosub except it looks neater to some people.What controller time and memory does it save and I'll use macros.
This doesn't look basicy
#macro turnOnRedLed
#ifdef PCBoardVersion1
low LEDRed
#else
high LEDRed
#endif
#endmacro
 

Technical

Technical Support
Staff member
Macros are often used with a gosub, not instead of. Here are a couple from our Sphero example:

Code:
#MACRO SetRgbColor(R, G, B)
   b0 = R
   b1 = G
   b2 = B
   gosub SendRGB
#ENDMACRO

#DEFINE SetHeading(H) w0 = H : Gosub SendHeading

#MACRO SetHeadingSpeed(H, S)
   w1 = H
   b0 = S
   gosub SendHeadingSpeed
#ENDMACRO
 

AllyCat

Senior Member
Hi,

1. What controller time and memory does it save and I'll use macros.
2. This doesn't look basicy
1. About 3,200 fewer PIC instruction cycles for each CALL !

2. Looks like fairly basic preprocessor stuff to me. Does this do what was required?
Code:
[color=Navy]#ifdef [/color][color=Black]PCBoardVersion1[/color]
[color=Navy]#define [/color][color=Black]turnon [/color][color=Blue]low[/color]
[color=Navy]#else
#define [/color][color=Black]turnon [/color][color=Blue]high[/color]
[color=Navy]#endif

#macro [/color][color=Black]turnOnRedLed
turnon LEDRed[/color]
[color=Navy]#endmacro[/color]
But I must admit, that's not enough to make me use PE6 routinely in place of PE5. ;)

Cheers, Alan.
 

stan74

Senior Member
pe5 doesn't compile stuff that pe6 does,same code,setup etc.
"1. About 3,200 fewer PIC instruction cycles for each CALL !"
ok Alan, I'll post some code in a new thread and someone can macro it and show it as an example of cool coding if that's safe.
This didn't compile in pe5.
Code:
[color=Navy]#picaxe [/color][color=Black]28x2[/color]
[color=Blue]setfreq m16
pwmout pwmdiv4[/color][color=Black], [/color][color=Blue]B.0[/color][color=Black], [/color][color=Navy]99[/color][color=Black], [/color][color=Navy]399 [/color][color=Green]; 10000Hz at 100% @ 16MHz[/color]
[color=Blue]pwmout pwmdiv4[/color][color=Black], [/color][color=Blue]B.5[/color][color=Black], [/color][color=Navy]99[/color][color=Black], [/color][color=Navy]399 [/color][color=Green]; 10000Hz at 100% @ 16MHz[/color]

[color=Black]main:[/color]
[color=Blue]pwmduty b.0[/color][color=Black],[/color][color=Navy]0[/color]
[color=Blue]pwmduty b.5[/color][color=Black],[/color][color=Navy]0[/color]
[color=Blue]pause [/color][color=Navy]300[/color]
[color=Blue]pwmduty b.0[/color][color=Black],[/color][color=Navy]399[/color]
[color=Blue]pwmduty b.5[/color][color=Black],[/color][color=Navy]0[/color]
[color=Blue]pause [/color][color=Navy]300[/color]
[color=Blue]pwmduty b.0[/color][color=Black],[/color][color=Navy]399[/color]
[color=Blue]pwmduty b.5[/color][color=Black],[/color][color=Navy]399[/color]
[color=Blue]pause [/color][color=Navy]300[/color]
[color=Blue]pwmduty b.0[/color][color=Black],[/color][color=Navy]399[/color]
[color=Blue]pwmduty b.5[/color][color=Black],[/color][color=Navy]0[/color]
[color=Blue]pause [/color][color=Navy]300[/color]
[color=Green];for w0=0 to 399
;;pwmout b.0, 39, 160   
;pwmduty b.0,w0
;
;;pwmout b.5, 39, 160
;pwmduty b.5,w0
;;sertxd (#w0,cr,lf)
;;pause 1000
;next w0
;pause 200[/color]

[color=Blue]goto [/color][color=Black]main[/color]
pe6 worked in win 10 until I installed some versions of basic. Maybe the .bas file extension.
 

stan74

Senior Member
next time or I can vex it I'll post code.I didn't imagine it,happens a lot. It's so dos looking,text n all. Where's the error,find it yourself.
I re-read the manual to see what works in m2 and x2 and think apart from physical size,if you get 1 picaxe,make it a 28x2,it seems to have the best hardware. the start1 m2 option wasn't worth the bother for me and things like // and other "special" functions don't work. 1k ram is nice to play with to and plenty of pins. 2 separate pwm,b.0 and b.5 and servo at the same time but not sure about that,didn't work for me..try again.
 
Top