Exemplo n.º 1
0
int
main(int argc, char **argv)
{
    int i;
    struct gpio_pin pin;
    struct gpio_req req;
    char *ctlfile = NULL;
    int pinn, pinv, fd, ch;
    int flags, flag, ok;
    int config, toggle, verbose, list;

    config = toggle = verbose = list = pinn = 0;

    while ((ch = getopt(argc, argv, "c:f:lt:v")) != -1) {
        switch (ch) {
        case 'c':
            config = 1;
            pinn = str2int(optarg, &ok);
            if (!ok)
                fail("Invalid pin number: %s\n", optarg);
            break;
        case 'f':
            ctlfile = optarg;
            break;
        case 'l':
            list = 1;
            break;
        case 't':
            toggle = 1;
            pinn = str2int(optarg, &ok);
            if (!ok)
                fail("Invalid pin number: %s\n", optarg);
            break;
        case 'v':
            verbose = 1;
            break;
        default:
            usage();
            break;
        }
    }
    argv += optind;
    argc -= optind;
    for (i = 0; i < argc; i++)
        printf("%d/%s\n", i, argv[i]);

    if (ctlfile == NULL)
        fail("No gpioctl device provided\n");

    fd = open(ctlfile, O_RDONLY);
    if (fd < 0) {
        perror("open");
        exit(1);
    }

    if (list) {
        dump_pins(fd, verbose);
        close(fd);
        exit(0);
    }

    if (toggle) {
        /*
         * -t pin assumes no additional arguments
         */
        if(argc > 0) {
            usage();
            exit(1);
        }

        req.gp_pin = pinn;
        if (ioctl(fd, GPIOTOGGLE, &req) < 0) {
            perror("ioctl(GPIOTOGGLE)");
            exit(1);
        }

        close(fd);
        exit (0);
    }

    if (config) {
        flags = 0;
        for (i = 0; i < argc; i++) {
            flag = 	str2cap(argv[i]);
            if (flag < 0)
                fail("Invalid flag: %s\n", argv[i]);
            flags |= flag;
        }

        pin.gp_pin = pinn;
        pin.gp_flags = flags;
        if (ioctl(fd, GPIOSETCONFIG, &pin) < 0) {
            perror("ioctl(GPIOSETCONFIG)");
            exit(1);
        }

        exit(0);
    }

    /*
     * Last two cases - set value or print value
     */
    if ((argc == 0) || (argc > 2)) {
        usage();
        exit(1);
    }

    pinn = str2int(argv[0], &ok);
    if (!ok)
        fail("Invalid pin number: %s\n", argv[0]);

    /*
     * Read pin value
     */
    if (argc == 1) {
        req.gp_pin = pinn;
        if (ioctl(fd, GPIOGET, &req) < 0) {
            perror("ioctl(GPIOGET)");
            exit(1);
        }
        printf("%d\n", req.gp_value);
        exit (0);
    }

    /* Is it valid number (0 or 1) ? */
    pinv = str2int(argv[1], &ok);
    if (!ok || ((pinv != 0) && (pinv != 1)))
        fail("Invalid pin value: %s\n", argv[1]);

    /*
     * Set pin value
     */
    req.gp_pin = pinn;
    req.gp_value = pinv;
    if (ioctl(fd, GPIOSET, &req) < 0) {
        perror("ioctl(GPIOSET)");
        exit(1);
    }

    close(fd);
    exit(0);
}
Exemplo n.º 2
0
int
main(int argc, char **argv)
{
	int i;
	gpio_config_t pin;
	gpio_handle_t handle;
	char *ctlfile = NULL;
	int pinn, pinv, pin_type, ch;
	int flags, flag, ok;
	int config, list, name, toggle, verbose;

	config = toggle = verbose = list = name = pin_type = 0;

	while ((ch = getopt(argc, argv, "cf:lntvNp")) != -1) {
		switch (ch) {
		case 'c':
			config = 1;
			break;
		case 'f':
			ctlfile = optarg;
			break;
		case 'l':
			list = 1;
			break;
		case 'n':
			name = 1;
			break;
		case 'N':
			pin_type = PIN_TYPE_NAME;
			break;
		case'p':
			pin_type = PIN_TYPE_NUMBER;
			break;
		case 't':
			toggle = 1;
			break;
		case 'v':
			verbose = 1;
			break;
		default:
			usage();
			break;
		}
	}
	argv += optind;
	argc -= optind;
	if (ctlfile == NULL)
		handle = gpio_open(0);
	else
		handle = gpio_open_device(ctlfile);
	if (handle == GPIO_INVALID_HANDLE) {
		perror("gpio_open");
		exit(1);
	}

	if (list) {
		dump_pins(handle, verbose);
		gpio_close(handle);
		exit(0);
	}

	if (argc == 0)
		usage();

	/* Find the pin number by the name */
	switch (pin_type) {
	default:
		/* First test if it is a pin number */
		pinn = str2int(argv[0], &ok);
		if (ok) {
			/* Test if we have any pin named by this number and tell the user */
			if (get_pinnum_by_name(handle, argv[0]) != -1)
				fail("%s is also a pin name, use -p or -N\n", argv[0]);
		} else {
			/* Test if it is a name */
			if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
				fail("Can't find pin named \"%s\"\n", argv[0]);
		}
		break;
	case PIN_TYPE_NUMBER:
		pinn = str2int(argv[0], &ok);
		if (!ok)
			fail("Invalid pin number: %s\n", argv[0]);
		break;
	case PIN_TYPE_NAME:
		if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
			fail("Can't find pin named \"%s\"\n", argv[0]);
		break;
	}

	/* Set the pin name. */
	if (name) {
		if (argc != 2)
			usage();
		if (gpio_pin_set_name(handle, pinn, argv[1]) < 0) {
			perror("gpio_pin_set_name");
			exit(1);
		}
		exit(0);
	}

	if (toggle) {
		/*
                * -t pin assumes no additional arguments
                */
		if (argc > 1)
			usage();
		if (gpio_pin_toggle(handle, pinn) < 0) {
			perror("gpio_pin_toggle");
			exit(1);
		}
		gpio_close(handle);
		exit(0);
	}

	if (config) {
		flags = 0;
		for (i = 1; i < argc; i++) {
			flag = 	str2cap(argv[i]);
			if (flag < 0)
				fail("Invalid flag: %s\n", argv[i]);
			flags |= flag;
		}
		pin.g_pin = pinn;
		pin.g_flags = flags;
		if (gpio_pin_set_flags(handle, &pin) < 0) {
			perror("gpio_pin_set_flags");
			exit(1);
		}
		exit(0);
	}

	/*
	 * Last two cases - set value or print value
	 */
	if ((argc == 0) || (argc > 2))
		usage();

	/*
	 * Read pin value
	 */
	if (argc == 1) {
		pinv = gpio_pin_get(handle, pinn);
		if (pinv < 0) {
			perror("gpio_pin_get");
			exit(1);
		}
		printf("%d\n", pinv);
		exit(0);
	}

	/* Is it valid number (0 or 1) ? */
	pinv = str2int(argv[1], &ok);
	if (ok == 0 || ((pinv != 0) && (pinv != 1)))
		fail("Invalid pin value: %s\n", argv[1]);

	/*
	 * Set pin value
	 */
	if (gpio_pin_set(handle, pinn, pinv) < 0) {
		perror("gpio_pin_set");
		exit(1);
	}

	gpio_close(handle);
	exit(0);
}
Exemplo n.º 3
0
int
main(int argc, char **argv)
{
	int i;
	gpio_config_t pin;
	gpio_handle_t handle;
	char *ctlfile = NULL;
	int pinn, pinv, ch;
	int flags, flag, ok;
	int config, toggle, verbose, list;

	config = toggle = verbose = list = pinn = 0;

	while ((ch = getopt(argc, argv, "c:f:lt:v")) != -1) {
		switch (ch) {
		case 'c':
			config = 1;
			pinn = str2int(optarg, &ok);
			if (!ok)
				fail("Invalid pin number: %s\n", optarg);
			break;
		case 'f':
			ctlfile = optarg;
			break;
		case 'l':
			list = 1;
			break;
		case 't':
			toggle = 1;
			pinn = str2int(optarg, &ok);
			if (!ok)
				fail("Invalid pin number: %s\n", optarg);
			break;
		case 'v':
			verbose = 1;
			break;
		default:
			usage();
			break;
		}
	}
	argv += optind;
	argc -= optind;
	if (ctlfile == NULL)
		handle = gpio_open(0);
	else
		handle = gpio_open_device(ctlfile);
	if (handle == GPIO_INVALID_HANDLE) {
		perror("gpio_open");
		exit(1);
	}

	if (list) {
		dump_pins(handle, verbose);
		gpio_close(handle);
		exit(0);
	}

	if (toggle) {
		/*
		 * -t pin assumes no additional arguments
		 */
		if (argc > 0) {
			usage();
			exit(1);
		}
		if (gpio_pin_toggle(handle, pinn) < 0) {
			perror("gpio_pin_toggle");
			exit(1);
		}
		gpio_close(handle);
		exit(0);
	}

	if (config) {
		flags = 0;
		for (i = 0; i < argc; i++) {
			flag = 	str2cap(argv[i]);
			if (flag < 0)
				fail("Invalid flag: %s\n", argv[i]);
			flags |= flag;
		}
		pin.g_pin = pinn;
		pin.g_flags = flags;
		if (gpio_pin_set_flags(handle, &pin) < 0) {
			perror("gpio_pin_set_flags");
			exit(1);
		}
		exit(0);
	}

	/*
	 * Last two cases - set value or print value
	 */
	if ((argc == 0) || (argc > 2)) {
		usage();
		exit(1);
	}

	pinn = str2int(argv[0], &ok);
	if (!ok)
		fail("Invalid pin number: %s\n", argv[0]);

	/*
	 * Read pin value
	 */
	if (argc == 1) {
		pinv = gpio_pin_get(handle, pinn);
		if (pinv < 0) {
			perror("gpio_pin_get");
			exit(1);
		}
		printf("%d\n", pinv);
		exit(0);
	}

	/* Is it valid number (0 or 1) ? */
	pinv = str2int(argv[1], &ok);
	if (!ok || ((pinv != 0) && (pinv != 1)))
		fail("Invalid pin value: %s\n", argv[1]);

	/*
	 * Set pin value
	 */
	if (gpio_pin_set(handle, pinn, pinv) < 0) {
		perror("gpio_pin_set");
		exit(1);
	}

	gpio_close(handle);
	exit(0);
}