-1

我想弄清楚为什么我的 LED 灯不闪烁。我有另一个名为 Wakeablewait 的函数,它应该在 LED 开启或关闭的情况下计算大约 1 秒。如果在计时器爆发之前单击按钮,它将返回 false(尽管在更进一步的时间点它将切换到另一个功能)。警告指示灯控制颜色输出。不知道我错过了什么。在日志语句中,它显示我一遍又一遍地输入这两个功能,但 LED 灯只是保持黄色(顺便说一下,我使用两个 LED 来制作黄色)。有人知道我错过了什么吗?

按下的按钮在微控制器上等于 0,否则为 1。

//This time spec is in nano seconds but equates to 10ms
const struct timespec sleepTime = { 0, 10000000 };

GPIO_Value_Type previous_button_value = BUTTON_UNPRESSED;

bool WakeableWait(int btnA, int milliseconds) {
    Log_Debug("In Wake\n");

    while (milliseconds > 0) {
        // Return true if full time elapsed, false if button was pressed
        GPIO_Value_Type value;
        GPIO_GetValue(btnA, &value);

        bool button_got_pressed = (previous_button_value == BUTTON_UNPRESSED) && (value == BUTTON_PRESSED);
        previous_button_value = value;

        if (button_got_pressed) {
            return false;
        }

        nanosleep(&sleepTime, NULL);
        milliseconds -= 10;

        return true;
    }

}
// This function should blink Yellow until button A is pressed to switch to typical working traffic light
void caution_light(int y1, int y2, int btnA) {
    //// This should blink on and off for one second
    while (true) {
        // Turn on Yellow
        GPIO_SetValue(y1, GPIO_Value_Low);
        GPIO_SetValue(y2, GPIO_Value_Low);

        // Switch to other function to wait time
        Log_Debug("Turning On\n");
        WakeableWait(btnA,1000);
 
        //Turn Led off
        GPIO_SetValue(y1, GPIO_Value_High);
        GPIO_SetValue(y2, GPIO_Value_High);

        // Leave to other function 
        Log_Debug("Turning Off\n");
        WakeableWait(btnA, 1000);
    }
}

Visual Studio 中的输出图像:

视觉工作室中的输出

谢谢你。

4

1 回答 1

2

WakeableWait不循环。无论如何,它只进行一次迭代,并立即返回:该return true;语句是循环体的一部分。您的 LED 可能会以 100 Hz(每 10 毫秒)闪烁,而您的眼睛无法察觉(您应该看到的是 LED 比正常情况更暗)。解决方案是return true;退出循环。

WakeableWait(....)
{
    while (milliseconds > 0) {
        ....
    }
    return true;
}
于 2020-06-28T23:49:32.100 回答