2020-09-10 13:23:23 +08:00
|
|
|
/*
|
|
|
|
* 设置PIN脚为1或0电位(电压)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <wiringPi.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-03-25 09:01:17 +08:00
|
|
|
#include <ctype.h>
|
2020-09-10 13:23:23 +08:00
|
|
|
|
|
|
|
|
2024-01-09 15:27:17 +08:00
|
|
|
int is_num(const char *str) {
|
|
|
|
while (*str != '\0') {
|
|
|
|
if (!isdigit(*str)) {
|
|
|
|
return 0;
|
2020-09-10 13:23:23 +08:00
|
|
|
}
|
2024-01-09 15:27:17 +08:00
|
|
|
str++;
|
2020-09-10 13:23:23 +08:00
|
|
|
}
|
2024-01-09 15:27:17 +08:00
|
|
|
return 1;
|
2020-09-10 13:23:23 +08:00
|
|
|
}
|
|
|
|
|
2024-01-09 15:27:17 +08:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2020-09-10 13:23:23 +08:00
|
|
|
if (argc != 3) {
|
2024-01-09 15:27:17 +08:00
|
|
|
printf("Error: Invalid number of arguments.\n");
|
|
|
|
printf("Usage: %s PIN VALUE\n", argv[0]);
|
2020-09-10 13:23:23 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2024-01-09 15:27:17 +08:00
|
|
|
|
|
|
|
int pin = atoi(argv[1]);
|
|
|
|
if (!is_num(argv[1]) || (pin < 0)) {
|
|
|
|
printf("Error: Invalid PIN value.\n");
|
2020-09-10 13:23:23 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2024-01-09 15:27:17 +08:00
|
|
|
|
|
|
|
int value = atoi(argv[2]);
|
|
|
|
if (!is_num(argv[2]) || (value != 0 && value != 1)) {
|
|
|
|
printf("Error: Invalid value. Please enter 0 or 1.\n");
|
2020-09-10 13:23:23 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
wiringPiSetup();
|
|
|
|
|
|
|
|
pinMode(pin, OUTPUT);
|
2024-01-09 15:27:17 +08:00
|
|
|
|
|
|
|
if (digitalRead(pin) != value) {
|
|
|
|
digitalWrite(pin, value);
|
2020-09-10 13:23:23 +08:00
|
|
|
}
|
2024-01-09 15:27:17 +08:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|