簡體   English   中英

如何修改我的 C 代碼以免陷入無限循環?

[英]How to modify my C code in order to not to stay in an infinite loop?

我為我的嵌入式板開發了一個 C 程序。 當我按下並釋放 BUTTON 時,該程序使綠色 LED 亮起。

綠色 LED 在"/sys/class/leds"下定義,BUTTON 在"/dev/input/event0"下。

這是代碼:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>

#include <linux/input.h>

#define BTN_FILE_PATH "/dev/input/event0"
#define LED_PATH "/sys/class/leds"

#define green "green"

void change_led_state(char *led_path, int led_value)
{
    char    lpath[64];
    FILE    *led_fd;

    strncpy(lpath, led_path, sizeof(lpath) - 1);
    lpath[sizeof(lpath) - 1] = '\0';

    led_fd = fopen(lpath, "w");

    if (led_fd == NULL) {
        fprintf(stderr, "simplekey: unable to access led\n");
        return;
    }

    fprintf(led_fd, "%d\n", led_value);

    fclose(led_fd);
}

void reset_leds(void)
{

    change_led_state(LED_PATH "/" green "/brightness", 0);
}

int configure_leds(void)
{
    FILE    *l_fd;
    FILE    *r_fd;
    char    *none_str = "none";

    /* Configure leds for hand control */

    r_fd = fopen(LED_PATH "/" green "/trigger", "w");




    fprintf(r_fd, "%s\n", none_str);


    fclose(r_fd);


    /* Switch off leds */
    reset_leds();

    return 0;
}

void eval_keycode(int code)
{

    static int green_state = 0;

    switch (code) {
    case 260:
        printf("BTN pressed\n");

        /* figure out green state */

        green_state = green_state ? 0 : 1;

        change_led_state(LED_PATH "/" green "/brightness", green_state);
        break;
    }
}


int main(void)
{
    int file;
    /* how many bytes were read */
    size_t  rb;
    int ret;
    int yalv;
    /* the events (up to 64 at once) */
    struct input_event  ev[64];
    char    *str = BTN_FILE_PATH;

    printf("Starting simplekey app\n");

    ret = configure_leds();
    if (ret < 0)
        exit(1);

    printf("File Path: %s\n", str);

    if((file = open(str, O_RDONLY)) < 0) {
        perror("simplekey: File can not open");
        exit(1);
    }

    for (;;) {
        /* Blocking read */
        rb= read(file, &ev, sizeof(ev));


        for (yalv = 0;
            yalv < (int) (rb / sizeof(struct input_event));
            yalv++) {
            if (ev[yalv].type == EV_KEY) {

                /* Change state on button pressed */
                if (ev[yalv].value == 0)
                    eval_keycode(ev[yalv].code);
            }
        }
    }

    close(file);
    reset_leds();
    exit(0);
}

編譯進展順利。

當我執行代碼時,它向我顯示:

Starting simplekey app
File Path: /dev/input/event0

當我按下按鈕時沒有任何反應,當我釋放它時,LED 會更改 state 並在終端中向我顯示:

BTN pressed

問題是代碼繼續執行,直到我按CTRL+C退出。

我只是希望它等到事件(按 BUTTON)發生然后更改 LED state 並最終自動退出。

我的問題是如何為此目的修改程序? 我考慮過使用線程和信號,但我對它們一無所知。 謝謝!

我假設您在符合 SVr4 或 4.3BSD ot POSIX.1-2001(或更高版本)的系統下運行所有這些。

您缺少對read()返回值的檢查,該值不是size_t而是ssize_t (即已簽名)。

然后可以像這樣更改您的代碼:

/* ... */
    ssize_t  rb; /* !!! */
/* ... */
for (;;) {
        /* Blocking read */
        rb= read(file, &ev, sizeof(ev));
        if (rb <= 0) /* Check for the EOF */
          break;

        for (yalv = 0;
            yalv < (int) (rb / sizeof(struct input_event));
            yalv++) {
            if (ev[yalv].type == EV_KEY) {

                /* Change state on button pressed */
                if (ev[yalv].value == 0)
                    eval_keycode(ev[yalv].code);
            }
        }
    }

請參閱友好的手冊頁

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM