raspberrypi/gpio/light.c

56 lines
1010 B
C

/*
* 设置PIN脚为1或0电位(电压)
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <type.h>
#include <ctype.h>
static int is_num(char *str)
{
int i, len;
for (i = 0, len = strlen(str); i < len; i++) {
if (isdigit(str[i]) == 0) {
printf("不是数字\n");
return 1;
}
}
return 0;
}
int main(int argc, char *argv[])
{
if (argc != 3) {
printf("Parameter error.\n");
exit(1);
}
if (is_num(argv[1]) == 1) {
printf("Parameter error, parameter 1 is PIN pin value\n");
exit(1);
}
if (is_num(argv[2]) == 1) {
printf("Parameter error, parameter 2 is true or false\n");
exit(1);
}
int pin = atol(argv[1]);
wiringPiSetup();
pinMode(pin, OUTPUT);
if (atol(argv[2]) == 1) {
digitalWrite(pin, HIGH);
}
if (atol(argv[2]) == 0) {
digitalWrite(pin, LOW);
}
return atol(argv[2]);
}