mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-07-13 17:02:06 +08:00
502cc665e0
* pigeon abstraction layer * Fix string literals * more generic pigeon class * add TTYpigon * nicer tty error handling * close tty fd on pigeon delete * pigeon receive return std::string * use sizeof * max receive size to prevent infinite loop * remove namespace * add unistd include for usleep * fix is pigeon * Handle tty error in opening * fix printing binary strings with dump.py * fix pigeon build on macos * Handle errors seperately Co-authored-by: Comma Device <device@comma.ai>
76 lines
1.4 KiB
C++
76 lines
1.4 KiB
C++
#include "gpio.h"
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
|
|
// We assume that all pins have already been exported on boot,
|
|
// and that we have permission to write to them.
|
|
|
|
int gpio_init(int pin_nr, bool output){
|
|
int ret = 0;
|
|
int fd, tmp;
|
|
|
|
char pin_dir_path[50];
|
|
int pin_dir_path_len = snprintf(pin_dir_path, sizeof(pin_dir_path),
|
|
"/sys/class/gpio/gpio%d/direction", pin_nr);
|
|
if(pin_dir_path_len <= 0){
|
|
ret = -1;
|
|
goto cleanup;
|
|
}
|
|
|
|
fd = open(pin_dir_path, O_WRONLY);
|
|
if(fd == -1){
|
|
ret = -1;
|
|
goto cleanup;
|
|
}
|
|
if(output){
|
|
tmp = write(fd, "out", 3);
|
|
if(tmp != 3){
|
|
ret = -1;
|
|
goto cleanup;
|
|
}
|
|
} else {
|
|
tmp = write(fd, "in", 2);
|
|
if(tmp != 2){
|
|
ret = -1;
|
|
goto cleanup;
|
|
}
|
|
}
|
|
|
|
cleanup:
|
|
if(fd >= 0){
|
|
close(fd);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int gpio_set(int pin_nr, bool high){
|
|
int ret = 0;
|
|
int fd, tmp;
|
|
|
|
char pin_val_path[50];
|
|
int pin_val_path_len = snprintf(pin_val_path, sizeof(pin_val_path),
|
|
"/sys/class/gpio/gpio%d/value", pin_nr);
|
|
if(pin_val_path_len <= 0){
|
|
ret = -1;
|
|
goto cleanup;
|
|
}
|
|
|
|
fd = open(pin_val_path, O_WRONLY);
|
|
if(fd == -1){
|
|
ret = -1;
|
|
goto cleanup;
|
|
}
|
|
tmp = write(fd, high ? "1" : "0", 1);
|
|
if(tmp != 1){
|
|
ret = -1;
|
|
goto cleanup;
|
|
}
|
|
|
|
cleanup:
|
|
if(fd >= 0){
|
|
close(fd);
|
|
}
|
|
return ret;
|
|
}
|