Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.1 kB
0
Indexable
Never
#include <iostream>
#include <gpiod.h>

int main() {
    // Open GPIO chip
    struct gpiod_chip *chip = gpiod_chip_open("/dev/gpiochip0");
    if (!chip) {
        std::cerr << "Failed to open GPIO chip" << std::endl;
        return 1;
    }

    // Get GPIO line
    struct gpiod_line *line = gpiod_chip_get_line(chip, 17); // Assuming GPIO pin 17
    if (!line) {
        std::cerr << "Failed to get GPIO line" << std::endl;
        gpiod_chip_close(chip);
        return 1;
    }

    // Request line
    if (gpiod_line_request_output(line, "example", 0) < 0) {
        std::cerr << "Failed to request GPIO line" << std::endl;
        gpiod_line_release(line);
        gpiod_chip_close(chip);
        return 1;
    }

    // Toggle the GPIO pin state
    bool value = false;
    while (true) {
        value = !value;
        gpiod_line_set_value(line, value);
        std::cout << "GPIO pin toggled to: " << (value ? "HIGH" : "LOW") << std::endl;
        sleep(1); // Sleep for 1 second
    }

    // Release resources
    gpiod_line_release(line);
    gpiod_chip_close(chip);

    return 0;
}
Leave a Comment