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-04 02:00 PM
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,
2022-03-05 10:30 AM
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.
2022-03-05 12:51 PM
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.
2022-03-05 02:47 PM
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
2022-03-05 05:41 PM
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!
2022-03-05 05:47 PM
Ok, corrected:
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
2022-03-05 06:14 PM
Thx!!!!
I´ve copy&paste:
And the compiler trow me this error:
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++;}