Last-modified: 2016-04-09 (土) 05:11:38
Raspberry Pi/Lチカ

概要

Raspberry Pi2とC++でLチカを試します。
GPIOのアクセスにはWiringPiを使います。

配線

000021.png
000001.jpg

手順

  1. WiringPiのインストール
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
    
     
     
     
     
    
    git clone git://git.drogon.net/wiringPi
    cd wiringPi
    git pull origin
    ./build
  2. vi led.cpp
    Everything is expanded.Everything is shortened.
      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
    
     
     
     
     
     
     
     
     
    -
    |
    |
    |
    -
    |
    !
    |
    -
    |
    |
    !
    !
     
    -
    |
    |
    |
    !
     
    -
    |
    -
    |
    -
    |
    !
    |
    |
    |
    -
    |
    |
    |
    |
    !
    !
    |
    -
    |
    !
    |
    -
    |
    !
    |
    !
    
    #include <iostream>
    #include <stdexcept>
    #include <thread>
    #include <wiringPi.h>
     
    using namespace std;
     
    namespace
    {
        constexpr int OutPin = 25;
        constexpr int WaitMicroSec = 500 * 1000;
     
        /**
    	 * @brief 信号の状態
    	 */
        enum class State : int
        {
            Low = 0,
            High = 1
        };
    };
     
    /**
     * @brief LEDを0.5秒ごとにオン/オフ
     * 
     * @return 常に0
     */
    int main()
    {
        try
        {
            if (wiringPiSetupGpio() == -1)
            {
                throw runtime_error("Failed to wiringPiSetupGpio");
            }
            pinMode(OutPin, OUTPUT);
     
            for (;;)
            {
                digitalWrite(OutPin, static_cast<int>(State::High));
                this_thread::sleep_for(chrono::microseconds(WaitMicroSec));
                digitalWrite(OutPin, static_cast<int>(State::Low));
                this_thread::sleep_for(chrono::microseconds(WaitMicroSec));
            }
        }
        catch (exception& e)
        {
            cerr << e.what() << "\n";
        }
        catch (...)
        {
            cerr << "Unknown exception!\n";
        }
        return 0;
    }
  3. vi Makefile
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
    
     
     
     
     
    
    app: app.cpp
        g++ app.cpp -O2 -Wall -Wextra -std=gnu++11 -lwiringPi -oapp
    clean:
        rm -f *.o app
  4. ビルド/実行
    Everything is expanded.Everything is shortened.
      1
      2
    
     
     
    
    make
    sudo ./app

pythonなら

  1. vi app.py
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
    
    -
    !
    -
    |
    !
     
     
     
    -
    !
     
     
     
     
     
     
     
    
    # coding:utf-8
     
    # GPIO25 - 抵抗 - LED - GND
    # LEDを0.5秒ごとにオン/オフする
     
    import RPi.GPIO as GPIO
    from time import sleep
     
    # 初期化
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(25, GPIO.OUT)
     
    while True:
        GPIO.output(25, GPIO.HIGH)
        sleep(0.5)
        GPIO.output(25, GPIO.LOW)
        sleep(0.5)
  2. 実行
    Everything is expanded.Everything is shortened.
      1
    
     
    
    sudo python app.py

検証時の環境