From 2374721c6840f56c07c31abcfddd6bd385d97d8b Mon Sep 17 00:00:00 2001 From: kutayboz Date: Sat, 3 Jun 2023 04:18:33 +0300 Subject: Added GPIO control. Seperated functions into libs --- src/gpio.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/gpio.c (limited to 'src/gpio.c') diff --git a/src/gpio.c b/src/gpio.c new file mode 100644 index 0000000..0ccfd6b --- /dev/null +++ b/src/gpio.c @@ -0,0 +1,44 @@ +#include +#include + +int modemPowerToggle(int pinNum) { + struct gpiod_chip *chip; + struct gpiod_line *line; + + chip = gpiod_chip_open("/dev/gpiochip0"); + if (!chip) { + perror("Error opening GPIO chip"); + return 1; + } + + line = gpiod_chip_get_line( + chip, pinNum); // Replace with the desired GPIO pin number + if (!line) { + perror("Error getting GPIO line"); + gpiod_chip_close(chip); + return 1; + } + + if (gpiod_line_request_output(line, "cellular modem power toggle", 0) < 0) { + perror("Error setting GPIO line as output"); + gpiod_chip_close(chip); + return 1; + } + + if (gpiod_line_set_value(line, 1) < 0) { + perror("Error setting GPIO line high"); + gpiod_chip_close(chip); + return 1; + } + + sleep(1); + + if (gpiod_line_set_value(line, 0) < 0) { + perror("Error setting GPIO line low"); + gpiod_chip_close(chip); + return 1; + } + + gpiod_chip_close(chip); + return 0; +} -- cgit v1.3