我怎样才能使微控制器按钮上的 while 循环更快?
How can I make this while loop faster on a microcontroller button?
我正在使用带有按钮 A 的微控制器。当我按下此按钮并按住它 2 秒时,它的值变为 0,颜色变为蓝色或绿色,当我松开时,它的值变为回到 1 但颜色保持不变,除非再次单击它并且颜色发生变化。问题是,更换 LED 灯不应该花费 2 秒。我该怎么做才能更快地读取值(0 或 1)?
这是 while 循环中的代码片段。
// here are the states for reference. Everything is either a 0 or a 1
const int BUTTON_PRESSED = 0;
const int BUTTON_UNPRESSED = 1;
const int GREEN_LED = 0;
const int BLUE_LED = 1;
const struct timespec sleepTime = { 1, 0 };
while (true) {
Value_Type value;
// this function get the input of button a when pressed
GetValue(button_A_fd, &value);
Log_Debug(
"Button value (%d)\n", value);
// Processing the button.
//Turns LED ON; Button not pressed down
if (value == BUTTON_UNPRESSED) {
last_button_state = BUTTON_UNPRESSED;
} else {
// if last button state is 1 then now it is being pressed
if (last_button_state == BUTTON_UNPRESSED) {
// Flip LEDs
if (active_led == BLUE_LED) {
active_led = GREEN_LED;
}
else if (active_led == GREEN_LED) {
active_led = BLUE_LED;
}
last_button_state = BUTTON_PRESSED;
// sets the pointer to the 0 bit of the file to write
lseek(fd_storage, 0, SEEK_SET);
// write current active led to mutable storage and save
write(fd_storage, &active_led, sizeof(active_led));
}
}
// Blinking the active LED.
// reading input only when pressed and turn off other led
if (active_led == GREEN_LED) {
// turn off blue led, then turn on green
SetValue(blue_led_fd, Value_High);
SetValue(green_led_fd, Value_Low);
nanosleep(&sleepTime, NULL);
SetValue(green_led_fd, Value_High);
nanosleep(&sleepTime, NULL);
}
else if (active_led == BLUE_LED) {
// turn off green led, then turn on blue
SetValue(green_led_fd, Value_High);
SetValue(blue_led_fd, Value_Low);
nanosleep(&sleepTime, NULL);
SetValue(blue_led_fd, Value_High);
nanosleep(&sleepTime, NULL);
}
}
}
我尝试将 GetValue() 放在代码的几个部分中,看看它是否可以更快地获取值,但它没有用。我怎样才能离开这里?我希望我分享了足够的代码来理解这个问题。谢谢。
我正在使用带有按钮 A 的微控制器。当我按下此按钮并按住它 2 秒时,它的值变为 0,颜色变为蓝色或绿色,当我松开时,它的值变为回到 1 但颜色保持不变,除非再次单击它并且颜色发生变化。问题是,更换 LED 灯不应该花费 2 秒。我该怎么做才能更快地读取值(0 或 1)?
这是 while 循环中的代码片段。
// here are the states for reference. Everything is either a 0 or a 1
const int BUTTON_PRESSED = 0;
const int BUTTON_UNPRESSED = 1;
const int GREEN_LED = 0;
const int BLUE_LED = 1;
const struct timespec sleepTime = { 1, 0 };
while (true) {
Value_Type value;
// this function get the input of button a when pressed
GetValue(button_A_fd, &value);
Log_Debug(
"Button value (%d)\n", value);
// Processing the button.
//Turns LED ON; Button not pressed down
if (value == BUTTON_UNPRESSED) {
last_button_state = BUTTON_UNPRESSED;
} else {
// if last button state is 1 then now it is being pressed
if (last_button_state == BUTTON_UNPRESSED) {
// Flip LEDs
if (active_led == BLUE_LED) {
active_led = GREEN_LED;
}
else if (active_led == GREEN_LED) {
active_led = BLUE_LED;
}
last_button_state = BUTTON_PRESSED;
// sets the pointer to the 0 bit of the file to write
lseek(fd_storage, 0, SEEK_SET);
// write current active led to mutable storage and save
write(fd_storage, &active_led, sizeof(active_led));
}
}
// Blinking the active LED.
// reading input only when pressed and turn off other led
if (active_led == GREEN_LED) {
// turn off blue led, then turn on green
SetValue(blue_led_fd, Value_High);
SetValue(green_led_fd, Value_Low);
nanosleep(&sleepTime, NULL);
SetValue(green_led_fd, Value_High);
nanosleep(&sleepTime, NULL);
}
else if (active_led == BLUE_LED) {
// turn off green led, then turn on blue
SetValue(green_led_fd, Value_High);
SetValue(blue_led_fd, Value_Low);
nanosleep(&sleepTime, NULL);
SetValue(blue_led_fd, Value_High);
nanosleep(&sleepTime, NULL);
}
}
}
我尝试将 GetValue() 放在代码的几个部分中,看看它是否可以更快地获取值,但它没有用。我怎样才能离开这里?我希望我分享了足够的代码来理解这个问题。谢谢。