I have a function that toggles a flag everytime when it is called, which is done repeatedly, to have a alternating behaviour everytime the function is called. But what if i call the function multiple times, with different parameters? Of course my toggle-flag should only be toggled by the same function call. Is there a way to detect have kind of a static behaviour of the flag, but one behaviour for each function caller?
void showText(uint8_t col, uint8_t line, const char *text, bool blinking)
{
if(blinking)
{
static bool flag = false;
if(flag)
{
lcd_setcursor(col, line);
for(int i = 0; i < strlen(text); i )
{
lcd_data(' ');
}
}
else
{
lcd_setcursor(col, line);
lcd_string(text);
}
flag = !flag;
}
else
{
lcd_setcursor(col, line);
lcd_string(text);
}
}
CodePudding user response:
void showText( uint8_t col, uint8_t line, const char *text, bool blinking, bool* flag_ptr ) {
...
if ( *flag_ptr ) { ... } else { ... }
*flag_ptr = !*flag_ptr;
...
}
Call site A:
static bool flag = false;
showText( ..., &flag );
Call site B:
static bool flag = false;
showText( ..., &flag );
CodePudding user response:
You could also have the flag pointer be the decision for blinking itself:
static bool blinkstat1 = false;
static bool blinkstat2 = false;
// Show with blinking
showText(col1, line1, text1, &blinkstat1);
// Show with blinking
showText(col2, line2, text2, &blinkstat2);
// Show text1 again w/o blinking
showText(col1, line1, text1, NULL);
void showText(uint8_t col, uint8_t line, const char *text, bool* blinking ) {
lcd_setcursor(col, line);
if (blinking != NULL) {
// Blink
if ( *blinking ) {
for(int i = 0; i < strlen(text); i ) {
lcd_data(' ');
}
} else {
lcd_string(text);
}
*blinking = !*blinking;
} else {
// Just show text non-blinking
lcd_string(text);
}
}