/*
 * 设置PIN脚为1或0电位(电压)
 *
 */

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


int is_num(const char *str) {
    while (*str != '\0') {
        if (!isdigit(*str)) {
            return 0;
        }
        str++;
    }
    return 1;
}


int main(int argc, char *argv[]) {
    if (argc != 3) {
        printf("Error: Invalid number of arguments.\n");
        printf("Usage: %s PIN VALUE\n", argv[0]);
        exit(1);
    }

    int pin = atoi(argv[1]);
    if (!is_num(argv[1]) || (pin < 0)) {
        printf("Error: Invalid PIN value.\n");
        exit(1);
    }

    int value = atoi(argv[2]);
    if (!is_num(argv[2]) || (value != 0 && value != 1)) {
        printf("Error: Invalid value. Please enter 0 or 1.\n");
        exit(1);
    }

    wiringPiSetup();

    pinMode(pin, OUTPUT);

    if (digitalRead(pin) != value) {
        digitalWrite(pin, value);
    }

    return value;
}