How can I capture microcontroller input clicks faster?

旧巷老猫 提交于 2020-06-09 05:38:46

问题


I'm currently testing a microcontroller (azure sphere) that captures clicks 1/20th of a second but there are clicks in between that are missed when clicking very fast. I want to capture the clicks at half the time. How can I improve this code to capture each and every click?

Here is part of the code.

const struct timespec sleepTime = { 0, 100000000};

    while (true) {

        GPIO_Value_Type value;
        GPIO_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(fd_storage, &active_led, sizeof(active_led));
            }
        }
        //wouldn't it be more for loops to catch button states faster?

        // Blinking the active LED.
        // reading input only when pressed and turn off other led
        if (active_led == GREEN_LED) {
            GPIO_SetValue(blue_led_fd, GPIO_Value_High);
            GPIO_SetValue(green_led_fd, GPIO_Value_Low);
            nanosleep(&sleepTime, NULL);
            GPIO_SetValue(green_led_fd, GPIO_Value_High);
            nanosleep(&sleepTime, NULL);
        }

        else if (active_led == BLUE_LED) {
            GPIO_SetValue(green_led_fd, GPIO_Value_High);
            GPIO_SetValue(blue_led_fd, GPIO_Value_Low);
            nanosleep(&sleepTime, NULL);
            GPIO_SetValue(blue_led_fd, GPIO_Value_High);
            nanosleep(&sleepTime, NULL);
        }
    }

How can I make the button capture all clicks? Thank you.

来源:https://stackoverflow.com/questions/62140536/how-can-i-capture-microcontroller-input-clicks-faster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!