2022-03-03 04: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.
2022-03-07 04: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:
This go, where i need it:
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++;}
2022-03-03 05: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;
}
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.
2022-03-04 09:36 AM
Thx for answer! I want to make my code simpler to understand.
2022-03-04 09:38 AM
Thx for answer! I will try to do it!
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
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.
2022-03-04 12:24 PM
2022-03-04 12:43 PM
He uses a "coma", after functions inside, not dot-coma...
2022-03-04 12:44 PM
Oh, ok ok, I'm going to think how to do it better, =)