반응형



1. WiringPi 다운로드 및 설치


$ git clone git://git.drogon.net/wiringPi

$ cd wiringPi

$ ./build




11번핀에 LED를 연결하고 16번 핀에 버튼을 연결합니다. 




아래처럼 컴파일해서 실행시켜보면

$ gcc main.c -lwiringPi

$ sudo ./a.out

버튼을 누를때 마다 LED가 켜지는 것을 확인 할 수 있습니다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <wiringPi.h>
 
const int ledPin = 17;//pin 11(GPIO 17)
const int butPin = 23;//pin 16(GPIO 23)
 
int main(void)
{
    wiringPiSetupGpio(); 
 
    pinMode(ledPin, OUTPUT);//LED를 위해 출력으로 설정 
    pinMode(butPin, INPUT);//버튼을 위해 입력으로 설정
    pullUpDnControl(butPin, PUD_UP);
 
    while(1)
    {
        
        int ret = digitalRead(butPin);
        if ( ret ) 
        {
            digitalWrite(ledPin, LOW); 
        }
        else 
        {
            digitalWrite(ledPin, HIGH); 
        }
    }
 
    return 0;
}
 
cs



2. pi camera 라이브러리 설치

$ wget https://sourceforge.net/projects/raspicam/files/raspicam-0.1.3.zip

$ unzip raspicam-0.1.3.zip

$ cd raspicam-0.1.3/

$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install
$ sudo ldconfig



파이 카메라 연결 및 설정은 아래 포스팅을 확인하세요 


[임베디드/Raspberry Pi] - Raspberry Pi 3에서 camera V2 사용 해보기



아래처럼 컴파일 후 실행시켜 보면 버튼을 누를때마다 raspicam_image.ppm라는 파일 이름으로 파이캠으로부터 캡처된 영상이 저장됩니다. 

$ g++  picam.cpp  -I/usr/local/include -lraspicam  -lwiringPi


$ sudo ./a.out


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <ctime>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <raspicam/raspicam.h>
#include <wiringPi.h>
 
using namespace std;
 
const int ledPin = 17;//pin 11(GPIO 17)
const int butPin = 23;//pin 16(GPIO 23)
 
 
int main ( int argc,char **argv ) 
{
 
    raspicam::RaspiCam Camera;
 
    //Open camera 
    cout<<"Opening Camera..."<<endl;
    if ( !Camera.open()) {
        cerr<<"Error opening camera"<<endl;
        return -1;
    }
 
    //wait a while until camera stabilizes
    cout<<"wait a minutes"<<endl;
    usleep(3000000);
 
 
 
    wiringPiSetupGpio(); 
 
    pinMode(ledPin, OUTPUT);//LED를 위해 출력으로 설정 
    pinMode(butPin, INPUT);//버튼을 위해 입력으로 설정
    pullUpDnControl(butPin, PUD_UP);
 
 
    cout<<"now press button"<<endl;
 
 
    while(1)
    {
        
        int ret = digitalRead(butPin);
        if ( ret ) 
        {
            digitalWrite(ledPin, LOW); 
        }
        else 
        {
            digitalWrite(ledPin, HIGH); 
 
            //capture
            Camera.grab();
 
            //allocate memory
            unsigned char *data=new unsigned char[  Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB )];
 
            //extract the image in rgb format
            Camera.retrieve ( data,raspicam::RASPICAM_FORMAT_RGB );//get camera image
 
            //save
            std::ofstream outFile ( "raspicam_image.ppm",std::ios::binary );
 
            outFile<<"P6\n"<<Camera.getWidth() <<" "<<Camera.getHeight() <<" 255\n";
            outFile.write ( ( char* ) data, Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB ) );
 
            cout<<"Image saved at raspicam_image.ppm"<<endl;
        
            //free resrources    
 
            delete data;
        }
    }
 
    return 0;
}
 
cs





웹캠으로 촬영된 영상을 보기 위해 패키지를 설치합니다. 

$ sudo apt-get install pqiv


VNC로 라즈베리파이를 연결 혹은 HDMI로 모니터랑 파이가 연결되어 있는 상황에서 실행시키면 촬영된 사진이 보입니다. 

$ pqiv -f raspicam_image.ppm





문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts