[no ci] Sigmastar: update motor_foscam sample

pull/1113/head
Viktor 2023-11-06 03:05:34 +01:00
parent 8b071fe345
commit 8a877a1f4a
1 changed files with 15 additions and 7 deletions

View File

@ -26,14 +26,14 @@ static int sequence[][4] = {
{1, 0, 0, 0}, {1, 0, 0, 0},
}; };
static void write_gpio(int pin, int value) { static int write_gpio(int pin, int value) {
struct gpiohandle_request rq; struct gpiohandle_request rq;
struct gpiohandle_data data; struct gpiohandle_data data;
int fd = open(DEV_NAME, O_RDONLY); int fd = open(DEV_NAME, O_RDONLY);
if (fd < 0) { if (fd < 0) {
printf("Unabled to open chip: %s\n", strerror(errno)); printf("Unabled to open chip: %s\n", strerror(errno));
return; return 1;
} }
rq.lineoffsets[0] = pin; rq.lineoffsets[0] = pin;
@ -42,7 +42,7 @@ static void write_gpio(int pin, int value) {
if (ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq) < 0) { if (ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq) < 0) {
printf("Unable to request gpio: %s\n", strerror(errno)); printf("Unable to request gpio: %s\n", strerror(errno));
return; return 1;
} }
close(fd); close(fd);
@ -50,17 +50,23 @@ static void write_gpio(int pin, int value) {
if (ioctl(rq.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data) < 0) { if (ioctl(rq.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data) < 0) {
printf("Unable to set value: %s\n", strerror(errno)); printf("Unable to set value: %s\n", strerror(errno));
return; return 1;
} }
close(rq.fd); close(rq.fd);
return 0;
} }
static void motor_control(int dir, int step) { static int motor_control(int dir, int step) {
usleep(STEP_TIME); usleep(STEP_TIME);
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
write_gpio(motor_gpio[dir][i], sequence[step][i]); if (write_gpio(motor_gpio[dir][i], sequence[step][i])) {
return 1;
} }
}
return 0;
} }
static void motor_reset(int dir) { static void motor_reset(int dir) {
@ -121,7 +127,9 @@ int main(int argc, char **argv) {
for (int i = c1; i < c2; i++) { for (int i = c1; i < c2; i++) {
for (int j = d1; j < d2; j++) { for (int j = d1; j < d2; j++) {
motor_control(dir, j); if (motor_control(dir, j)) {
return -1;
}
} }
} }