◆ 라즈베리파이의 GPIO를 쉽게 제어하려면, 파이썬 코드를 작성하거나 WiringPi 라이브러리를 사용해서 C언어 코드를 작성하면 된다.
◆ 리눅스는 유저 영역에서 GPIO핀에 접근할 수 있도록 가상 파일 시스템을 제공해서, 파일을 조작해서 GPIO를 제어할 수 있다.
◆ 라즈비안 리눅스의 경우 /sys/class/gpio 디렉토리와 하위 디렉토리에 GPIO 제어를 위한 디바이스 파일이 있다.
◆ 디바이스 파일에 값을 출력해서 라즈베리파이의 GPIO를 제어할 수도 있다.
--->>> 예제 gpioled.c <<<---------------------------------
▶ 디바이스 파일에 값을 출력하는데 open( ), write( ), close( ) 이렇게 세 시스템 호출(system call) 함수를 사용
▶ open( ) 함수는 디바이스 파일을 열고, write( ) 함수는 값을 출력하고, close( ) 함수는 파일을 닫는데, 이들 함수는 아래처럼 사용된다.
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
int led(int gpio)
{
int fd;
char buf[BUFSIZ];
fd = open("/sys/class/gpio/export",O_WRONLY); /* Export */
sprintf(buf, "%d", gpio);
write(fd, buf, strlen(buf));
close(fd);
sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio); /* Direction */
fd = open(buf, O_WRONLY);
write(fd, "out", 4); /* input: write(fd, "in", 3) */
close(fd);
sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio); /* Output value */
fd = open(buf, O_WRONLY);
write(fd, "1", 2);
close(fd);
getchar(); /* Wait for key input */
fd = open("/sys/class/gpio/unexport", O_WRONLY); /* Unexport */
sprintf(buf, "%d", gpio);
write(fd, buf, strlen(buf));
close(fd);
return 0;
}
int main(int argc, char** argv)
{
int gno;
if(argc < 2)
{
printf("Usage : %s GPIO_NO\n", argv[0]);
return -1;
}
gno = atoi(argv[1]);
led(gno);
return 0;
}