making a macro

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello, 

 

i think i have a verry simple question, but i can't find the solution.

 

i have a piece of code, that i have to use maybe 10 times in my program, so i want to use a macro (i think that is what i need)

so that i have to program that piece of code once. and give a name to it: for example "select_counter"

thank in advance.

                                if  button_increase
				counter = counter + 1;
				temp_incr_decr_was_pressed = 1;

				if  button_decrease
				counter = counter - 1;
				temp_incr_decr_was_pressed = 1;

				if (counter > 3)   {counter = 1;}
				if (counter < 1)   {counter = 3;}

				if (counter == 1)         // spray_speed
				{
					Lcd4_Set_Cursor(1,0);
					Lcd4_Write_Char('*');
					Lcd4_Set_Cursor(2,0);
					Lcd4_Write_Char(' ');
					Lcd4_Set_Cursor(1,16); // line 3
					Lcd4_Write_Char(' ');
				}

				if (counter == 2)         // spray_distance
				{
					Lcd4_Set_Cursor(1,0);
					Lcd4_Write_Char(' ');
					Lcd4_Set_Cursor(2,0);
					Lcd4_Write_Char('*');
					Lcd4_Set_Cursor(1,16); // line 3
					Lcd4_Write_Char(' ');
				}

				if (counter == 3)         // spray_offset
				{
					Lcd4_Set_Cursor(1,0);
					Lcd4_Write_Char(' ');
					Lcd4_Set_Cursor(2,0);
					Lcd4_Write_Char(' ');
					Lcd4_Set_Cursor(1,16); // line 3
					Lcd4_Write_Char('*');
				}

 

Last Edited: Sun. Mar 15, 2020 - 09:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

why would you not just use a function? I think you probably need to rethink your code structure.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
why would you not just use a function?
+1

 

If you ever find yourself writing code with virtually identical repeated sequences it's time to parametrise it and put it into a function.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, yes you can do it as a macro, even if it may not be the best choice.

 

#define MACRONAME(a, b, c)            \
    Lcd4_Set_Cursor(1,0);             \
    Lcd4_Write_Char(a);               \
    Lcd4_Set_Cursor(2,0);             \
    Lcd4_Write_Char(b);               \
    Lcd4_Set_Cursor(1,16); // line 3  \
    Lcd4_Write_Char(c);
    
/* to use it: */

				if (counter == 1)         // spray_speed
				{
					MACRONAME('*', ' ', ' ')
				}

				if (counter == 2)         // spray_distance
				{
					MACRONAME(' ', '*', ' ')
				}

				if (counter == 3)         // spray_offset
				{
					MACRONAME(' ', ' ', '*')
				}

 

The trick is that C preprocessor macros must be just one line, so for readability the line continuation '\' is used at the end of each line, except the last one.

I may have made some mistake somewhere, but basically this is it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i thought i needed a macro, i was not sure about that.

if a function is a better idea.........then i need a function.

how can i use that in my case ?

 

for the  clearness, ihave to use the code that i posted in total abbout 10 times in my programm.

Last Edited: Sun. Mar 15, 2020 - 05:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You take one of the blocks and convert into a function e.g.

void helper_function(char letter1, char letter2, char letter3)
				{
					Lcd4_Set_Cursor(1,0);
					Lcd4_Write_Char(letter1);
					Lcd4_Set_Cursor(2,0);
					Lcd4_Write_Char(letter2);
					Lcd4_Set_Cursor(1,16); // line 3
					Lcd4_Write_Char(letter3);
				}

and call with

if (counter == 1)         // spray_speed
{
    helper_function('*', ' ', ' ');
}

My main advice is:  choose intuitive names for functions, arguments, variables, ...

 

David.

 

p.s.  I don't know what type of LCD you have.   But your set_cursor() arguments look very unlikely.

Last Edited: Sun. Mar 15, 2020 - 05:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i check some things, and i think i can do it whit a function.

thanks for your help.