You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无conio.h库时在KISS控制器上实现实时按键输入的持续运行程序

Real-Time Keystroke Detection Without conio.h/getch() for KISS Controller

Got it, since your KISS controller doesn't support conio.h and getch(), here are a few reliable, lightweight ways to detect keystrokes in real time for your persistent program:

1. Modify Terminal Attributes (Termios Library)

Most embedded Linux/Unix-like controllers support the termios library, which lets you tweak terminal settings to read input without waiting for a newline. This is the most common replacement for getch().

How it works:

We'll switch the terminal from canonical mode (waits for enter) to raw mode (reads characters immediately), then reset it when the program exits to avoid messing up the terminal.

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>

struct termios original_termios;

// Restore terminal settings on exit
void restore_terminal() {
    tcsetattr(STDIN_FILENO, TCSANOW, &original_termios);
}

int main() {
    struct termios new_termios;
    char c;

    // Save original terminal state
    tcgetattr(STDIN_FILENO, &original_termios);
    atexit(restore_terminal); // Ensure we reset on exit

    // Configure raw mode
    new_termios = original_termios;
    new_termios.c_lflag &= ~(ICANON | ECHO); // Disable canonical mode and echo
    new_termios.c_cc[VMIN] = 1; // Wait for at least 1 character
    new_termios.c_cc[VTIME] = 0; // No timeout

    tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);

    // Persistent loop to detect keystrokes
    printf("Press any key (Ctrl+C to exit)...\n");
    while (1) {
        if (read(STDIN_FILENO, &c, 1) == 1) {
            printf("You pressed: %c (ASCII: %d)\n", c, c);
            // Add your custom logic here based on the key pressed
        }
        // Add other background tasks here if needed
    }

    return 0;
}

2. Use select() for Non-Blocking Input

If your program needs to run other tasks while listening for keystrokes, select() lets you check if there's input available on stdin without blocking the entire program.

#include <stdio.h>
#include <sys/select.h>
#include <unistd.h>

int main() {
    fd_set read_fds;
    struct timeval timeout;
    char c;

    printf("Listening for keys (Ctrl+C to exit)...\n");
    while (1) {
        FD_ZERO(&read_fds);
        FD_SET(STDIN_FILENO, &read_fds);

        // Set timeout (adjust as needed; 100ms here)
        timeout.tv_sec = 0;
        timeout.tv_usec = 100000;

        // Check if stdin has data
        int ready = select(STDIN_FILENO + 1, &read_fds, NULL, NULL, &timeout);
        if (ready > 0 && FD_ISSET(STDIN_FILENO, &read_fds)) {
            read(STDIN_FILENO, &c, 1);
            printf("Detected key: %c\n", c);
            // Handle key input here
        }

        // Run your other persistent tasks here (e.g., sensor reads, controller logic)
        // printf("Running background task...\n");
    }

    return 0;
}

3. Direct Hardware GPIO Reading (For Physical Buttons)

If your KISS controller has physical buttons connected directly to GPIO pins, you can skip standard input entirely and read the pin states directly. This is super efficient for embedded systems.

Example Pseudo-Code (Depends on Controller GPIO API):

#include <stdio.h>
#include <gpio.h> // Hypothetical GPIO library for your controller

#define KEY_PIN 17 // Adjust to your button's GPIO pin

int main() {
    // Initialize GPIO pin as input with pull-up/pull-down
    gpio_set_direction(KEY_PIN, GPIO_INPUT);
    gpio_set_pullup(KEY_PIN, GPIO_PULLUP);

    int last_state = 1; // Initial state (pull-up = high)
    printf("Listening for button presses...\n");

    while (1) {
        int current_state = gpio_read(KEY_PIN);
        if (current_state != last_state) {
            if (current_state == 0) { // Button pressed (assuming active low)
                printf("Button pressed!\n");
                // Handle button press logic here
            }
            last_state = current_state;
        }
        // Add delay to avoid bouncing (adjust based on your button)
        usleep(5000);
    }

    return 0;
}

Key Notes:

  • Always restore terminal settings (like in method 1) if you modify them—otherwise, your terminal might behave weirdly after the program exits.
  • For resource-constrained controllers, method 3 is the lightest option, but it only works for physical hardware buttons.
  • If your KISS controller uses a proprietary SDK, check its documentation for built-in input handling functions—many embedded platforms have their own APIs for this.

内容的提问来源于stack exchange,提问作者Lorem Ipsum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:08:21