cancel
Showing results for 
Search instead for 
Did you mean: 

How to made a function to call other functions?

angeliran
Senior

Hi to all!!!

I do repeteadly this function:

switch(segundero_d)

{

case 0: numero_0(); break;

case 1: numero_1(); break;

case 2: numero_2(); break;

case 3: numero_3(); break;

case 4: numero_4(); break;

case 5: numero_5(); break;

case 6: numero_6(); break;

case 7: numero_7(); break;

case 8: numero_8(); break;

case 9: numero_9(); break;

}

switch(segundero)

{

case 0: numero_0(); break;

case 1: numero_1(); break;

case 2: numero_2(); break;

case 3: numero_3(); break;

case 4: numero_4(); break;

case 5: numero_5(); break;

case 6: numero_6(); break;

case 7: numero_7(); break;

case 8: numero_8(); break;

case 9: numero_9(); break;

}

...

where the function numero_x are:

void numero_0(void)

{l=m;

 fb[l][n]=0x3E; //b"00111110";

fb[l+1][n]=0x22; //b"00100010";

fb[l+2][n]=0x22; //b"00100010";

fb[l+3][n]=0x22; //b"00100010";

fb[l+4][n]=0x3E; //b"00111110";

n++;}

void numero_1(void)

{l=m;

 fb[l][n]=0x08; //b"00001000";

fb[l+1][n]=0x18; //b"00011000";

fb[l+2][n]=0x08; //b"00001000";

fb[l+3][n]=0x08; //b"00001000";

fb[l+4][n]=0x3E; //b"00111110";

n++;}

...

And i want to do this, with a function, to call the functions numero_x, to reduce my code, can somebody help me?

Thanx in advice!!!

EDIT: I added a pic, to show, that my code works, but my objetive, is to pulish the code, =)

17 REPLIES 17
hwa
Associate II

It seems that all you're trying to do is copy a few literal values to one column of the 2-d array fb[], where the particular literal values you want copied depend on the value of an int (secundo or secondo_d). So my suggestion doesn't answer your question literally, since you don't need the switch statements or all the almost identical functions to do this.

Instead, just define a table of your literal values, and one function to copy the selected values to the selected column of your array fb[].

#define NUM_VALUES 5
 
uint8_t value_table[][NUM_VALUES] {
  { 0x3E, 0x22, 0x22, 0x22, 0x3E }, // your values from function_0
  { 0x08, 0x18, 0x08, 0x08, 0x3E }, // your values from function_1
 ... etc.
}
 
void numero_x(int select) 
{
   l = m;
 
   for (int i = 0; i < NUM_VALUES; i++) {
     fb[l+i][n] = values_table[select][i];
   }
   n++;
}
 

So now you just call numero_x() as often as you like, with the value you would have used in the switch statements.

Also, I'd think about your use of globals (l, m, n), and whether incrementing n is really best done inside your function. Hope this helps.

Cheers,

Thx for the answer! fb it´s a matriz: fb[ ][ ], and i put variables (letters and numbers), to generate a vga signal, by the spi[ ], and the corresponding syncs.

So, i use switch function, to chose, if i need to show, 0 or 1 or 2... or 9, write to fb[ ][ ], and, when it´s time to draw, read from fb[ ] [ ], to spi tx.

Ok, but that's exactly what my numero_x() function does for you. Instead of

switch (secundero) ...

just call

numero_x(secundero);

All you need to do is fill in the rest of value_table[][]. You don't need any switch statements or a bunch of almost identical functions, and your code would not only be more compact, but more efficient also.

That sounds good!!!

I put this:

#define NUM_VALUES 5;

uint8_t values_table[10][NUM_VALUES]=

{

//numero_0

{

0x3E, //b"00111110"

0x22, //b"00100010"

0x22, //b"00100010"

0x22, //b"00100010"

0x3E, //b"00111110"

},

 

//numero_1

{

0x08, //b"00001000"

0x18, //b"00011000"

0x08, //b"00001000"

0x08, //b"00001000"

0x3E, //b"00111110"

},

 

//numero_2

{

0x3E, //b"00111110"

0x02, //b"00000010"

0x3E, //b"00111110"

0x20, //b"00100000"

0x3E, //b"00111110"

},

 

//numero_3

{

0x3E, //b"00111110"

0x02, //b"00000010"

0x3E, //b"00111110"

0x02, //b"00000010"

0x3E, //b"00111110"

},

 

//numero_4

{

0x22, //b"00100010"

0x22, //b"00100010"

0x3E, //b"00111110"

0x02, //b"00000010"

0x02, //b"00000010"

},

//numero_5

{

0x3E, //b"00111110"

0x20, //b"00100000"

0x3E, //b"00111110"

0x02, //b"00000010"

0x3E, //b"00111110"

},

 

//numero_6

{

0x3E, //b"00111110"

0x20, //b"00100000"

0x3E, //b"00111110"

0x22, //b"00100010"

0x3E, //b"00111110"

},

 

//numero_7

{

0x3E, //b"00111110"

0x02, //b"00000010"

0x02, //b"00000010"

0x02, //b"00000010"

0x02, //b"00000010"

},

 

//numero_8

{

0x3E, //b"00111110"

0x22, //b"00100010"

0x3E, //b"00111110"

0x22, //b"00100010"

0x3E, //b"00111110"

},

 

//numero_9

{

0x3E, //b"00111110"

0x22, //b"00100010"

0x3E, //b"00111110"

0x02, //b"00000010"

0x02, //b"00000010"

}

};

void numero_x(int select) 

{

  l = m;

  for (int i = 0; i < NUM_VALUES; i++) {

   fb[l+i][n] = values_table[select][i];

  }

  n++;

}

But i get these errors:

../Src/main.c(766): error: #17: expected a "]"

 uint8_t values_table[10][NUM_VALUES]=

../Src/main.c(766): error: #169: expected a declaration

 uint8_t values_table[10][NUM_VALUES]=

../Src/main.c(859): warning: #12-D: parsing restarts here after previous syntax error

 };

../Src/main.c(865): error: #29: expected an expression

   for (int i = 0; i < NUM_VALUES; i++) {

../Src/main.c: 1 warning, 3 errors

hwa
Associate II

Look carefully at your line where you define NUM_VALUES, and compare it to the line in my example code. See the difference?

I'm guessing you're a beginner, and learning to program by yourself (not taking any course). That's fine, everybody starts out a beginner. I'd suggest if you want to make faster progress that you get a book on introduction to programming in C, and study it very carefully, and do lots of the included exercises. It's really hard to write working programs before you have a solid understanding of the basics.

Good luck!

Ok, corrected:

  1. uint8_t values_table[10][5];//MATRIZ 50
  2.  
  3. uint8_t values_table[][5]={
  4. //numero_0
  5. {
  6. 0x3E, //b"00111110"
  7. 0x22, //b"00100010"
  8. 0x22, //b"00100010"
  9. 0x22, //b"00100010"
  10. 0x3E, //b"00111110"
  11. },
  12.  
  13. //numero_1
  14. {
  15. 0x08, //b"00001000"
  16. 0x18, //b"00011000"
  17. 0x08, //b"00001000"
  18. 0x08, //b"00001000"
  19. 0x3E, //b"00111110"
  20. },
  21.  
  22. //numero_2
  23. {
  24. 0x3E, //b"00111110"
  25. 0x02, //b"00000010"
  26. 0x3E, //b"00111110"
  27. 0x20, //b"00100000"
  28. 0x3E, //b"00111110"
  29. },
  30.  
  31. //numero_3
  32. {
  33. 0x3E, //b"00111110"
  34. 0x02, //b"00000010"
  35. 0x3E, //b"00111110"
  36. 0x02, //b"00000010"
  37. 0x3E, //b"00111110"
  38. },
  39.  
  40. //numero_4
  41. {
  42. 0x22, //b"00100010"
  43. 0x22, //b"00100010"
  44. 0x3E, //b"00111110"
  45. 0x02, //b"00000010"
  46. 0x02, //b"00000010"
  47. },
  48. //numero_5
  49. {
  50. 0x3E, //b"00111110"
  51. 0x20, //b"00100000"
  52. 0x3E, //b"00111110"
  53. 0x02, //b"00000010"
  54. 0x3E, //b"00111110"
  55. },
  56.  
  57. //numero_6
  58. {
  59. 0x3E, //b"00111110"
  60. 0x20, //b"00100000"
  61. 0x3E, //b"00111110"
  62. 0x22, //b"00100010"
  63. 0x3E, //b"00111110"
  64. },
  65.  
  66. //numero_7
  67. {
  68. 0x3E, //b"00111110"
  69. 0x02, //b"00000010"
  70. 0x02, //b"00000010"
  71. 0x02, //b"00000010"
  72. 0x02, //b"00000010"
  73. },
  74.  
  75. //numero_8
  76. {
  77. 0x3E, //b"00111110"
  78. 0x22, //b"00100010"
  79. 0x3E, //b"00111110"
  80. 0x22, //b"00100010"
  81. 0x3E, //b"00111110"
  82. },
  83.  
  84. //numero_9
  85. {
  86. 0x3E, //b"00111110"
  87. 0x22, //b"00100010"
  88. 0x3E, //b"00111110"
  89. 0x02, //b"00000010"
  90. 0x02, //b"00000010"
  91. }
  92.  
  93.  
  94. };
  95.  
  96. void numero_x(uint8_t select) 
  97. {
  98.   l = m;
  99.  
  100.   for (uint8_t i = 0; i < 5; i++) {
  101.    fb[l+i][n] = values_table[select][i];
  102.   }
  103.   n++;
  104. }

And keil give me this error:

linking...

test\test.axf: Error: L6406E: No space in execution regions with .ANY selector matching stm32f0xx_hal_flash.o(.bss).

test\test.axf: Error: L6406E: No space in execution regions with .ANY selector matching stm32f0xx_hal.o(.data).

test\test.axf: Error: L6407E: Sections of aggregate size 0x2c bytes could not fit into .ANY selector(s).

Not enough information to list image symbols.

Not enough information to list the image map.

Finished: 2 information, 0 warning and 3 error messages.

"test\test.axf" - 3 Error(s), 0 Warning(s).

Target not created

Thx!!!!

I´ve copy&paste:

  1. #define NUM_VALUES 5
  2.  
  3. uint8_t value_table[][NUM_VALUES] {
  4.  { 0x3E, 0x22, 0x22, 0x22, 0x3E }, // your values from function_0
  5.  { 0x08, 0x18, 0x08, 0x08, 0x3E }, // your values from function_1
  6.  { 0x3E, 0x22, 0x22, 0x22, 0x3E }, // your values from function_2
  7.  { 0x08, 0x18, 0x08, 0x08, 0x3E }, // your values from function_3
  8.  { 0x3E, 0x22, 0x22, 0x22, 0x3E }, // your values from function_4
  9.  
  10. }

And the compiler trow me this error:

  1. compiling main.c...
  2. ../Src/main.c(742): error: #65: expected a ";"
  3.  uint8_t value_table[][NUM_VALUES] {
  4. ../Src/main.c(969): warning: #12-D: parsing restarts here after previous syntax error
  5.             (quita_pon=0);
  6. ../Src/main.c(970): error: #169: expected a declaration
  7.             }

angeliran
Senior

In advance, I thank @hwa​ , @Bob S​ , @TDK​ , @S.Ma​  , for dedicating their valuable time, in answering something, which perhaps, from another perspective, was something simple.

Regardless, if I am an electronics engineer, and a couple of years ago, I actually took programming courses, or as @hwa​  comments, self-learning.

I would like to share with you the answer to my question, through which was the solution in the end:

  1. uint8_t tabla_de_valores[10][5];//MATRIZ 50, yes, keil, my compiler, only accept this way
  2.  
  3. uint8_t tabla_de_valores[][5]={
  4. //numero_0
  5. {
  6. 0x3E, //b"00111110"
  7. 0x22, //b"00100010"
  8. 0x22, //b"00100010"
  9. 0x22, //b"00100010"
  10. 0x3E, //b"00111110"
  11. },
  12.  
  13. //numero_1
  14. {
  15. 0x08, //b"00001000"
  16. 0x18, //b"00011000"
  17. 0x08, //b"00001000"
  18. 0x08, //b"00001000"
  19. 0x3E, //b"00111110"
  20. },
  21.  
  22. //numero_2
  23. {
  24. 0x3E, //b"00111110"
  25. 0x02, //b"00000010"
  26. 0x3E, //b"00111110"
  27. 0x20, //b"00100000"
  28. 0x3E, //b"00111110"
  29. },
  30.  
  31. //numero_3
  32. {
  33. 0x3E, //b"00111110"
  34. 0x02, //b"00000010"
  35. 0x3E, //b"00111110"
  36. 0x02, //b"00000010"
  37. 0x3E, //b"00111110"
  38. },
  39.  
  40. //numero_4
  41. {
  42. 0x22, //b"00100010"
  43. 0x22, //b"00100010"
  44. 0x3E, //b"00111110"
  45. 0x02, //b"00000010"
  46. 0x02, //b"00000010"
  47. },
  48. //numero_5
  49. {
  50. 0x3E, //b"00111110"
  51. 0x20, //b"00100000"
  52. 0x3E, //b"00111110"
  53. 0x02, //b"00000010"
  54. 0x3E, //b"00111110"
  55. },
  56.  
  57. //numero_6
  58. {
  59. 0x3E, //b"00111110"
  60. 0x20, //b"00100000"
  61. 0x3E, //b"00111110"
  62. 0x22, //b"00100010"
  63. 0x3E, //b"00111110"
  64. },
  65.  
  66. //numero_7
  67. {
  68. 0x3E, //b"00111110"
  69. 0x02, //b"00000010"
  70. 0x02, //b"00000010"
  71. 0x02, //b"00000010"
  72. 0x02, //b"00000010"
  73. },
  74.  
  75. //numero_8
  76. {
  77. 0x3E, //b"0011 1110"
  78. 0x22, //b"0010 0010"
  79. 0x3E, //b"0011 1110"
  80. 0x22, //b"0010 0010"
  81. 0x3E, //b"0011 1110"
  82. },
  83.  
  84. //numero_9
  85. {
  86. 0x3E, //b"0011 1110"
  87. 0x22, //b"0010 0010"
  88. 0x3E, //b"0011 1110"
  89. 0x02, //b"0000 0010"
  90. 0x02, //b"0000 0010"
  91. },
  92.  
  93.  
  94. };
  95.  
  96.  
  97. void numero_x(uint8_t select) 
  98. {
  99.   l = m;
  100.  
  101.   for (uint8_t interno = 0; interno < 5; interno++) {
  102.    fb[l+interno][n] = tabla_de_valores[select][interno];
  103.   }
  104.   n++;
  105. }

This go, where i need it:

  1. numero_x(horatero_d);
  2. numero_x(horatero);
  3. letra_dos_puntos(); // 15
  4. numero_x(minutero_d);
  5. numero_x(minutero);
  6. letra_dos_puntos();
  7. numero_x(segundero_d);
  8. numero_x(segundero); // 20

I hope it works for someone =)

P.D.: The next step, it´s to made with letters:

void letra_A(void){

l=m;

 fb[l][n]=0x1C; //b"00011100";

fb[l+1][n]=0x22; //b"00100010";

fb[l+2][n]=0x3E; //b"00111110";

fb[l+3][n]=0x22; //b"00100010";

fb[l+4][n]=0x22; //b"00100010";

n++;}

void letra_B(void)

{l=m;

 fb[l][n]=0x3C; //b"00111100";

fb[l+1][n]=0x22; //b"00100010";

fb[l+2][n]=0x3C; //b"00111100";

fb[l+3][n]=0x22; //b"00100010";

fb[l+4][n]=0x3C; //b"00111100";

n++;}

void letra_C(void)

{l=m;

 fb[l][n]=0x1E; //b"00011110";

fb[l+1][n]=0x20; //b"00100000";

fb[l+2][n]=0x20; //b"00100000";

fb[l+3][n]=0x20; //b"00100000";

fb[l+4][n]=0x1E; //b"00011110";

n++;}