概要 †
Raspberry Pi2とC++でLチカを試します。
GPIOのアクセスにはWiringPiを使います。
配線 †
手順 †
- WiringPiのインストール
1
2
3
4
|
| git clone git://git.drogon.net/wiringPi
cd wiringPi
git pull origin
./build
|
- vi led.cpp
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;
enum class State : int
{
Low = 0,
High = 1
};
};
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;
}
|
- vi Makefile
1
2
3
4
|
| app: app.cpp
g++ app.cpp -O2 -Wall -Wextra -std=gnu++11 -lwiringPi -oapp
clean:
rm -f *.o app
|
- ビルド/実行
pythonなら †
- vi app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| -
!
-
|
!
-
!
|
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)
|
- 実行
検証時の環境 †