我有一个函数,每当一个标志被调用时,它都会切换,这是反复进行的,每次调用该函数时,它都有一个交替的行为。但是如果我用不同的参数多次调用这个函数呢?当然,我的切换标志只应该由相同的函数调用切换。是否有一种方法来检测,有某种静态行为的标志,但为每个函数调用者的一种行为?
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);
}
}发布于 2022-08-23 22:02:04
void showText( uint8_t col, uint8_t line, const char *text, bool blinking, bool* flag_ptr ) {
...
if ( *flag_ptr ) { ... } else { ... }
*flag_ptr = !*flag_ptr;
...
}呼叫站点A:
static bool flag = false;
showText( ..., &flag );呼叫B站点:
static bool flag = false;
showText( ..., &flag );发布于 2022-08-23 23:59:35
您还可以让标志指针决定是否闪烁:
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);
}
}https://stackoverflow.com/questions/73464803
复制相似问题