How to made a function to call other functions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-03 4:48 PM
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, =)
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-07 4:07 PM
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:
- uint8_t tabla_de_valores[10][5];//MATRIZ 50, yes, keil, my compiler, only accept this way
- uint8_t tabla_de_valores[][5]={
- //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"0011 1110"
- 0x22, //b"0010 0010"
- 0x3E, //b"0011 1110"
- 0x22, //b"0010 0010"
- 0x3E, //b"0011 1110"
- },
- //numero_9
- {
- 0x3E, //b"0011 1110"
- 0x22, //b"0010 0010"
- 0x3E, //b"0011 1110"
- 0x02, //b"0000 0010"
- 0x02, //b"0000 0010"
- },
- };
- void numero_x(uint8_t select)
- {
- l = m;
- for (uint8_t interno = 0; interno < 5; interno++) {
- fb[l+interno][n] = tabla_de_valores[select][interno];
- }
- n++;
- }
This go, where i need it:
- numero_x(horatero_d);
- numero_x(horatero);
- letra_dos_puntos(); // 15
- numero_x(minutero_d);
- numero_x(minutero);
- letra_dos_puntos();
- numero_x(segundero_d);
- 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++;}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-03 5:41 PM
> And i want to do this, with a function, to call the functions numero_x,
Make an array of function pointers and put them in there.
void Function1() {
printf("Function1\n");
}
void Function2() {
printf("Function2\n");
}
void Function3() {
printf("Function3\n");
}
typedef void (*VoidFunction)();
VoidFunction function[] = {
Function1,
Function2,
Function3,
};
void call_function(int i) {
function[i]();
}
int main()
{
call_function(0);
call_function(1);
call_function(2);
return 0;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-03 10:43 PM
Usually compiler in optimisation mode will replace switch case from an index with function array for you in assembly. What most compilers wont guess for you is create data tables. You could create like string arrays for exemple as a starting point.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-04 9:36 AM
Thx for answer! I want to make my code simpler to understand.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-04 9:38 AM
Thx for answer! I will try to do it!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-04 10:25 AM
Ok, i´ve tried:
int temporal=0;
typedef void (*escoger_numero_vector)();
escoger_numero_vector escoger_numero [int temporal] =
{
switch(temporal)
{
case 0: numero_0; break, //numero_x are functions, where x is numbers from 0 to 9
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;
}
};
But keil give to me this errors:
../Src/main.c(781): error: #29: expected an expression
escoger_numero_vector escoger_numero [int temporal] =
../Src/main.c(784): error: #29: expected an expression
switch(temporal)
../Src/main.c(796): warning: #12-D: parsing restarts here after previous syntax error
},
../Src/main.c: 1 warning, 2 errors
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-04 12:21 PM
Go back and look at @TDK​ 's example code, specificall lines 13-17. You are defining an array or function pointers, not a function with a "switch" statement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-04 12:24 PM
Adding a switch statement inside of a variable definition is nonsense.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-04 12:43 PM
He uses a "coma", after functions inside, not dot-coma...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-03-04 12:44 PM
Oh, ok ok, I'm going to think how to do it better, =)
