[no ci] Package: add select pin support for gpio-motors (#1866)

pull/1867/head
viktorxda 2025-08-23 20:15:56 +02:00 committed by GitHub
parent 845083969f
commit eb714194ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 167 additions and 140 deletions

View File

@ -7,6 +7,7 @@
int PAN_PINS[4];
int TILT_PINS[4];
int SELECT_PIN = -1;
int STEP_SEQUENCE[8][4] = {
{1, 0, 0, 0}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 0},
@ -25,8 +26,6 @@ void release_gpio(int pin) {
if (file) {
fprintf(file, "%d", 0);
fclose(file);
} else {
printf("Unable to set value of GPIO %d to 0: [%d] %s\n", pin, errno, strerror(errno));
}
file = fopen("/sys/class/gpio/unexport", "w");
@ -43,6 +42,11 @@ void cleanup() {
release_gpio(PAN_PINS[i]);
release_gpio(TILT_PINS[i]);
}
if (SELECT_PIN != -1) {
release_gpio(SELECT_PIN);
}
exit(EXIT_FAILURE);
}
@ -101,31 +105,38 @@ void get_gpio_config() {
exit(EXIT_FAILURE);
}
char line[32];
char line[64];
if (fgets(line, sizeof(line), fp) != NULL) {
char *token = strtok(line, " ");
int index = 0;
int value[8];
int count = 0;
while (token != NULL && index < 8) {
if (index < 4) {
PAN_PINS[index] = atoi(token);
} else {
TILT_PINS[index - 4] = atoi(token);
}
while (token != NULL && count < 8) {
value[count++] = atoi(token);
token = strtok(NULL, " ");
index++;
}
if (index != 8) {
printf("Error: Expected 8 GPIO values, but got %d.\n", index);
if (count == 8) {
for (int i = 0; i < 4; i++) {
PAN_PINS[i] = value[i];
TILT_PINS[i] = value[i + 4];
}
} else if (count == 5) {
for (int i = 0; i < 4; i++) {
PAN_PINS[i] = value[i];
TILT_PINS[i] = value[i];
}
SELECT_PIN = value[4];
} else {
printf("Error: Expected 8 or 5 GPIO values, but got %d\n", count);
exit(EXIT_FAILURE);
}
} else {
printf("Error: Unable to read gpio_motors from fw_printenv.\n");
printf("Error: Unable to read gpio_motors from fw_printenv\n");
exit(EXIT_FAILURE);
}
fclose(fp);
pclose(fp);
}
int main(int argc, char *argv[]) {
@ -145,6 +156,10 @@ int main(int argc, char *argv[]) {
export_gpio(TILT_PINS[i]);
}
if (SELECT_PIN != -1) {
export_gpio(SELECT_PIN);
}
int pan_remaining = abs(pan_steps);
int tilt_remaining = abs(tilt_steps);
int pan_reverse = (pan_steps < 0);
@ -161,6 +176,10 @@ int main(int argc, char *argv[]) {
int eff_delay = (pan_eff > tilt_eff) ? pan_eff : tilt_eff;
if (pan_has) {
if (SELECT_PIN != -1) {
set_gpio(SELECT_PIN, 0);
}
const int (*seq)[4] = pan_reverse ? REVERSE_STEP_SEQUENCE : STEP_SEQUENCE;
for (int k = 0; k < 4; k++) {
set_gpio(PAN_PINS[k], seq[pan_micro][k]);
@ -172,6 +191,10 @@ int main(int argc, char *argv[]) {
}
if (tilt_has) {
if (SELECT_PIN != -1) {
set_gpio(SELECT_PIN, 1);
}
const int (*seq)[4] = tilt_reverse ? REVERSE_STEP_SEQUENCE : STEP_SEQUENCE;
for (int k = 0; k < 4; k++) {
set_gpio(TILT_PINS[k], seq[tilt_micro][k]);
@ -190,5 +213,9 @@ int main(int argc, char *argv[]) {
release_gpio(TILT_PINS[i]);
}
if (SELECT_PIN != -1) {
release_gpio(SELECT_PIN);
}
return 0;
}