From 8b071fe3452f59597422f5cf5299b522502803d8 Mon Sep 17 00:00:00 2001
From: viktorxda <35473052+viktorxda@users.noreply.github.com>
Date: Mon, 6 Nov 2023 02:38:50 +0100
Subject: [PATCH] [no ci] Sigmastar: add motors sample package (#1111)

---
 .../configs/ssc337de_ultimate_defconfig       |   1 +
 general/package/Config.in                     |   1 +
 general/package/sigmastar-motors/Config.in    |   4 +
 .../sigmastar-motors/sigmastar-motors.mk      |  19 +++
 general/package/sigmastar-motors/src/Makefile |   3 +
 .../sigmastar-motors/src/motor_foscam.c       | 131 ++++++++++++++++++
 6 files changed, 159 insertions(+)
 create mode 100644 general/package/sigmastar-motors/Config.in
 create mode 100644 general/package/sigmastar-motors/sigmastar-motors.mk
 create mode 100644 general/package/sigmastar-motors/src/Makefile
 create mode 100644 general/package/sigmastar-motors/src/motor_foscam.c

diff --git a/br-ext-chip-sigmastar/configs/ssc337de_ultimate_defconfig b/br-ext-chip-sigmastar/configs/ssc337de_ultimate_defconfig
index 109d0c91..25bd9513 100644
--- a/br-ext-chip-sigmastar/configs/ssc337de_ultimate_defconfig
+++ b/br-ext-chip-sigmastar/configs/ssc337de_ultimate_defconfig
@@ -85,6 +85,7 @@ BR2_PACKAGE_OPUS_OPENIPC=y
 BR2_PACKAGE_OPUS_OPENIPC_FIXED_POINT=y
 BR2_PACKAGE_SIGMASTAR_OSDRV_INFINITY6B0=y
 BR2_PACKAGE_SIGMASTAR_OSDRV_ULTIMATE=y
+BR2_PACKAGE_SIGMASTAR_MOTORS=y
 BR2_PACKAGE_DOSFSTOOLS=y
 BR2_PACKAGE_DOSFSTOOLS_FSCK_FAT=y
 BR2_PACKAGE_EXFAT_OPENIPC=y
diff --git a/general/package/Config.in b/general/package/Config.in
index a30cab10..d3ed86e5 100644
--- a/general/package/Config.in
+++ b/general/package/Config.in
@@ -86,6 +86,7 @@ source "$BR2_EXTERNAL_GENERAL_PATH/package/rtl8192eu-openipc/Config.in"
 source "$BR2_EXTERNAL_GENERAL_PATH/package/rtl8733bu-openipc/Config.in"
 source "$BR2_EXTERNAL_GENERAL_PATH/package/rtl8812au-openipc/Config.in"
 source "$BR2_EXTERNAL_GENERAL_PATH/package/rtw-hostapd/Config.in"
+source "$BR2_EXTERNAL_GENERAL_PATH/package/sigmastar-motors/Config.in"
 source "$BR2_EXTERNAL_GENERAL_PATH/package/sigmastar-osdrv-infinity6/Config.in"
 source "$BR2_EXTERNAL_GENERAL_PATH/package/sigmastar-osdrv-infinity6b0/Config.in"
 source "$BR2_EXTERNAL_GENERAL_PATH/package/sigmastar-osdrv-infinity6e/Config.in"
diff --git a/general/package/sigmastar-motors/Config.in b/general/package/sigmastar-motors/Config.in
new file mode 100644
index 00000000..6d0abbc9
--- /dev/null
+++ b/general/package/sigmastar-motors/Config.in
@@ -0,0 +1,4 @@
+config BR2_PACKAGE_SIGMASTAR_MOTORS
+	bool "sigmastar-motors"
+	help
+	  Sample code to control Sigmastar motors.
diff --git a/general/package/sigmastar-motors/sigmastar-motors.mk b/general/package/sigmastar-motors/sigmastar-motors.mk
new file mode 100644
index 00000000..065c6323
--- /dev/null
+++ b/general/package/sigmastar-motors/sigmastar-motors.mk
@@ -0,0 +1,19 @@
+################################################################################
+#
+# sigmastar-motors
+#
+################################################################################
+
+define SIGMASTAR_MOTORS_EXTRACT_CMDS
+	cp -r $(SIGMASTAR_MOTORS_PKGDIR)/src/* $(@D)
+endef
+
+define SIGMASTAR_MOTORS_BUILD_CMDS
+	$(MAKE) CC=$(TARGET_CC) -C $(@D)
+endef
+
+define SIGMASTAR_MOTORS_INSTALL_TARGET_CMDS
+	$(INSTALL) -m 755 -t $(TARGET_DIR)/usr/bin $(@D)/output/*
+endef
+
+$(eval $(generic-package))
diff --git a/general/package/sigmastar-motors/src/Makefile b/general/package/sigmastar-motors/src/Makefile
new file mode 100644
index 00000000..a148bf64
--- /dev/null
+++ b/general/package/sigmastar-motors/src/Makefile
@@ -0,0 +1,3 @@
+motor_foscam:
+	mkdir -p output
+	$(CC) motor_foscam.c -o output/$@ -s -Wall
diff --git a/general/package/sigmastar-motors/src/motor_foscam.c b/general/package/sigmastar-motors/src/motor_foscam.c
new file mode 100644
index 00000000..b31c3d45
--- /dev/null
+++ b/general/package/sigmastar-motors/src/motor_foscam.c
@@ -0,0 +1,131 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <linux/gpio.h>
+#include <sys/ioctl.h>
+
+#define DEV_NAME "/dev/gpiochip0"
+#define STEP_TIME 2 * 1000
+
+static int motor_gpio[][4] = {
+	{01, 02, 12, 13},
+	{62, 63, 64, 65},
+};
+
+static int sequence[][4] = {
+	{1, 0, 0, 0},
+	{0, 1, 0, 0},
+	{0, 0, 1, 0},
+	{0, 0, 0, 1},
+	{0, 0, 0, 1},
+	{0, 0, 1, 0},
+	{0, 1, 0, 0},
+	{1, 0, 0, 0},
+};
+
+static void write_gpio(int pin, int value) {
+	struct gpiohandle_request rq;
+	struct gpiohandle_data data;
+
+	int fd = open(DEV_NAME, O_RDONLY);
+	if (fd < 0) {
+		printf("Unabled to open chip: %s\n", strerror(errno));
+		return;
+	}
+
+	rq.lineoffsets[0] = pin;
+	rq.flags = GPIOHANDLE_REQUEST_OUTPUT;
+	rq.lines = 1;
+
+	if (ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq) < 0) {
+		printf("Unable to request gpio: %s\n", strerror(errno));
+		return;
+	}
+
+	close(fd);
+	data.values[0] = value;
+
+	if (ioctl(rq.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data) < 0) {
+		printf("Unable to set value: %s\n", strerror(errno));
+		return;
+	}
+
+	close(rq.fd);
+}
+
+static void motor_control(int dir, int step) {
+	usleep(STEP_TIME);
+	for (int i = 0; i < 4; i++) {
+		write_gpio(motor_gpio[dir][i], sequence[step][i]);
+	}
+}
+
+static void motor_reset(int dir) {
+	for (int i = 0; i < 4; i++) {
+		write_gpio(motor_gpio[dir][i], 0);
+	}
+}
+
+static void usage(const char *name) {
+	printf("Usage: %s [options]\n"
+		"Options:\n"
+		"\t-x [count]\tHorizontal direction step (-10 | +10)\n"
+		"\t-y [count]\tVertical direction step (-10 | +10)\n", name);
+}
+
+int main(int argc, char **argv) {
+	int cnt = 0;
+	int dir = 0;
+	int opt = 0;
+
+	while ((opt = getopt(argc, argv, "hx:y:")) != -1) {
+		switch (opt) {
+			case 'h':
+				usage(argv[0]);
+				return -1;
+
+			case 'x':
+				cnt = atoi(optarg);
+				break;
+
+			case 'y':
+				dir = 1;
+				cnt = atoi(optarg);
+				break;
+
+			default:
+				return -1;
+		}
+	}
+
+	if (argc == 1 || argc != optind) {
+		usage(argv[0]);
+		return -1;
+	}
+
+	if (cnt > 10) {
+		cnt = 10;
+	}
+
+	if (cnt < -10) {
+		cnt = -10;
+	}
+
+	int c1 = (cnt < 0) ? cnt * 10 : 0;
+	int c2 = (cnt < 0) ? 0 : cnt * 10;
+	int d1 = (cnt < 0) ? 4 : 0;
+	int d2 = (cnt < 0) ? 8 : 4;
+
+	for (int i = c1; i < c2; i++) {
+		for (int j = d1; j < d2; j++) {
+			motor_control(dir, j);
+		}
+	}
+
+	motor_reset(dir);
+
+	return 0;
+}